Skip to content
Permalink
Browse files

Simplified usage of select() for simultaneous listening via IPv4 and …

…IPv6

On the introducion of select() to simultaneously listen on the
assigned port on localhost both via IPv4 and via IPv6 unnecessarily a
timeout was used and the select() call repeated in a loop if the
timeout expired without incoming data. Now the code is simplified to
not use a timeout and a loop but let select() wait for input
indefinitely.
  • Loading branch information...
tillkamppeter committed Aug 10, 2015
1 parent 4684440 commit a632841f8e65d402e13e81921515f5a1e2736c82
Showing with 19 additions and 26 deletions.
  1. +19 −26 src/tcp.c
@@ -242,35 +242,28 @@ struct tcp_conn_t *tcp_conn_select(struct tcp_sock_t *sock,
goto error;
}
fd_set rfds;
struct timeval tv;
int retval = 0;
int nfds = 0;
while (retval == 0) {
FD_ZERO(&rfds);
if (sock) {
FD_SET(sock->sd, &rfds);
nfds = sock->sd;
}
if (sock6) {
FD_SET(sock6->sd, &rfds);
if (sock6->sd > nfds)
nfds = sock6->sd;
}
if (nfds == 0) {
ERR("No valid TCP socket supplied.");
goto error;
}
nfds += 1;
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(nfds, &rfds, NULL, NULL, &tv);
if (retval == -1) {
ERR("Failed to open tcp connection");
goto error;
}
FD_ZERO(&rfds);
if (sock) {
FD_SET(sock->sd, &rfds);
nfds = sock->sd;
}
if (sock6) {
FD_SET(sock6->sd, &rfds);
if (sock6->sd > nfds)
nfds = sock6->sd;
}
if (nfds == 0) {
ERR("No valid TCP socket supplied.");
goto error;
}
nfds += 1;
retval = select(nfds, &rfds, NULL, NULL, NULL);
if (retval < 1) {
ERR("Failed to open tcp connection");
goto error;
}

if (sock && FD_ISSET(sock->sd, &rfds)) {
conn->sd = accept(sock->sd, NULL, NULL);
NOTE ("Using IPv4");

0 comments on commit a632841

Please sign in to comment.
You can’t perform that action at this time.