GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/tcp_wrap.cc Lines: 157 162 96.9 %
Date: 2019-03-02 22:23:06 Branches: 49 100 49.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
#include "tcp_wrap.h"
23
24
#include "connection_wrap.h"
25
#include "env-inl.h"
26
#include "handle_wrap.h"
27
#include "node_buffer.h"
28
#include "node_internals.h"
29
#include "connect_wrap.h"
30
#include "stream_base-inl.h"
31
#include "stream_wrap.h"
32
#include "util-inl.h"
33
34
#include <cstdlib>
35
36
37
namespace node {
38
39
using v8::Boolean;
40
using v8::Context;
41
using v8::EscapableHandleScope;
42
using v8::Function;
43
using v8::FunctionCallbackInfo;
44
using v8::FunctionTemplate;
45
using v8::HandleScope;
46
using v8::Int32;
47
using v8::Integer;
48
using v8::Local;
49
using v8::MaybeLocal;
50
using v8::Object;
51
using v8::String;
52
using v8::Uint32;
53
using v8::Value;
54
55
using AsyncHooks = Environment::AsyncHooks;
56
57
21634
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
58
                                        AsyncWrap* parent,
59
                                        TCPWrap::SocketType type) {
60
21634
  EscapableHandleScope handle_scope(env->isolate());
61
43268
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
62
43268
  CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
63
  Local<Function> constructor = env->tcp_constructor_template()
64
64902
                                    ->GetFunction(env->context())
65
43268
                                    .ToLocalChecked();
66
21634
  CHECK_EQ(constructor.IsEmpty(), false);
67
21634
  Local<Value> type_value = Int32::New(env->isolate(), type);
68
  return handle_scope.EscapeMaybe(
69
64902
      constructor->NewInstance(env->context(), 1, &type_value));
70
}
71
72
73
4052
void TCPWrap::Initialize(Local<Object> target,
74
                         Local<Value> unused,
75
                         Local<Context> context,
76
                         void* priv) {
77
4052
  Environment* env = Environment::GetCurrent(context);
78
79
4052
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
80
4052
  Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
81
4052
  t->SetClassName(tcpString);
82
8104
  t->InstanceTemplate()->SetInternalFieldCount(1);
83
84
  // Init properties
85
12156
  t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
86
16208
                             Boolean::New(env->isolate(), false));
87
16208
  t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
88
16208
  t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
89
16208
  t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
90
91
8104
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
92
93
4052
  env->SetProtoMethod(t, "open", Open);
94
4052
  env->SetProtoMethod(t, "bind", Bind);
95
4052
  env->SetProtoMethod(t, "listen", Listen);
96
4052
  env->SetProtoMethod(t, "connect", Connect);
97
4052
  env->SetProtoMethod(t, "bind6", Bind6);
98
4052
  env->SetProtoMethod(t, "connect6", Connect6);
99
  env->SetProtoMethod(t, "getsockname",
100
4052
                      GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
101
  env->SetProtoMethod(t, "getpeername",
102
4052
                      GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
103
4052
  env->SetProtoMethod(t, "setNoDelay", SetNoDelay);
104
4052
  env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive);
105
106
#ifdef _WIN32
107
  env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
108
#endif
109
110
  target->Set(env->context(),
111
              tcpString,
112
20260
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
113
4052
  env->set_tcp_constructor_template(t);
114
115
  // Create FunctionTemplate for TCPConnectWrap.
116
  Local<FunctionTemplate> cwt =
117
4052
      BaseObject::MakeLazilyInitializedJSTemplate(env);
118
8104
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
119
  Local<String> wrapString =
120
4052
      FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
121
4052
  cwt->SetClassName(wrapString);
122
  target->Set(env->context(),
123
              wrapString,
124
20260
              cwt->GetFunction(env->context()).ToLocalChecked()).FromJust();
125
126
  // Define constants
127
4052
  Local<Object> constants = Object::New(env->isolate());
128
16208
  NODE_DEFINE_CONSTANT(constants, SOCKET);
129
16208
  NODE_DEFINE_CONSTANT(constants, SERVER);
130
16208
  NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
131
  target->Set(context,
132
              env->constants_string(),
133
12156
              constants).FromJust();
134
4052
}
135
136
137
28051
void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
138
  // This constructor should not be exposed to public javascript.
139
  // Therefore we assert that we are not trying to call this as a
140
  // normal function.
141
28051
  CHECK(args.IsConstructCall());
142
56102
  CHECK(args[0]->IsInt32());
143
28051
  Environment* env = Environment::GetCurrent(args);
144
145
84153
  int type_value = args[0].As<Int32>()->Value();
146
28051
  TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value);
