stream_wrap: use `uv_try_write` where 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   const char* Error();
54   int TryWrite(uv_buf_t** bufs, size_t* count);
55   int DoWrite(WriteWrap* w,
56               uv_buf_t* bufs,
57               size_t count,
58               uv_stream_t* send_handle,
59               uv_write_cb cb);
60   void AfterWrite(WriteWrap* w);
61   void DoAlloc(uv_handle_t* handle,
62                size_t suggested_size,
63                uv_buf_t* buf);
64   void DoRead(uv_stream_t* handle,
65               ssize_t nread,
66               const uv_buf_t* buf,
67               uv_handle_type pending);
68   int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
69
70  protected:
71   static const int kClearOutChunkSize = 1024;
72
73   // Maximum number of buffers passed to uv_write()
74   static const int kSimultaneousBufferCount = 10;
75
76   // Write callback queue's item
77   class WriteItem {
78    public:
79     WriteItem(WriteWrap* w, uv_write_cb cb) : w_(w), cb_(cb) {
80     }
81     ~WriteItem() {
82       w_ = NULL;
83       cb_ = NULL;
84     }
85
86     WriteWrap* w_;
87     uv_write_cb cb_;
88     QUEUE member_;
89   };
90
91   TLSCallbacks(Environment* env,
92                Kind kind,
93                v8::Handle<v8::Object> sc,
94                StreamWrapCallbacks* old);
95   ~TLSCallbacks();
96
97   static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
98   void InitSSL();
99   void EncOut();
100   static void EncOutCb(uv_write_t* req, int status);
101   bool ClearIn();
102   void ClearOut();
103   void MakePending();
104   bool InvokeQueued(int status);
105
106   inline void Cycle() {
107     ClearIn();
108     ClearOut();
109     EncOut();
110   }
111
112   v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
113   const char* PrintErrors();
114
115   static int PrintErrorsCb(const char* str, size_t len, void* arg);
116   static void OnClientHelloParseEnd(void* arg);
117
118   static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
119   static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
120   static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
121   static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
122   static void EnableSessionCallbacks(
123       const v8::FunctionCallbackInfo<v8::Value>& args);
124   static void EnableHelloParser(
125       const v8::FunctionCallbackInfo<v8::Value>& args);
126
127 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
128   static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
129   static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
130   static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
131 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
132
133   crypto::SecureContext* sc_;
134   v8::Persistent<v8::Object> sc_handle_;
135   BIO* enc_in_;
136   BIO* enc_out_;
137   NodeBIO* clear_in_;
138   uv_write_t write_req_;
139   size_t write_size_;
140   size_t write_queue_size_;
141   QUEUE write_item_queue_;
142   QUEUE pending_write_items_;
143   bool started_;
144   bool established_;
145   bool shutdown_;
146   const char* error_;
147
148   // If true - delivered EOF to the js-land, either after `close_notify`, or
149   // after the `UV_EOF` on socket.
150   bool eof_;
151
152 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
153   v8::Persistent<v8::Value> sni_context_;
154 #endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
155
156   static size_t error_off_;
157   static char error_buf_[1024];
158 };
159
160 }  // namespace node
161
162 #endif  // SRC_TLS_WRAP_H_