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: 36 157 22.9 %
Date: 2019-02-01 22:03:38 Branches: 0 100 0.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
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
58
                                        AsyncWrap* parent,
59
                                        TCPWrap::SocketType type) {
60
  EscapableHandleScope handle_scope(env->isolate());
61
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
62
  CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false);
63
  Local<Function> constructor = env->tcp_constructor_template()
64
                                    ->GetFunction(env->context())
65
                                    .ToLocalChecked();
66
  CHECK_EQ(constructor.IsEmpty(), false);
67
  Local<Value> type_value = Int32::New(env->isolate(), type);
68
  return handle_scope.EscapeMaybe(
69
      constructor->NewInstance(env->context(), 1, &type_value));
70
}
71
72
73
156
void TCPWrap::Initialize(Local<Object> target,
74
                         Local<Value> unused,
75
                         Local<Context> context,
76
                         void* priv) {
77
156
  Environment* env = Environment::GetCurrent(context);
78
79
156
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
80
156
  Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
81
156
  t->SetClassName(tcpString);
82
312
  t->InstanceTemplate()->SetInternalFieldCount(1);
83
84
  // Init properties
85
468
  t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
86
624
                             Boolean::New(env->isolate(), false));
87
624
  t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
88
624
  t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
89
624
  t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
90
91
312
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
92
93
156
  env->SetProtoMethod(t, "open", Open);
94
156
  env->SetProtoMethod(t, "bind", Bind);
95
156
  env->SetProtoMethod(t, "listen", Listen);
96
156
  env->SetProtoMethod(t, "connect", Connect);
97
156
  env->SetProtoMethod(t, "bind6", Bind6);
98
156
  env->SetProtoMethod(t, "connect6", Connect6);
99
  env->SetProtoMethod(t, "getsockname",
100
156
                      GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>);
101
  env->SetProtoMethod(t, "getpeername",
102
156
                      GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>);
103
156
  env->SetProtoMethod(t, "setNoDelay", SetNoDelay);
104
156
  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
780
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
113
156
  env->set_tcp_constructor_template(t);
114
115
  // Create FunctionTemplate for TCPConnectWrap.
116
  Local<FunctionTemplate> cwt =
117
156
      BaseObject::MakeLazilyInitializedJSTemplate(env);
118
312
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
119
  Local<String> wrapString =
120
156
      FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap");
121
156
  cwt->SetClassName(wrapString);
122
  target->Set(env->context(),
123
              wrapString,
124
780
              cwt->GetFunction(env->context()).ToLocalChecked()).FromJust();
125
126
  // Define constants
127
156
  Local<Object> constants = Object::New(env->isolate());
128
624
  NODE_DEFINE_CONSTANT(constants, SOCKET);
129
624
  NODE_DEFINE_CONSTANT(constants, SERVER);
130
624
  NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
131
  target->Set(context,
132
              env->constants_string(),
133
468
              constants).FromJust();
134
156
}
135
136
137
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
  CHECK(args.IsConstructCall());
142
  CHECK(args[0]->IsInt32());
143
  Environment* env = Environment::GetCurrent(args);
144
145
  int type_value = args[0].As<Int32>()->Value();
146
  TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value);
147
148
  ProviderType provider;
149
  switch (type) {
150
    case SOCKET:
151
      provider = PROVIDER_TCPWRAP;
152
      break;
153
    case SERVER:
154
      provider = PROVIDER_TCPSERVERWRAP;
155
      break;
156
    default:
157
      UNREACHABLE();
158
  }
159
160
  new TCPWrap(env, args.This(), provider);
161
}
162
163
164
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
165
    : ConnectionWrap(env, object, provider) {
166
  int r = uv_tcp_init(env->event_loop(), &handle_);
167
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
168
                   // Suggestion: uv_tcp_init() returns void.
169
}
170
171
172
void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
173
  TCPWrap* wrap;
174
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
175
                          args.Holder(),
176
                          args.GetReturnValue().Set(UV_EBADF));
177
  int enable = static_cast<int>(args[0]->IsTrue());
178
  int err = uv_tcp_nodelay(&wrap->handle_, enable);
179
  args.GetReturnValue().Set(err);
180
}
181
182
183
void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
184
  TCPWrap* wrap;
185
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
186
                          args.Holder(),
187
                          args.GetReturnValue().Set(UV_EBADF));
188
  Environment* env = wrap->env();
189
  int enable;
190
  if (!args[0]->Int32Value(env->context()).To(&enable)) return;
191
  unsigned int delay = args[1].As<Uint32>()->Value();
192
  int err = uv_tcp_keepalive(&wrap->handle_, enable, delay);
193
  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
void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
211
  TCPWrap* wrap;
212
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
213
                          args.Holder(),
214
                          args.GetReturnValue().Set(UV_EBADF));
215
  int64_t val;
216
  if (!args[0]->IntegerValue(args.GetIsolate()->GetCurrentContext()).To(&val))
217
    return;
218
  int fd = static_cast<int>(val);
219
  int err = uv_tcp_open(&wrap->handle_, fd);
220
221
  if (err == 0)
