Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: extract common sockaddr creation code #26070

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ void UDPWrap::GetFD(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(fd);
}

int uv_sockaddr_for_family(int address_family,
danbev marked this conversation as resolved.
Show resolved Hide resolved
const char* address,
const unsigned short port,
struct sockaddr* 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");
ABORT();
danbev marked this conversation as resolved.
Show resolved Hide resolved
}
}

void UDPWrap::DoBind(const FunctionCallbackInfo<Value>& args, int family) {
UDPWrap* wrap;
Expand All @@ -191,21 +205,8 @@ 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 addr;
danbev marked this conversation as resolved.
Show resolved Hide resolved
int err = uv_sockaddr_for_family(family, address.out(), port, &addr);
if (err == 0) {
err = uv_udp_bind(&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
Expand Down Expand Up @@ -392,21 +393,8 @@ 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 addr;
int err = uv_sockaddr_for_family(family, address.out(), port, &addr);
if (err == 0) {
err = req_wrap->Dispatch(uv_udp_send,
&wrap->handle_,
Expand Down