diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 04d65c3635737e..e17dea0e0e31df 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -165,6 +165,19 @@ void UDPWrap::GetFD(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(fd); } +int sockaddr_for_family(int address_family, + const char* address, + const unsigned short port, + struct sockaddr_storage* addr) { + switch (address_family) { + case AF_INET: + return uv_ip4_addr(address, port, reinterpret_cast(addr)); + case AF_INET6: + return uv_ip6_addr(address, port, reinterpret_cast(addr)); + default: + CHECK(0 && "unexpected address family"); + } +} void UDPWrap::DoBind(const FunctionCallbackInfo& args, int family) { UDPWrap* wrap; @@ -181,24 +194,11 @@ void UDPWrap::DoBind(const FunctionCallbackInfo& args, int family) { if (!args[1]->Uint32Value(ctx).To(&port) || !args[2]->Uint32Value(ctx).To(&flags)) return; - char addr[sizeof(sockaddr_in6)]; - int err; - - switch (family) { - case AF_INET: - err = uv_ip4_addr(*address, port, reinterpret_cast(&addr)); - break; - case AF_INET6: - err = uv_ip6_addr(*address, port, reinterpret_cast(&addr)); - break; - default: - CHECK(0 && "unexpected address family"); - ABORT(); - } - + struct sockaddr_storage addr_storage; + int err = sockaddr_for_family(family, address.out(), port, &addr_storage); if (err == 0) { err = uv_udp_bind(&wrap->handle_, - reinterpret_cast(&addr), + reinterpret_cast(&addr_storage), flags); } @@ -369,27 +369,14 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { req_wrap->msg_size = msg_size; - char addr[sizeof(sockaddr_in6)]; - int err; - - switch (family) { - case AF_INET: - err = uv_ip4_addr(*address, port, reinterpret_cast(&addr)); - break; - case AF_INET6: - err = uv_ip6_addr(*address, port, reinterpret_cast(&addr)); - break; - default: - CHECK(0 && "unexpected address family"); - ABORT(); - } - + struct sockaddr_storage addr_storage; + int err = sockaddr_for_family(family, address.out(), port, &addr_storage); if (err == 0) { err = req_wrap->Dispatch(uv_udp_send, &wrap->handle_, *bufs, count, - reinterpret_cast(&addr), + reinterpret_cast(&addr_storage), OnSend); }