diff options
author | Daniel Bevenius <daniel.bevenius@gmail.com> | 2019-02-13 06:59:48 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-02-17 20:55:15 +0300 |
commit | 05292cbf6f645c46711f3fa5f1619a18501dd068 (patch) | |
tree | 1010549a5d2633402cf3cd25b2356191ab90849e /src/udp_wrap.cc | |
parent | 48491ce40518516c76427bb6948e1c7db67c324f (diff) |
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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Diffstat (limited to 'src/udp_wrap.cc')
-rw-r--r-- | src/udp_wrap.cc | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index e4aca28c895..be1f59d2c52 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -175,6 +175,19 @@ void UDPWrap::GetFD(const FunctionCallbackInfo<Value>& 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<sockaddr_in*>(addr)); + case AF_INET6: + return uv_ip6_addr(address, port, reinterpret_cast<sockaddr_in6*>(addr)); + default: + CHECK(0 && "unexpected address family"); + } +} void UDPWrap::DoBind(const FunctionCallbackInfo<Value>& args, int family) { UDPWrap* wrap; @@ -191,24 +204,11 @@ void UDPWrap::DoBind(const FunctionCallbackInfo<Value>& 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<sockaddr_in*>(&addr)); - break; - case AF_INET6: - err = uv_ip6_addr(*address, port, reinterpret_cast<sockaddr_in6*>(&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<const sockaddr*>(&addr), + reinterpret_cast<const sockaddr*>(&addr_storage), flags); } @@ -392,27 +392,14 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& 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<sockaddr_in*>(&addr)); - break; - case AF_INET6: - err = uv_ip6_addr(*address, port, reinterpret_cast<sockaddr_in6*>(&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<const sockaddr*>(&addr), + reinterpret_cast<const sockaddr*>(&addr_storage), OnSend); } |