Handle port==0 when socket is bound on ipv6
I discovered
0515c6aad6
doesn't work when the server is bound on an AF_INET6 address on Windows
due to the getsockname() call failing.
This commit is contained in:
parent
40662d5e3c
commit
f275352cba
14
httplib.h
14
httplib.h
@ -1626,12 +1626,18 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (port == 0) {
|
if (port == 0) {
|
||||||
struct sockaddr_in sin;
|
struct sockaddr_storage address;
|
||||||
socklen_t len = sizeof(sin);
|
socklen_t len = sizeof(address);
|
||||||
if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&sin), &len) == -1) {
|
if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&address), &len) == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
return ntohs(sin.sin_port);
|
|
||||||
} else {
|
} else {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user