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

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

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



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

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

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

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

9766
  CHECK(args[0]->IsObject());
312

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

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

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