147
148
  ProviderType provider;
149
28051
  switch (type) {
150
    case SOCKET:
151
26914
      provider = PROVIDER_TCPWRAP;
152
26914
      break;
153
    case SERVER:
154
1137
      provider = PROVIDER_TCPSERVERWRAP;
155
1137
      break;
156
    default:
157
      UNREACHABLE();
158
  }
159
160
28051
  new TCPWrap(env, args.This(), provider);
161
28051
}
162
163
164
28051
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
165
28051
    : ConnectionWrap(env, object, provider) {
166
28051
  int r = uv_tcp_init(env->event_loop(), &handle_);
167
28051
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
168
                   // Suggestion: uv_tcp_init() returns void.
169
28051
}
170
171
172
560
void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
173
  TCPWrap* wrap;
174
1120
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
175
                          args.Holder(),
176
                          args.GetReturnValue().Set(UV_EBADF));
177
1120
  int enable = static_cast<int>(args[0]->IsTrue());
178
560
  int err = uv_tcp_nodelay(&wrap->handle_, enable);
179
1120
  args.GetReturnValue().Set(err);
180
}
181
182
183
99
void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
184
  TCPWrap* wrap;
185
99
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
186
                          args.Holder(),
187
                          args.GetReturnValue().Set(UV_EBADF));
188
99
  Environment* env = wrap->env();
189
  int enable;
190
396
  if (!args[0]->Int32Value(env->context()).To(&enable)) return;
191
297
  unsigned int delay = args[1].As<Uint32>()->Value();
192
99
  int err = uv_tcp_keepalive(&wrap->handle_, enable, delay);
193
198
  args.GetReturnValue().Set(err);
194
}
195
196
197
#ifdef _WIN32
198
void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
199
  TCPWrap* wrap;
200
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
201
                          args.Holder(),
202
                          args.GetReturnValue().Set(UV_EBADF));
203
  bool enable = args[0]->IsTrue();
204
  int err = uv_tcp_simultaneous_accepts(&wrap->handle_, enable);
205
  args.GetReturnValue().Set(err);
206
}
207
#endif
208
209
210
2
void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
211
  TCPWrap* wrap;
212
2
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
213
                          args.Holder(),
214
                          args.GetReturnValue().Set(UV_EBADF));
215
  int64_t val;
216
8
  if (!args[0]->IntegerValue(args.GetIsolate()->GetCurrentContext()).To(&val))
217
    return;
218
2
  int fd = static_cast<int>(val);
219
2
  int err = uv_tcp_open(&wrap->handle_, fd);
220
221
2
  if (err == 0)
222
2
    wrap->set_fd(fd);
223
224
4
  args.GetReturnValue().Set(err);
225
}
226
227
template <typename T>
228
1157
void TCPWrap::Bind(
229
    const FunctionCallbackInfo<Value>& args,
230
    int family,
231
    std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) {
232
  TCPWrap* wrap;
233

1157
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
234
                          args.Holder(),
235
                          args.GetReturnValue().Set(UV_EBADF));
236
1157
  Environment* env = wrap->env();
237
1157
  node::Utf8Value ip_address(env->isolate(), args[0]);
238
  int port;
239
1157
  unsigned int flags = 0;
240

4628
  if (!args[1]->Int32Value(env->context()).To(&port)) return;
241



4489
  if (family == AF_INET6 &&
242


6247
      !args[2]->Uint32Value(env->context()).To(&flags)) {
243
    return;
244
  }
245
246
  T addr;
247
1157
  int err = uv_ip_addr(*ip_address, port, &addr);
248
249

1157
  if (err == 0) {
250
1157
    err = uv_tcp_bind(&wrap->handle_,
251
                      reinterpret_cast<const sockaddr*>(&addr),
252
1157
                      flags);
253
  }
254

2314
  args.GetReturnValue().Set(err);
255
}
256
257
139
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
258
139
  Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
259
139
}
260
261
262
1018
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
263
1018
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
264
1018
}
265
266
267
1143
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
268
  TCPWrap* wrap;
