From e4638515a1ade5a23f3b8eb9ca7dad249a3d6903 Mon Sep 17 00:00:00 2001 From: Adrian Friedli Date: Thu, 2 Sep 2010 14:26:42 +0200 Subject: [PATCH 1/5] use getaddrinfo for lookup in unix.c --- unix.c | 99 +++++++++++++++++++++++++-------------------------------------- 1 files changed, 39 insertions(+), 60 deletions(-) diff --git a/unix.c b/unix.c index 6971541..7329e8d 100644 --- a/unix.c +++ b/unix.c @@ -74,82 +74,61 @@ enet_time_set (enet_uint32 newTimeBase) int enet_address_set_host (ENetAddress * address, const char * name) { - struct hostent * hostEntry = NULL; -#ifdef HAS_GETHOSTBYNAME_R - struct hostent hostData; - char buffer [2048]; - int errnum; - -#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); -#else - hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); -#endif -#else - hostEntry = gethostbyname (name); -#endif + struct addrinfo hints; + struct addrinfo * result; + struct addrinfo * res; + + memset(& hints, 0, sizeof (hints)); + hints.ai_flags = AI_NUMERICSERV; + hints.ai_family = AF_INET; - if (hostEntry == NULL || - hostEntry -> h_addrtype != AF_INET) + if ( getaddrinfo(name, NULL, &hints, &result) ) { -#ifdef HAS_INET_PTON - if (! inet_pton (AF_INET, name, & address -> host)) -#else - if (! inet_aton (name, (struct in_addr *) & address -> host)) -#endif - return -1; - return 0; + return -1; } - address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; + for (res = result; res != NULL; res = res -> ai_next) + { + if (res -> ai_family == AF_INET) + { + address -> host = ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr; + break; + } + } + freeaddrinfo(result); + if (res == NULL) return -1; return 0; } -int -enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) +static int +enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) { -#ifdef HAS_INET_NTOP - if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL) -#else - char * addr = inet_ntoa (* (struct in_addr *) & address -> host); - if (addr != NULL) - strncpy (name, addr, nameLength); - else -#endif + struct sockaddr_in sin; + + memset (& sin, 0, sizeof (struct sockaddr_in)); + + sin.sin_family = AF_INET; + sin.sin_addr = * (struct in_addr *) & address -> host; + + if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags)) + { return -1; + } + return 0; } int -enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) +enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) { - struct in_addr in; - struct hostent * hostEntry = NULL; -#ifdef HAS_GETHOSTBYADDR_R - struct hostent hostData; - char buffer [2048]; - int errnum; - - in.s_addr = address -> host; - -#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); -#else - hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum); -#endif -#else - in.s_addr = address -> host; - - hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); -#endif - - if (hostEntry == NULL) - return enet_address_get_host_ip (address, name, nameLength); - - strncpy (name, hostEntry -> h_name, nameLength); + return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); +} - return 0; +int +enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) +{ + return enet_address_get_host_x(address, name, nameLength, 0); } int -- 1.7.1