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 ++++++++++------ 2 files changed, 42 insertions(+), 21 deletions(-) (limited to 'core') 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 } -- cgit v1.2.3