1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "node_crypto_bio.h"
23 #include "openssl/bio.h"
28 BIO_METHOD NodeBIO::method_ = {
42 int NodeBIO::New(BIO* bio) {
43 bio->ptr = new NodeBIO();
45 // XXX Why am I doing it?!
54 int NodeBIO::Free(BIO* bio) {
55 if (bio == NULL) return 0;
58 if (bio->init && bio->ptr != NULL) {
68 int NodeBIO::Read(BIO* bio, char* out, int len) {
70 BIO_clear_retry_flags(bio);
72 bytes = FromBIO(bio)->Read(out, len);
77 BIO_set_retry_read(bio);
85 char* NodeBIO::Peek(size_t* size) {
86 *size = read_head_->write_pos_ - read_head_->read_pos_;
87 return read_head_->data_ + read_head_->read_pos_;
91 int NodeBIO::Write(BIO* bio, const char* data, int len) {
92 BIO_clear_retry_flags(bio);
94 FromBIO(bio)->Write(data, len);
100 int NodeBIO::Puts(BIO* bio, const char* str) {
101 return Write(bio, str, strlen(str));
105 int NodeBIO::Gets(BIO* bio, char* out, int size) {
106 NodeBIO* nbio = FromBIO(bio);
108 if (nbio->Length() == 0)
111 int i = nbio->IndexOf('\n', size);
116 // Shift `i` a bit to NULL-terminate string later
128 long NodeBIO::Ctrl(BIO* bio, int cmd, long num, void* ptr) {
140 ret = nbio->Length() == 0;
142 case BIO_C_SET_BUF_MEM_EOF_RETURN:
146 ret = nbio->Length();
148 *reinterpret_cast<void**>(ptr) = NULL;
150 case BIO_C_SET_BUF_MEM:
151 assert(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
154 case BIO_C_GET_BUF_MEM_PTR:
155 assert(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
158 case BIO_CTRL_GET_CLOSE:
161 case BIO_CTRL_SET_CLOSE:
164 case BIO_CTRL_WPENDING:
167 case BIO_CTRL_PENDING:
168 ret = nbio->Length();
184 size_t NodeBIO::Read(char* out, size_t size) {
185 size_t bytes_read = 0;
186 size_t expected = Length() > size ? size : Length();
190 while (bytes_read < expected) {
191 assert(read_head_->read_pos_ <= read_head_->write_pos_);
192 size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
198 memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
199 read_head_->read_pos_ += avail;
206 // Move to next buffer
207 if (read_head_->read_pos_ == read_head_->write_pos_) {
208 read_head_->read_pos_ = 0;
209 read_head_->write_pos_ = 0;
211 // But not get beyond write_head_
212 if (length_ != bytes_read) {
213 assert(read_head_ != write_head_);
214 read_head_ = read_head_->next_;
218 assert(expected == bytes_read);
219 length_ -= bytes_read;
221 // Free all empty buffers, but write_head's child
228 void NodeBIO::FreeEmpty() {
229 Buffer* child = write_head_->next_;
230 if (child == write_head_ || child == read_head_)
232 Buffer* cur = child->next_;
233 if (cur == write_head_ || cur == read_head_)
236 while (cur != read_head_) {
237 // Skip embedded buffer
242 assert(cur != write_head_);
243 assert(cur->write_pos_ == cur->read_pos_);
245 Buffer* next = cur->next_;
254 size_t NodeBIO::IndexOf(char delim, size_t limit) {
255 size_t bytes_read = 0;
256 size_t max = Length() > limit ? limit : Length();
258 Buffer* current = read_head_;
260 while (bytes_read < max) {
261 assert(current->read_pos_ <= current->write_pos_);
262 size_t avail = current->write_pos_ - current->read_pos_;
267 char* tmp = current->data_ + current->read_pos_;
269 while (off < avail && *tmp != delim) {
283 // Move to next buffer
284 if (current->read_pos_ + avail == kBufferLength) {
285 current = current->next_;
288 assert(max == bytes_read);
294 void NodeBIO::Write(const char* data, size_t size) {
298 size_t to_write = left;
299 assert(write_head_->write_pos_ <= kBufferLength);
300 size_t avail = kBufferLength - write_head_->write_pos_;
302 if (to_write > avail)
306 memcpy(write_head_->data_ + write_head_->write_pos_,
314 write_head_->write_pos_ += to_write;
315 assert(write_head_->write_pos_ <= kBufferLength);
317 // Go to next buffer if there still are some bytes to write
319 TryAllocateForWrite();
320 write_head_ = write_head_->next_;
327 char* NodeBIO::PeekWritable(size_t* size) {
328 size_t available = kBufferLength - write_head_->write_pos_;
329 if (*size != 0 && available > *size)
334 return write_head_->data_ + write_head_->write_pos_;
338 void NodeBIO::Commit(size_t size) {
339 write_head_->write_pos_ += size;
341 assert(write_head_->write_pos_ <= kBufferLength);
343 // Allocate new buffer if write head is full,
344 // and there're no other place to go
345 TryAllocateForWrite();
346 write_head_ = write_head_->next_;
350 void NodeBIO::TryAllocateForWrite() {
351 // If write head is full, next buffer is either read head or not empty.
352 if (write_head_->write_pos_ == kBufferLength &&
353 (write_head_->next_ == read_head_ ||
354 write_head_->next_->write_pos_ != 0)) {
355 Buffer* next = new Buffer();
356 next->next_ = write_head_->next_;
357 write_head_->next_ = next;
362 void NodeBIO::Reset() {
363 while (read_head_->read_pos_ != read_head_->write_pos_) {
364 assert(read_head_->write_pos_ > read_head_->read_pos_);
366 length_ -= read_head_->write_pos_ - read_head_->read_pos_;
367 read_head_->write_pos_ = 0;
368 read_head_->read_pos_ = 0;
370 read_head_ = read_head_->next_;
372 write_head_ = read_head_;
373 assert(length_ == 0);
377 NodeBIO::~NodeBIO() {
378 Buffer* current = head_.next_;
379 while (current != &head_) {
380 Buffer* next = current->next_;