VirtualBox

Ignore:
Timestamp:
Oct 20, 2013 10:19:35 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
90080
Message:

Use getaddrinfo(3) on Unix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/portfwd.c

    r49027 r49197  
    55#ifndef RT_OS_WINDOWS
    66#include <arpa/inet.h>
     7#include <netdb.h>
    78#include <poll.h>
    89#else
     
    133134           const char *dst_addr_str, uint16_t dst_port)
    134135{
     136    struct addrinfo hints;
     137    struct addrinfo *ai;
    135138    int status;
    136     int saf;
    137139    void *src_addr, *dst_addr;
    138140
     
    143145    fwspec->stype = stype;
    144146
     147    memset(&hints, 0, sizeof(hints));
     148    hints.ai_family = (sdom == PF_INET) ? AF_INET : AF_INET6;
     149    hints.ai_socktype = stype;
     150    hints.ai_flags = AI_NUMERICHOST;
     151
     152    status = getaddrinfo(src_addr_str, NULL, &hints, &ai);
     153    if (status != 0) {
     154        DPRINTF(("%s - %s\n", src_addr_str, gai_strerror(status)));
     155        return -1;
     156    }
     157    LWIP_ASSERT1(ai != NULL);
     158    LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->src));
     159    memcpy(&fwspec->src, ai->ai_addr, ai->ai_addrlen);
     160    freeaddrinfo(ai);
     161    ai = NULL;
     162
     163    status = getaddrinfo(dst_addr_str, NULL, &hints, &ai);
     164    if (status != 0) {
     165        DPRINTF(("%s - %s\n", dst_addr_str, gai_strerror(status)));
     166        return -1;
     167    }
     168    LWIP_ASSERT1(ai != NULL);
     169    LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->dst));
     170    memcpy(&fwspec->dst, ai->ai_addr, ai->ai_addrlen);
     171    freeaddrinfo(ai);
     172    ai = NULL;
     173
    145174    if (sdom == PF_INET) {
    146         struct sockaddr_in *src = &fwspec->src.sin;
    147         struct sockaddr_in *dst = &fwspec->dst.sin;
    148 
    149         saf = AF_INET;
    150 
    151         src->sin_family = saf;
    152 #if HAVE_SA_LEN
    153         src->sin_len = sizeof(*src);
    154 #endif
    155         src->sin_port = htons(src_port);
    156         src_addr = &src->sin_addr;
    157 
    158         dst->sin_family = saf;
    159 #if HAVE_SA_LEN
    160         dst->sin_len = sizeof(*dst);
    161 #endif
    162         dst->sin_port = htons(dst_port);
    163         dst_addr = &dst->sin_addr;
     175        fwspec->src.sin.sin_port = htons(src_port);
     176        fwspec->dst.sin.sin_port = htons(dst_port);
    164177    }
    165178    else { /* PF_INET6 */
    166         struct sockaddr_in6 *src = &fwspec->src.sin6;
    167         struct sockaddr_in6 *dst = &fwspec->dst.sin6;
    168 
    169         saf = AF_INET6;
    170 
    171         src->sin6_family = saf;
    172 #if HAVE_SA_LEN
    173         src->sin6_len = sizeof(*src);
    174 #endif
    175         src->sin6_port = htons(src_port);
    176         src_addr = &src->sin6_addr;
    177 
    178         dst->sin6_family = saf;
    179 #if HAVE_SA_LEN
    180         dst->sin6_len = sizeof(*dst);
    181 #endif
    182         dst->sin6_port = htons(dst_port);
    183         dst_addr = &dst->sin6_addr;
    184     }
    185 
    186     status = inet_pton(saf, src_addr_str, src_addr);
    187     LWIP_ASSERT1(status >= 0);
    188     if (status == 0) {
    189         DPRINTF(("bad address: %s\n", src_addr_str));
    190         return -1;
    191     }
    192 
    193     status = inet_pton(saf, dst_addr_str, dst_addr);
    194     LWIP_ASSERT1(status >= 0);
    195     if (status == 0) {
    196         DPRINTF(("bad address: %s\n", dst_addr_str));
    197         return -1;
     179        fwspec->src.sin6.sin6_port = htons(src_port);
     180        fwspec->dst.sin6.sin6_port = htons(dst_port);
    198181    }
    199182
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette