Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/external/enet/patches/0001-Add-IPv6-support-to-Enet.patch @ 8328

Last change on this file since 8328 was 8088, checked in by adrfried, 14 years ago

Updated Enet to version 1.3.1

updated patch

  • Property svn:eol-style set to native
File size: 45.0 KB
  • host.c

    From 7a7274dad3f90d206e91c0712ea0d2599850b24e Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Thu, 2 Sep 2010 14:26:42 +0200
    Subject: [PATCH] Add IPv6 support to Enet
    
    ---
     host.c               |   57 +++++++---
     include/enet/enet.h  |   64 ++++++++---
     include/enet/win32.h |    4 +
     protocol.c           |   73 ++++++++++--
     unix.c               |  312 ++++++++++++++++++++++++++++++--------------------
     win32.c              |  247 ++++++++++++++++++++++++++--------------
     6 files changed, 507 insertions(+), 250 deletions(-)
     mode change 100755 => 100644 Doxyfile
     mode change 100755 => 100644 docs/FAQ.dox
     mode change 100755 => 100644 docs/design.dox
     mode change 100755 => 100644 docs/install.dox
     mode change 100755 => 100644 docs/license.dox
     mode change 100755 => 100644 docs/mainpage.dox
     mode change 100755 => 100644 docs/tutorial.dox
     mode change 100755 => 100644 enet.dsp
    
    diff --git a/Doxyfile b/Doxyfile
    old mode 100755
    new mode 100644
    diff --git a/docs/FAQ.dox b/docs/FAQ.dox
    old mode 100755
    new mode 100644
    diff --git a/docs/design.dox b/docs/design.dox
    old mode 100755
    new mode 100644
    diff --git a/docs/install.dox b/docs/install.dox
    old mode 100755
    new mode 100644
    diff --git a/docs/license.dox b/docs/license.dox
    old mode 100755
    new mode 100644
    diff --git a/docs/mainpage.dox b/docs/mainpage.dox
    old mode 100755
    new mode 100644
    diff --git a/docs/tutorial.dox b/docs/tutorial.dox
    old mode 100755
    new mode 100644
    diff --git a/enet.dsp b/enet.dsp
    old mode 100755
    new mode 100644
    diff --git a/host.c b/host.c
    index 8bb2433..a9d157b 100644
    a b  
    77#include <time.h>
    88#include "enet/enet.h"
    99
     10static ENetSocket
     11enet_socket_create_bind (const ENetAddress * address, ENetAddressFamily family)
     12{
     13    ENetSocket socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, family);
     14    if (socket == ENET_SOCKET_NULL)
     15        return ENET_SOCKET_NULL;
     16
     17    /* This is not a conditional bind anymore,
     18     * because WSARecvFrom returned WSAEINVAL on the IPv6 socket.
     19     * TODO: Check for it's consequences. */
     20    if (enet_socket_bind (socket, address, family) < 0)
     21    {
     22        enet_socket_destroy (socket);
     23        return ENET_SOCKET_NULL;
     24    }
     25
     26    enet_socket_set_option (socket, ENET_SOCKOPT_NONBLOCK, 1);
     27    enet_socket_set_option (socket, ENET_SOCKOPT_BROADCAST, 1);
     28    enet_socket_set_option (socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
     29    enet_socket_set_option (socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
     30
     31    return socket;
     32}
     33
    1034/** @defgroup host ENet host functions
    1135    @{
    1236*/
    enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 
    3155{
    3256    ENetHost * host;
    3357    ENetPeer * currentPeer;
     58    int family;
    3459
    3560    if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID)
    3661      return NULL;
    enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 
    4873    }
    4974    memset (host -> peers, 0, peerCount * sizeof (ENetPeer));
    5075
    51     host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM);
    52     if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0))
    53     {
    54        if (host -> socket != ENET_SOCKET_NULL)
    55          enet_socket_destroy (host -> socket);
     76    family = (address == NULL || !memcmp (& address -> host, & ENET_HOST_ANY, sizeof (ENetHostAddress))) ?
     77        ENET_IPV4 | ENET_IPV6 :
     78        enet_get_address_family (address);
    5679
    57        enet_free (host -> peers);
    58        enet_free (host);
     80    host -> socket4 = (family & ENET_IPV4) ?
     81      enet_socket_create_bind (address, ENET_IPV4) :
     82      ENET_SOCKET_NULL;
     83    host -> socket6 = (family & ENET_IPV6) ?
     84      enet_socket_create_bind (address, ENET_IPV6) :
     85      ENET_SOCKET_NULL;
    5986
    60        return NULL;
     87    if (host -> socket4 == ENET_SOCKET_NULL && host -> socket6 == ENET_SOCKET_NULL)
     88    {
     89        enet_free (host -> peers);
     90        enet_free (host);
     91        return NULL;
    6192    }
    6293
    63     enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1);
    64     enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1);
    65     enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
    66     enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
    67 
    6894    if (address != NULL)
    6995      host -> address = * address;
    7096
    enet_host_destroy (ENetHost * host) 
    133159{
    134160    ENetPeer * currentPeer;
    135161
    136     enet_socket_destroy (host -> socket);
     162    if (host -> socket4 != ENET_SOCKET_NULL)
     163      enet_socket_destroy (host -> socket4);
     164    if (host -> socket6 != ENET_SOCKET_NULL)
     165      enet_socket_destroy (host -> socket6);
    137166
    138167    for (currentPeer = host -> peers;
    139168         currentPeer < & host -> peers [host -> peerCount];
  • include/enet/enet.h

    diff --git a/include/enet/enet.h b/include/enet/enet.h
    index b572590..8351166 100644
    a b typedef enum _ENetSocketOption 
    5353   ENET_SOCKOPT_REUSEADDR = 5
    5454} ENetSocketOption;
    5555
    56 enum
     56typedef struct _ENetHostAddress
    5757{
    58    ENET_HOST_ANY       = 0,            /**< specifies the default server host */
    59    ENET_HOST_BROADCAST = 0xFFFFFFFF,   /**< specifies a subnet-wide broadcast */
    60 
    61    ENET_PORT_ANY       = 0             /**< specifies that a port should be automatically chosen */
     58   enet_uint8 addr[16];
     59} ENetHostAddress;
     60
     61#define ENET_HOST_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }                         /**< specifies the default server host (macro for variable initialization) */
     62static const ENetHostAddress ENET_HOST_ANY = ENET_HOST_ANY_INIT;                           /**< specifies the default server host (global constant variable) */
     63#define ENET_IPV4MAPPED_PREFIX_INIT { { 0,0,0,0,0,0,0,0,0,0,0xff,0xff,0,0,0,0 } }          /**< specifies the IPv4-mapped IPv6 prefix (macro for variable initialization) */
     64static const ENetHostAddress ENET_IPV4MAPPED_PREFIX = ENET_IPV4MAPPED_PREFIX_INIT;         /**< specifies the IPv4-mapped IPv6 prefix (global constant variable) */
     65#define ENET_HOST_BROADCAST_INIT { { 0,0,0,0,0,0,0,0,0,0,0xff,0xff,0xff,0xff,0xff,0xff } } /**< specifies a IPv4 subnet-wide broadcast (macro for variable initialization) */
     66static const ENetHostAddress ENET_HOST_BROADCAST = ENET_HOST_BROADCAST_INIT;               /**< specifies a IPv4 subnet-wide broadcast (global constant variable) */
     67enum {
     68    ENET_IPV4MAPPED_PREFIX_LEN = 12,                                                       /**< specifies the length of the IPv4-mapped IPv6 prefix */
     69    ENET_PORT_ANY              = 0                                                         /**< specifies that a port should be automatically chosen */
    6270};
    6371
    6472/**
    enum 
    7381 */
    7482typedef struct _ENetAddress
    7583{
    76    enet_uint32 host;
     84   ENetHostAddress host;
     85#ifdef WIN32
     86   u_long scopeID;
     87#else
     88   uint32_t scopeID;
     89#endif
    7790   enet_uint16 port;
    7891} ENetAddress;
    7992
    8093/**
     94 * The address family type.
     95 */
     96typedef enum _ENetAddressFamily
     97{
     98    ENET_NO_ADDRESS_FAMILY = 0,
     99    ENET_IPV4 = (1 << 0),
     100    ENET_IPV6 = (1 << 1)
     101} ENetAddressFamily;
     102
     103/**
    81104 * Packet flag bit constants.
    82105 *
    83106 * The host must be specified in network byte-order, and the port must be in
    typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * b 
    321344  */
    322345typedef struct _ENetHost
    323346{
    324    ENetSocket           socket;
     347   ENetSocket           socket4;
     348   ENetSocket           socket6;
    325349   ENetAddress          address;                     /**< Internet address of the host */
    326350   enet_uint32          incomingBandwidth;           /**< downstream bandwidth of the host */
    327351   enet_uint32          outgoingBandwidth;           /**< upstream bandwidth of the host */
    ENET_API void enet_time_set (enet_uint32); 
    441465/** @defgroup socket ENet socket functions
    442466    @{
    443467*/
    444 ENET_API ENetSocket enet_socket_create (ENetSocketType);
    445 ENET_API int        enet_socket_bind (ENetSocket, const ENetAddress *);
     468ENET_API ENetSocket enet_socket_create (ENetSocketType, ENetAddressFamily);
     469ENET_API int        enet_socket_bind (ENetSocket, const ENetAddress *, ENetAddressFamily);
    446470ENET_API int        enet_socket_listen (ENetSocket, int);
    447 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
    448 ENET_API int        enet_socket_connect (ENetSocket, const ENetAddress *);
    449 ENET_API int        enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
    450 ENET_API int        enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
    451 ENET_API int        enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
     471ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFamily);
     472ENET_API int        enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily);
     473ENET_API int        enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily);
     474ENET_API int        enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily);
     475ENET_API int        enet_socket_wait (ENetSocket, ENetSocket, enet_uint32 *, enet_uint32);
    452476ENET_API int        enet_socket_set_option (ENetSocket, ENetSocketOption, int);
    453477ENET_API void       enet_socket_destroy (ENetSocket);
    454478ENET_API int        enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32);
    ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostN 
    488512*/
    489513ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
    490514
     515/** Maps an IPv4 Address to an IPv6 address.
     516    @param address IPv4 address in network byte order
     517    @returns the IPv4-mapped IPv6 address in network byte order
     518*/
     519ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address);
     520
     521/** Returns the Address family of an (IPv4-mapped) IPv6 address.
     522    @param address IPv6 address
     523    @returns address family
     524*/
     525ENET_API ENetAddressFamily enet_get_address_family (const ENetAddress * address);
     526
    491527/** @} */
    492528
    493529ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32);
  • include/enet/win32.h

    diff --git a/include/enet/win32.h b/include/enet/win32.h
    index 0e1cf0c..6145851 100644
    a b typedef fd_set ENetSocketSet; 
    5353#define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset))
    5454#define ENET_SOCKETSET_CHECK(sockset, socket)  FD_ISSET (socket, & (sockset))
    5555
     56#ifndef AI_ADDRCONFIG
     57#define AI_ADDRCONFIG 0x0400 /* AI_ADDRCONFIG is not defined everywhere */
     58#endif
     59
    5660#endif /* __ENET_WIN32_H__ */
    5761
    5862
  • protocol.c

    diff --git a/protocol.c b/protocol.c
    index 2237ba8..7205d1b 100644
    a b static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] = 
    2525    sizeof (ENetProtocolThrottleConfigure),
    2626};
    2727
     28ENetHostAddress
     29enet_address_map4 (enet_uint32 address)
     30{
     31    ENetHostAddress addr = ENET_IPV4MAPPED_PREFIX_INIT;
     32    ((enet_uint32 *)addr.addr)[3] = address;
     33    return addr;
     34}
     35
     36ENetAddressFamily
     37enet_get_address_family (const ENetAddress * address)
     38{
     39    if (!memcmp(& address->host, & ENET_IPV4MAPPED_PREFIX, ENET_IPV4MAPPED_PREFIX_LEN))
     40        return ENET_IPV4;
     41    return ENET_IPV6;
     42}
     43
    2844size_t
    2945enet_protocol_command_size (enet_uint8 commandNumber)
    3046{
    enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet 
    266282         ++ currentPeer)
    267283    {
    268284        if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
    269             currentPeer -> address.host == host -> receivedAddress.host &&
    270285            currentPeer -> address.port == host -> receivedAddress.port &&
    271             currentPeer -> connectID == command -> connect.connectID)
     286            currentPeer -> connectID == command -> connect.connectID &&
     287            !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)))
    272288          return NULL;
    273289    }
    274290
    enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 
    852868
    853869       if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
    854870           peer -> state == ENET_PEER_STATE_ZOMBIE ||
    855            (host -> receivedAddress.host != peer -> address.host &&
    856              peer -> address.host != ENET_HOST_BROADCAST) ||
    857871           (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
    858             sessionID != peer -> incomingSessionID))
     872            sessionID != peer -> incomingSessionID) ||
     873           ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) &&
     874             memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) &&
     875             peer -> address.host.addr[0] != 0xff ) )
    859876         return 0;
    860877    }
    861878 
    enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 
    895912       
    896913    if (peer != NULL)
    897914    {
    898        peer -> address.host = host -> receivedAddress.host;
    899        peer -> address.port = host -> receivedAddress.port;
     915       peer -> address = host -> receivedAddress;
    900916       peer -> incomingDataTotal += host -> receivedDataLength;
    901917    }
    902918   
    commandError: 
    10251041}
    10261042 
    10271043static int
    1028 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
     1044enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family)
    10291045{
    10301046    for (;;)
    10311047    {
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10351051       buffer.data = host -> packetData [0];
    10361052       buffer.dataLength = sizeof (host -> packetData [0]);
    10371053
    1038        receivedLength = enet_socket_receive (host -> socket,
     1054       receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6,
    10391055                                             & host -> receivedAddress,
    10401056                                             & buffer,
    1041                                              1);
     1057                                             1,
     1058                                             family);
    10421059
    10431060       if (receivedLength < 0)
    10441061         return -1;
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10461063       if (receivedLength == 0)
    10471064         return 0;
    10481065
     1066       if (enet_get_address_family (& host -> receivedAddress) != family)
     1067         return -1;
     1068
    10491069       host -> receivedData = host -> packetData [0];
    10501070       host -> receivedDataLength = receivedLength;
    10511071     
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    13971417         currentPeer < & host -> peers [host -> peerCount];
    13981418         ++ currentPeer)
    13991419    {
     1420        ENetAddressFamily family;
     1421        ENetSocket socket;
     1422
    14001423        if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
    14011424            currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
    14021425          continue;
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    15201543
    15211544        currentPeer -> lastSendTime = host -> serviceTime;
    15221545
    1523         sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
     1546        family = enet_get_address_family (& currentPeer -> address);
     1547        socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6;
     1548        if (socket == ENET_SOCKET_NULL)
     1549          return -1;
     1550        sentLength = enet_socket_send (socket,
     1551                                           & currentPeer -> address,
     1552                                           host -> buffers,
     1553                                           host -> bufferCount,
     1554                                           family);
    15241555
    15251556        enet_protocol_remove_sent_unreliable_commands (currentPeer);
    15261557
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16311662          break;
    16321663       }
    16331664
    1634        switch (enet_protocol_receive_incoming_commands (host, event))
     1665       if (host -> socket4 != ENET_SOCKET_NULL)
     1666         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4))
     1667       {
     1668       case 1:
     1669          return 1;
     1670
     1671       case -1:
     1672          perror ("Error receiving incoming packets");
     1673
     1674          return -1;
     1675
     1676       default:
     1677          break;
     1678       }
     1679
     1680       if (host -> socket6 != ENET_SOCKET_NULL)
     1681         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6))
    16351682       {
    16361683       case 1:
    16371684          return 1;
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16831730
    16841731       waitCondition = ENET_SOCKET_WAIT_RECEIVE;
    16851732
    1686        if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
     1733       if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
    16871734         return -1;
    16881735       
    16891736       host -> serviceTime = enet_time_get ();
  • unix.c

    diff --git a/unix.c b/unix.c
    index 6971541..992ecd3 100644
    a b enet_time_set (enet_uint32 newTimeBase) 
    7171    timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
    7272}
    7373
    74 int
    75 enet_address_set_host (ENetAddress * address, const char * name)
     74static enet_uint16
     75enet_af (ENetAddressFamily family)
    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    if (family == ENET_IPV4)
     78        return AF_INET;
     79    if (family == ENET_IPV6)
     80        return AF_INET6;
     81    return 0;
     82}
     83
     84static socklen_t
     85enet_sa_size (ENetAddressFamily family)
     86{
     87    if (family == ENET_IPV4)
     88        return sizeof (struct sockaddr_in);
     89    if (family == ENET_IPV6)
     90        return sizeof (struct sockaddr_in6);
     91    return 0;
     92}
    9193
    92     if (hostEntry == NULL ||
    93         hostEntry -> h_addrtype != AF_INET)
     94static ENetAddressFamily
     95enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
     96{
     97    memset (address, 0, sizeof (ENetAddress));
     98    if (sin -> sa_family == AF_INET)
    9499    {
    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;
     100        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
     101        /* address -> scopeID = 0; */
     102        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
     103        return ENET_IPV4;
    102104    }
    103 
    104     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
    105 
    106     return 0;
     105    if (sin -> sa_family == AF_INET6)
     106    {
     107        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
     108        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
     109        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
     110        return ENET_IPV6;
     111    }
     112    return ENET_NO_ADDRESS_FAMILY;
    107113}
    108114
    109 int
    110 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
     115static int
     116enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
    111117{
    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
    120         return -1;
    121     return 0;
     118    memset (sin, 0, enet_sa_size(family));
     119    if (family == ENET_IPV4 &&
     120      (enet_get_address_family (address) == ENET_IPV4 ||
     121      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
     122    {
     123        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
     124        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
     125        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
     126        return 0;
     127    }
     128    else if (family == ENET_IPV6)
     129    {
     130        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
     131        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
     132        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
     133        ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     134        return 0;
     135    }
     136    return -1;
    122137}
    123138
    124139int
    125 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     140enet_address_set_host (ENetAddress * address, const char * name)
    126141{
    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;
     142    enet_uint16 port = address -> port;
     143    struct addrinfo hints;
     144    struct addrinfo * result;
     145    struct addrinfo * res;
    133146
    134     in.s_addr = address -> host;
     147    memset(& hints, 0, sizeof (hints));
     148    hints.ai_flags = AI_ADDRCONFIG;
     149    hints.ai_family = AF_UNSPEC;
    135150
    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;
     151    if ( getaddrinfo(name, NULL, &hints, &result) )
     152        return -1;
    143153
    144     hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
    145 #endif
     154    for (res = result; res != NULL; res = res -> ai_next)
     155    {
     156        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
     157            break;
     158    }
    146159
    147     if (hostEntry == NULL)
    148       return enet_address_get_host_ip (address, name, nameLength);
     160    address -> port = port;
     161    freeaddrinfo(result);
     162    if (res == NULL) return -1;
    149163
    150     strncpy (name, hostEntry -> h_name, nameLength);
     164    return 0;
     165}
     166
     167static int
     168enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
     169{
     170    struct sockaddr_storage sin;
     171    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
     172
     173    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
     174        return -1;
    151175
    152176    return 0;
    153177}
    154178
    155179int
    156 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     180enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    157181{
    158     struct sockaddr_in sin;
     182    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     183}
    159184
    160     memset (& sin, 0, sizeof (struct sockaddr_in));
     185int
     186enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     187{
     188    return enet_address_get_host_x(address, name, nameLength, 0);
     189}
    161190
    162     sin.sin_family = AF_INET;
     191int
     192enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
     193{
     194    struct sockaddr_storage sin;
    163195
    164196    if (address != NULL)
    165197    {
    166        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    167        sin.sin_addr.s_addr = address -> host;
     198        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    168199    }
    169200    else
    170201    {
    171        sin.sin_port = 0;
    172        sin.sin_addr.s_addr = INADDR_ANY;
     202        ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 };
     203        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
    173204    }
    174205
    175     return bind (socket,
    176                  (struct sockaddr *) & sin,
    177                  sizeof (struct sockaddr_in));
     206    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family));
    178207}
    179208
    180 int 
     209int
    181210enet_socket_listen (ENetSocket socket, int backlog)
    182211{
    183212    return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
    184213}
    185214
    186215ENetSocket
    187 enet_socket_create (ENetSocketType type)
     216enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    188217{
    189     return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     218    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     219
     220#ifdef IPV6_V6ONLY
     221    if (family == ENET_IPV6)
     222    {
     223        int value = 1;
     224        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
     225    }
     226#endif /* IPV6_V6ONLY */
     227
     228    return sock;
    190229}
    191230
    192231int
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    226265}
    227266
    228267int
    229 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     268enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    230269{
    231     struct sockaddr_in sin;
    232 
    233     memset (& sin, 0, sizeof (struct sockaddr_in));
     270    struct sockaddr_storage sin;
     271    enet_address_set_sin((struct sockaddr *) & sin, address, family);
    234272
    235     sin.sin_family = AF_INET;
    236     sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    237     sin.sin_addr.s_addr = address -> host;
    238 
    239     return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
     273    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family));
    240274}
    241275
    242276ENetSocket
    243 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     277enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    244278{
    245279    int result;
    246     struct sockaddr_in sin;
    247     socklen_t sinLength = sizeof (struct sockaddr_in);
     280    struct sockaddr_storage sin;
     281    socklen_t sinLength = enet_sa_size (family);
    248282
    249283    result = accept (socket,
    250                      address != NULL ? (struct sockaddr *) & sin : NULL, 
     284                     address != NULL ? (struct sockaddr *) & sin : NULL,
    251285                     address != NULL ? & sinLength : NULL);
    252    
     286
    253287    if (result == -1)
    254288      return ENET_SOCKET_NULL;
    255289
    256290    if (address != NULL)
    257291    {
    258         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    259         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     292        enet_address_set_address(address, (struct sockaddr *) & sin);
    260293    }
    261294
    262295    return result;
    263 } 
    264    
     296}
     297
    265298void
    266299enet_socket_destroy (ENetSocket socket)
    267300{
    int 
    272305enet_socket_send (ENetSocket socket,
    273306                  const ENetAddress * address,
    274307                  const ENetBuffer * buffers,
    275                   size_t bufferCount)
     308                  size_t bufferCount,
     309                  ENetAddressFamily family)
    276310{
    277311    struct msghdr msgHdr;
    278     struct sockaddr_in sin;
     312    struct sockaddr_storage sin;
    279313    int sentLength;
    280314
    281315    memset (& msgHdr, 0, sizeof (struct msghdr));
    282316
    283317    if (address != NULL)
    284318    {
    285         memset (& sin, 0, sizeof (struct sockaddr_in));
    286 
    287         sin.sin_family = AF_INET;
    288         sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    289         sin.sin_addr.s_addr = address -> host;
    290 
     319        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    291320        msgHdr.msg_name = & sin;
    292         msgHdr.msg_namelen = sizeof (struct sockaddr_in);
     321        msgHdr.msg_namelen = enet_sa_size (family);
    293322    }
    294323
    295324    msgHdr.msg_iov = (struct iovec *) buffers;
    int 
    312341enet_socket_receive (ENetSocket socket,
    313342                     ENetAddress * address,
    314343                     ENetBuffer * buffers,
    315                      size_t bufferCount)
     344                     size_t bufferCount,
     345                     ENetAddressFamily family)
    316346{
    317347    struct msghdr msgHdr;
    318     struct sockaddr_in sin;
     348    struct sockaddr_storage sin;
    319349    int recvLength;
    320350
    321351    memset (& msgHdr, 0, sizeof (struct msghdr));
    enet_socket_receive (ENetSocket socket, 
    323353    if (address != NULL)
    324354    {
    325355        msgHdr.msg_name = & sin;
    326         msgHdr.msg_namelen = sizeof (struct sockaddr_in);
     356        msgHdr.msg_namelen = enet_sa_size (family);
    327357    }
    328358
    329359    msgHdr.msg_iov = (struct iovec *) buffers;
    enet_socket_receive (ENetSocket socket, 
    346376
    347377    if (address != NULL)
    348378    {
    349         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    350         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     379        enet_address_set_address(address, (struct sockaddr *) & sin);
    351380    }
    352381
    353382    return recvLength;
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    365394}
    366395
    367396int
    368 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     397enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    369398{
    370399#ifdef HAS_POLL
    371     struct pollfd pollSocket;
     400    struct pollfd pollSocket[2];
    372401    int pollCount;
    373    
    374     pollSocket.fd = socket;
    375     pollSocket.events = 0;
     402
     403    pollSocket[0].fd = socket4;
     404    pollSocket[1].fd = socket6;
     405    pollSocket[0].events = 0;
     406    pollSocket[1].events = 0;
     407    /* pollSocket[0].revents = 0; */
     408    pollSocket[1].revents = 0;
     409
     410    if (pollSocket[0].fd == ENET_SOCKET_NULL)
     411    {
     412        pollSocket[0].fd = pollSocket[1].fd;
     413        pollSocket[1].fd = ENET_SOCKET_NULL;
     414    }
    376415
    377416    if (* condition & ENET_SOCKET_WAIT_SEND)
    378       pollSocket.events |= POLLOUT;
     417    {
     418        pollSocket[0].events |= POLLOUT;
     419        pollSocket[1].events |= POLLOUT;
     420    }
    379421
    380422    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    381       pollSocket.events |= POLLIN;
     423    {
     424        pollSocket[0].events |= POLLIN;
     425        pollSocket[1].events |= POLLIN;
     426    }
    382427
    383     pollCount = poll (& pollSocket, 1, timeout);
     428    pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout);
    384429
    385430    if (pollCount < 0)
    386431      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    390435    if (pollCount == 0)
    391436      return 0;
    392437
    393     if (pollSocket.revents & POLLOUT)
     438    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT)
    394439      * condition |= ENET_SOCKET_WAIT_SEND;
    395440   
    396     if (pollSocket.revents & POLLIN)
     441    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN)
    397442      * condition |= ENET_SOCKET_WAIT_RECEIVE;
    398443
    399444    return 0;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    401446    fd_set readSet, writeSet;
    402447    struct timeval timeVal;
    403448    int selectCount;
     449    ENetSocket maxSocket;
    404450
    405451    timeVal.tv_sec = timeout / 1000;
    406452    timeVal.tv_usec = (timeout % 1000) * 1000;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    409455    FD_ZERO (& writeSet);
    410456
    411457    if (* condition & ENET_SOCKET_WAIT_SEND)
    412       FD_SET (socket, & writeSet);
     458    {
     459        if (socket4 != ENET_SOCKET_NULL)
     460            FD_SET (socket4, & writeSet);
     461        if (socket6 != ENET_SOCKET_NULL)
     462            FD_SET (socket6, & writeSet);
     463    }
    413464
    414465    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    415       FD_SET (socket, & readSet);
     466    {
     467        if (socket4 != ENET_SOCKET_NULL)
     468            FD_SET (socket4, & readSet);
     469        if (socket6 != ENET_SOCKET_NULL)
     470            FD_SET (socket6, & readSet);
     471    }
     472
     473    maxSocket = 0;
     474    if (socket4 != ENET_SOCKET_NULL)
     475        maxSocket = socket4;
     476    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
     477        maxSocket = socket6;
    416478
    417     selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
     479    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
    418480
    419481    if (selectCount < 0)
    420482      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    424486    if (selectCount == 0)
    425487      return 0;
    426488
    427     if (FD_ISSET (socket, & writeSet))
    428       * condition |= ENET_SOCKET_WAIT_SEND;
     489    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
     490        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
     491        * condition |= ENET_SOCKET_WAIT_SEND;
    429492
    430     if (FD_ISSET (socket, & readSet))
    431       * condition |= ENET_SOCKET_WAIT_RECEIVE;
     493    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
     494        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
     495        * condition |= ENET_SOCKET_WAIT_RECEIVE;
    432496
    433497    return 0;
    434498#endif
  • win32.c

    diff --git a/win32.c b/win32.c
    index e1fae23..15b9888 100644
    a b  
    44*/
    55#ifdef WIN32
    66
     7#define _WIN32_WINNT 0x0501
     8
    79#include <time.h>
     10#include <ws2tcpip.h>
    811#define ENET_BUILDING_LIB 1
    912#include "enet/enet.h"
     13#include <ws2tcpip.h>
    1014
    1115static enet_uint32 timeBase = 0;
    1216
    1317int
    1418enet_initialize (void)
    1519{
    16     WORD versionRequested = MAKEWORD (1, 1);
     20    WORD versionRequested = MAKEWORD (2, 2);
    1721    WSADATA wsaData;
    1822   
    1923    if (WSAStartup (versionRequested, & wsaData))
    2024       return -1;
    2125
    22     if (LOBYTE (wsaData.wVersion) != 1||
    23         HIBYTE (wsaData.wVersion) != 1)
     26    if (LOBYTE (wsaData.wVersion) != 2||
     27        HIBYTE (wsaData.wVersion) != 2)
    2428    {
    2529       WSACleanup ();
    2630       
    enet_time_set (enet_uint32 newTimeBase) 
    5256    timeBase = (enet_uint32) timeGetTime () - newTimeBase;
    5357}
    5458
    55 int
    56 enet_address_set_host (ENetAddress * address, const char * name)
     59static enet_uint16
     60enet_af (ENetAddressFamily family)
    5761{
    58     struct hostent * hostEntry;
     62    if (family == ENET_IPV4)
     63        return AF_INET;
     64    if (family == ENET_IPV6)
     65        return AF_INET6;
     66    return 0;
     67}
    5968
    60     hostEntry = gethostbyname (name);
    61     if (hostEntry == NULL ||
    62         hostEntry -> h_addrtype != AF_INET)
     69static socklen_t
     70enet_sa_size (ENetAddressFamily family)
     71{
     72    if (family == ENET_IPV4)
     73        return sizeof (struct sockaddr_in);
     74    if (family == ENET_IPV6)
     75        return sizeof (struct sockaddr_in6);
     76    return 0;
     77}
     78
     79static ENetAddressFamily
     80enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
     81{
     82    memset (address, 0, sizeof (ENetAddress));
     83    if (sin -> sa_family == AF_INET)
    6384    {
    64         unsigned long host = inet_addr (name);
    65         if (host == INADDR_NONE)
    66             return -1;
    67         address -> host = host;
    68         return 0;
     85        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
     86        /* address -> scopeID = 0; */
     87        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
     88        return ENET_IPV4;
    6989    }
     90    if (sin -> sa_family == AF_INET6)
     91    {
     92        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
     93        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
     94        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
     95        return ENET_IPV6;
     96    }
     97    return ENET_NO_ADDRESS_FAMILY;
     98}
    7099
    71     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
    72 
    73     return 0;
     100static int
     101enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
     102{
     103    memset (sin, 0, enet_sa_size(family));
     104    if (family == ENET_IPV4 &&
     105      (enet_get_address_family (address) == ENET_IPV4 ||
     106      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
     107    {
     108        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
     109        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
     110        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
     111        return 0;
     112    }
     113    else if (family == ENET_IPV6)
     114    {
     115        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
     116        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
     117        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
     118        ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     119        return 0;
     120    }
     121    return -1;
    74122}
    75123
    76124int
    77 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
     125enet_address_set_host (ENetAddress * address, const char * name)
    78126{
    79     char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
    80     if (addr == NULL)
     127    enet_uint16 port = address -> port;
     128    struct addrinfo hints;
     129    struct addrinfo * result;
     130    struct addrinfo * res;
     131
     132    memset(& hints, 0, sizeof (hints));
     133    hints.ai_flags = AI_ADDRCONFIG;
     134    hints.ai_family = AF_UNSPEC;
     135
     136    if ( getaddrinfo(name, NULL, &hints, &result) )
    81137        return -1;
    82     strncpy (name, addr, nameLength);
     138
     139    for (res = result; res != NULL; res = res -> ai_next)
     140    {
     141        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
     142            break;
     143    }
     144
     145    address -> port = port;
     146    freeaddrinfo(result);
     147    if (res == NULL) return -1;
     148
    83149    return 0;
    84150}
    85151
    86 int
    87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     152static int
     153enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    88154{
    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);
     155    struct sockaddr_storage sin;
     156    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
    97157
    98     strncpy (name, hostEntry -> h_name, nameLength);
     158    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
     159        return -1;
    99160
    100161    return 0;
    101162}
    102163
    103164int
    104 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     165enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    105166{
    106     struct sockaddr_in sin;
     167    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     168}
    107169
    108     memset (& sin, 0, sizeof (struct sockaddr_in));
     170int
     171enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     172{
     173    return enet_address_get_host_x(address, name, nameLength, 0);
     174}
    109175
    110     sin.sin_family = AF_INET;
     176int
     177enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
     178{
     179    struct sockaddr_storage sin;
    111180
    112181    if (address != NULL)
    113182    {
    114        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    115        sin.sin_addr.s_addr = address -> host;
     183        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    116184    }
    117185    else
    118186    {
    119        sin.sin_port = 0;
    120        sin.sin_addr.s_addr = INADDR_ANY;
     187        ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 };
     188        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
    121189    }
    122190
    123     return bind (socket,
    124                  (struct sockaddr *) & sin,
    125                  sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
     191    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0;
    126192}
    127193
    128194int
    enet_socket_listen (ENetSocket socket, int backlog) 
    132198}
    133199
    134200ENetSocket
    135 enet_socket_create (ENetSocketType type)
     201enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    136202{
    137     return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     203    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     204    return sock;
    138205}
    139206
    140207int
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    173240}
    174241
    175242int
    176 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     243enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    177244{
    178     struct sockaddr_in sin;
    179 
    180     memset (& sin, 0, sizeof (struct sockaddr_in));
     245    struct sockaddr_storage sin;
     246    enet_address_set_sin((struct sockaddr *) & sin, address, family);
    181247
    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;
     248    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0;
    187249}
    188250
    189251ENetSocket
    190 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     252enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    191253{
    192254    SOCKET result;
    193     struct sockaddr_in sin;
    194     int sinLength = sizeof (struct sockaddr_in);
     255    struct sockaddr_storage sin;
     256    socklen_t sinLength = enet_sa_size (family);
    195257
    196258    result = accept (socket,
    197                      address != NULL ? (struct sockaddr *) & sin : NULL, 
     259                     address != NULL ? (struct sockaddr *) & sin : NULL,
    198260                     address != NULL ? & sinLength : NULL);
    199261
    200262    if (result == INVALID_SOCKET)
    enet_socket_accept (ENetSocket socket, ENetAddress * address) 
    202264
    203265    if (address != NULL)
    204266    {
    205         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    206         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     267        enet_address_set_address(address, (struct sockaddr *) & sin);
    207268    }
    208269
    209270    return result;
    int 
    219280enet_socket_send (ENetSocket socket,
    220281                  const ENetAddress * address,
    221282                  const ENetBuffer * buffers,
    222                   size_t bufferCount)
     283                  size_t bufferCount,
     284                  ENetAddressFamily family)
    223285{
    224     struct sockaddr_in sin;
     286    struct sockaddr_storage sin;
    225287    DWORD sentLength;
    226288
    227289    if (address != NULL)
    228290    {
    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;
     291        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    234292    }
    235293
    236294    if (WSASendTo (socket,
    enet_socket_send (ENetSocket socket, 
    239297                   & sentLength,
    240298                   0,
    241299                   address != NULL ? (struct sockaddr *) & sin : 0,
    242                    address != NULL ? sizeof (struct sockaddr_in) : 0,
     300                   address != NULL ? enet_sa_size (family) : 0,
    243301                   NULL,
    244302                   NULL) == SOCKET_ERROR)
    245303    {
    int 
    256314enet_socket_receive (ENetSocket socket,
    257315                     ENetAddress * address,
    258316                     ENetBuffer * buffers,
    259                      size_t bufferCount)
     317                     size_t bufferCount,
     318                     ENetAddressFamily family)
    260319{
    261     INT sinLength = sizeof (struct sockaddr_in);
     320    INT sinLength = enet_sa_size (family);
    262321    DWORD flags = 0,
    263322          recvLength;
    264     struct sockaddr_in sin;
     323    struct sockaddr_storage sin;
    265324
    266325    if (WSARecvFrom (socket,
    267326                     (LPWSABUF) buffers,
    enet_socket_receive (ENetSocket socket, 
    288347
    289348    if (address != NULL)
    290349    {
    291         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    292         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     350        enet_address_set_address(address, (struct sockaddr *) & sin);
    293351    }
    294352
    295353    return (int) recvLength;
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    307365}
    308366
    309367int
    310 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     368enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    311369{
    312370    fd_set readSet, writeSet;
    313371    struct timeval timeVal;
    314372    int selectCount;
    315    
     373    ENetSocket maxSocket;
     374
    316375    timeVal.tv_sec = timeout / 1000;
    317376    timeVal.tv_usec = (timeout % 1000) * 1000;
    318    
     377
    319378    FD_ZERO (& readSet);
    320379    FD_ZERO (& writeSet);
    321380
    322381    if (* condition & ENET_SOCKET_WAIT_SEND)
    323       FD_SET (socket, & writeSet);
     382    {
     383        if (socket4 != ENET_SOCKET_NULL)
     384            FD_SET (socket4, & writeSet);
     385        if (socket6 != ENET_SOCKET_NULL)
     386            FD_SET (socket6, & writeSet);
     387    }
    324388
    325389    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    326       FD_SET (socket, & readSet);
     390    {
     391        if (socket4 != ENET_SOCKET_NULL)
     392            FD_SET (socket4, & readSet);
     393        if (socket6 != ENET_SOCKET_NULL)
     394            FD_SET (socket6, & readSet);
     395    }
    327396
    328     selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
     397    maxSocket = 0;
     398    if (socket4 != ENET_SOCKET_NULL)
     399        maxSocket = socket4;
     400    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
     401        maxSocket = socket6;
     402
     403    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
    329404
    330405    if (selectCount < 0)
    331406      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    335410    if (selectCount == 0)
    336411      return 0;
    337412
    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;
     413    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
     414        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
     415        * condition |= ENET_SOCKET_WAIT_SEND;
     416
     417    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
     418        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
     419        * condition |= ENET_SOCKET_WAIT_RECEIVE;
    343420
    344421    return 0;
    345 } 
     422}
    346423
    347424#endif
    348425
Note: See TracBrowser for help on using the repository browser.