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-02-23 22:23:05 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 <stdlib.h>
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
21943
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
58
                                        AsyncWrap* parent,
59
                                        TCPWrap::SocketType type) {
60
21943
  EscapableHandleScope handle_scope(env->isolate());
61
43886
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
62
43886
  CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
63
  Local<Function> constructor = env->tcp_constructor_template()
64
65829
                                    ->GetFunction(env->context())
65
43886
                                    .ToLocalChecked();
66
21943
  CHECK_EQ(constructor.IsEmpty(), false);
67
21943
  Local<Value> type_value = Int32::New(env->isolate(), type);
68
  return handle_scope.EscapeMaybe(
69
65829
      constructor->NewInstance(env->context(), 1, &type_value));
70
}
71
72
73
4034
void TCPWrap::Initialize(Local<Object> target,
74
                         Local<Value> unused,
75
                         Local<Context> context,
76
                         void* priv) {
77
4034
  Environment* env = Environment::GetCurrent(context);
78
79
4034
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
80
4034
  Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
81
4034
  t->SetClassName(tcpString);
82
8068
  t->InstanceTemplate()->SetInternalFieldCount(1);
83
84
  // Init properties
85
12102
  t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
86
16136
                             Boolean::New(env->isolate(), false));
87
16136
  t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
88
16136
  t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
89
16136
  t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
90
91
8068
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
92
93
4034
  env->SetProtoMethod(t, "open", Open);
94
4034
  env->SetProtoMethod(t, "bind", Bind);
95
4034
  env->SetProtoMethod(t, "listen", Listen);
96
4034
  env->SetProtoMethod(t, "connect", Connect);
97
4034
  env->SetProtoMethod(t, "bind6", Bind6);
98
4034
  env->SetProtoMethod(t, "connect6", Connect6);
99
  env->SetProtoMethod(t, "getsockname",
100
4034
                      GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
101
  env->SetProtoMethod(t, "getpeername",
102
4034
                      GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
103
4034
  env->SetProtoMethod(t, "setNoDelay", SetNoDelay);
104
4034
  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
20170
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
113
4034
  env->set_tcp_constructor_template(t);
114
115
  // Create FunctionTemplate for TCPConnectWrap.
116
  Local<FunctionTemplate> cwt =
117
4034
      BaseObject::MakeLazilyInitializedJSTemplate(env);
118
8068
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
119
  Local<String> wrapString =
120
4034
      FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
121
4034
  cwt->SetClassName(wrapString);
122
  target->Set(env->context(),
123
              wrapString,
124
20170
              cwt->GetFunction(env->context()).ToLocalChecked()).FromJust();
125
126
  // Define constants
127
4034
  Local<Object> constants = Object::New(env->isolate());
128
16136
  NODE_DEFINE_CONSTANT(constants, SOCKET);
129
16136
  NODE_DEFINE_CONSTANT(constants, SERVER);
130
16136
  NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
131
  target->Set(context,
132
              env->constants_string(),
133
12102
              constants).FromJust();
134
4034
}
135
136
137
28358
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
28358
  CHECK(args.IsConstructCall());
142
56716
  CHECK(args[0]->IsInt32());
143
28358
  Environment* env = Environment::GetCurrent(args);
144
145
85074
  int type_value = args[0].As<Int32>()->Value();
146
28358
  TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value);
147
148
  ProviderType provider;
149
28358
  switch (type) {
150
    case SOCKET:
151
27222
      provider = PROVIDER_TCPWRAP;
152
27222
      break;
153
    case SERVER:
154
1136
      provider = PROVIDER_TCPSERVERWRAP;
155
1136
      break;
156
    default:
157
      UNREACHABLE();
158
  }
159
160
28358
  new TCPWrap(env, args.This(), provider);
161
28358
}
162
163
164
28358
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
165
28358
    : ConnectionWrap(env, object, provider) {
166
28358
  int r = uv_tcp_init(env->event_loop(), &handle_);
167
28358
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
168
                   // Suggestion: uv_tcp_init() returns void.
169
28358
}
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
1156
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

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

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



4485
  if (family == AF_INET6 &&
242


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

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

2312
  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
1017
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
263
1017
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
264
1017
}
265
266
267
1142
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
268
  TCPWrap* wrap;
269
1142
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
270
                          args.Holder(),
271
                          args.GetReturnValue().Set(UV_EBADF));
272
1142
  Environment* env = wrap->env();
273
  int backlog;
274
4568
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
275
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
276
                      backlog,
277
1142
                      OnConnection);
278
2284
  args.GetReturnValue().Set(err);
279
}
280
281
282
4936
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
283
9872
  CHECK(args[2]->IsUint32());
284
14808
  int port = args[2].As<Uint32>()->Value();
285
  Connect<sockaddr_in>(args,
286
4936
                       [port](const char* ip_address, sockaddr_in* addr) {
287
4936
      return uv_ip4_addr(ip_address, port, addr);
288
9872
  });
289
4936
}
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
4947
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
305
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
306
4947
  Environment* env = Environment::GetCurrent(args);
307
308
  TCPWrap* wrap;
309

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

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

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

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

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