Skip to content

Commit

Permalink
Updated websocket binding in an attempt to support a dual stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Jun 16, 2023
1 parent b7f8790 commit bfacede
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2760,32 +2760,40 @@ ws_socket (int *listener) {
if (bind (*listener, (struct sockaddr *) &servaddr, sizeof (servaddr)) != 0)
FATAL ("Unable to set bind: %s.", strerror (errno));
} else {
int ov = 1;
struct addrinfo hints, *ai;
int ov = 1, bind_result = 0;
struct addrinfo hints, *ai, *p;

/* get a socket and bind it */
memset (&hints, 0, sizeof hints);
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/*hints.ai_flags = AI_PASSIVE; */

if (getaddrinfo (wsconfig.host, wsconfig.port, &hints, &ai) != 0)
FATAL ("Unable to set server: %s.", gai_strerror (errno));

/* Create a TCP socket. */
if ((*listener = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1)
FATAL ("Unable to open socket: %s.", strerror (errno));
for (p = ai; p != NULL; p = p->ai_next) {
*listener = socket (p->ai_family, p->ai_socktype, p->ai_protocol);
if (*listener == -1)
continue;

/* Options */
if (setsockopt (*listener, SOL_SOCKET, SO_REUSEADDR, &ov, sizeof (ov)) == -1)
FATAL ("Unable to set setsockopt: %s.", strerror (errno));
if (setsockopt (*listener, SOL_SOCKET, SO_REUSEADDR, &ov, sizeof (ov)) == -1) {
close (*listener);
continue;
}

if (bind (*listener, p->ai_addr, p->ai_addrlen) == 0) {
bind_result = 1;
break;
}

close (*listener);
}

/* Bind the socket to the address. */
if (bind (*listener, ai->ai_addr, ai->ai_addrlen) != 0)
FATAL ("Unable to set bind: %s.", strerror (errno));
freeaddrinfo (ai);

if (bind_result == 0)
FATAL ("Unable to set bind: %s.", strerror (errno));
}

/* Tell the socket to accept connections. */
if (listen (*listener, SOMAXCONN) == -1)
FATAL ("Unable to listen: %s.", strerror (errno));
}
Expand Down

0 comments on commit bfacede

Please sign in to comment.