tls_wrap: use writev when possible
[platform/upstream/nodejs.git] / src / node_crypto_bio.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef SRC_NODE_CRYPTO_BIO_H_
23 #define SRC_NODE_CRYPTO_BIO_H_
24
25 #include "openssl/bio.h"
26 #include <assert.h>
27
28 namespace node {
29
30 class NodeBIO {
31  public:
32   NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
33     // Loop head
34     head_.next_ = &head_;
35   }
36
37   ~NodeBIO();
38
39   static BIO* New();
40
41   // Move read head to next buffer if needed
42   void TryMoveReadHead();
43
44   // Allocate new buffer for write if needed
45   void TryAllocateForWrite();
46
47   // Read `len` bytes maximum into `out`, return actual number of read bytes
48   size_t Read(char* out, size_t size);
49
50   // Memory optimization:
51   // Deallocate children of write head's child if they're empty
52   void FreeEmpty();
53
54   // Return pointer to internal data and amount of
55   // contiguous data available to read
56   char* Peek(size_t* size);
57
58   // Return pointers and sizes of multiple internal data chunks available for
59   // reading
60   size_t PeekMultiple(char** out, size_t* size, size_t* count);
61
62   // Find first appearance of `delim` in buffer or `limit` if `delim`
63   // wasn't found.
64   size_t IndexOf(char delim, size_t limit);
65
66   // Discard all available data
67   void Reset();
68
69   // Put `len` bytes from `data` into buffer
70   void Write(const char* data, size_t size);
71
72   // Return pointer to internal data and amount of
73   // contiguous data available for future writes
74   char* PeekWritable(size_t* size);
75
76   // Commit reserved data
77   void Commit(size_t size);
78
79   // Return size of buffer in bytes
80   size_t inline Length() {
81     return length_;
82   }
83
84   static inline NodeBIO* FromBIO(BIO* bio) {
85     assert(bio->ptr != NULL);
86     return static_cast<NodeBIO*>(bio->ptr);
87   }
88
89  private:
90   static int New(BIO* bio);
91   static int Free(BIO* bio);
92   static int Read(BIO* bio, char* out, int len);
93   static int Write(BIO* bio, const char* data, int len);
94   static int Puts(BIO* bio, const char* str);
95   static int Gets(BIO* bio, char* out, int size);
96   static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
97
98   // NOTE: Size is maximum TLS frame length, this is required if we want
99   // to fit whole ClientHello into one Buffer of NodeBIO.
100   static const size_t kBufferLength = 16 * 1024 + 5;
101   static const BIO_METHOD method;
102
103   class Buffer {
104    public:
105     Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
106     }
107
108     size_t read_pos_;
109     size_t write_pos_;
110     Buffer* next_;
111     char data_[kBufferLength];
112   };
113
114   size_t length_;
115   Buffer head_;
116   Buffer* read_head_;
117   Buffer* write_head_;
118 };
119
120 }  // namespace node
121
122 #endif  // SRC_NODE_CRYPTO_BIO_H_