core-legacy/helpers/get_server_port.cc
Tobias Reisinger be84c0e4c2 fix: cleanup
2019-09-08 23:42:48 +02:00

18 lines
No EOL
325 B
C++

#include <netdb.h>
#include <helpers.h>
int
helpers::get_server_port(int fd)
{
if(fd == -1)
{
return -1;
}
struct sockaddr_in sin{};
socklen_t addr_len = sizeof(sin);
if(getsockname(fd, (struct sockaddr *)&sin, &addr_len) == 0)
{
return ntohs(sin.sin_port);
}
return -1;
}