diff options
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/core/network.c b/core/network.c index dbe4574c..6337651a 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -159,3 +159,28 @@ void shutdown_networking() | |||
159 | #endif | 159 | #endif |
160 | return; | 160 | return; |
161 | } | 161 | } |
162 | |||
163 | /* resolves provided address to a binary data in network byte order | ||
164 | address is ASCII null terminated string | ||
165 | address should represent IPv4, IPv6 or a hostname | ||
166 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
167 | on failure returns -1 */ | ||
168 | int resolve_addr(char *address) | ||
169 | { | ||
170 | struct addrinfo hints; | ||
171 | memset(&hints, 0, sizeof(hints)); | ||
172 | hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6 | ||
173 | hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses | ||
174 | |||
175 | struct addrinfo *server = NULL; | ||
176 | |||
177 | int success = getaddrinfo(address, "7", &hints, &server); | ||
178 | if(success != 0) | ||
179 | { | ||
180 | return -1; | ||
181 | } | ||
182 | |||
183 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | ||
184 | freeaddrinfo(server); | ||
185 | return resolved; | ||
186 | } \ No newline at end of file | ||