222
    wrap->set_fd(fd);
223
224
  args.GetReturnValue().Set(err);
225
}
226
227
template <typename T>
228
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
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
234
                          args.Holder(),
235
                          args.GetReturnValue().Set(UV_EBADF));
236
  Environment* env = wrap->env();
237
  node::Utf8Value ip_address(env->isolate(), args[0]);
238
  int port;
239
  unsigned int flags = 0;
240
  if (!args[1]->Int32Value(env->context()).To(&port)) return;
241
  if (family == AF_INET6 &&
242
      !args[2]->Uint32Value(env->context()).To(&flags)) {
243
    return;
244
  }
245
246
  T addr;
247
  int err = uv_ip_addr(*ip_address, port, &addr);
248
249
  if (err == 0) {
250
    err = uv_tcp_bind(&wrap->handle_,
251
                      reinterpret_cast<const sockaddr*>(&addr),
252
                      flags);
253
  }
254
  args.GetReturnValue().Set(err);
255
}
256
257
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
258
  Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
259
}
260
261
262
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
263
  Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
264
}
265
266
267
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
268
  TCPWrap* wrap;
269
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
270
                          args.Holder(),
271
                          args.GetReturnValue().Set(UV_EBADF));
272
  Environment* env = wrap->env();
273
  int backlog;
274
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
275
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
276
                      backlog,
277
                      OnConnection);
278
  args.GetReturnValue().Set(err);
279
}
280
281
282
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
283
  CHECK(args[2]->IsUint32());
284
  int port = args[2].As<Uint32>()->Value();
285
  Connect<sockaddr_in>(args,
286
                       [port](const char* ip_address, sockaddr_in* addr) {
287
      return uv_ip4_addr(ip_address, port, addr);
288
  });
289
}
290
291
292
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
293
  Environment* env = Environment::GetCurrent(args);
294
  CHECK(args[2]->IsUint32());
295
  int port;
296
  if (!args[2]->Int32Value(env->context()).To(&port)) return;
297
  Connect<sockaddr_in6>(args,
298
                        [port](const char* ip_address, sockaddr_in6* addr) {
299
      return uv_ip6_addr(ip_address, port, addr);
300
  });
301
}
302
303
template <typename T>
304
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
305
    std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
306
  Environment* env = Environment::GetCurrent(args);
307
308
  TCPWrap* wrap;
309
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
310
                          args.Holder(),
311
                          args.GetReturnValue().Set(UV_EBADF));
312
313
  CHECK(args[0]->IsObject());
314
  CHECK(args[1]->IsString());
315
316
  Local<Object> req_wrap_obj = args[0].As<Object>();
317
  node::Utf8Value ip_address(env->isolate(), args[1]);
318
319
  T addr;
320
  int err = uv_ip_addr(*ip_address, &addr);
321
322
  if (err == 0) {
323
    AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
324
    ConnectWrap* req_wrap =
325
        new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
326
    err = req_wrap->Dispatch(uv_tcp_connect,
327
                             &wrap->handle_,
328
                             reinterpret_cast<const sockaddr*>(&addr),
329
                             AfterConnect);
330
    if (err)
331
      delete req_wrap;
332
  }
333
334
  args.GetReturnValue().Set(err);
335
}
336
337
338
// also used by udp_wrap.cc
339
Local<Object> AddressToJS(Environment* env,
340
                          const sockaddr* addr,
341
                          Local<Object> info) {
342
  EscapableHandleScope scope(env->isolate());
343
  char ip[INET6_ADDRSTRLEN];
344
  const sockaddr_in* a4;
345
  const sockaddr_in6* a6;
346
  int port;
347
348
  if (info.IsEmpty())
349
    info = Object::New(env->isolate());
350
351
  switch (addr->sa_family) {
352
  case AF_INET6:
353
    a6 = reinterpret_cast<const sockaddr_in6*>(addr);
354
    uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
355
    port = ntohs(a6->sin6_port);
356
    info->Set(env->context(),
357
              env->address_string(),
358
              OneByteString(env->isolate(), ip)).FromJust();
359
    info->Set(env->context(),
360
              env->family_string(),
361
              env->ipv6_string()).FromJust();
362
    info->Set(env->context(),
363
              env->port_string(),
364
              Integer::New(env->isolate(), port)).FromJust();
365
    break;
366
367
  case AF_INET:
368
    a4 = reinterpret_cast<const sockaddr_in*>(addr);
369
    uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
370
    port = ntohs(a4->sin_port);
371
    info->Set(env->context(),
372
              env->address_string(),
373
              OneByteString(env->isolate(), ip)).FromJust();
374
    info->Set(env->context(),
375
              env->family_string(),
376
              env->ipv4_string()).FromJust();
377
    info->Set(env->context(),
378
              env->port_string(),
379
              Integer::New(env->isolate(), port)).FromJust();
380
    break;
381
382
  default:
383
    info->Set(env->context(),
384
              env->address_string(),
385
              String::Empty(env->isolate())).FromJust();
386
  }
387
388
  return scope.Escape(info);
389
}
390
391
392
}  // namespace node
393
394
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)