controller-legacy/helpers/get_port.c

25 lines
478 B
C
Raw Normal View History

2020-04-13 22:50:55 +00:00
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <helpers.h>
#include <logger.h>
uint16_t
helper_get_port(int sock)
{
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
{
LOG_ERROR("could not get socket name for port: %s", strerror(errno));
return 0;
}
else
{
return ntohs(sin.sin_port);
}
}