2424#include < winsock2.h>
2525#include < ws2tcpip.h>
2626#endif
27+ #include < cerrno>
28+ #include < cstdlib>
2729#include " connect_wrap.h"
2830#include " connection_wrap.h"
2931#include " env-inl.h"
3537#include " stream_base-inl.h"
3638#include " stream_wrap.h"
3739#include " util-inl.h"
38- #include < cerrno>
39- #include < cstdlib>
4040
4141#ifndef _WIN32
4242#include < netinet/in.h>
@@ -80,7 +80,6 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
8080 constructor->NewInstance (env->context (), 1 , &type_value));
8181}
8282
83-
8483void TCPWrap::Initialize (Local<Object> target,
8584 Local<Value> unused,
8685 Local<Context> context,
@@ -138,9 +137,7 @@ void TCPWrap::Initialize(Local<Object> target,
138137 NODE_DEFINE_CONSTANT (constants, SERVER);
139138 NODE_DEFINE_CONSTANT (constants, UV_TCP_IPV6ONLY);
140139 NODE_DEFINE_CONSTANT (constants, UV_TCP_REUSEPORT);
141- target->Set (context,
142- env->constants_string (),
143- constants).Check ();
140+ target->Set (context, env->constants_string (), constants).Check ();
144141}
145142
146143void TCPWrap::RegisterExternalReferences (ExternalReferenceRegistry* registry) {
@@ -190,15 +187,13 @@ void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
190187 new TCPWrap (env, args.This (), provider);
191188}
192189
193-
194190TCPWrap::TCPWrap (Environment* env, Local<Object> object, ProviderType provider)
195191 : ConnectionWrap(env, object, provider) {
196192 int r = uv_tcp_init (env->event_loop (), &handle_);
197193 CHECK_EQ (r, 0 ); // How do we proxy this error up to javascript?
198194 // Suggestion: uv_tcp_init() returns void.
199195}
200196
201-
202197void TCPWrap::SetNoDelay (const FunctionCallbackInfo<Value>& args) {
203198 TCPWrap* wrap;
204199 ASSIGN_OR_RETURN_UNWRAP (
@@ -208,7 +203,6 @@ void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
208203 args.GetReturnValue ().Set (err);
209204}
210205
211-
212206void TCPWrap::SetKeepAlive (const FunctionCallbackInfo<Value>& args) {
213207 TCPWrap* wrap;
214208 ASSIGN_OR_RETURN_UNWRAP (
@@ -245,9 +239,8 @@ void TCPWrap::SetTOS(const FunctionCallbackInfo<Value>& args) {
245239 // 1. Detect the socket family (IPv4 vs IPv6)
246240 sockaddr_storage storage;
247241 int addrlen = sizeof (storage);
248- int sock_err = uv_tcp_getsockname (&wrap->handle_ ,
249- reinterpret_cast <sockaddr*>(&storage),
250- &addrlen);
242+ int sock_err = uv_tcp_getsockname (
243+ &wrap->handle_ , reinterpret_cast <sockaddr*>(&storage), &addrlen);
251244
252245 // If we can't determine the family (e.g. closed socket), fail gracefully.
253246 if (sock_err != 0 ) {
@@ -306,9 +299,8 @@ void TCPWrap::GetTOS(const FunctionCallbackInfo<Value>& args) {
306299 // Detect socket family explicitly
307300 sockaddr_storage storage;
308301 int addrlen = sizeof (storage);
309- int sock_err = uv_tcp_getsockname (&wrap->handle_ ,
310- reinterpret_cast <sockaddr*>(&storage),
311- &addrlen);
302+ int sock_err = uv_tcp_getsockname (
303+ &wrap->handle_ , reinterpret_cast <sockaddr*>(&storage), &addrlen);
312304
313305 int level;
314306 int option;
@@ -367,7 +359,6 @@ void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
367359}
368360#endif
369361
370-
371362void TCPWrap::Open (const FunctionCallbackInfo<Value>& args) {
372363 TCPWrap* wrap;
373364 ASSIGN_OR_RETURN_UNWRAP (
@@ -378,8 +369,7 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
378369 int fd = static_cast <int >(val);
379370 int err = uv_tcp_open (&wrap->handle_ , fd);
380371
381- if (err == 0 )
382- wrap->set_fd (fd);
372+ if (err == 0 ) wrap->set_fd (fd);
383373
384374 args.GetReturnValue ().Set (err);
385375}
@@ -412,9 +402,8 @@ void TCPWrap::Bind(
412402 int err = uv_ip_addr (*ip_address, port, &addr);
413403
414404 if (err == 0 ) {
415- err = uv_tcp_bind (&wrap->handle_ ,
416- reinterpret_cast <const sockaddr*>(&addr),
417- flags);
405+ err = uv_tcp_bind (
406+ &wrap->handle_ , reinterpret_cast <const sockaddr*>(&addr), flags);
418407 }
419408 args.GetReturnValue ().Set (err);
420409}
@@ -423,12 +412,10 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
423412 Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
424413}
425414
426-
427415void TCPWrap::Bind6 (const FunctionCallbackInfo<Value>& args) {
428416 Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
429417}
430418
431-
432419void TCPWrap::Listen (const FunctionCallbackInfo<Value>& args) {
433420 TCPWrap* wrap;
434421 ASSIGN_OR_RETURN_UNWRAP (
@@ -439,37 +426,34 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
439426
440427 THROW_IF_INSUFFICIENT_PERMISSIONS (env, permission::PermissionScope::kNet , " " );
441428
442- int err = uv_listen (reinterpret_cast <uv_stream_t *>(&wrap->handle_ ),
443- backlog,
444- OnConnection);
429+ int err = uv_listen (
430+ reinterpret_cast <uv_stream_t *>(&wrap->handle_ ), backlog, OnConnection);
445431 args.GetReturnValue ().Set (err);
446432}
447433
448-
449434void TCPWrap::Connect (const FunctionCallbackInfo<Value>& args) {
450435 CHECK (args[2 ]->IsUint32 ());
451436 // explicit cast to fit to libuv's type expectation
452437 int port = static_cast <int >(args[2 ].As <Uint32>()->Value ());
453- Connect<sockaddr_in>(args,
454- [port](const char * ip_address, sockaddr_in* addr) {
455- return uv_ip4_addr (ip_address, port, addr);
438+ Connect<sockaddr_in>(args, [port](const char * ip_address, sockaddr_in* addr) {
439+ return uv_ip4_addr (ip_address, port, addr);
456440 });
457441}
458442
459-
460443void TCPWrap::Connect6 (const FunctionCallbackInfo<Value>& args) {
461444 Environment* env = Environment::GetCurrent (args);
462445 CHECK (args[2 ]->IsUint32 ());
463446 int port;
464447 if (!args[2 ]->Int32Value (env->context ()).To (&port)) return ;
465448 Connect<sockaddr_in6>(args,
466449 [port](const char * ip_address, sockaddr_in6* addr) {
467- return uv_ip6_addr (ip_address, port, addr);
468- });
450+ return uv_ip6_addr (ip_address, port, addr);
451+ });
469452}
470453
471454template <typename T>
472- void TCPWrap::Connect (const FunctionCallbackInfo<Value>& args,
455+ void TCPWrap::Connect (
456+ const FunctionCallbackInfo<Value>& args,
473457 std::function<int (const char * ip_address, T* addr)> uv_ip_addr) {
474458 Environment* env = Environment::GetCurrent (args);
475459
@@ -557,70 +541,68 @@ MaybeLocal<Object> AddressToJS(Environment* env,
557541 }
558542
559543 switch (addr->sa_family ) {
560- case AF_INET6:
561- a6 = reinterpret_cast <const sockaddr_in6*>(addr);
562- uv_inet_ntop (AF_INET6, &a6->sin6_addr , ip, sizeof ip);
563- // Add an interface identifier to a link local address.
564- if (IN6_IS_ADDR_LINKLOCAL (&a6->sin6_addr ) && a6->sin6_scope_id > 0 ) {
565- const size_t addrlen = strlen (ip);
566- CHECK_LT (addrlen, sizeof (ip));
567- ip[addrlen] = ' %' ;
568- size_t scopeidlen = sizeof (ip) - addrlen - 1 ;
569- CHECK_GE (scopeidlen, UV_IF_NAMESIZE);
570- const int r = uv_if_indextoiid (a6->sin6_scope_id ,
571- ip + addrlen + 1 ,
572- &scopeidlen);
573- if (r) {
574- env->ThrowUVException (r, " uv_if_indextoiid" );
544+ case AF_INET6:
545+ a6 = reinterpret_cast <const sockaddr_in6*>(addr);
546+ uv_inet_ntop (AF_INET6, &a6->sin6_addr , ip, sizeof ip);
547+ // Add an interface identifier to a link local address.
548+ if (IN6_IS_ADDR_LINKLOCAL (&a6->sin6_addr ) && a6->sin6_scope_id > 0 ) {
549+ const size_t addrlen = strlen (ip);
550+ CHECK_LT (addrlen, sizeof (ip));
551+ ip[addrlen] = ' %' ;
552+ size_t scopeidlen = sizeof (ip) - addrlen - 1 ;
553+ CHECK_GE (scopeidlen, UV_IF_NAMESIZE);
554+ const int r =
555+ uv_if_indextoiid (a6->sin6_scope_id , ip + addrlen + 1 , &scopeidlen);
556+ if (r) {
557+ env->ThrowUVException (r, " uv_if_indextoiid" );
558+ return {};
559+ }
560+ }
561+ port = ntohs (a6->sin6_port );
562+ if (info->Set (env->context (),
563+ env->address_string (),
564+ OneByteString (env->isolate (), ip))
565+ .IsNothing () ||
566+ info->Set (env->context (), env->family_string (), env->ipv6_string ())
567+ .IsNothing () ||
568+ info->Set (env->context (),
569+ env->port_string (),
570+ Integer::New (env->isolate (), port))
571+ .IsNothing ()) {
572+ return {};
573+ }
574+ break ;
575+
576+ case AF_INET:
577+ a4 = reinterpret_cast <const sockaddr_in*>(addr);
578+ uv_inet_ntop (AF_INET, &a4->sin_addr , ip, sizeof ip);
579+ port = ntohs (a4->sin_port );
580+ if (info->Set (env->context (),
581+ env->address_string (),
582+ OneByteString (env->isolate (), ip))
583+ .IsNothing () ||
584+ info->Set (env->context (), env->family_string (), env->ipv4_string ())
585+ .IsNothing () ||
586+ info->Set (env->context (),
587+ env->port_string (),
588+ Integer::New (env->isolate (), port))
589+ .IsNothing ()) {
590+ return {};
591+ }
592+ break ;
593+
594+ default :
595+ if (info->Set (env->context (),
596+ env->address_string (),
597+ String::Empty (env->isolate ()))
598+ .IsNothing ()) {
575599 return {};
576600 }
577- }
578- port = ntohs (a6->sin6_port );
579- if (info->Set (env->context (),
580- env->address_string (),
581- OneByteString (env->isolate (), ip))
582- .IsNothing () ||
583- info->Set (env->context (), env->family_string (), env->ipv6_string ())
584- .IsNothing () ||
585- info->Set (env->context (),
586- env->port_string (),
587- Integer::New (env->isolate (), port))
588- .IsNothing ()) {
589- return {};
590- }
591- break ;
592-
593- case AF_INET:
594- a4 = reinterpret_cast <const sockaddr_in*>(addr);
595- uv_inet_ntop (AF_INET, &a4->sin_addr , ip, sizeof ip);
596- port = ntohs (a4->sin_port );
597- if (info->Set (env->context (),
598- env->address_string (),
599- OneByteString (env->isolate (), ip))
600- .IsNothing () ||
601- info->Set (env->context (), env->family_string (), env->ipv4_string ())
602- .IsNothing () ||
603- info->Set (env->context (),
604- env->port_string (),
605- Integer::New (env->isolate (), port))
606- .IsNothing ()) {
607- return {};
608- }
609- break ;
610-
611- default :
612- if (info->Set (env->context (),
613- env->address_string (),
614- String::Empty (env->isolate ()))
615- .IsNothing ()) {
616- return {};
617- }
618601 }
619602
620603 return scope.Escape (info);
621604}
622605
623-
624606} // namespace node
625607
626608NODE_BINDING_CONTEXT_AWARE_INTERNAL (tcp_wrap, node::TCPWrap::Initialize)
0 commit comments