44 lines
1,014 B
C++
44 lines
1,014 B
C++
|
#include <netdb.h>
|
||
|
#include <trantor/utils/Logger.h>
|
||
|
#include <helpers.h>
|
||
|
|
||
|
#include "config.h"
|
||
|
|
||
|
int
|
||
|
helpers::bind_tcp_server(const char *addr, const char *port, int max_client_backlog)
|
||
|
{
|
||
|
struct addrinfo hints, *res;
|
||
|
int fd;
|
||
|
int status;
|
||
|
|
||
|
memset(&hints, 0, sizeof hints);
|
||
|
hints.ai_family = AF_UNSPEC;
|
||
|
hints.ai_socktype = SOCK_STREAM;
|
||
|
hints.ai_flags = AI_PASSIVE;
|
||
|
|
||
|
if ((status = getaddrinfo(addr, port, &hints, &res)) != 0)
|
||
|
{
|
||
|
LOG_ERROR << "Error getting address info: " << gai_strerror(status);
|
||
|
}
|
||
|
|
||
|
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||
|
|
||
|
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
|
||
|
{
|
||
|
LOG_ERROR << "Error binding socket. " << status;
|
||
|
freeaddrinfo(res);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if ((status = listen(fd, max_client_backlog)) == -1)
|
||
|
{
|
||
|
LOG_ERROR << "Error setting up listener. " << status;
|
||
|
freeaddrinfo(res);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
freeaddrinfo(res);
|
||
|
|
||
|
return fd;
|
||
|
}
|