From f44f33569d5c6021253b37c7eb9038cfe24143d4 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 13 Feb 2019 04:59:48 +0100 Subject: [PATCH] src: extract common sockaddr creation code This commit extracts code to create sockaddr which is shared by both UDPWrap::DoBind and UDPWrap::DoSend. PR-URL: https://github.com/nodejs/node/pull/26070 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Richard Lau --- src/udp_wrap.cc | 51 ++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index e4aca28c89500e..be1f59d2c52863 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -175,6 +175,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; @@ -191,24 +204,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); } @@ -392,27 +392,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); }