]> git.stg.codes - stg.git/blob - stglibs/sgcp.lib/utils.cpp
Introduced SGCP library.
[stg.git] / stglibs / sgcp.lib / utils.cpp
1 #include "stg/sgcp_utils.h"
2
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netdb.h>
6
7 std::vector<in_addr> STG::SGCP::resolve(const std::string& address)
8 {
9     addrinfo* result = NULL;
10
11     addrinfo hints;
12     memset(&hints, 0, sizeof(hints));
13
14     hints.ai_family = AF_INET;    /* Allow IPv4 */
15     hints.ai_socktype = 0; /* Datagram socket */
16     hints.ai_flags = 0;    /* For wildcard IP address */
17     hints.ai_protocol = 0;          /* Any protocol */
18     hints.ai_canonname = NULL;
19     hints.ai_addr = NULL;
20     hints.ai_next = NULL;
21
22     int res = getaddrinfo(address.c_str(), NULL, &hints, &result);
23     if (res != 0)
24         throw TransportProto::Error(gai_error(res));
25
26     std::vector as;
27     for (const addrinfo* p = result; p != NULL; p = p->next)
28         as.push_back(p->ai_addr.sin_addr);
29     return as;
30 }