diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 6aee0c9093..a158c4587b 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -74,15 +74,14 @@ using v8::Undefined; using v8::Value; static Persistent tcpConstructor; -static Persistent family_symbol; -static Persistent address_symbol; -static Persistent port_symbol; static Persistent oncomplete_sym; static Persistent onconnection_sym; typedef class ReqWrap ConnectWrap; +Local AddressToJS(const sockaddr* addr); + Local TCPWrap::Instantiate() { // If this assert fire then process.binding('tcp_wrap') hasn't been @@ -130,9 +129,6 @@ void TCPWrap::Initialize(Handle target) { tcpConstructor = Persistent::New(t->GetFunction()); - family_symbol = NODE_PSYMBOL("family"); - address_symbol = NODE_PSYMBOL("address"); - port_symbol = NODE_PSYMBOL("port"); onconnection_sym = NODE_PSYMBOL("onconnection"); oncomplete_sym = NODE_PSYMBOL("oncomplete"); @@ -171,10 +167,6 @@ TCPWrap::~TCPWrap() { Handle TCPWrap::GetSockName(const Arguments& args) { HandleScope scope; struct sockaddr_storage address; - int family; - int port; - char ip[INET6_ADDRSTRLEN]; - const char *family_name; UNWRAP @@ -183,42 +175,19 @@ Handle TCPWrap::GetSockName(const Arguments& args) { reinterpret_cast(&address), &addrlen); - Local sockname = Object::New(); - if (r != 0) { + if (r) { SetErrno(uv_last_error(uv_default_loop())); - } else { - family = address.ss_family; - - if (family == AF_INET) { - struct sockaddr_in* addrin = (struct sockaddr_in*)&address; - uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN); - port = ntohs(addrin->sin_port); - family_name = "IPv4"; - } else if (family == AF_INET6) { - struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address; - uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN); - port = ntohs(addrin6->sin6_port); - family_name = "IPv6"; - } else { - assert(0 && "bad address family"); - abort(); - } - - sockname->Set(port_symbol, Integer::New(port)); - sockname->Set(family_symbol, String::New(family_name)); - sockname->Set(address_symbol, String::New(ip)); + return Null(); } - return scope.Close(sockname); + const sockaddr* addr = reinterpret_cast(&address); + return scope.Close(AddressToJS(addr)); } Handle TCPWrap::GetPeerName(const Arguments& args) { HandleScope scope; struct sockaddr_storage address; - int family; - int port; - char ip[INET6_ADDRSTRLEN]; UNWRAP @@ -227,31 +196,13 @@ Handle TCPWrap::GetPeerName(const Arguments& args) { reinterpret_cast(&address), &addrlen); - Local sockname = Object::New(); - if (r != 0) { + if (r) { SetErrno(uv_last_error(uv_default_loop())); - } else { - family = address.ss_family; - - if (family == AF_INET) { - struct sockaddr_in* addrin = (struct sockaddr_in*)&address; - uv_inet_ntop(AF_INET, &(addrin->sin_addr), ip, INET6_ADDRSTRLEN); - port = ntohs(addrin->sin_port); - } else if (family == AF_INET6) { - struct sockaddr_in6* addrin6 = (struct sockaddr_in6*)&address; - uv_inet_ntop(AF_INET6, &(addrin6->sin6_addr), ip, INET6_ADDRSTRLEN); - port = ntohs(addrin6->sin6_port); - } else { - assert(0 && "bad address family"); - abort(); - } - - sockname->Set(port_symbol, Integer::New(port)); - sockname->Set(family_symbol, Integer::New(family)); - sockname->Set(address_symbol, String::New(ip)); + return Null(); } - return scope.Close(sockname); + const sockaddr* addr = reinterpret_cast(&address); + return scope.Close(AddressToJS(addr)); } @@ -473,6 +424,57 @@ Handle TCPWrap::Connect6(const Arguments& args) { } +// also used by udp_wrap.cc +Local AddressToJS(const sockaddr* addr) { + static Persistent address_sym; + static Persistent family_sym; + static Persistent port_sym; + static Persistent ipv4_sym; + static Persistent ipv6_sym; + + HandleScope scope; + char ip[INET6_ADDRSTRLEN]; + const sockaddr_in *a4; + const sockaddr_in6 *a6; + int port; + + if (address_sym.IsEmpty()) { + address_sym = NODE_PSYMBOL("address"); + family_sym = NODE_PSYMBOL("family"); + port_sym = NODE_PSYMBOL("port"); + ipv4_sym = NODE_PSYMBOL("IPv4"); + ipv6_sym = NODE_PSYMBOL("IPv6"); + } + + Local info = Object::New(); + + switch (addr->sa_family) { + case AF_INET6: + a6 = reinterpret_cast(addr); + uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip); + port = ntohs(a6->sin6_port); + info->Set(address_sym, String::New(ip)); + info->Set(family_sym, ipv6_sym); + info->Set(port_sym, Integer::New(port)); + break; + + case AF_INET: + a4 = reinterpret_cast(addr); + uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip); + port = ntohs(a4->sin_port); + info->Set(address_sym, String::New(ip)); + info->Set(family_sym, ipv4_sym); + info->Set(port_sym, Integer::New(port)); + break; + + default: + info->Set(address_sym, String::Empty()); + } + + return scope.Close(info); +} + + } // namespace node NODE_MODULE(node_tcp_wrap, node::TCPWrap::Initialize) diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 244bc67165..709bcc4745 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -63,13 +63,9 @@ namespace node { typedef ReqWrap SendWrap; +// see tcp_wrap.cc Local AddressToJS(const sockaddr* addr); -static Persistent address_sym; -static Persistent port_sym; -static Persistent family_sym; -static Persistent ipv4_sym; -static Persistent ipv6_sym; static Persistent buffer_sym; static Persistent oncomplete_sym; static Persistent onmessage_sym; @@ -133,13 +129,8 @@ void UDPWrap::Initialize(Handle target) { HandleScope scope; buffer_sym = NODE_PSYMBOL("buffer"); - port_sym = NODE_PSYMBOL("port"); - address_sym = NODE_PSYMBOL("address"); oncomplete_sym = NODE_PSYMBOL("oncomplete"); onmessage_sym = NODE_PSYMBOL("onmessage"); - family_sym = NODE_PSYMBOL("family"); - ipv4_sym = NODE_PSYMBOL("IPv4"); - ipv6_sym = NODE_PSYMBOL("IPv6"); Local t = FunctionTemplate::New(New); t->InstanceTemplate()->SetInternalFieldCount(1); @@ -447,42 +438,6 @@ void UDPWrap::OnRecv(uv_udp_t* handle, } -Local AddressToJS(const sockaddr* addr) { - HandleScope scope; - char ip[INET6_ADDRSTRLEN]; - const sockaddr_in *a4; - const sockaddr_in6 *a6; - int port; - - Local info = Object::New(); - - switch (addr->sa_family) { - case AF_INET6: - a6 = reinterpret_cast(addr); - uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip); - port = ntohs(a6->sin6_port); - info->Set(address_sym, String::New(ip)); - info->Set(port_sym, Integer::New(port)); - info->Set(family_sym, ipv6_sym); - break; - - case AF_INET: - a4 = reinterpret_cast(addr); - uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip); - port = ntohs(a4->sin_port); - info->Set(address_sym, String::New(ip)); - info->Set(port_sym, Integer::New(port)); - info->Set(family_sym, ipv4_sym); - break; - - default: - info->Set(address_sym, String::Empty()); - } - - return scope.Close(info); -} - - } // namespace node NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize)