From 0ec0209f65064651bace00b0ac55d2268dec2b3a Mon Sep 17 00:00:00 2001 From: vboxsync Date: Sun, 20 Oct 2013 22:19:35 +0000 Subject: Use getaddrinfo(3) on Unix. git-svn-id: https://www.virtualbox.org/svn/vbox/trunk@49197 cfe28804-0f27-0410-a406-dd0f0b0b656f --- src/VBox/NetworkServices/NAT/portfwd.c | 81 ++++++++++++++-------------------- 1 file changed, 32 insertions(+), 49 deletions(-) (limited to 'src/VBox/NetworkServices/NAT/portfwd.c') diff --git a/src/VBox/NetworkServices/NAT/portfwd.c b/src/VBox/NetworkServices/NAT/portfwd.c index fbc80bc2115..7ea39d61411 100644 --- a/src/VBox/NetworkServices/NAT/portfwd.c +++ b/src/VBox/NetworkServices/NAT/portfwd.c @@ -4,6 +4,7 @@ #ifndef RT_OS_WINDOWS #include +#include #include #else # include "winpoll.h" @@ -132,8 +133,9 @@ fwspec_set(struct fwspec *fwspec, int sdom, int stype, const char *src_addr_str, uint16_t src_port, const char *dst_addr_str, uint16_t dst_port) { + struct addrinfo hints; + struct addrinfo *ai; int status; - int saf; void *src_addr, *dst_addr; LWIP_ASSERT1(sdom == PF_INET || sdom == PF_INET6); @@ -142,59 +144,40 @@ fwspec_set(struct fwspec *fwspec, int sdom, int stype, fwspec->sdom = sdom; fwspec->stype = stype; - if (sdom == PF_INET) { - struct sockaddr_in *src = &fwspec->src.sin; - struct sockaddr_in *dst = &fwspec->dst.sin; - - saf = AF_INET; - - src->sin_family = saf; -#if HAVE_SA_LEN - src->sin_len = sizeof(*src); -#endif - src->sin_port = htons(src_port); - src_addr = &src->sin_addr; - - dst->sin_family = saf; -#if HAVE_SA_LEN - dst->sin_len = sizeof(*dst); -#endif - dst->sin_port = htons(dst_port); - dst_addr = &dst->sin_addr; - } - else { /* PF_INET6 */ - struct sockaddr_in6 *src = &fwspec->src.sin6; - struct sockaddr_in6 *dst = &fwspec->dst.sin6; - - saf = AF_INET6; - - src->sin6_family = saf; -#if HAVE_SA_LEN - src->sin6_len = sizeof(*src); -#endif - src->sin6_port = htons(src_port); - src_addr = &src->sin6_addr; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = (sdom == PF_INET) ? AF_INET : AF_INET6; + hints.ai_socktype = stype; + hints.ai_flags = AI_NUMERICHOST; - dst->sin6_family = saf; -#if HAVE_SA_LEN - dst->sin6_len = sizeof(*dst); -#endif - dst->sin6_port = htons(dst_port); - dst_addr = &dst->sin6_addr; + status = getaddrinfo(src_addr_str, NULL, &hints, &ai); + if (status != 0) { + DPRINTF(("%s - %s\n", src_addr_str, gai_strerror(status))); + return -1; } - - status = inet_pton(saf, src_addr_str, src_addr); - LWIP_ASSERT1(status >= 0); - if (status == 0) { - DPRINTF(("bad address: %s\n", src_addr_str)); + LWIP_ASSERT1(ai != NULL); + LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->src)); + memcpy(&fwspec->src, ai->ai_addr, ai->ai_addrlen); + freeaddrinfo(ai); + ai = NULL; + + status = getaddrinfo(dst_addr_str, NULL, &hints, &ai); + if (status != 0) { + DPRINTF(("%s - %s\n", dst_addr_str, gai_strerror(status))); return -1; } + LWIP_ASSERT1(ai != NULL); + LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->dst)); + memcpy(&fwspec->dst, ai->ai_addr, ai->ai_addrlen); + freeaddrinfo(ai); + ai = NULL; - status = inet_pton(saf, dst_addr_str, dst_addr); - LWIP_ASSERT1(status >= 0); - if (status == 0) { - DPRINTF(("bad address: %s\n", dst_addr_str)); - return -1; + if (sdom == PF_INET) { + fwspec->src.sin.sin_port = htons(src_port); + fwspec->dst.sin.sin_port = htons(dst_port); + } + else { /* PF_INET6 */ + fwspec->src.sin6.sin6_port = htons(src_port); + fwspec->dst.sin6.sin6_port = htons(dst_port); } return 0; -- cgit v1.2.1