Browse Source

crypto: improve memory usage

ClientHelloParser used to contain an 18k buffer that was kept around
for the life of the connection, even though it was not needed in many
situations. I changed it to be deallocated when it's determined to
be no longer needed.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
v0.10.29-release
Alexis Campailla 11 years ago
committed by Fedor Indutny
parent
commit
c06495713a
  1. 10
      src/node_crypto.cc
  2. 14
      src/node_crypto.h

10
src/node_crypto.cc

@ -784,12 +784,14 @@ Handle<Value> SecureContext::LoadPKCS12(const Arguments& args) {
size_t ClientHelloParser::Write(const uint8_t* data, size_t len) {
HandleScope scope;
assert(state_ != kEnded);
// Just accumulate data, everything will be pushed to BIO later
if (state_ == kPaused) return 0;
// Copy incoming data to the internal buffer
// (which has a size of the biggest possible TLS frame)
size_t available = sizeof(data_) - offset_;
size_t available = kBufferSize - offset_;
size_t copied = len < available ? len : available;
memcpy(data_ + offset_, data, copied);
offset_ += copied;
@ -824,7 +826,7 @@ size_t ClientHelloParser::Write(const uint8_t* data, size_t len) {
}
// Sanity check (too big frame, or too small)
if (frame_len_ >= sizeof(data_)) {
if (frame_len_ >= kBufferSize) {
// Let OpenSSL handle it
Finish();
return copied;
@ -905,7 +907,6 @@ size_t ClientHelloParser::Write(const uint8_t* data, size_t len) {
argv[0] = hello;
MakeCallback(conn_->handle_, onclienthello_sym, 1, argv);
break;
case kEnded:
default:
break;
}
@ -922,6 +923,9 @@ void ClientHelloParser::Finish() {
int r = BIO_write(conn_->bio_read_, reinterpret_cast<char*>(data_), offset_);
conn_->HandleBIOError(conn_->bio_read_, "BIO_write", r);
conn_->SetShutdownFlags();
delete[] data_;
data_ = NULL;
}

14
src/node_crypto.h

@ -43,7 +43,6 @@
#define EVP_F_EVP_DECRYPTFINAL 101
namespace node {
namespace crypto {
@ -137,6 +136,16 @@ class ClientHelloParser {
state_(kWaiting),
offset_(0),
body_offset_(0) {
data_ = new uint8_t[kBufferSize];
if (!data_)
abort();
}
~ClientHelloParser() {
if (data_) {
delete[] data_;
data_ = NULL;
}
}
size_t Write(const uint8_t* data, size_t len);
@ -145,11 +154,12 @@ class ClientHelloParser {
inline bool ended() { return state_ == kEnded; }
private:
static const int kBufferSize = 18432;
Connection* conn_;
ParseState state_;
size_t frame_len_;
uint8_t data_[18432];
uint8_t* data_;
size_t offset_;
size_t body_offset_;
};

Loading…
Cancel
Save