Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/external/enet/patches/0001-IPv6-support.patch @ 8077

Last change on this file since 8077 was 7459, checked in by adrfried, 14 years ago

Merged ipv6 branch

Orxonox now includes a modified version of ENet 1.3.0

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

    From 68e3ac1c2876aedb62a77fdc7ba71e4983a6b99d Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Thu, 2 Sep 2010 14:26:42 +0200
    Subject: [PATCH] IPv6 support
    
    ---
     host.c               |   57 +++++++---
     include/enet/enet.h  |   64 ++++++++---
     include/enet/win32.h |    5 +
     protocol.c           |   73 ++++++++++--
     unix.c               |  312 ++++++++++++++++++++++++++++++--------------------
     win32.c              |  245 ++++++++++++++++++++++++++--------------
     6 files changed, 506 insertions(+), 250 deletions(-)
    
    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 2f656d6..7b468a0 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..53a7ff7 100644
    a b  
    1414
    1515#include <stdlib.h>
    1616#include <winsock2.h>
     17#include <ws2tcpip.h>
    1718
    1819typedef SOCKET ENetSocket;
    1920
    typedef fd_set ENetSocketSet; 
    5354#define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset))
    5455#define ENET_SOCKETSET_CHECK(sockset, socket)  FD_ISSET (socket, & (sockset))
    5556
     57#ifndef AI_ADDRCONFIG
     58#define AI_ADDRCONFIG 0x0400 /* AI_ADDRCONFIG is not defined everywhere */
     59#endif
     60
    5661#endif /* __ENET_WIN32_H__ */
    5762
    5863
  • protocol.c

    diff --git a/protocol.c b/protocol.c
    index 8e26dfb..cfef646 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 
    262278         ++ currentPeer)
    263279    {
    264280        if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
    265             currentPeer -> address.host == host -> receivedAddress.host &&
    266281            currentPeer -> address.port == host -> receivedAddress.port &&
    267             currentPeer -> connectID == command -> connect.connectID)
     282            currentPeer -> connectID == command -> connect.connectID &&
     283            !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)))
    268284          return NULL;
    269285    }
    270286
    enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 
    848864
    849865       if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
    850866           peer -> state == ENET_PEER_STATE_ZOMBIE ||
    851            (host -> receivedAddress.host != peer -> address.host &&
    852              peer -> address.host != ENET_HOST_BROADCAST) ||
    853867           (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
    854             sessionID != peer -> incomingSessionID))
     868            sessionID != peer -> incomingSessionID) ||
     869           ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) &&
     870             memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) &&
     871             peer -> address.host.addr[0] != 0xff ) )
    855872         return 0;
    856873    }
    857874 
    enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 
    891908       
    892909    if (peer != NULL)
    893910    {
    894        peer -> address.host = host -> receivedAddress.host;
    895        peer -> address.port = host -> receivedAddress.port;
     911       peer -> address = host -> receivedAddress;
    896912       peer -> incomingDataTotal += host -> receivedDataLength;
    897913    }
    898914   
    commandError: 
    10211037}
    10221038 
    10231039static int
    1024 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
     1040enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family)
    10251041{
    10261042    for (;;)
    10271043    {
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10311047       buffer.data = host -> packetData [0];
    10321048       buffer.dataLength = sizeof (host -> packetData [0]);
    10331049
    1034        receivedLength = enet_socket_receive (host -> socket,
     1050       receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6,
    10351051                                             & host -> receivedAddress,
    10361052                                             & buffer,
    1037                                              1);
     1053                                             1,
     1054                                             family);
    10381055
    10391056       if (receivedLength < 0)
    10401057         return -1;
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10421059       if (receivedLength == 0)
    10431060         return 0;
    10441061
     1062       if (enet_get_address_family (& host -> receivedAddress) != family)
     1063         return -1;
     1064
    10451065       host -> receivedData = host -> packetData [0];
    10461066       host -> receivedDataLength = receivedLength;
    10471067     
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    13731393         currentPeer < & host -> peers [host -> peerCount];
    13741394         ++ currentPeer)
    13751395    {
     1396        ENetAddressFamily family;
     1397        ENetSocket socket;
     1398
    13761399        if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED ||
    13771400            currentPeer -> state == ENET_PEER_STATE_ZOMBIE)
    13781401          continue;
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    14971520
    14981521        currentPeer -> lastSendTime = host -> serviceTime;
    14991522
    1500         sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
     1523        family = enet_get_address_family (& currentPeer -> address);
     1524        socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6;
     1525        if (socket == ENET_SOCKET_NULL)
     1526          return -1;
     1527        sentLength = enet_socket_send (socket,
     1528                                           & currentPeer -> address,
     1529                                           host -> buffers,
     1530                                           host -> bufferCount,
     1531                                           family);
    15011532
    15021533        enet_protocol_remove_sent_unreliable_commands (currentPeer);
    15031534
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16081639          break;
    16091640       }
    16101641
    1611        switch (enet_protocol_receive_incoming_commands (host, event))
     1642       if (host -> socket4 != ENET_SOCKET_NULL)
     1643         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4))
     1644       {
     1645       case 1:
     1646          return 1;
     1647
     1648       case -1:
     1649          perror ("Error receiving incoming packets");
     1650
     1651          return -1;
     1652
     1653       default:
     1654          break;
     1655       }
     1656
     1657       if (host -> socket6 != ENET_SOCKET_NULL)
     1658         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6))
    16121659       {
    16131660       case 1:
    16141661          return 1;
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16601707
    16611708       waitCondition = ENET_SOCKET_WAIT_RECEIVE;
    16621709
    1663        if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
     1710       if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
    16641711         return -1;
    16651712       
    16661713       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..2607160 100644
    a b  
    44*/
    55#ifdef WIN32
    66
     7#define _WIN32_WINNT 0x0501
     8
    79#include <time.h>
    810#define ENET_BUILDING_LIB 1
    911#include "enet/enet.h"
    static enet_uint32 timeBase = 0; 
    1315int
    1416enet_initialize (void)
    1517{
    16     WORD versionRequested = MAKEWORD (1, 1);
     18    WORD versionRequested = MAKEWORD (2, 2);
    1719    WSADATA wsaData;
    1820   
    1921    if (WSAStartup (versionRequested, & wsaData))
    2022       return -1;
    2123
    22     if (LOBYTE (wsaData.wVersion) != 1||
    23         HIBYTE (wsaData.wVersion) != 1)
     24    if (LOBYTE (wsaData.wVersion) != 2||
     25        HIBYTE (wsaData.wVersion) != 2)
    2426    {
    2527       WSACleanup ();
    2628       
    enet_time_set (enet_uint32 newTimeBase) 
    5254    timeBase = (enet_uint32) timeGetTime () - newTimeBase;
    5355}
    5456
    55 int
    56 enet_address_set_host (ENetAddress * address, const char * name)
     57static enet_uint16
     58enet_af (ENetAddressFamily family)
    5759{
    58     struct hostent * hostEntry;
     60    if (family == ENET_IPV4)
     61        return AF_INET;
     62    if (family == ENET_IPV6)
     63        return AF_INET6;
     64    return 0;
     65}
    5966
    60     hostEntry = gethostbyname (name);
    61     if (hostEntry == NULL ||
    62         hostEntry -> h_addrtype != AF_INET)
     67static socklen_t
     68enet_sa_size (ENetAddressFamily family)
     69{
     70    if (family == ENET_IPV4)
     71        return sizeof (struct sockaddr_in);
     72    if (family == ENET_IPV6)
     73        return sizeof (struct sockaddr_in6);
     74    return 0;
     75}
     76
     77static ENetAddressFamily
     78enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
     79{
     80    memset (address, 0, sizeof (ENetAddress));
     81    if (sin -> sa_family == AF_INET)
    6382    {
    64         unsigned long host = inet_addr (name);
    65         if (host == INADDR_NONE)
    66             return -1;
    67         address -> host = host;
    68         return 0;
     83        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
     84        /* address -> scopeID = 0; */
     85        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
     86        return ENET_IPV4;
    6987    }
     88    if (sin -> sa_family == AF_INET6)
     89    {
     90        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
     91        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
     92        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
     93        return ENET_IPV6;
     94    }
     95    return ENET_NO_ADDRESS_FAMILY;
     96}
    7097
    71     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
    72 
    73     return 0;
     98static int
     99enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
     100{
     101    memset (sin, 0, enet_sa_size(family));
     102    if (family == ENET_IPV4 &&
     103      (enet_get_address_family (address) == ENET_IPV4 ||
     104      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
     105    {
     106        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
     107        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
     108        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
     109        return 0;
     110    }
     111    else if (family == ENET_IPV6)
     112    {
     113        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
     114        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
     115        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
     116        ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     117        return 0;
     118    }
     119    return -1;
    74120}
    75121
    76122int
    77 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
     123enet_address_set_host (ENetAddress * address, const char * name)
    78124{
    79     char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
    80     if (addr == NULL)
     125    enet_uint16 port = address -> port;
     126    struct addrinfo hints;
     127    struct addrinfo * result;
     128    struct addrinfo * res;
     129
     130    memset(& hints, 0, sizeof (hints));
     131    hints.ai_flags = AI_ADDRCONFIG;
     132    hints.ai_family = AF_UNSPEC;
     133
     134    if ( getaddrinfo(name, NULL, &hints, &result) )
    81135        return -1;
    82     strncpy (name, addr, nameLength);
     136
     137    for (res = result; res != NULL; res = res -> ai_next)
     138    {
     139        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
     140            break;
     141    }
     142
     143    address -> port = port;
     144    freeaddrinfo(result);
     145    if (res == NULL) return -1;
     146
    83147    return 0;
    84148}
    85149
    86 int
    87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     150static int
     151enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    88152{
    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);
     153    struct sockaddr_storage sin;
     154    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
    97155
    98     strncpy (name, hostEntry -> h_name, nameLength);
     156    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
     157        return -1;
    99158
    100159    return 0;
    101160}
    102161
    103162int
    104 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     163enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    105164{
    106     struct sockaddr_in sin;
     165    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     166}
    107167
    108     memset (& sin, 0, sizeof (struct sockaddr_in));
     168int
     169enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     170{
     171    return enet_address_get_host_x(address, name, nameLength, 0);
     172}
    109173
    110     sin.sin_family = AF_INET;
     174int
     175enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
     176{
     177    struct sockaddr_storage sin;
    111178
    112179    if (address != NULL)
    113180    {
    114        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    115        sin.sin_addr.s_addr = address -> host;
     181        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    116182    }
    117183    else
    118184    {
    119        sin.sin_port = 0;
    120        sin.sin_addr.s_addr = INADDR_ANY;
     185        ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 };
     186        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
    121187    }
    122188
    123     return bind (socket,
    124                  (struct sockaddr *) & sin,
    125                  sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
     189    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0;
    126190}
    127191
    128192int
    enet_socket_listen (ENetSocket socket, int backlog) 
    132196}
    133197
    134198ENetSocket
    135 enet_socket_create (ENetSocketType type)
     199enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    136200{
    137     return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     201    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     202    return sock;
    138203}
    139204
    140205int
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    173238}
    174239
    175240int
    176 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     241enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    177242{
    178     struct sockaddr_in sin;
    179 
    180     memset (& sin, 0, sizeof (struct sockaddr_in));
     243    struct sockaddr_storage sin;
     244    enet_address_set_sin((struct sockaddr *) & sin, address, family);
    181245
    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;
     246    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0;
    187247}
    188248
    189249ENetSocket
    190 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     250enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    191251{
    192252    SOCKET result;
    193     struct sockaddr_in sin;
    194     int sinLength = sizeof (struct sockaddr_in);
     253    struct sockaddr_storage sin;
     254    socklen_t sinLength = enet_sa_size (family);
    195255
    196256    result = accept (socket,
    197                      address != NULL ? (struct sockaddr *) & sin : NULL, 
     257                     address != NULL ? (struct sockaddr *) & sin : NULL,
    198258                     address != NULL ? & sinLength : NULL);
    199259
    200260    if (result == INVALID_SOCKET)
    enet_socket_accept (ENetSocket socket, ENetAddress * address) 
    202262
    203263    if (address != NULL)
    204264    {
    205         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    206         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     265        enet_address_set_address(address, (struct sockaddr *) & sin);
    207266    }
    208267
    209268    return result;
    int 
    219278enet_socket_send (ENetSocket socket,
    220279                  const ENetAddress * address,
    221280                  const ENetBuffer * buffers,
    222                   size_t bufferCount)
     281                  size_t bufferCount,
     282                  ENetAddressFamily family)
    223283{
    224     struct sockaddr_in sin;
     284    struct sockaddr_storage sin;
    225285    DWORD sentLength;
    226286
    227287    if (address != NULL)
    228288    {
    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;
     289        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    234290    }
    235291
    236292    if (WSASendTo (socket,
    enet_socket_send (ENetSocket socket, 
    239295                   & sentLength,
    240296                   0,
    241297                   address != NULL ? (struct sockaddr *) & sin : 0,
    242                    address != NULL ? sizeof (struct sockaddr_in) : 0,
     298                   address != NULL ? enet_sa_size (family) : 0,
    243299                   NULL,
    244300                   NULL) == SOCKET_ERROR)
    245301    {
    int 
    256312enet_socket_receive (ENetSocket socket,
    257313                     ENetAddress * address,
    258314                     ENetBuffer * buffers,
    259                      size_t bufferCount)
     315                     size_t bufferCount,
     316                     ENetAddressFamily family)
    260317{
    261     INT sinLength = sizeof (struct sockaddr_in);
     318    INT sinLength = enet_sa_size (family);
    262319    DWORD flags = 0,
    263320          recvLength;
    264     struct sockaddr_in sin;
     321    struct sockaddr_storage sin;
    265322
    266323    if (WSARecvFrom (socket,
    267324                     (LPWSABUF) buffers,
    enet_socket_receive (ENetSocket socket, 
    288345
    289346    if (address != NULL)
    290347    {
    291         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    292         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     348        enet_address_set_address(address, (struct sockaddr *) & sin);
    293349    }
    294350
    295351    return (int) recvLength;
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    307363}
    308364
    309365int
    310 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     366enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    311367{
    312368    fd_set readSet, writeSet;
    313369    struct timeval timeVal;
    314370    int selectCount;
    315    
     371    ENetSocket maxSocket;
     372
    316373    timeVal.tv_sec = timeout / 1000;
    317374    timeVal.tv_usec = (timeout % 1000) * 1000;
    318    
     375
    319376    FD_ZERO (& readSet);
    320377    FD_ZERO (& writeSet);
    321378
    322379    if (* condition & ENET_SOCKET_WAIT_SEND)
    323       FD_SET (socket, & writeSet);
     380    {
     381        if (socket4 != ENET_SOCKET_NULL)
     382            FD_SET (socket4, & writeSet);
     383        if (socket6 != ENET_SOCKET_NULL)
     384            FD_SET (socket6, & writeSet);
     385    }
    324386
    325387    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    326       FD_SET (socket, & readSet);
     388    {
     389        if (socket4 != ENET_SOCKET_NULL)
     390            FD_SET (socket4, & readSet);
     391        if (socket6 != ENET_SOCKET_NULL)
     392            FD_SET (socket6, & readSet);
     393    }
    327394
    328     selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
     395    maxSocket = 0;
     396    if (socket4 != ENET_SOCKET_NULL)
     397        maxSocket = socket4;
     398    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
     399        maxSocket = socket6;
     400
     401    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
    329402
    330403    if (selectCount < 0)
    331404      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    335408    if (selectCount == 0)
    336409      return 0;
    337410
    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;
     411    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
     412        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
     413        * condition |= ENET_SOCKET_WAIT_SEND;
     414
     415    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
     416        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
     417        * condition |= ENET_SOCKET_WAIT_RECEIVE;
    343418
    344419    return 0;
    345 } 
     420}
    346421
    347422#endif
    348423
Note: See TracBrowser for help on using the repository browser.