From 3d916b35f2dadd17a7c9f5acd08ef396b8c8263c Mon Sep 17 00:00:00 2001 From: plutooo Date: Thu, 1 Aug 2013 11:52:13 -0700 Subject: core: getaddrinfo() lookup error handling --- core/network.c | 47 +++++++++++++++------- core/network.h | 16 +++++--- .../bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 2 +- testing/nTox.c | 2 +- testing/nTox_win32.c | 4 +- testing/toxic/prompt.c | 4 +- 6 files changed, 48 insertions(+), 27 deletions(-) diff --git a/core/network.c b/core/network.c index a7a4efcd..c58549bf 100644 --- a/core/network.c +++ b/core/network.c @@ -164,25 +164,42 @@ void shutdown_networking() 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(const char *address) +/* + resolve_addr(): + address should represent IPv4 or a hostname with A record + + returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + returns 0 on failure + + TODO: Fix ipv6 support +*/ +uint32_t resolve_addr(const char *address) { - struct addrinfo hints; + struct addrinfo *server = NULL; + struct addrinfo hints; + int rc; + uint32_t addr; + 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 + hints.ai_family = AF_INET; // IPv4 only right now. + hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. - struct addrinfo *server = NULL; + rc = getaddrinfo(address, "echo", &hints, &server); - int success = getaddrinfo(address, "echo", &hints, &server); - if(success != 0) - return -1; + // Lookup failed. + if(rc != 0) { + return 0; + } + + // IPv4 records only.. + if(server->ai_family != AF_INET) { + freeaddrinfo(server); + return 0; + } + + + addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; - int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; freeaddrinfo(server); - return resolved; + return addr; } diff --git a/core/network.h b/core/network.h index 3277070c..a5f7899b 100644 --- a/core/network.h +++ b/core/network.h @@ -116,12 +116,16 @@ 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(const char *address); +/* + resolve_addr(): + address should represent IPv4 or a hostname with A record + + returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + returns 0 on failure + + TODO: Fix ipv6 support +*/ +uint32_t resolve_addr(const char *address); #ifdef __cplusplus } diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 4f28fb3c..48152744 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c @@ -245,7 +245,7 @@ struct server_conf_s configure_server(char *cfg_file) it away in the server_info struct */ server_conf.info[i].valid = 1; - if(resolve_addr(strcpy(tmp_ip, bs_ip)) == -1) { + if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid IP\n", i); } diff --git a/testing/nTox.c b/testing/nTox.c index 17f4b0e7..f87a2a25 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -423,7 +423,7 @@ int main(int argc, char *argv[]) IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); - if (resolved_address != -1) + if (resolved_address != 0) bootstrap_ip_port.ip.i = resolved_address; else exit(1); diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index b9208d4f..2394877f 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c @@ -346,7 +346,7 @@ int main(int argc, char *argv[]) IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); - if (resolved_address != -1) + if (resolved_address != 0) bootstrap_ip_port.ip.i = resolved_address; else exit(1); @@ -384,4 +384,4 @@ int main(int argc, char *argv[]) Sleep(1); } return 0; -} \ No newline at end of file +} diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 3c4a27dd..a3cf2d94 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -83,8 +83,8 @@ static void execute(ToxWindow* self, char* cmd) { dht.port = htons(atoi(port)); - int resolved_address = resolve_addr(ip); - if (resolved_address == -1) { + uint32_t resolved_address = resolve_addr(ip); + if (resolved_address == 0) { return; } -- cgit v1.2.3