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: 176 182 96.7 %
Date: 2020-08-07 22:13:19 Branches: 52 104 50.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::Int32;
46
using v8::Integer;
47
using v8::Local;
48
using v8::MaybeLocal;
49
using v8::Object;
50
using v8::String;
51
using v8::Uint32;
52
using v8::Value;
53
54
4802
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
55
                                        AsyncWrap* parent,
56
                                        TCPWrap::SocketType type) {
57
4802
  EscapableHandleScope handle_scope(env->isolate());
58
9604
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
59
9604
  CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
60
9604
  Local<Function> constructor = env->tcp_constructor_template()
61
14406
                                    ->GetFunction(env->context())
62
4802
                                    .ToLocalChecked();
63
4802
  CHECK_EQ(constructor.IsEmpty(), false);
64
4802
  Local<Value> type_value = Int32::New(env->isolate(), type);
65
  return handle_scope.EscapeMaybe(
66
14406
      constructor->NewInstance(env->context(), 1, &type_value));
67
}
68
69
70
4236
void TCPWrap::Initialize(Local<Object> target,
71
                         Local<Value> unused,
72
                         Local<Context> context,
73
                         void* priv) {
74
4236
  Environment* env = Environment::GetCurrent(context);
75
76
4236
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
77
4236
  Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
78
4236
  t->SetClassName(tcpString);
79
8472
  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
80
81
  // Init properties
82
21180
  t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
83
4236
                             Boolean::New(env->isolate(), false));
84
16944
  t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
85
16944
  t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
86
87
8472
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
88
89
4236
  env->SetProtoMethod(t, "open", Open);
90
4236
  env->SetProtoMethod(t, "bind", Bind);
91
4236
  env->SetProtoMethod(t, "listen", Listen);
92
4236
  env->SetProtoMethod(t, "connect", Connect);
93
4236
  env->SetProtoMethod(t, "bind6", Bind6);
94
4236
  env->SetProtoMethod(t, "connect6", Connect6);
95
  env->SetProtoMethod(t, "getsockname",
96
4236
                      GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
97
  env->SetProtoMethod(t, "getpeername",
98
4236
                      GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
99
4236
  env->SetProtoMethod(t, "setNoDelay", SetNoDelay);
100
4236
  env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive);
101
102
#ifdef _WIN32
103
  env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
104
#endif
105
106
8472
  target->Set(env->context(),
107
              tcpString,
108
21180
              t->GetFunction(env->context()).ToLocalChecked()).Check();
109
4236
  env->set_tcp_constructor_template(t);
110
111
  // Create FunctionTemplate for TCPConnectWrap.
112
  Local<FunctionTemplate> cwt =
113
4236
      BaseObject::MakeLazilyInitializedJSTemplate(env);
114
8472
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
115
  Local<String> wrapString =
116
4236
      FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
117
4236
  cwt->SetClassName(wrapString);
118
8472
  target->Set(env->context(),
119
              wrapString,
120
21180
              cwt->GetFunction(env->context()).ToLocalChecked()).Check();
121
122
  // Define constants
123
4236
  Local<Object> constants = Object::New(env->isolate());
124
16944
  NODE_DEFINE_CONSTANT(constants, SOCKET);
125
16944
  NODE_DEFINE_CONSTANT(constants, SERVER);
126
16944
  NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
127
8472
  target->Set(context,
128
              env->constants_string(),
129
12708
              constants).Check();
130
4236
}
131
132
133
11553
void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
134
  // This constructor should not be exposed to public javascript.
135
  // Therefore we assert that we are not trying to call this as a
136
  // normal function.
137
11553
  CHECK(args.IsConstructCall());
138
23106
  CHECK(args[0]->IsInt32());
139
11553
  Environment* env = Environment::GetCurrent(args);
140
141
34659
  int type_value = args[0].As<Int32>()->Value();
142
11553
  TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value);
143
144
  ProviderType provider;
145
11553
  switch (type) {
146
    case SOCKET:
147
10030
      provider = PROVIDER_TCPWRAP;
148
10030
      break;
149
    case SERVER:
150
1523
      provider = PROVIDER_TCPSERVERWRAP;
151
1523
      break;
152
    default:
153
      UNREACHABLE();
154
  }
