tls_wrap: use writev when possible
[platform/upstream/nodejs.git] / src / tls_wrap.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_TLS_WRAP_H_
23 #define SRC_TLS_WRAP_H_
24
25 #include "node.h"
26 #include "node_crypto.h"  // SSLWrap
27
28 #include "async-wrap.h"
29 #include "env.h"
30 #include "queue.h"
31 #include "stream_wrap.h"
32 #include "v8.h"
33
34 #include <openssl/ssl.h>
35
36 namespace node {
37
38 // Forward-declarations
39 class NodeBIO;
40 class WriteWrap;
41 namespace crypto {
42   class SecureContext;
43 }
44
45 class TLSCallbacks : public crypto::SSLWrap<TLSCallbacks>,
46                      public StreamWrapCallbacks,
47                      public AsyncWrap {
48  public:
49   static void Initialize(v8::Handle<v8::Object> target,
50                          v8::Handle<v8::Value> unused,
51                          v8::Handle<v8::Context> context);
52
53   int DoWrite(WriteWrap* w,
54               uv_buf_t* bufs,
55               size_t count,
56               uv_stream_t* send_handle,
57               uv_write_cb cb);
58   void AfterWrite(WriteWrap* w);
59   void DoAlloc(uv_handle_t* handle,
60                size_t suggested_size,
61                uv_buf_t* buf);
62   void DoRead(uv_stream_t* handle,
63               ssize_t nread,
64               const uv_buf_t* buf,
65               uv_handle_type pending);
66   int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
67
68  protected:
69   static const int kClearOutChunkSize = 1024;
70
71   // Maximum number of buffers passed to uv_write()
72   static const int kSimultaneousBufferCount = 10;
73
74   // Write callback queue's item
75   class WriteItem {
76    public:
77     WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
78     }
79     ~WriteItem() {
80       w_ = NULL;
81       cb_ = NULL;
82     }
83
84     WriteWrap* w_;
85     uv_write_cb cb_;
86     QUEUE member_;
87   };
88
89   TLSCallbacks(Environment* env,
90                Kind kind,
91                v8::Handle<v8::Object> sc,
92                StreamWrapCallbacks* old);
93   ~TLSCallbacks();
94
95   static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
96   void InitSSL();
97   void EncOut();
98   static void EncOutCb(uv_write_t* req, int status);
99   bool ClearIn();
100   void ClearOut();
101   void InvokeQueued(int status);
102
103   inline void Cycle() {
104     ClearIn();
105     ClearOut();
106     EncOut();
107   }
108
109   v8::Local<v8::Value> GetSSLError(int status, int* err);
110   static void OnClientHelloParseEnd(void* arg);
111
112   static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
113   static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
114   static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
115   static void EnableSessionCallbacks(
116       const v8::FunctionCallbackInfo<v8::Value>& args);
117   static void EnableHelloParser(
118       const v8::FunctionCallbackInfo<v8::Value>& args);
119
120 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
121   static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
122   static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
123   static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
124 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
125
126   crypto::SecureContext* sc_;
127   v8::Persistent<v8::Object> sc_handle_;
128   BIO* enc_in_;
129   BIO* enc_out_;
130   NodeBIO* clear_in_;
131   uv_write_t write_req_;
132   size_t write_size_;
133   size_t write_queue_size_;
134   QUEUE write_item_queue_;
135   WriteItem* pending_write_item_;
136   bool started_;
137   bool established_;
138   bool shutdown_;
139
140 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
141   v8::Persistent<v8::Value> sni_context_;
142 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
143 };
144
145 }  // namespace node
146
147 #endif  // SRC_TLS_WRAP_H_