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

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

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



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

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

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

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

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

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

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

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

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