Skip to content

Commit

Permalink
Merge pull request yhirose#70 from sgraham/ipv6-getsockname
Browse files Browse the repository at this point in the history
Handle port==0 when socket is bound on ipv6
  • Loading branch information
yhirose authored May 29, 2018
2 parents 40662d5 + f275352 commit a7f4709
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1626,12 +1626,18 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags)
}

if (port == 0) {
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&sin), &len) == -1) {
struct sockaddr_storage address;
socklen_t len = sizeof(address);
if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&address), &len) == -1) {
return -1;
}
return ntohs(sin.sin_port);
if (address.ss_family == AF_INET) {
return ntohs(reinterpret_cast<struct sockaddr_in*>(&address)->sin_port);
} else if (address.ss_family == AF_INET6) {
return ntohs(reinterpret_cast<struct sockaddr_in6*>(&address)->sin6_port);
} else {
return -1;
}
} else {
return port;
}
Expand Down

0 comments on commit a7f4709

Please sign in to comment.