From da49d6a3b05cd7d3c9d417f4f08cb63431635617 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 10 Feb 2009 19:43:11 +0000 Subject: [PATCH] Stop using platform inet_aton/inet_addr; they can behave strangely on certain misformed addresses. svn:r1111 --- evutil.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/evutil.c b/evutil.c index 9c2e9530..e3ddb96a 100644 --- a/evutil.c +++ b/evutil.c @@ -479,21 +479,17 @@ evutil_inet_pton(int af, const char *src, void *dst) return inet_pton(af, src, dst); #else if (af == AF_INET) { -#ifdef _EVENT_HAVE_INET_ATON - return inet_aton(src, dst); -#else - ev_uint32_t r; - struct in_addr *out = dst; - if (strcmp(src, "255.255.255.255") == 0) { - out->s_addr = 0xffffffffu; - } else { - r = inet_addr(src); - if (r == INADDR_NONE) - return 0; - out->s_addr = r; - } - return 1; -#endif + int a,b,c,d; + char more; + struct in_addr *addr = dst; + if (sscanf(src, "%d.%d.%d.%d%c", &a,&b,&c,&d,&more) != 4) + return 0; + if (a < 0 || a > 255) return 0; + if (b < 0 || b > 255) return 0; + if (c < 0 || c > 255) return 0; + if (d < 0 || d > 255) return 0; + addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); + return 1; #ifdef AF_INET6 } else if (af == AF_INET6) { struct in6_addr *out = dst;