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 |
|
5220 |
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env, |
55 |
|
|
AsyncWrap* parent, |
56 |
|
|
TCPWrap::SocketType type) { |
57 |
|
5220 |
EscapableHandleScope handle_scope(env->isolate()); |
58 |
|
10440 |
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); |
59 |
✗✓ |
10440 |
CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false); |
60 |
|
10440 |
Local<Function> constructor = env->tcp_constructor_template() |
61 |
|
15660 |
->GetFunction(env->context()) |
62 |
|
5220 |
.ToLocalChecked(); |
63 |
✗✓ |
5220 |
CHECK_EQ(constructor.IsEmpty(), false); |
64 |
|
5220 |
Local<Value> type_value = Int32::New(env->isolate(), type); |
65 |
|
|
return handle_scope.EscapeMaybe( |
66 |
|
15660 |
constructor->NewInstance(env->context(), 1, &type_value)); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
|
4469 |
void TCPWrap::Initialize(Local<Object> target, |
71 |
|
|
Local<Value> unused, |
72 |
|
|
Local<Context> context, |
73 |
|
|
void* priv) { |
74 |
|
4469 |
Environment* env = Environment::GetCurrent(context); |
75 |
|
|
|
76 |
|
4469 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
77 |
|
8938 |
t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount); |
78 |
|
|
|
79 |
|
|
// Init properties |
80 |
|
22345 |
t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"), |
81 |
|
4469 |
Boolean::New(env->isolate(), false)); |
82 |
|
17876 |
t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate())); |
83 |
|
17876 |
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate())); |
84 |
|
|
|
85 |
|
8938 |
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); |
86 |
|
|
|
87 |
|
4469 |
env->SetProtoMethod(t, "open", Open); |
88 |
|
4469 |
env->SetProtoMethod(t, "bind", Bind); |
89 |
|
4469 |
env->SetProtoMethod(t, "listen", Listen); |
90 |
|
4469 |
env->SetProtoMethod(t, "connect", Connect); |
91 |
|
4469 |
env->SetProtoMethod(t, "bind6", Bind6); |
92 |
|
4469 |
env->SetProtoMethod(t, "connect6", Connect6); |
93 |
|
|
env->SetProtoMethod(t, "getsockname", |
94 |
|
4469 |
GetSockOrPeerName<TCPWrap, uv_tcp_getsockname>); |
95 |
|
|
env->SetProtoMethod(t, "getpeername", |
96 |
|
4469 |
GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); |
97 |
|
4469 |
env->SetProtoMethod(t, "setNoDelay", SetNoDelay); |
98 |
|
4469 |
env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive); |
99 |
|
|
|
100 |
|
|
#ifdef _WIN32 |
101 |
|
|
env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts); |
102 |
|
|
#endif |
103 |
|
|
|
104 |
|
4469 |
env->SetConstructorFunction(target, "TCP", t); |
105 |
|
4469 |
env->set_tcp_constructor_template(t); |
106 |
|
|
|
107 |
|
|
// Create FunctionTemplate for TCPConnectWrap. |
108 |
|
|
Local<FunctionTemplate> cwt = |
109 |
|
4469 |
BaseObject::MakeLazilyInitializedJSTemplate(env); |
110 |
|
8938 |
cwt->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
111 |
|
4469 |
env->SetConstructorFunction(target, "TCPConnectWrap", cwt); |
112 |
|
|
|
113 |
|
|
// Define constants |
114 |
|
4469 |
Local<Object> constants = Object::New(env->isolate()); |
115 |
|
17876 |
NODE_DEFINE_CONSTANT(constants, SOCKET); |
116 |
|
4469 |
NODE_DEFINE_CONSTANT(constants, SERVER); |
117 |
|
13407 |
NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY); |
118 |
|
22345 |
target->Set(context, |
119 |
|
31283 |
env->constants_string(), |
120 |
|
35752 |
constants).Check(); |
121 |
|
13407 |
} |
122 |
|
|
|
123 |
|
|
|
124 |
|
12426 |
void TCPWrap::New(const FunctionCallbackInfo<Value>& args) { |
125 |
|
|
// This constructor should not be exposed to public javascript. |
126 |
|
|
// Therefore we assert that we are not trying to call this as a |
127 |
|
|
// normal function. |
128 |
✗✓ |
12426 |
CHECK(args.IsConstructCall()); |
129 |
✗✓ |
24852 |
CHECK(args[0]->IsInt32()); |
130 |
|
12426 |
Environment* env = Environment::GetCurrent(args); |
131 |
|
|
|
132 |
|
37278 |
int type_value = args[0].As<Int32>()->Value(); |
133 |
|
12426 |
TCPWrap::SocketType type = static_cast<TCPWrap::SocketType>(type_value); |
134 |
|
|
|
135 |
|
|
ProviderType provider; |
136 |
✓✓✗ |
12426 |
switch (type) { |
137 |
|
|
case SOCKET: |
138 |
|
10841 |
provider = PROVIDER_TCPWRAP; |
139 |
|
10841 |
break; |
140 |
|
|
case SERVER: |
141 |
|
1585 |
provider = PROVIDER_TCPSERVERWRAP; |
142 |
|
1585 |
break; |
143 |
|
|
default: |
144 |
|
|
UNREACHABLE(); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
12426 |
new TCPWrap(env, args.This(), provider); |
148 |
|
12426 |
} |
149 |
|
|
|
150 |
|
|
|
151 |
|
12426 |
TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider) |
152 |
|
12426 |
: ConnectionWrap(env, object, provider) { |
153 |
|
12426 |
int r = uv_tcp_init(env->event_loop(), &handle_); |
154 |
✗✓ |
12426 |
CHECK_EQ(r, 0); // How do we proxy this error up to javascript? |
155 |
|
|
// Suggestion: uv_tcp_init() returns void. |
156 |
|
12426 |
} |
157 |
|
|
|
158 |
|
|
|
159 |
|
677 |
void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) { |
160 |
|
|
TCPWrap* wrap; |
161 |
✗✓ |
677 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
162 |
|
|
args.Holder(), |
163 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
164 |
|
1354 |
int enable = static_cast<int>(args[0]->IsTrue()); |
165 |
|
677 |
int err = uv_tcp_nodelay(&wrap->handle_, enable); |
166 |
|
1354 |
args.GetReturnValue().Set(err); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|
170 |
|
191 |
void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) { |
171 |
|
|
TCPWrap* wrap; |
172 |
✗✓ |
191 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
173 |
|
|
args.Holder(), |
174 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
175 |
|
191 |
Environment* env = wrap->env(); |
176 |
|
|
int enable; |
177 |
✗✓ |
764 |
if (!args[0]->Int32Value(env->context()).To(&enable)) return; |
178 |
|
573 |
unsigned int delay = static_cast<unsigned int>(args[1].As<Uint32>()->Value()); |
179 |
|
191 |
int err = uv_tcp_keepalive(&wrap->handle_, enable, delay); |
180 |
|
382 |
args.GetReturnValue().Set(err); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
#ifdef _WIN32 |
185 |
|
|
void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) { |
186 |
|
|
TCPWrap* wrap; |
187 |
|
|
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
188 |
|
|
args.Holder(), |
189 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
190 |
|
|
bool enable = args[0]->IsTrue(); |
191 |
|
|
int err = uv_tcp_simultaneous_accepts(&wrap->handle_, enable); |
192 |
|
|
args.GetReturnValue().Set(err); |
193 |
|
|
} |
194 |
|
|
#endif |
195 |
|
|
|
196 |
|
|
|
197 |
|
2 |
void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) { |
198 |
|
|
TCPWrap* wrap; |
199 |
✗✓ |
2 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
200 |
|
|
args.Holder(), |
201 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
202 |
|
|
int64_t val; |
203 |
✗✓ |
8 |
if (!args[0]->IntegerValue(args.GetIsolate()->GetCurrentContext()).To(&val)) |
204 |
|
|
return; |
205 |
|
2 |
int fd = static_cast<int>(val); |
206 |
|
2 |
int err = uv_tcp_open(&wrap->handle_, fd); |
207 |
|
|
|
208 |
✓✗ |
2 |
if (err == 0) |
209 |
|
2 |
wrap->set_fd(fd); |
210 |
|
|
|
211 |
|
4 |
args.GetReturnValue().Set(err); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
template <typename T> |
215 |
|
1608 |
void TCPWrap::Bind( |
216 |
|
|
const FunctionCallbackInfo<Value>& args, |
217 |
|
|
int family, |
218 |
|
|
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) { |
219 |
|
|
TCPWrap* wrap; |
220 |
✗✓✗✓
|
1608 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
221 |
|
|
args.Holder(), |
222 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
223 |
|
1608 |
Environment* env = wrap->env(); |
224 |
|
3216 |
node::Utf8Value ip_address(env->isolate(), args[0]); |
225 |
|
|
int port; |
226 |
|
1608 |
unsigned int flags = 0; |
227 |
✗✓✗✓
|
6432 |
if (!args[1]->Int32Value(env->context()).To(&port)) return; |
228 |
✓✗✗✓ ✗✓✗✓ ✗✗✗✓
|
4683 |
if (family == AF_INET6 && |
229 |
|
7476 |
!args[2]->Uint32Value(env->context()).To(&flags)) { |
230 |
|
|
return; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
T addr; |
234 |
|
1608 |
int err = uv_ip_addr(*ip_address, port, &addr); |
235 |
|
|
|
236 |
✓✗✓✗
|
1608 |
if (err == 0) { |
237 |
|
1608 |
err = uv_tcp_bind(&wrap->handle_, |
238 |
|
|
reinterpret_cast<const sockaddr*>(&addr), |
239 |
|
|
flags); |
240 |
|
|
} |
241 |
✓✗✓✗
|
3216 |
args.GetReturnValue().Set(err); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
141 |
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) { |
245 |
|
141 |
Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr); |
246 |
|
141 |
} |
247 |
|
|
|
248 |
|
|
|
249 |
|
1467 |
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) { |
250 |
|
1467 |
Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr); |
251 |
|
1467 |
} |
252 |
|
|
|
253 |
|
|
|
254 |
|
1591 |
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) { |
255 |
|
|
TCPWrap* wrap; |
256 |
✗✓ |
1591 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
257 |
|
|
args.Holder(), |
258 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
259 |
|
1591 |
Environment* env = wrap->env(); |
260 |
|
|
int backlog; |
261 |
✗✓ |
6364 |
if (!args[0]->Int32Value(env->context()).To(&backlog)) return; |
262 |
|
1591 |
int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_), |
263 |
|
|
backlog, |
264 |
|
1591 |
OnConnection); |
265 |
|
3182 |
args.GetReturnValue().Set(err); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
5271 |
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) { |
270 |
✗✓ |
10542 |
CHECK(args[2]->IsUint32()); |
271 |
|
|
// explicit cast to fit to libuv's type expectation |
272 |
|
15813 |
int port = static_cast<int>(args[2].As<Uint32>()->Value()); |
273 |
|
10542 |
Connect<sockaddr_in>(args, |
274 |
|
5271 |
[port](const char* ip_address, sockaddr_in* addr) { |
275 |
|
|
return uv_ip4_addr(ip_address, port, addr); |
276 |
|
10542 |
}); |
277 |
|
5271 |
} |
278 |
|
|
|
279 |
|
|
|
280 |
|
12 |
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) { |
281 |
|
12 |
Environment* env = Environment::GetCurrent(args); |
282 |
✗✓ |
24 |
CHECK(args[2]->IsUint32()); |
283 |
|
|
int port; |
284 |
✗✓ |
48 |
if (!args[2]->Int32Value(env->context()).To(&port)) return; |
285 |
|
24 |
Connect<sockaddr_in6>(args, |
286 |
|
12 |
[port](const char* ip_address, sockaddr_in6* addr) { |
287 |
|
|
return uv_ip6_addr(ip_address, port, addr); |
288 |
|
24 |
}); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
template <typename T> |
292 |
|
5283 |
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args, |
293 |
|
|
std::function<int(const char* ip_address, T* addr)> uv_ip_addr) { |
294 |
|
5283 |
Environment* env = Environment::GetCurrent(args); |
295 |
|
|
|
296 |
|
|
TCPWrap* wrap; |
297 |
✗✓✗✓
|
5283 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
298 |
|
|
args.Holder(), |
299 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
300 |
|
|
|
301 |
✗✓✗✓
|
10566 |
CHECK(args[0]->IsObject()); |
302 |
✗✓✗✓
|
15849 |
CHECK(args[1]->IsString()); |
303 |
|
|
|
304 |
|
10566 |
Local<Object> req_wrap_obj = args[0].As<Object>(); |
305 |
|
10566 |
node::Utf8Value ip_address(env->isolate(), args[1]); |
306 |
|
|
|
307 |
|
|
T addr; |
308 |
|
5283 |
int err = uv_ip_addr(*ip_address, &addr); |
309 |
|
|
|
310 |
✓✗✓✗
|
5283 |
if (err == 0) { |
311 |
|
10566 |
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); |
312 |
|
|
ConnectWrap* req_wrap = |
313 |
|
5283 |
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); |
314 |
|
5283 |
err = req_wrap->Dispatch(uv_tcp_connect, |
315 |
|
5283 |
&wrap->handle_, |
316 |
|
|
reinterpret_cast<const sockaddr*>(&addr), |
317 |
|
|
AfterConnect); |
318 |
✗✓✗✓
|
5283 |
if (err) |
319 |
|
|
delete req_wrap; |
320 |
|
|
} |
321 |
|
|
|
322 |
|
10566 |
args.GetReturnValue().Set(err); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
|
326 |
|
|
// also used by udp_wrap.cc |
327 |
|
4798 |
Local<Object> AddressToJS(Environment* env, |
328 |
|
|
const sockaddr* addr, |
329 |
|
|
Local<Object> info) { |
330 |
|
4798 |
EscapableHandleScope scope(env->isolate()); |
331 |
|
|
char ip[INET6_ADDRSTRLEN + UV_IF_NAMESIZE]; |
332 |
|
|
const sockaddr_in* a4; |
333 |
|
|
const sockaddr_in6* a6; |
334 |
|
|
|
335 |
|
|
int port; |
336 |
|
|
|
337 |
✓✓ |
4798 |
if (info.IsEmpty()) |
338 |
|
344 |
info = Object::New(env->isolate()); |
339 |
|
|
|
340 |
✓✓✗ |
4798 |
switch (addr->sa_family) { |
341 |
|
|
case AF_INET6: |
342 |
|
4172 |
a6 = reinterpret_cast<const sockaddr_in6*>(addr); |
343 |
|
4172 |
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip); |
344 |
|
|
// Add an interface identifier to a link local address. |
345 |
✓✓ |
4172 |
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) { |
346 |
|
2 |
const size_t addrlen = strlen(ip); |
347 |
✗✓ |
2 |
CHECK_LT(addrlen, sizeof(ip)); |
348 |
|
2 |
ip[addrlen] = '%'; |
349 |
|
2 |
size_t scopeidlen = sizeof(ip) - addrlen - 1; |
350 |
✗✓ |
2 |
CHECK_GE(scopeidlen, UV_IF_NAMESIZE); |
351 |
|
2 |
const int r = uv_if_indextoiid(a6->sin6_scope_id, |
352 |
|
2 |
ip + addrlen + 1, |
353 |
|
2 |
&scopeidlen); |
354 |
✗✓ |
2 |
CHECK_EQ(r, 0); |
355 |
|
|
} |
356 |
|
4172 |
port = ntohs(a6->sin6_port); |
357 |
|
8344 |
info->Set(env->context(), |
358 |
|
|
env->address_string(), |
359 |
|
20860 |
OneByteString(env->isolate(), ip)).Check(); |
360 |
|
8344 |
info->Set(env->context(), |
361 |
|
|
env->family_string(), |
362 |
|
20860 |
env->ipv6_string()).Check(); |
363 |
|
8344 |
info->Set(env->context(), |
364 |
|
|
env->port_string(), |
365 |
|
20860 |
Integer::New(env->isolate(), port)).Check(); |
366 |
|
4172 |
break; |
367 |
|
|
|
368 |
|
|
case AF_INET: |
369 |
|
626 |
a4 = reinterpret_cast<const sockaddr_in*>(addr); |
370 |
|
626 |
uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip); |
371 |
|
626 |
port = ntohs(a4->sin_port); |
372 |
|
1252 |
info->Set(env->context(), |
373 |
|
|
env->address_string(), |
374 |
|
3130 |
OneByteString(env->isolate(), ip)).Check(); |
375 |
|
1252 |
info->Set(env->context(), |
376 |
|
|
env->family_string(), |
377 |
|
3130 |
env->ipv4_string()).Check(); |
378 |
|
1252 |
info->Set(env->context(), |
379 |
|
|
env->port_string(), |
380 |
|
3130 |
Integer::New(env->isolate(), port)).Check(); |
381 |
|
626 |
break; |
382 |
|
|
|
383 |
|
|
default: |
384 |
|
|
info->Set(env->context(), |
385 |
|
|
env->address_string(), |
386 |
|
|
String::Empty(env->isolate())).Check(); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
4798 |
return scope.Escape(info); |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
|
393 |
|
|
} // namespace node |
394 |
|
|
|
395 |
✓✗✓✗
|
18788 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize) |