diff --git a/mk/re.mk b/mk/re.mk index f99c844b6..19762107f 100644 --- a/mk/re.mk +++ b/mk/re.mk @@ -555,20 +555,11 @@ ifneq ($(HAVE_SYSLOG),) CFLAGS += -DHAVE_SYSLOG endif -HAVE_INET_NTOP := 1 - CFLAGS += -DHAVE_FORK -ifneq ($(HAVE_INET_NTOP),) -CFLAGS += -DHAVE_INET_NTOP -endif CFLAGS += -DHAVE_PWD_H ifneq ($(OS),darwin) CFLAGS += -DHAVE_POLL # Darwin: poll() does not support devices -HAVE_INET_PTON := 1 -endif -ifneq ($(HAVE_INET_PTON),) -CFLAGS += -DHAVE_INET_PTON endif CFLAGS += -DHAVE_SELECT -DHAVE_SELECT_H CFLAGS += -DHAVE_SETRLIMIT diff --git a/src/sa/mod.mk b/src/sa/mod.mk index 78f05e0f5..dde788c52 100644 --- a/src/sa/mod.mk +++ b/src/sa/mod.mk @@ -4,7 +4,5 @@ # Copyright (C) 2010 Creytiv.com # -SRCS += sa/ntop.c SRCS += sa/printaddr.c -SRCS += sa/pton.c SRCS += sa/sa.c diff --git a/src/sa/ntop.c b/src/sa/ntop.c deleted file mode 100644 index 30aab583f..000000000 --- a/src/sa/ntop.c +++ /dev/null @@ -1,222 +0,0 @@ -/** - * @file ntop.c Network address structure functions - * - * Copyright (C) 2010 Creytiv.com - */ - -#define _BSD_SOURCE 1 -#define _DEFAULT_SOURCE 1 - -#ifdef HAVE_INET_NTOP -#ifdef WIN32 -#ifdef _MSC_VER -#pragma warning (disable: 4090) -#endif -#include -#else -#define __USE_BSD 1 /**< Use BSD code */ -#include -#include -#include -#define __USE_POSIX 1 /**< Use POSIX code */ -#include -#endif /* WIN32 */ -#endif -#include -#include -#include -#include -#include -#include "sa.h" - - -#define DEBUG_MODULE "net_ntop" -#define DEBUG_LEVEL 5 -#include - - -#ifndef HAVE_INET_NTOP - - -#define NS_IN6ADDRSZ 16 /**< IPv6 T_AAAA */ -#define NS_INT16SZ 2 /**< #/bytes of data in a u_int16_t */ - - -static const char* -inet_ntop4(const u_char *src, char *dst, size_t size) -{ - if (re_snprintf(dst, size, "%u.%u.%u.%u", - src[0], src[1], src[2], src[3]) < 0) { - errno = ENOSPC; - dst[size-1] = 0; - return NULL; - } - - return dst; -} - - -#ifdef HAVE_INET6 -/* const char * - * inet_ntop6(src, dst, size) - * convert IPv6 binary address into presentation (printable) format - * author: - * Paul Vixie, 1996. - */ - -static const char * -inet_ntop6(const u_char *src, char *dst, size_t size) -{ - /* - * Note that int32_t and int16_t need only be "at least" large enough - * to contain a value of the specified size. On some systems, like - * Crays, there is no such thing as an integer variable with 16 bits. - * Keep this in mind if you think this function should have been coded - * to use pointer overlays. All the world's not a VAX. - */ - char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; - struct { int base, len; } best, cur; - u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; - int i; - - /* - * Preprocess: - * Copy the input (bytewise) array into a wordwise array. - * Find the longest run of 0x00's in src[] for :: shorthanding. - */ - memset(words, '\0', sizeof words); - for (i = 0; i < NS_IN6ADDRSZ; i++) - words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); - best.base = -1; - best.len = 0; - cur.base = -1; - cur.len = 0; - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { - if (words[i] == 0) { - if (cur.base == -1) - cur.base = i, cur.len = 1; - else - cur.len++; - } - else { - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - } - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - } - if (best.base != -1 && best.len < 2) - best.base = -1; - - /* - * Format the result. - */ - tp = tmp; - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { - /* Are we inside the best run of 0x00's? */ - if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) { - if (i == best.base) - *tp++ = ':'; - continue; - } - /* Are we following an initial run of 0x00s or any real hex?*/ - if (i != 0) - *tp++ = ':'; - /* Is this address an encapsulated IPv4? */ - if (i == 6 && best.base == 0 && - (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { - if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) - return NULL; - tp += strlen(tp); - break; - } - tp += sprintf(tp, "%x", words[i]); - } - /* Was it a trailing run of 0x00's? */ - if (best.base != -1 && (best.base + best.len) == - (NS_IN6ADDRSZ / NS_INT16SZ)) - *tp++ = ':'; - *tp++ = '\0'; - - /* - * Check for overflow, copy, and we're done. - */ - if ((size_t)(tp - tmp) > size) { - errno = ENOSPC; - return NULL; - } - strcpy(dst, tmp); - - return dst; -} -#endif - - -/* - * Implementation of inet_ntop() - */ -const char* inet_ntop(int af, const void *src, char *dst, size_t size); -const char* inet_ntop(int af, const void *src, char *dst, size_t size) -{ - switch (af) { - - case AF_INET: - return inet_ntop4(src, dst, size); - -#ifdef HAVE_INET6 - case AF_INET6: - return inet_ntop6(src, dst, size); -#endif - - default: - DEBUG_WARNING("inet_ntop: unknown address family %d\n", af); - return NULL; - } -} -#endif - - -/* - * POSIX 1003.1-2001 marks gethostbyaddr() and gethostbyname() obsolescent. - * See getaddrinfo(3), getnameinfo(3), gai_strerror(3). - */ - - -/** - * Convert network address structure to a character string - * - * @param sa Socket address - * @param buf Buffer to return IP address - * @param size Size of buffer - * - * @return 0 if success, otherwise errorcode - */ -int net_inet_ntop(const struct sa *sa, char *buf, int size) -{ - if (!sa || !buf || !size) - return EINVAL; - - switch (sa->u.sa.sa_family) { - - case AF_INET: - inet_ntop(AF_INET, &sa->u.in.sin_addr, buf, size); - break; - -#ifdef HAVE_INET6 - case AF_INET6: - inet_ntop(AF_INET6, &sa->u.in6.sin6_addr, buf, size); - break; -#endif - - default: - return EAFNOSUPPORT; - } - - return 0; -} diff --git a/src/sa/pton.c b/src/sa/pton.c deleted file mode 100644 index a9b7ae5cf..000000000 --- a/src/sa/pton.c +++ /dev/null @@ -1,255 +0,0 @@ -/** - * @file pton.c Network address structure functions - * - * Copyright (C) 2010 Creytiv.com - */ - -#define _BSD_SOURCE 1 -#define _DEFAULT_SOURCE 1 - -#ifdef HAVE_INET_PTON -#ifdef WIN32 -#include -#else -#include -#include -#include -#define __USE_POSIX 1 /**< Use POSIX code */ -#include -#endif /* WIN32 */ -#endif -#include -#include -#include -#include -#include -#include "sa.h" - - -#define DEBUG_MODULE "net_pton" -#define DEBUG_LEVEL 5 -#include - - -#ifndef HAVE_INET_PTON - - -#define NS_INADDRSZ 4 /**< IPv4 T_A */ -#define NS_IN6ADDRSZ 16 /**< IPv6 T_AAAA */ -#define NS_INT16SZ 2 /**< #/bytes of data in a u_int16_t */ - - -/* int - * inet_pton4(src, dst) - * like inet_aton() but without all the hexadecimal and shorthand. - * return: - * 1 if `src' is a valid dotted quad, else 0. - * notice: - * does not touch `dst' unless it's returning 1. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton4(const char *src, u_char *dst) -{ - static const char digits[] = "0123456789"; - int saw_digit, octets, ch; - u_char tmp[NS_INADDRSZ], *tp; - - saw_digit = 0; - octets = 0; - *(tp = tmp) = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr(digits, ch)) != NULL) { - u_int newVal = (u_int) (*tp * 10 + (pch - digits)); - - if (newVal > 255) - return 0; - *tp = newVal; - if (! saw_digit) { - if (++octets > 4) - return 0; - saw_digit = 1; - } - } - else if (ch == '.' && saw_digit) { - if (octets == 4) - return 0; - *++tp = 0; - saw_digit = 0; - } - else - return 0; - } - if (octets < 4) - return 0; - - memcpy(dst, tmp, NS_INADDRSZ); - return 1; -} - - -#ifdef HAVE_INET6 -/* int - * inet_pton6(src, dst) - * convert presentation level address to network order binary form. - * return: - * 1 if `src' is a valid [RFC1884 2.2] address, else 0. - * notice: - * (1) does not touch `dst' unless it's returning 1. - * (2) :: in a full address is silently ignored. - * credit: - * inspired by Mark Andrews. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton6(const char *src, u_char *dst) -{ - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; - u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; - const char *xdigits, *curtok; - int ch, saw_xdigit; - u_int val; - - memset((tp = tmp), '\0', NS_IN6ADDRSZ); - endp = tp + NS_IN6ADDRSZ; - colonp = NULL; - /* Leading :: requires some special handling. */ - if (*src == ':') - if (*++src != ':') - return 0; - curtok = src; - saw_xdigit = 0; - val = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); - if (pch != NULL) { - val <<= 4; - val |= (u_int)(pch - xdigits); - if (val > 0xffff) - return 0; - saw_xdigit = 1; - continue; - } - if (ch == ':') { - curtok = src; - if (!saw_xdigit) { - if (colonp) - return 0; - colonp = tp; - continue; - } - if (tp + NS_INT16SZ > endp) - return 0; - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - saw_xdigit = 0; - val = 0; - continue; - } - if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && - inet_pton4(curtok, tp) > 0) { - tp += NS_INADDRSZ; - saw_xdigit = 0; - break; /* '\0' was seen by inet_pton4(). */ - } - return 0; - } - if (saw_xdigit) { - if (tp + NS_INT16SZ > endp) - return 0; - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - } - if (colonp != NULL) { - /* - * Since some memmove()'s erroneously fail to handle - * overlapping regions, we'll do the shift by hand. - */ - const int n = (int)(tp - colonp); - int i; - - for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; - colonp[n - i] = 0; - } - tp = endp; - } - if (tp != endp) - return 0; - memcpy(dst, tmp, NS_IN6ADDRSZ); - - return 1; -} -#endif - - -/** - * Implementation of inet_pton() - */ -static int inet_pton(int af, const char *src, void *dst) -{ - if (!src || !dst) - return 0; - - switch (af) { - - case AF_INET: - return inet_pton4(src, (u_char*) dst); - -#ifdef HAVE_INET6 - case AF_INET6: - return inet_pton6(src, (u_char*) dst); -#endif - - default: - DEBUG_INFO("inet_pton: unknown address family %d\n", af); - errno = EAFNOSUPPORT; - return -1; - } -} -#endif - - -/** - * Convert character string to a network address structure - * - * @param addr IP address string - * @param sa Returned socket address - * - * @return 0 if success, otherwise errorcode - */ -int net_inet_pton(const char *addr, struct sa *sa) -{ - if (!addr) - return EINVAL; - - if (inet_pton(AF_INET, addr, &sa->u.in.sin_addr) > 0) { - sa->u.in.sin_family = AF_INET; - } -#ifdef HAVE_INET6 - else if (inet_pton(AF_INET6, addr, &sa->u.in6.sin6_addr) > 0) { - - if (IN6_IS_ADDR_V4MAPPED(&sa->u.in6.sin6_addr)) { - const uint8_t *a = &sa->u.in6.sin6_addr.s6_addr[12]; - sa->u.in.sin_family = AF_INET; - memcpy(&sa->u.in.sin_addr.s_addr, a, 4); - } - else { - sa->u.in6.sin6_family = AF_INET6; - } - } -#endif - else { - return EINVAL; - } - - return 0; -} diff --git a/src/sa/sa.c b/src/sa/sa.c index ab9fc8a7e..a50e66c1c 100644 --- a/src/sa/sa.c +++ b/src/sa/sa.c @@ -5,12 +5,17 @@ */ #define _BSD_SOURCE 1 #define _DEFAULT_SOURCE 1 + +#ifdef WIN32 +#include +#else +#include +#endif #include #include #include #include #include -#include "sa.h" #define DEBUG_MODULE "sa" @@ -53,6 +58,43 @@ int sa_set(struct sa *sa, const struct pl *addr, uint16_t port) } +/** + * Convert character string to a network address structure + * + * @param addr IP address string + * @param sa Returned socket address + * + * @return 0 if success, otherwise errorcode + */ +static int net_inet_pton(const char *addr, struct sa *sa) +{ + if (!addr) + return EINVAL; + + if (inet_pton(AF_INET, addr, &sa->u.in.sin_addr) > 0) { + sa->u.in.sin_family = AF_INET; + } +#ifdef HAVE_INET6 + else if (inet_pton(AF_INET6, addr, &sa->u.in6.sin6_addr) > 0) { + + if (IN6_IS_ADDR_V4MAPPED(&sa->u.in6.sin6_addr)) { + const uint8_t *a = &sa->u.in6.sin6_addr.s6_addr[12]; + sa->u.in.sin_family = AF_INET; + memcpy(&sa->u.in.sin_addr.s_addr, a, 4); + } + else { + sa->u.in6.sin6_family = AF_INET6; + } + } +#endif + else { + return EINVAL; + } + + return 0; +} + + /** * Set a Socket Address from a string * @@ -319,7 +361,26 @@ void sa_in6(const struct sa *sa, uint8_t *addr) */ int sa_ntop(const struct sa *sa, char *buf, int size) { - return net_inet_ntop(sa, buf, size); + if (!sa || !buf || !size) + return EINVAL; + + switch (sa->u.sa.sa_family) { + + case AF_INET: + inet_ntop(AF_INET, &sa->u.in.sin_addr, buf, size); + break; + +#ifdef HAVE_INET6 + case AF_INET6: + inet_ntop(AF_INET6, &sa->u.in6.sin6_addr, buf, size); + break; +#endif + + default: + return EAFNOSUPPORT; + } + + return 0; } diff --git a/src/sa/sa.h b/src/sa/sa.h deleted file mode 100644 index fdc2216ec..000000000 --- a/src/sa/sa.h +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @file sa.h Internal interface to Socket Address - * - * Copyright (C) 2010 Creytiv.com - */ - - -/* Net ntop/pton */ -int net_inet_ntop(const struct sa *sa, char *buf, int size); -int net_inet_pton(const char *addr, struct sa *sa);