From e4469b5130ecf2b74f714bee93cab4755fc0543a Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 20 Jul 2013 17:31:36 -0400 Subject: [PATCH] Added address resolving function (Thank you stal). --- core/network.c | 25 +++++++++++++++++++++++++ core/network.h | 11 ++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) 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() #endif return; } + +/* resolves provided address to a binary data in network byte order + address is ASCII null terminated string + address should represent IPv4, IPv6 or a hostname + on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + on failure returns -1 */ +int resolve_addr(char *address) +{ + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6 + hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses + + struct addrinfo *server = NULL; + + int success = getaddrinfo(address, "7", &hints, &server); + if(success != 0) + { + return -1; + } + + int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; + freeaddrinfo(server); + return resolved; +} \ No newline at end of file diff --git a/core/network.h b/core/network.h index fe0c48eb..daab6e41 100644 --- a/core/network.h +++ b/core/network.h @@ -38,6 +38,7 @@ #include #include +#include #undef VANILLA_NACL /* make sure on windows we use libsodium */ @@ -48,7 +49,8 @@ #include #include #include - +#include +#include #endif @@ -121,4 +123,11 @@ int init_networking(IP ip ,uint16_t port); /* function to cleanup networking stuff(doesn't do much right now) */ void shutdown_networking(); + +/* resolves provided address to a binary data in network byte order + address is ASCII null terminated string + address should represent IPv4, IPv6 or a hostname + on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + on failure returns -1 */ +int resolve_addr(char *address); #endif