| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8da4983 commit efd9bc3
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,24 +28,20 @@ | |||
| 28 | 28 | namespace node { | |
| 29 | 29 | namespace crypto { | |
| 30 | 30 | ||
| 31 | - const BIO_METHOD NodeBIO::method = { | ||
| 32 | - BIO_TYPE_MEM, | ||
| 33 | - "node.js SSL buffer", | ||
| 34 | - NodeBIO::Write, | ||
| 35 | - NodeBIO::Read, | ||
| 36 | - NodeBIO::Puts, | ||
| 37 | - NodeBIO::Gets, | ||
| 38 | - NodeBIO::Ctrl, | ||
| 39 | - NodeBIO::New, | ||
| 40 | - NodeBIO::Free, | ||
| 41 | - nullptr | ||
| 42 | - }; | ||
| 31 | + #if OPENSSL_VERSION_NUMBER < 0x10100000L | ||
| 32 | + #define BIO_set_data(bio, data) bio->ptr = data | ||
| 33 | + #define BIO_get_data(bio) bio->ptr | ||
| 34 | + #define BIO_set_shutdown(bio, shutdown_) bio->shutdown = shutdown_ | ||
| 35 | + #define BIO_get_shutdown(bio) bio->shutdown | ||
| 36 | + #define BIO_set_init(bio, init_) bio->init = init_ | ||
| 37 | + #define BIO_get_init(bio) bio->init | ||
| 38 | + #endif | ||
| 43 | 39 | ||
| 44 | 40 | ||
| 45 | 41 | BIO* NodeBIO::New() { | |
| 46 | 42 | // The const_cast doesn't violate const correctness. OpenSSL's usage of | |
| 47 | 43 | // BIO_METHOD is effectively const but BIO_new() takes a non-const argument. | |
| 48 | - return BIO_new(const_cast<BIO_METHOD*>(&method)); | ||
| 44 | + return BIO_new(const_cast<BIO_METHOD*>(GetMethod())); | ||
| 49 | 45 | } | |
| 50 | 46 | ||
| 51 | 47 | ||
@@ -70,12 +66,11 @@ void NodeBIO::AssignEnvironment(Environment* env) { | |||
| 70 | 66 | ||
| 71 | 67 | ||
| 72 | 68 | int NodeBIO::New(BIO* bio) { | |
| 73 | - bio->ptr = new NodeBIO(); | ||
| 69 | + BIO_set_data(bio, new NodeBIO()); | ||
| 74 | 70 | ||
| 75 | 71 | // XXX Why am I doing it?! | |
| 76 | - bio->shutdown = 1; | ||
| 77 | - bio->init = 1; | ||
| 78 | - bio->num = -1; | ||
| 72 | + BIO_set_shutdown(bio, 1); | ||
| 73 | + BIO_set_init(bio, 1); | ||
| 79 | 74 | ||
| 80 | 75 | return 1; | |
| 81 | 76 | } | |
@@ -85,10 +80,10 @@ int NodeBIO::Free(BIO* bio) { | |||
| 85 | 80 | if (bio == nullptr) | |
| 86 | 81 | return 0; | |
| 87 | 82 | ||
| 88 | - if (bio->shutdown) { | ||
| 89 | - if (bio->init && bio->ptr != nullptr) { | ||
| 83 | + if (BIO_get_shutdown(bio)) { | ||
| 84 | + if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) { | ||
| 90 | 85 | delete FromBIO(bio); | |
| 91 | - bio->ptr = nullptr; | ||
| 86 | + BIO_set_data(bio, nullptr); | ||
| 92 | 87 | } | |
| 93 | 88 | } | |
| 94 | 89 | ||
@@ -97,13 +92,13 @@ int NodeBIO::Free(BIO* bio) { | |||
| 97 | 92 | ||
| 98 | 93 | ||
| 99 | 94 | int NodeBIO::Read(BIO* bio, char* out, int len) { | |
| 100 | - int bytes; | ||
| 101 | 95 | BIO_clear_retry_flags(bio); | |
| 102 | 96 | ||
| 103 | - bytes = FromBIO(bio)->Read(out, len); | ||
| 97 | + NodeBIO* nbio = FromBIO(bio); | ||
| 98 | + int bytes = nbio->Read(out, len); | ||
| 104 | 99 | ||
| 105 | 100 | if (bytes == 0) { | |
| 106 | - bytes = bio->num; | ||
| 101 | + bytes = nbio->eof_return(); | ||
| 107 | 102 | if (bytes != 0) { | |
| 108 | 103 | BIO_set_retry_read(bio); | |
| 109 | 104 | } | |
@@ -161,7 +156,7 @@ int NodeBIO::Puts(BIO* bio, const char* str) { | |||
| 161 | 156 | ||
| 162 | 157 | ||
| 163 | 158 | int NodeBIO::Gets(BIO* bio, char* out, int size) { | |
| 164 | - NodeBIO* nbio = FromBIO(bio); | ||
| 159 | + NodeBIO* nbio = FromBIO(bio); | ||
| 165 | 160 | ||
| 166 | 161 | if (nbio->Length() == 0) | |
| 167 | 162 | return 0; | |
@@ -201,7 +196,7 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) | |||
| 201 | 196 | ret = nbio->Length() == 0; | |
| 202 | 197 | break; | |
| 203 | 198 | case BIO_C_SET_BUF_MEM_EOF_RETURN: | |
| 204 | - bio->num = num; | ||
| 199 | + nbio->set_eof_return(num); | ||
| 205 | 200 | break; | |
| 206 | 201 | case BIO_CTRL_INFO: | |
| 207 | 202 | ret = nbio->Length(); | |
@@ -216,10 +211,10 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) | |||
| 216 | 211 | ret = 0; | |
| 217 | 212 | break; | |
| 218 | 213 | case BIO_CTRL_GET_CLOSE: | |
| 219 | - ret = bio->shutdown; | ||
| 214 | + ret = BIO_get_shutdown(bio); | ||
| 220 | 215 | break; | |
| 221 | 216 | case BIO_CTRL_SET_CLOSE: | |
| 222 | - bio->shutdown = num; | ||
| 217 | + BIO_set_shutdown(bio, num); | ||
| 223 | 218 | break; | |
| 224 | 219 | case BIO_CTRL_WPENDING: | |
| 225 | 220 | ret = 0; | |
@@ -241,6 +236,41 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) | |||
| 241 | 236 | } | |
| 242 | 237 | ||
| 243 | 238 | ||
| 239 | + const BIO_METHOD* NodeBIO::GetMethod() { | ||
| 240 | + #if OPENSSL_VERSION_NUMBER < 0x10100000L | ||
| 241 | + static const BIO_METHOD method = { | ||
| 242 | + BIO_TYPE_MEM, | ||
| 243 | + "node.js SSL buffer", | ||
| 244 | + Write, | ||
| 245 | + Read, | ||
| 246 | + Puts, | ||
| 247 | + Gets, | ||
| 248 | + Ctrl, | ||
| 249 | + New, | ||
| 250 | + Free, | ||
| 251 | + nullptr | ||
| 252 | + }; | ||
| 253 | + | ||
| 254 | + return &method; | ||
| 255 | + #else | ||
| 256 | + static BIO_METHOD* method = nullptr; | ||
| 257 | + | ||
| 258 | + if (method == nullptr) { | ||
| 259 | + method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer"); | ||
| 260 | + BIO_meth_set_write(method, Write); | ||
| 261 | + BIO_meth_set_read(method, Read); | ||
| 262 | + BIO_meth_set_puts(method, Puts); | ||
| 263 | + BIO_meth_set_gets(method, Gets); | ||
| 264 | + BIO_meth_set_ctrl(method, Ctrl); | ||
| 265 | + BIO_meth_set_create(method, New); | ||
| 266 | + BIO_meth_set_destroy(method, Free); | ||
| 267 | + } | ||
| 268 | + | ||
| 269 | + return method; | ||
| 270 | + #endif | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + | ||
| 244 | 274 | void NodeBIO::TryMoveReadHead() { | |
| 245 | 275 | // `read_pos_` and `write_pos_` means the position of the reader and writer | |
| 246 | 276 | // inside the buffer, respectively. When they're equal - its safe to reset | |
@@ -488,5 +518,12 @@ NodeBIO::~NodeBIO() { | |||
| 488 | 518 | write_head_ = nullptr; | |
| 489 | 519 | } | |
| 490 | 520 | ||
| 521 | + | ||
| 522 | + NodeBIO* NodeBIO::FromBIO(BIO* bio) { | ||
| 523 | + CHECK_NE(BIO_get_data(bio), nullptr); | ||
| 524 | + return static_cast<NodeBIO*>(BIO_get_data(bio)); | ||
| 525 | + } | ||
| 526 | + | ||
| 527 | + | ||
| 491 | 528 | } // namespace crypto | |
| 492 | 529 | } // namespace node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ class NodeBIO { | |||
| 37 | 37 | NodeBIO() : env_(nullptr), | |
| 38 | 38 | initial_(kInitialBufferLength), | |
| 39 | 39 | length_(0), | |
| 40 | + eof_return_(-1), | ||
| 40 | 41 | read_head_(nullptr), | |
| 41 | 42 | write_head_(nullptr) { | |
| 42 | 43 | } | |
@@ -95,14 +96,19 @@ class NodeBIO { | |||
| 95 | 96 | return length_; | |
| 96 | 97 | } | |
| 97 | 98 | ||
| 99 | + inline void set_eof_return(int num) { | ||
| 100 | + eof_return_ = num; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + inline int eof_return() { | ||
| 104 | + return eof_return_; | ||
| 105 | + } | ||
| 106 | + | ||
| 98 | 107 | inline void set_initial(size_t initial) { | |
| 99 | 108 | initial_ = initial; | |
| 100 | 109 | } | |
| 101 | 110 | ||
| 102 | - static inline NodeBIO* FromBIO(BIO* bio) { | ||
| 103 | - CHECK_NE(bio->ptr, nullptr); | ||
| 104 | - return static_cast<NodeBIO*>(bio->ptr); | ||
| 105 | - } | ||
| 111 | + static NodeBIO* FromBIO(BIO* bio); | ||
| 106 | 112 | ||
| 107 | 113 | private: | |
| 108 | 114 | static int New(BIO* bio); | |
@@ -114,12 +120,12 @@ class NodeBIO { | |||
| 114 | 120 | static long Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int) | |
| 115 | 121 | void* ptr); | |
| 116 | 122 | ||
| 123 | + static const BIO_METHOD* GetMethod(); | ||
| 124 | + | ||
| 117 | 125 | // Enough to handle the most of the client hellos | |
| 118 | 126 | static const size_t kInitialBufferLength = 1024; | |
| 119 | 127 | static const size_t kThroughputBufferLength = 16384; | |
| 120 | 128 | ||
| 121 | - static const BIO_METHOD method; | ||
| 122 | - | ||
| 123 | 129 | class Buffer { | |
| 124 | 130 | public: | |
| 125 | 131 | Buffer(Environment* env, size_t len) : env_(env), | |
@@ -151,6 +157,7 @@ class NodeBIO { | |||
| 151 | 157 | Environment* env_; | |
| 152 | 158 | size_t initial_; | |
| 153 | 159 | size_t length_; | |
| 160 | + int eof_return_; | ||
| 154 | 161 | Buffer* read_head_; | |
| 155 | 162 | Buffer* write_head_; | |
| 156 | 163 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments