GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/tls_wrap.h Lines: 13 14 92.9 %
Date: 2017-10-21 Branches: 4 4 100.0 %

Line Branch Exec Source
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
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "node.h"
28
#include "node_crypto.h"  // SSLWrap
29
30
#include "async-wrap.h"
31
#include "env.h"
32
#include "stream_wrap.h"
33
#include "util.h"
34
#include "v8.h"
35
36
#include <openssl/ssl.h>
37
38
#include <string>
39
40
namespace node {
41
42
// Forward-declarations
43
class WriteWrap;
44
namespace crypto {
45
class SecureContext;
46
class NodeBIO;
47
}
48
49
class TLSWrap : public AsyncWrap,
50
                public crypto::SSLWrap<TLSWrap>,
51
                public StreamBase {
52
 public:
53
  ~TLSWrap() override;
54
55
  static void Initialize(v8::Local<v8::Object> target,
56
                         v8::Local<v8::Value> unused,
57
                         v8::Local<v8::Context> context);
58
59
  void* Cast() override;
60
  int GetFD() override;
61
  bool IsAlive() override;
62
  bool IsClosing() override;
63
64
  // JavaScript functions
65
  int ReadStart() override;
66
  int ReadStop() override;
67
68
  int DoShutdown(ShutdownWrap* req_wrap) override;
69
  int DoWrite(WriteWrap* w,
70
              uv_buf_t* bufs,
71
              size_t count,
72
              uv_stream_t* send_handle) override;
73
  const char* Error() const override;
74
  void ClearError() override;
75
76
  void NewSessionDoneCb();
77
78
  size_t self_size() const override { return sizeof(*this); }
79
80
761
  void clear_stream() { stream_ = nullptr; }
81
82
 protected:
83
  static const int kClearOutChunkSize = 16384;
84
85
  // Maximum number of bytes for hello parser
86
  static const int kMaxHelloLength = 16384;
87
88
  // Usual ServerHello + Certificate size
89
  static const int kInitialClientBufferLength = 4096;
90
91
  // Maximum number of buffers passed to uv_write()
92
  static const int kSimultaneousBufferCount = 10;
93
94
  // Write callback queue's item
95
  class WriteItem {
96
   public:
97
896
    explicit WriteItem(WriteWrap* w) : w_(w) {
98
896
    }
99
1790
    ~WriteItem() {
100
895
      w_ = nullptr;
101
895
    }
102
103
    WriteWrap* w_;
104
    ListNode<WriteItem> member_;
105
  };
106
107
  TLSWrap(Environment* env,
108
          Kind kind,
109
          StreamBase* stream,
110
          crypto::SecureContext* sc);
111
112
  static void SSLInfoCallback(const SSL* ssl_, int where, int ret);
113
  void InitSSL();
114
  void EncOut();
115
  static void EncOutCb(WriteWrap* req_wrap, int status);
116
  bool ClearIn();
117
  void ClearOut();
118
  void MakePending();
119
  bool InvokeQueued(int status, const char* error_str = nullptr);
120
121
2785
  inline void Cycle() {
122
    // Prevent recursion
123
2785
    if (++cycle_depth_ > 1)
124
2796
      return;
125
126
5553
    for (; cycle_depth_ > 0; cycle_depth_--) {
127
2785
      ClearIn();
128
2785
      ClearOut();
129
2782
      EncOut();
130
    }
131
  }
132
133
  AsyncWrap* GetAsyncWrap() override;
134
  bool IsIPCPipe() override;
135
  uint32_t UpdateWriteQueueSize(uint32_t write_queue_size = 0);
136
137
  // Resource implementation
138
  static void OnAfterWriteImpl(WriteWrap* w, void* ctx);
139
  static void OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx);
140
  static void OnReadImpl(ssize_t nread,
141
                         const uv_buf_t* buf,
142
                         uv_handle_type pending,
143
                         void* ctx);
144
  static void OnAfterWriteSelf(WriteWrap* w, void* ctx);
145
  static void OnAllocSelf(size_t size, uv_buf_t* buf, void* ctx);
146
  static void OnReadSelf(ssize_t nread,
147
                         const uv_buf_t* buf,
148
                         uv_handle_type pending,
149
                         void* ctx);
150
  static void OnDestructImpl(void* ctx);
151
152
  void DoRead(ssize_t nread, const uv_buf_t* buf, uv_handle_type pending);
153
154
  v8::Local<v8::Value> GetSSLError(int status, int* err, std::string* msg);
155
156
  static void OnClientHelloParseEnd(void* arg);
157
  static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
158
  static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
159
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
160
  static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
161
  static void EnableSessionCallbacks(
162
      const v8::FunctionCallbackInfo<v8::Value>& args);
163
  static void EnableCertCb(
164
      const v8::FunctionCallbackInfo<v8::Value>& args);
165
  static void DestroySSL(const v8::FunctionCallbackInfo<v8::Value>& args);
166
167
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
168
  static void GetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
169
  static void SetServername(const v8::FunctionCallbackInfo<v8::Value>& args);
170
  static int SelectSNIContextCallback(SSL* s, int* ad, void* arg);
171
#endif  // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
172
173
  crypto::SecureContext* sc_;
174
  StreamBase* stream_;
175
  BIO* enc_in_;
176
  BIO* enc_out_;
177
  crypto::NodeBIO* clear_in_;
178
  size_t write_size_;
179
  typedef ListHead<WriteItem, &WriteItem::member_> WriteItemList;
180
  WriteItemList write_item_queue_;
181
  WriteItemList pending_write_items_;
182
  bool started_;
183
  bool established_;
184
  bool shutdown_;
185
  std::string error_;
186
  int cycle_depth_;
187
188
  // If true - delivered EOF to the js-land, either after `close_notify`, or
189
  // after the `UV_EOF` on socket.
190
  bool eof_;
191
192
 private:
193
  static void UpdateWriteQueueSize(
194
      const v8::FunctionCallbackInfo<v8::Value>& args);
195
};
196
197
}  // namespace node
198
199
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
200
201
#endif  // SRC_TLS_WRAP_H_