155
156
11553
  new TCPWrap(env, args.This(), provider);
157
11553
}
158
159
160
11553
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
161
11553
    : ConnectionWrap(env, object, provider) {
162
11553
  int r = uv_tcp_init(env->event_loop(), &handle_);
163
11553
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
164
                   // Suggestion: uv_tcp_init() returns void.
165
11553
}
166
167
168
625
void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
169
  TCPWrap* wrap;
170
625
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
171
                          args.Holder(),
172
                          args.GetReturnValue().Set(UV_EBADF));
173
1250
  int enable = static_cast<int>(args[0]->IsTrue());
174
625
  int err = uv_tcp_nodelay(&wrap->handle_, enable);
175
1250
  args.GetReturnValue().Set(err);
176
}
177
178
179
146
void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
180
  TCPWrap* wrap;
181
146
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
182
                          args.Holder(),
183
                          args.GetReturnValue().Set(UV_EBADF));
184
146
  Environment* env = wrap->env();
185
  int enable;
186
584
  if (!args[0]->Int32Value(env->context()).To(&enable)) return;
187
438
  unsigned int delay = static_cast<unsigned int>(args[1].As<Uint32>()->Value());
188
146
  int err = uv_tcp_keepalive(&wrap->handle_, enable, delay);
189
292
  args.GetReturnValue().Set(err);
190
}
191
192
193
#ifdef _WIN32
194
void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
195
  TCPWrap* wrap;
196
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
197
                          args.Holder(),
198
                          args.GetReturnValue().Set(UV_EBADF));
199
  bool enable = args[0]->IsTrue();
200
  int err = uv_tcp_simultaneous_accepts(&wrap->handle_, enable);
201
  args.GetReturnValue().Set(err);
202
}
203
#endif
204
205
206
2
void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
207
  TCPWrap* wrap;
208
2
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
209
                          args.Holder(),
210
                          args.GetReturnValue().Set(UV_EBADF));
211
  int64_t val;
212
8
  if (!args[0]->IntegerValue(args.GetIsolate()->GetCurrentContext()).To(&val))
213
    return;
214
2
  int fd = static_cast<int>(val);
215
2
  int err = uv_tcp_open(&wrap->handle_, fd);
216
217
2
  if (err == 0)
218
2
    wrap->set_fd(fd);
219
220
4
  args.GetReturnValue().Set(err);
221
}
222
223
template <typename T>
224
1546
void TCPWrap::Bind(
225
    const FunctionCallbackInfo<Value>& args,
226
    int family,
227
    std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) {
228
  TCPWrap* wrap;
229

1546
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
230
                          args.Holder(),
231
                          args.GetReturnValue().Set(UV_EBADF));
232
1546
  Environment* env = wrap->env();
233
3092
  node::Utf8Value ip_address(env->isolate(), args[0]);
234
  int port;
235
1546
  unsigned int flags = 0;
236

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



4499
  if (family == AF_INET6 &&
238
7174
      !args[2]->Uint32Value(env->context()).To(&flags)) {
239
    return;
240
  }
241
242
  T addr;
243
1546
  int err = uv_ip_addr(*ip_address, port, &addr);
244
245

1546
  if (err == 0) {
246
1546
    err = uv_tcp_bind(&wrap->handle_,
247
                      reinterpret_cast<const sockaddr*>(&addr),
248
                      flags);
249
  }
250

3092
  args.GetReturnValue().Set(err);
251
}
252
253
139
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
254
139
  Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
255
139
}
256
257
258
1407
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
259
1407
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
260
1407
}
261
262
263
1529
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
264
  TCPWrap* wrap;
265
1529
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
266
                          args.Holder(),
267
                          args.GetReturnValue().Set(UV_EBADF));
268
1529
  Environment* env = wrap->env();
269
  int backlog;
270
6116
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
271
1529
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
272
                      backlog,
273
1529
                      OnConnection);
274
3058
  args.GetReturnValue().Set(err);
275
}
276
277
278
4843
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
279
9686
  CHECK(args[2]->IsUint32());
280
  // explicit cast to fit to libuv's type expectation
281
14529
  int port = static_cast<int>(args[2].As<Uint32>()->Value());
