controller-legacy/helpers/get_port.c

25 lines
480 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)
{
2020-04-24 13:08:26 +00:00
LOG_ERROR("could not get socket name for port: %s\n", strerror(errno));
2020-04-13 22:50:55 +00:00
return 0;
}
else
{
return ntohs(sin.sin_port);
}
}