Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/external/enet/patches/0001-use-getaddrinfo-for-lookup-in-unix.c.patch @ 7395

Last change on this file since 7395 was 7390, checked in by adrfried, 14 years ago

IPv6 for Windows

I have not tested this, please test this and report issues to me.

File size: 4.1 KB
  • unix.c

    From e4638515a1ade5a23f3b8eb9ca7dad249a3d6903 Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Thu, 2 Sep 2010 14:26:42 +0200
    Subject: [PATCH 1/5] use getaddrinfo for lookup in unix.c
    
    ---
     unix.c |   99 +++++++++++++++++++++++++--------------------------------------
     1 files changed, 39 insertions(+), 60 deletions(-)
    
    diff --git a/unix.c b/unix.c
    index 6971541..7329e8d 100644
    a b enet_time_set (enet_uint32 newTimeBase) 
    7474int
    7575enet_address_set_host (ENetAddress * address, const char * name)
    7676{
    77     struct hostent * hostEntry = NULL;
    78 #ifdef HAS_GETHOSTBYNAME_R
    79     struct hostent hostData;
    80     char buffer [2048];
    81     int errnum;
    82 
    83 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
    84     gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
    85 #else
    86     hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
    87 #endif
    88 #else
    89     hostEntry = gethostbyname (name);
    90 #endif
     77    struct addrinfo hints;
     78    struct addrinfo * result;
     79    struct addrinfo * res;
     80
     81    memset(& hints, 0, sizeof (hints));
     82    hints.ai_flags = AI_NUMERICSERV;
     83    hints.ai_family = AF_INET;
    9184
    92     if (hostEntry == NULL ||
    93         hostEntry -> h_addrtype != AF_INET)
     85    if ( getaddrinfo(name, NULL, &hints, &result) )
    9486    {
    95 #ifdef HAS_INET_PTON
    96         if (! inet_pton (AF_INET, name, & address -> host))
    97 #else
    98         if (! inet_aton (name, (struct in_addr *) & address -> host))
    99 #endif
    100             return -1;
    101         return 0;
     87        return -1;
    10288    }
    10389
    104     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
     90    for (res = result; res != NULL; res = res -> ai_next)
     91    {
     92        if (res -> ai_family == AF_INET)
     93        {
     94            address -> host = ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr;
     95            break;
     96        }
     97    }
     98    freeaddrinfo(result);
     99    if (res == NULL) return -1;
    105100
    106101    return 0;
    107102}
    108103
    109 int
    110 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
     104static int
     105enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    111106{
    112 #ifdef HAS_INET_NTOP
    113     if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
    114 #else
    115     char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
    116     if (addr != NULL)
    117         strncpy (name, addr, nameLength);
    118     else
    119 #endif
     107    struct sockaddr_in sin;
     108
     109    memset (& sin, 0, sizeof (struct sockaddr_in));
     110
     111    sin.sin_family = AF_INET;
     112    sin.sin_addr = * (struct in_addr *) & address -> host;
     113
     114    if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags))
     115    {
    120116        return -1;
     117    }
     118
    121119    return 0;
    122120}
    123121
    124122int
    125 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     123enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    126124{
    127     struct in_addr in;
    128     struct hostent * hostEntry = NULL;
    129 #ifdef HAS_GETHOSTBYADDR_R
    130     struct hostent hostData;
    131     char buffer [2048];
    132     int errnum;
    133 
    134     in.s_addr = address -> host;
    135 
    136 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
    137     gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
    138 #else
    139     hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
    140 #endif
    141 #else
    142     in.s_addr = address -> host;
    143 
    144     hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
    145 #endif
    146 
    147     if (hostEntry == NULL)
    148       return enet_address_get_host_ip (address, name, nameLength);
    149 
    150     strncpy (name, hostEntry -> h_name, nameLength);
     125    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     126}
    151127
    152     return 0;
     128int
     129enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     130{
     131    return enet_address_get_host_x(address, name, nameLength, 0);
    153132}
    154133
    155134int
Note: See TracBrowser for help on using the repository browser.