282
9686
  Connect<sockaddr_in>(args,
283
4843
                       [port](const char* ip_address, sockaddr_in* addr) {
284
      return uv_ip4_addr(ip_address, port, addr);
285
9686
  });
286
4843
}
287
288
289
12
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
290
12
  Environment* env = Environment::GetCurrent(args);
291
24
  CHECK(args[2]->IsUint32());
292
  int port;
293
48
  if (!args[2]->Int32Value(env->context()).To(&port)) return;
294
24
  Connect<sockaddr_in6>(args,
295
12
                        [port](const char* ip_address, sockaddr_in6* addr) {
296
      return uv_ip6_addr(ip_address, port, addr);
297
24
  });
298
}
299
300
template <typename T>
301
4855
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
302
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
303
4855
  Environment* env = Environment::GetCurrent(args);
304
305
  TCPWrap* wrap;
306

4855
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
307
                          args.Holder(),
308
                          args.GetReturnValue().Set(UV_EBADF));
309
310

9710
  CHECK(args[0]->IsObject());
311

14565
  CHECK(args[1]->IsString());
312
313
9710
  Local<Object> req_wrap_obj = args[0].As<Object>();
314
9710
  node::Utf8Value ip_address(env->isolate(), args[1]);
315
316
  T addr;
317
4855
  int err = uv_ip_addr(*ip_address, &addr);
318
319

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

4855
    if (err)
328
      delete req_wrap;
329
  }
330
331
9710
  args.GetReturnValue().Set(err);
332
}
333
334
335
// also used by udp_wrap.cc
336
4568
Local<Object> AddressToJS(Environment* env,
337
                          const sockaddr* addr,
338
                          Local<Object> info) {
339
4568
  EscapableHandleScope scope(env->isolate());
340
  char ip[INET6_ADDRSTRLEN + UV_IF_NAMESIZE];
341
  const sockaddr_in* a4;
342
  const sockaddr_in6* a6;
343
344
  int port;
345
346
4568
  if (info.IsEmpty())
347
512
    info = Object::New(env->isolate());
348
349
4568
  switch (addr->sa_family) {
350
  case AF_INET6:
351
3782
    a6 = reinterpret_cast<const sockaddr_in6*>(addr);
352
3782
    uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
353
    // Add an interface identifier to a link local address.
354
3782
    if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) {
355
2
        const size_t addrlen = strlen(ip);
356
2
        CHECK_LT(addrlen, sizeof(ip));
357
2
        ip[addrlen] = '%';
358
2
        size_t scopeidlen = sizeof(ip) - addrlen - 1;
359
2
        CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
360
2
        const int r = uv_if_indextoiid(a6->sin6_scope_id,
361
2
                                       ip + addrlen + 1,
362
2
                                       &scopeidlen);
363
2
        CHECK_EQ(r, 0);
364
    }
365
3782
    port = ntohs(a6->sin6_port);
366
7564
    info->Set(env->context(),
367
              env->address_string(),
368
18910
              OneByteString(env->isolate(), ip)).Check();
369
7564
    info->Set(env->context(),
370
              env->family_string(),
371
18910
              env->ipv6_string()).Check();
372
7564
    info->Set(env->context(),
373
              env->port_string(),
374
18910
              Integer::New(env->isolate(), port)).Check();
375
3782
    break;
376
377
  case AF_INET:
378
786
    a4 = reinterpret_cast<const sockaddr_in*>(addr);
379
786
    uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
380
786
    port = ntohs(a4->sin_port);
381
1572
    info->Set(env->context(),
382
              env->address_string(),
383
3930
              OneByteString(env->isolate(), ip)).Check();
384
1572
    info->Set(env->context(),
385
              env->family_string(),
386
3930
              env->ipv4_string()).Check();
387
1572
    info->Set(env->context(),
388
              env->port_string(),
389
3930
              Integer::New(env->isolate(), port)).Check();
390
786
    break;
391
392
  default:
393
    info->Set(env->context(),
394
              env->address_string(),
395
              String::Empty(env->isolate())).Check();
396
  }
397
398
4568
  return scope.Escape(info);
399
}
400
401
402
}  // namespace node
403
404

17763
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)