2019-07-29 20:02:38 +00:00
|
|
|
#include <helpers.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <trantor/utils/Logger.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
helpers::open_tcp_connection(char *host, char *port)
|
|
|
|
{
|
|
|
|
int s, status;
|
|
|
|
struct addrinfo hints{}, *res;
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if ((status = getaddrinfo(host, port, &hints, &res)) != 0)
|
|
|
|
{
|
2019-09-08 21:42:48 +00:00
|
|
|
LOG_ERROR << "Error getting address info: " << gai_strerror(status);
|
2019-07-29 20:02:38 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
|
|
|
|
|
|
|
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0)
|
|
|
|
{
|
2019-08-06 22:21:06 +00:00
|
|
|
LOG_ERROR << "Error opening connection " << status;
|
2019-07-29 20:02:38 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|