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: 166 172 96.5 %
Date: 2020-06-24 22:13:30 Branches: 45 92 48.9 %

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
4814
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
55
                                        AsyncWrap* parent,
56
                                        TCPWrap::SocketType type) {
57
4814
  EscapableHandleScope handle_scope(env->isolate());
58
9628
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
59
9628
  CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
60
9628
  Local<Function> constructor = env->tcp_constructor_template()
61
14442
                                    ->GetFunction(env->context())
62
4814
                                    .ToLocalChecked();
63
4814
  CHECK_EQ(constructor.IsEmpty(), false);
64
4814
  Local<Value> type_value = Int32::New(env->isolate(), type);
65
  return handle_scope.EscapeMaybe(
66
14442
      constructor->NewInstance(env->context(), 1, &type_value));
67
}
68
69
70
4176
void TCPWrap::Initialize(Local<Object> target,
71
                         Local<Value> unused,
72
                         Local<Context> context,
73
                         void* priv) {
74
4176
  Environment* env = Environment::GetCurrent(context);
75
76
4176
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
77
4176
  Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
78
4176
  t->SetClassName(tcpString);
79
8352
  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
80
81
  // Init properties
82
20880
  t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
83
4176
                             Boolean::New(env->isolate(), false));
84
16704
  t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
85
16704
  t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
86
87
8352
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
88
89
4176
  env->SetProtoMethod(t, "open", Open);
90
4176
  env->SetProtoMethod(t, "bind", Bind);
91
4176
  env->SetProtoMethod(t, "listen", Listen);
92
4176
  env->SetProtoMethod(t, "connect", Connect);
93
4176
  env->SetProtoMethod(t, "bind6", Bind6);
94
4176
  env->SetProtoMethod(t, "connect6", Connect6);
95
  env->SetProtoMethod(t, "getsockname",
96
4176
                      GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
97
  env->SetProtoMethod(t, "getpeername",
98
4176
                      GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
99
4176
  env->SetProtoMethod(t, "setNoDelay", SetNoDelay);
100
4176
  env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive);
101
102
#ifdef _WIN32
103
  env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
104
#endif
105
106
8352
  target->Set(env->context(),
107
              tcpString,
108
20880
              t->GetFunction(env->context()).ToLocalChecked()).Check();
109
4176
  env->set_tcp_constructor_template(t);
110
111
  // Create FunctionTemplate for TCPConnectWrap.
112
  Local<FunctionTemplate> cwt =
113
4176
      BaseObject::MakeLazilyInitializedJSTemplate(env);
114
8352
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
115
  Local<String> wrapString =
116
4176
      FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
117
4176
  cwt->SetClassName(wrapString);
118
8352
  target->Set(env->context(),
119
              wrapString,
120
20880
              cwt->GetFunction(env->context()).ToLocalChecked()).Check();
121
122
  // Define constants
123
4176
  Local<Object> constants = Object::New(env->isolate());
124
16704
  NODE_DEFINE_CONSTANT(constants, SOCKET);
125
16704
  NODE_DEFINE_CONSTANT(constants, SERVER);
126
16704
  NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
127
8352
  target->Set(context,
128
              env->constants_string(),
129
12528
              constants).Check();
130
4176
}
131
132
133
11561
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
11561
  CHECK(args.IsConstructCall());
138
23122
  CHECK(args[0]->IsInt32());
139
11561
  Environment* env = Environment::GetCurrent(args);
140
141
34683
  int type_value = args[0].As<Int32>()->Value();
142
11561
  TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value);
143
144
  ProviderType provider;
145
11561
  switch (type) {
146
    case SOCKET:
147
10048
      provider = PROVIDER_TCPWRAP;
148
10048
      break;
149
    case SERVER:
150
1513
      provider = PROVIDER_TCPSERVERWRAP;
151
1513
      break;
152
    default:
153
      UNREACHABLE();
154
  }
155
156
11561
  new TCPWrap(env, args.This(), provider);
157
11561
}
158
159
160
11561
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
161
11561
    : ConnectionWrap(env, object, provider) {
162
11561
  int r = uv_tcp_init(env->event_loop(), &handle_);
163
11561
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
164
                   // Suggestion: uv_tcp_init() returns void.
165
11561
}
166
167
168
637
void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
169
  TCPWrap* wrap;
170
637
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
171
                          args.Holder(),
172
                          args.GetReturnValue().Set(UV_EBADF));
173
1274
  int enable = static_cast<int>(args[0]->IsTrue());
174
637
  int err = uv_tcp_nodelay(&wrap->handle_, enable);
175
1274
  args.GetReturnValue().Set(err);
176
}
177
178
179
202
void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
180
  TCPWrap* wrap;
181
202
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
182
                          args.Holder(),
183
                          args.GetReturnValue().Set(UV_EBADF));
184
202
  Environment* env = wrap->env();
185
  int enable;
186
808
  if (!args[0]->Int32Value(env->context()).To(&enable)) return;
187
606
  unsigned int delay = static_cast<unsigned int>(args[1].As<Uint32>()->Value());
188
202
  int err = uv_tcp_keepalive(&wrap->handle_, enable, delay);
189
404
  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
1536
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

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

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



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

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

3072
  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
1397
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
259
1397
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
260
1397
}
261
262
263
1519
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
264
  TCPWrap* wrap;
265
1519
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
266
                          args.Holder(),
267
                          args.GetReturnValue().Set(UV_EBADF));
268
1519
  Environment* env = wrap->env();
269
  int backlog;
270
6076
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
271
1519
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
272
                      backlog,
273
1519
                      OnConnection);
274
3038
  args.GetReturnValue().Set(err);
275
}
276
277
278
4867
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
279
9734
  CHECK(args[2]->IsUint32());
280
  // explicit cast to fit to libuv's type expectation
281
14601
  int port = static_cast<int>(args[2].As<Uint32>()->Value());
282
9734
  Connect<sockaddr_in>(args,
283
4867
                       [port](const char* ip_address, sockaddr_in* addr) {
284
      return uv_ip4_addr(ip_address, port, addr);
285
9734
  });
286
4867
}
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
4879
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
302
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
303
4879
  Environment* env = Environment::GetCurrent(args);
304
305
  TCPWrap* wrap;
306

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

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

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

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

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