269
1143
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
270
                          args.Holder(),
271
                          args.GetReturnValue().Set(UV_EBADF));
272
1143
  Environment* env = wrap->env();
273
  int backlog;
274
4572
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
275
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
276
                      backlog,
277
1143
                      OnConnection);
278
2286
  args.GetReturnValue().Set(err);
279
}
280
281
282
4924
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
283
9848
  CHECK(args[2]->IsUint32());
284
14772
  int port = args[2].As<Uint32>()->Value();
285
  Connect<sockaddr_in>(args,
286
4924
                       [port](const char* ip_address, sockaddr_in* addr) {
287
4924
      return uv_ip4_addr(ip_address, port, addr);
288
9848
  });
289
4924
}
290
291
292
11
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
293
11
  Environment* env = Environment::GetCurrent(args);
294
22
  CHECK(args[2]->IsUint32());
295
  int port;
296
55
  if (!args[2]->Int32Value(env->context()).To(&port)) return;
297
  Connect<sockaddr_in6>(args,
298
11
                        [port](const char* ip_address, sockaddr_in6* addr) {
299
11
      return uv_ip6_addr(ip_address, port, addr);
300
22
  });
301
}
302
303
template <typename T>
304
4935
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
305
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
306
4935
  Environment* env = Environment::GetCurrent(args);
307
308
  TCPWrap* wrap;
309

9870
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
310
                          args.Holder(),
311
                          args.GetReturnValue().Set(UV_EBADF));
312
313

9870
  CHECK(args[0]->IsObject());
314

14805
  CHECK(args[1]->IsString());
315
316
9870
  Local<Object> req_wrap_obj = args[0].As<Object>();
317
4935
  node::Utf8Value ip_address(env->isolate(), args[1]);
318
319
  T addr;
320
4935
  int err = uv_ip_addr(*ip_address, &addr);
321
322

4935
  if (err == 0) {
323
4935
    AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
324
    ConnectWrap* req_wrap =
325
4935
        new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
326
4935
    err = req_wrap->Dispatch(uv_tcp_connect,
327
                             &wrap->handle_,
328
                             reinterpret_cast<const sockaddr*>(&addr),
329
4935
                             AfterConnect);
330

4935
    if (err)
331
      delete req_wrap;
332
  }
333
334
9870
  args.GetReturnValue().Set(err);
335
}
336
337
338
// also used by udp_wrap.cc
339
11563
Local<Object> AddressToJS(Environment* env,
340
                          const sockaddr* addr,
341
                          Local<Object> info) {
342
11563
  EscapableHandleScope scope(env->isolate());
343
  char ip[INET6_ADDRSTRLEN];
344
  const sockaddr_in* a4;
345
  const sockaddr_in6* a6;
346
  int port;
347
348
11563
  if (info.IsEmpty())
349
8011
    info = Object::New(env->isolate());
350
351
11563
  switch (addr->sa_family) {
352
  case AF_INET6:
353
3283
    a6 = reinterpret_cast<const sockaddr_in6*>(addr);
354
3283
    uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
355
3283
    port = ntohs(a6->sin6_port);
356
    info->Set(env->context(),
357
              env->address_string(),
358
16415
              OneByteString(env->isolate(), ip)).FromJust();
359
    info->Set(env->context(),
360
              env->family_string(),
361
16415
              env->ipv6_string()).FromJust();
362
    info->Set(env->context(),
363
              env->port_string(),
364
16415
              Integer::New(env->isolate(), port)).FromJust();
365
3283
    break;
366
367
  case AF_INET:
368
8280
    a4 = reinterpret_cast<const sockaddr_in*>(addr);
369
8280
    uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
370
8280
    port = ntohs(a4->sin_port);
371
    info->Set(env->context(),
372
              env->address_string(),
373
41400
              OneByteString(env->isolate(), ip)).FromJust();
374
    info->Set(env->context(),
375
              env->family_string(),
376
41400
              env->ipv4_string()).FromJust();
377
    info->Set(env->context(),
378
              env->port_string(),
379
41400
              Integer::New(env->isolate(), port)).FromJust();
380
8280
    break;
381
382
  default:
383
    info->Set(env->context(),
384
              env->address_string(),
385
              String::Empty(env->isolate())).FromJust();
386
  }
387
388
11563
  return scope.Escape(info);
389
}
390
391
392
}  // namespace node
393
394
4292
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)