From 3800be3898f8b37d139ac997fbe79125a1727e5a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 23 Sep 2008 19:17:19 +0000 Subject: [PATCH] Remove usage of inet_ntoa and inet_ntoa_r --- lib/inet_ntop.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/inet_ntop.c b/lib/inet_ntop.c index 9501bfa6b..26867f4d2 100644 --- a/lib/inet_ntop.c +++ b/lib/inet_ntop.c @@ -42,12 +42,6 @@ #include "inet_ntop.h" -#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL) -/* this platform has a inet_ntoa_r() function, but no proto declared anywhere - so we include our own proto to make compilers happy */ -#include "inet_ntoa_r.h" -#endif - #define IN6ADDRSZ 16 #define INADDRSZ 4 #define INT16SZ 2 @@ -62,30 +56,26 @@ */ static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size) { -#if defined(HAVE_INET_NTOA_R_2_ARGS) - const char *ptr; + char tmp[sizeof "255.255.255.255"]; + size_t len; + DEBUGASSERT(size >= 16); - ptr = inet_ntoa_r(*(struct in_addr*)src, dst); - return (char *)memmove(dst, ptr, strlen(ptr)+1); -#elif defined(HAVE_INET_NTOA_R) + tmp[0] = '\0'; + (void)snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d", + ((int)((unsigned char)src[0])) & 0xff, + ((int)((unsigned char)src[1])) & 0xff, + ((int)((unsigned char)src[2])) & 0xff, + ((int)((unsigned char)src[3])) & 0xff); -#if defined(HAVE_INT_INET_NTOA_R) - return inet_ntoa_r(*(struct in_addr*)src, dst, size)? NULL: dst; -#else - return inet_ntoa_r(*(struct in_addr*)src, dst, size); -#endif - -#else - const char *addr = inet_ntoa(*(struct in_addr*)src); - - if(strlen(addr) >= size) + len = strlen(tmp); + if(len == 0 || len >= size) { SET_ERRNO(ENOSPC); return (NULL); } - return strcpy(dst, addr); -#endif + strcpy(dst, tmp); + return dst; } #ifdef ENABLE_IPV6 @@ -192,7 +182,8 @@ static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size) SET_ERRNO(ENOSPC); return (NULL); } - return strcpy (dst, tmp); + strcpy(dst, tmp); + return dst; } #endif /* ENABLE_IPV6 */