Skip to content

Commit

Permalink
Make the s_server command listen on IPv6 only when requested
Browse files Browse the repository at this point in the history
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from openssl#5152)
  • Loading branch information
bernd-edlinger committed Jan 25, 2018
1 parent f1a0f9f commit eee8a40
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions apps/s_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ int do_server(int *accept_sock, const char *host, const char *port,
int sock;
int i;
BIO_ADDRINFO *res = NULL;
const BIO_ADDRINFO *next;
int sock_family, sock_type, sock_protocol;
const BIO_ADDR *sock_address;
int sock_options = BIO_SOCK_REUSEADDR;
int ret = 0;

if (BIO_sock_init() != 1)
Expand All @@ -178,10 +182,27 @@ int do_server(int *accept_sock, const char *host, const char *port,
&& (type == 0 || type == BIO_ADDRINFO_socktype(res))
&& (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res)));

asock = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
BIO_ADDRINFO_protocol(res), 0);
sock_family = BIO_ADDRINFO_family(res);
sock_type = BIO_ADDRINFO_socktype(res);
sock_protocol = BIO_ADDRINFO_protocol(res);
sock_address = BIO_ADDRINFO_address(res);
next = BIO_ADDRINFO_next(res);
if(sock_family == AF_INET6)
sock_options |= BIO_SOCK_V6_ONLY;
if (next != NULL
&& BIO_ADDRINFO_socktype(next) == sock_type
&& BIO_ADDRINFO_protocol(next) == sock_protocol) {
if (sock_family == AF_INET && BIO_ADDRINFO_family(next) == AF_INET6) {
sock_family = AF_INET6;
sock_address = BIO_ADDRINFO_address(next);
}
else if (sock_family == AF_INET6 && BIO_ADDRINFO_family(next) == AF_INET)
sock_options &= ~BIO_SOCK_V6_ONLY;
}

asock = BIO_socket(sock_family, sock_type, sock_protocol, 0);
if (asock == INVALID_SOCKET
|| !BIO_listen(asock, BIO_ADDRINFO_address(res), BIO_SOCK_REUSEADDR)) {
|| !BIO_listen(asock, sock_address, sock_options)) {
BIO_ADDRINFO_free(res);
ERR_print_errors(bio_err);
if (asock != INVALID_SOCKET)
Expand Down

0 comments on commit eee8a40

Please sign in to comment.