core-legacy/helpers/get_server_port.cc

18 lines
325 B
C++
Raw Normal View History

2019-07-14 22:39:37 +00:00
#include <netdb.h>
#include <helpers.h>
int
helpers::get_server_port(int fd)
{
if(fd == -1)
{
return -1;
}
2019-07-20 12:51:45 +00:00
struct sockaddr_in sin{};
2019-09-08 21:42:48 +00:00
socklen_t addr_len = sizeof(sin);
if(getsockname(fd, (struct sockaddr *)&sin, &addr_len) == 0)
2019-07-14 22:39:37 +00:00
{
2019-07-19 09:41:39 +00:00
return ntohs(sin.sin_port);
2019-07-14 22:39:37 +00:00
}
return -1;
}