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 2 commits
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
52 changes: 20 additions & 32 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_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");
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,24 +205,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 = uv_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);
}

Expand Down Expand Up @@ -392,27 +393,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 = uv_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);
}

Expand Down