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

Short-circuit interface binding on error #221

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
16 changes: 12 additions & 4 deletions receptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bindlisten(
struct addrinfo *res, *resw;
char buf[128];
char saddr[INET6_ADDRSTRLEN];
int err;
int err, binderr = 0;
int curlen_stream = 0;
int curlen_dgram = 0;
int socktypes[] = {SOCK_STREAM, SOCK_DGRAM, 0};
Expand All @@ -64,7 +64,7 @@ bindlisten(
tv.tv_sec = 0;
tv.tv_usec = 500 * 1000;

for (; *socktype != 0; socktype++) {
for (; *socktype != 0 && binderr == 0; socktype++) {
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_socktype = *socktype;
Expand All @@ -84,8 +84,14 @@ bindlisten(
continue;
if (resw->ai_protocol != IPPROTO_TCP && resw->ai_protocol != IPPROTO_UDP)
continue;
if ((sock = socket(resw->ai_family, resw->ai_socktype, resw->ai_protocol)) < 0)
continue;
if ((sock = socket(resw->ai_family, resw->ai_socktype, resw->ai_protocol)) < 0) {
while (curlen_dgram > 0)
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1;
break;
}

(void) setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
optval = 1; /* allow takeover */
Expand Down Expand Up @@ -118,6 +124,7 @@ bindlisten(
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1;
break;
}

Expand All @@ -128,6 +135,7 @@ bindlisten(
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1
break;
}
if (curlen_stream < *retlen_stream) {
Expand Down