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

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

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



4233
  if (family == AF_INET6 &&
243


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

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

2184
  args.GetReturnValue().Set(err);
256
}
257
258
135
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
259
135
  Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
260
135
}
261
262
263
957
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
264
957
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
265
957
}
266
267
268
1078
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
269
  TCPWrap* wrap;
270
1078
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
271
                          args.Holder(),
272
                          args.GetReturnValue().Set(UV_EBADF));
273
1078
  Environment* env = wrap->env();
274
  int backlog;
275
4312
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
276
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
277
                      backlog,
278
1078
                      OnConnection);
279
2156
  args.GetReturnValue().Set(err);
280
}
281
282
283
4350
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
284
8700
  CHECK(args[2]->IsUint32());
285
13050
  int port = args[2].As<Uint32>()->Value();
286
  Connect<sockaddr_in>(args,
287
4350
                       [port](const char* ip_address, sockaddr_in* addr) {
288
4350
      return uv_ip4_addr(ip_address, port, addr);
289
8700
  });
290
4350
}
291
292
293
10
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
294
10
  Environment* env = Environment::GetCurrent(args);
295
20
  CHECK(args[2]->IsUint32());
296
  int port;
297
50
  if (!args[2]->Int32Value(env->context()).To(&port)) return;
298
  Connect<sockaddr_in6>(args,
299
10
                        [port](const char* ip_address, sockaddr_in6* addr) {
300
10
      return uv_ip6_addr(ip_address, port, addr);
301
20
  });
302
}
303
304
template <typename T>
305
4360
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
306
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
307
4360
  Environment* env = Environment::GetCurrent(args);
308
309
  TCPWrap* wrap;
310

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

8720
  CHECK(args[0]->IsObject());
315

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

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

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