Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/external/enet/patches/0005-IPv6-for-Windows.patch @ 7390

Last change on this file since 7390 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: 13.5 KB
  • include/enet/enet.h

    From 93248e1cd75da47980e0d69428747ff6471be1ed Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Thu, 9 Sep 2010 16:02:21 +0200
    Subject: [PATCH 5/5] IPv6 for Windows
    
    ---
     include/enet/enet.h  |    6 +-
     include/enet/win32.h |    1 +
     win32.c              |  237 ++++++++++++++++++++++++++++++++------------------
     3 files changed, 160 insertions(+), 84 deletions(-)
    
    diff --git a/include/enet/enet.h b/include/enet/enet.h
    index 616fe7f..54e3b3b 100644
    a b extern const ENetHostAddress ENET_HOST_BROADCAST; /**< specifies a IPv4 subne 
    7777typedef struct _ENetAddress
    7878{
    7979   ENetHostAddress host;
    80    enet_uint32 scopeID; //FIXME: this is of different size on Windows
     80#ifdef WIN32
     81   u_long scopeID;
     82#else
     83   uint32_t scopeID;
     84#endif
    8185   enet_uint16 port;
    8286} ENetAddress;
    8387
  • include/enet/win32.h

    diff --git a/include/enet/win32.h b/include/enet/win32.h
    index 0e1cf0c..9f3f6e5 100644
    a b  
    1414
    1515#include <stdlib.h>
    1616#include <winsock2.h>
     17#include <ws2tcpip.h>
    1718
    1819typedef SOCKET ENetSocket;
    1920
  • win32.c

    diff --git a/win32.c b/win32.c
    index e1fae23..dbbe85a 100644
    a b enet_time_set (enet_uint32 newTimeBase) 
    5252    timeBase = (enet_uint32) timeGetTime () - newTimeBase;
    5353}
    5454
    55 int
    56 enet_address_set_host (ENetAddress * address, const char * name)
     55static enet_uint16
     56enet_af (ENetAddressFamily family)
    5757{
    58     struct hostent * hostEntry;
     58    if (family == ENET_IPV4)
     59        return AF_INET;
     60    if (family == ENET_IPV6)
     61        return AF_INET6;
     62    return 0;
     63}
     64
     65static socklen_t
     66enet_sa_size (ENetAddressFamily family)
     67{
     68    if (family == ENET_IPV4)
     69        return sizeof (SOCKADDR_IN);
     70    if (family == ENET_IPV6)
     71        return sizeof (SOCKADDR_IN6);
     72    return 0;
     73}
    5974
    60     hostEntry = gethostbyname (name);
    61     if (hostEntry == NULL ||
    62         hostEntry -> h_addrtype != AF_INET)
     75static ENetAddressFamily
     76enet_address_set_address (ENetAddress * address, const SOCKADDR * sin)
     77{
     78    memset (address, 0, sizeof (ENetAddress));
     79    if (sin -> sa_family == AF_INET)
    6380    {
    64         unsigned long host = inet_addr (name);
    65         if (host == INADDR_NONE)
    66             return -1;
    67         address -> host = host;
    68         return 0;
     81        address -> host = enet_address_map4 ((((SOCKADDR_IN *) sin) -> sin_addr.s_addr));
     82        //address -> scopeID = 0;
     83        address -> port = ENET_NET_TO_HOST_16 (((SOCKADDR_IN *) sin) -> sin_port);
     84        return ENET_IPV4;
    6985    }
     86    if (sin -> sa_family == AF_INET6)
     87    {
     88        address -> host = * (ENetHostAddress *) & ((SOCKADDR_IN6 *) sin) -> sin6_addr;
     89        address -> scopeID = ((SOCKADDR_IN6 *) sin) -> sin6_scope_id;
     90        address -> port = ENET_NET_TO_HOST_16 (((SOCKADDR_IN6 *) sin) -> sin6_port);
     91        return ENET_IPV6;
     92    }
     93    return ENET_NO_ADDRESS_FAMILY;
     94}
    7095
    71     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
    72 
    73     return 0;
     96static int
     97enet_address_set_sin (SOCKADDR * sin, const ENetAddress * address, ENetAddressFamily family)
     98{
     99    memset (sin, 0, enet_sa_size(family));
     100    if (family == ENET_IPV4 &&
     101      (enet_get_address_family (address) == ENET_IPV4 ||
     102      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
     103    {
     104        ((SOCKADDR_IN *) sin) -> sin_family = AF_INET;
     105        ((SOCKADDR_IN *) sin) -> sin_addr = * (IN_ADDR *) & address -> host.addr[12];
     106        ((SOCKADDR_IN *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
     107        return 0;
     108    }
     109    else if (family == ENET_IPV6)
     110    {
     111        ((SOCKADDR_IN6 *) sin) -> sin6_family = AF_INET6;
     112        ((SOCKADDR_IN6 *) sin) -> sin6_addr = * (IN6_ADDR *) & address -> host;
     113        ((SOCKADDR_IN6 *) sin) -> sin6_scope_id = address -> scopeID;
     114        ((SOCKADDR_IN6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     115        return 0;
     116    }
     117    return -1;
    74118}
    75119
    76120int
    77 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
     121enet_address_set_host (ENetAddress * address, const char * name)
    78122{
    79     char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
    80     if (addr == NULL)
     123    enet_uint16 port = address -> port;
     124    ADDRINFO hints;
     125    ADDRINFO * result;
     126    ADDRINFO * res;
     127
     128    memset(& hints, 0, sizeof (hints));
     129    hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
     130    hints.ai_family = AF_UNSPEC;
     131
     132    if ( getaddrinfo(name, NULL, &hints, &result) )
    81133        return -1;
    82     strncpy (name, addr, nameLength);
     134
     135    for (res = result; res != NULL; res = res -> ai_next)
     136    {
     137        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
     138            break;
     139    }
     140
     141    address -> port = port;
     142    freeaddrinfo(result);
     143    if (res == NULL) return -1;
     144
    83145    return 0;
    84146}
    85147
    86 int
    87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     148static int
     149enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    88150{
    89     struct in_addr in;
    90     struct hostent * hostEntry;
    91    
    92     in.s_addr = address -> host;
    93    
    94     hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
    95     if (hostEntry == NULL)
    96       return enet_address_get_host_ip (address, name, nameLength);
     151    SOCKADDR_STORAGE sin;
     152    enet_address_set_sin((SOCKADDR *) & sin, address, ENET_IPV6);
    97153
    98     strncpy (name, hostEntry -> h_name, nameLength);
     154    if ( getnameinfo((SOCKADDR *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
     155        return -1;
    99156
    100157    return 0;
    101158}
    102159
    103160int
    104 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     161enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    105162{
    106     struct sockaddr_in sin;
     163    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     164}
    107165
    108     memset (& sin, 0, sizeof (struct sockaddr_in));
     166int
     167enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     168{
     169    return enet_address_get_host_x(address, name, nameLength, 0);
     170}
    109171
    110     sin.sin_family = AF_INET;
     172int
     173enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
     174{
     175    SOCKADDR_STORAGE sin;
    111176
    112177    if (address != NULL)
    113178    {
    114        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    115        sin.sin_addr.s_addr = address -> host;
     179        enet_address_set_sin((SOCKADDR *) & sin, address, family);
    116180    }
    117181    else
    118182    {
    119        sin.sin_port = 0;
    120        sin.sin_addr.s_addr = INADDR_ANY;
     183        ENetAddress address_ = { ENET_HOST_ANY, 0, 0 };
     184        enet_address_set_sin((SOCKADDR *) & sin, & address_, family);
    121185    }
    122186
    123     return bind (socket,
    124                  (struct sockaddr *) & sin,
    125                  sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
     187    return bind (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0;
    126188}
    127189
    128190int
    enet_socket_listen (ENetSocket socket, int backlog) 
    132194}
    133195
    134196ENetSocket
    135 enet_socket_create (ENetSocketType type)
     197enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    136198{
    137199    return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
    138200}
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    173235}
    174236
    175237int
    176 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     238enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    177239{
    178     struct sockaddr_in sin;
    179 
    180     memset (& sin, 0, sizeof (struct sockaddr_in));
     240    SOCKADDR_STORAGE sin;
     241    enet_address_set_sin((SOCKADDR *) & sin, address, family);
    181242
    182     sin.sin_family = AF_INET;
    183     sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    184     sin.sin_addr.s_addr = address -> host;
    185 
    186     return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
     243    return connect (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0;
    187244}
    188245
    189246ENetSocket
    190 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     247enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    191248{
    192249    SOCKET result;
    193     struct sockaddr_in sin;
    194     int sinLength = sizeof (struct sockaddr_in);
     250    SOCKADDR_STORAGE sin;
     251    int sinLength = enet_sa_size (family);
    195252
    196253    result = accept (socket,
    197                      address != NULL ? (struct sockaddr *) & sin : NULL,
     254                     address != NULL ? (SOCKADDR *) & sin : NULL,
    198255                     address != NULL ? & sinLength : NULL);
    199256
    200257    if (result == INVALID_SOCKET)
    enet_socket_accept (ENetSocket socket, ENetAddress * address) 
    202259
    203260    if (address != NULL)
    204261    {
    205         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    206         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     262        enet_address_set_address(address, (SOCKADDR *) & sin);
    207263    }
    208264
    209265    return result;
    int 
    219275enet_socket_send (ENetSocket socket,
    220276                  const ENetAddress * address,
    221277                  const ENetBuffer * buffers,
    222                   size_t bufferCount)
     278                  size_t bufferCount,
     279                  ENetAddressFamily family)
    223280{
    224     struct sockaddr_in sin;
     281    SOCKADDR_STORAGE sin;
    225282    DWORD sentLength;
    226283
    227284    if (address != NULL)
    228285    {
    229         memset (& sin, 0, sizeof (struct sockaddr_in));
    230 
    231         sin.sin_family = AF_INET;
    232         sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    233         sin.sin_addr.s_addr = address -> host;
     286        enet_address_set_sin((SOCKADDR *) & sin, address, family);
    234287    }
    235288
    236289    if (WSASendTo (socket,
    enet_socket_send (ENetSocket socket, 
    238291                   (DWORD) bufferCount,
    239292                   & sentLength,
    240293                   0,
    241                    address != NULL ? (struct sockaddr *) & sin : 0,
    242                    address != NULL ? sizeof (struct sockaddr_in) : 0,
     294                   address != NULL ? (SOCKADDR *) & sin : 0,
     295                   address != NULL ? enet_sa_size (family) : 0,
    243296                   NULL,
    244297                   NULL) == SOCKET_ERROR)
    245298    {
    int 
    256309enet_socket_receive (ENetSocket socket,
    257310                     ENetAddress * address,
    258311                     ENetBuffer * buffers,
    259                      size_t bufferCount)
     312                     size_t bufferCount,
     313                     ENetAddressFamily family)
    260314{
    261     INT sinLength = sizeof (struct sockaddr_in);
     315    INT sinLength = enet_sa_size (family);
    262316    DWORD flags = 0,
    263317          recvLength;
    264     struct sockaddr_in sin;
     318    SOCKADDR_STORAGE sin;
    265319
    266320    if (WSARecvFrom (socket,
    267321                     (LPWSABUF) buffers,
    268322                     (DWORD) bufferCount,
    269323                     & recvLength,
    270324                     & flags,
    271                      address != NULL ? (struct sockaddr *) & sin : NULL,
     325                     address != NULL ? (SOCKADDR *) & sin : NULL,
    272326                     address != NULL ? & sinLength : NULL,
    273327                     NULL,
    274328                     NULL) == SOCKET_ERROR)
    enet_socket_receive (ENetSocket socket, 
    288342
    289343    if (address != NULL)
    290344    {
    291         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    292         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     345        enet_address_set_address(address, (SOCKADDR *) & sin);
    293346    }
    294347
    295348    return (int) recvLength;
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    307360}
    308361
    309362int
    310 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     363enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    311364{
    312365    fd_set readSet, writeSet;
    313366    struct timeval timeVal;
    314367    int selectCount;
    315    
     368
    316369    timeVal.tv_sec = timeout / 1000;
    317370    timeVal.tv_usec = (timeout % 1000) * 1000;
    318    
     371
    319372    FD_ZERO (& readSet);
    320373    FD_ZERO (& writeSet);
    321374
    322375    if (* condition & ENET_SOCKET_WAIT_SEND)
    323       FD_SET (socket, & writeSet);
     376    {
     377        if (socket4 != ENET_SOCKET_NULL)
     378            FD_SET (socket4, & writeSet);
     379        if (socket6 != ENET_SOCKET_NULL)
     380            FD_SET (socket6, & writeSet);
     381    }
    324382
    325383    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    326       FD_SET (socket, & readSet);
     384    {
     385        if (socket4 != ENET_SOCKET_NULL)
     386            FD_SET (socket4, & readSet);
     387        if (socket6 != ENET_SOCKET_NULL)
     388            FD_SET (socket6, & readSet);
     389    }
    327390
    328     selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
     391    ENetSocket maxSocket = 0;
     392    if (socket4 != ENET_SOCKET_NULL)
     393        maxSocket = socket4;
     394    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
     395        maxSocket = socket6;
     396
     397    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
    329398
    330399    if (selectCount < 0)
    331400      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    335404    if (selectCount == 0)
    336405      return 0;
    337406
    338     if (FD_ISSET (socket, & writeSet))
    339       * condition |= ENET_SOCKET_WAIT_SEND;
    340    
    341     if (FD_ISSET (socket, & readSet))
    342       * condition |= ENET_SOCKET_WAIT_RECEIVE;
     407    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
     408        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
     409        * condition |= ENET_SOCKET_WAIT_SEND;
     410
     411    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
     412        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
     413        * condition |= ENET_SOCKET_WAIT_RECEIVE;
    343414
    344415    return 0;
    345 } 
     416}
    346417
    347418#endif
    348419
Note: See TracBrowser for help on using the repository browser.