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