Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/external/enet/patches/0004-using-two-separate-sockets-for-IPv4-and-IPv6.patch @ 7438

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

set svn props and fix line endings in libenet files

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

    From 73fa28a84ce456561d3912578121a30bc8263c50 Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Wed, 8 Sep 2010 12:50:04 +0200
    Subject: [PATCH 4/5] using two separate sockets for IPv4 and IPv6
    
    ---
     host.c              |   53 +++++++++++++++++++++++++++---------
     include/enet/enet.h |   11 ++++++-
     protocol.c          |   47 ++++++++++++++++++++++++++++----
     unix.c              |   74 +++++++++++++++++++++++++++++++++++++-------------
     4 files changed, 144 insertions(+), 41 deletions(-)
    
    diff --git a/host.c b/host.c
    index 9ccf894..46e52c9 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    if (address != NULL && enet_socket_bind (socket, address, family) < 0)
     18    {
     19        enet_socket_destroy (socket);
     20        return ENET_SOCKET_NULL;
     21    }
     22
     23    enet_socket_set_option (socket, ENET_SOCKOPT_NONBLOCK, 1);
     24    enet_socket_set_option (socket, ENET_SOCKOPT_BROADCAST, 1);
     25    enet_socket_set_option (socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
     26    enet_socket_set_option (socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
     27
     28    return socket;
     29}
     30
    1031/** @defgroup host ENet host functions
    1132    @{
    1233*/
    enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 
    4869    }
    4970    memset (host -> peers, 0, peerCount * sizeof (ENetPeer));
    5071
    51     host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6);
    52     if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address, ENET_IPV6) < 0))
    53     {
    54        if (host -> socket != ENET_SOCKET_NULL)
    55          enet_socket_destroy (host -> socket);
     72    int family = (address == NULL || !memcmp (& address -> host, & ENET_HOST_ANY, sizeof (ENetHostAddress))) ?
     73        ENET_IPV4 | ENET_IPV6 :
     74        enet_get_address_family (address);
    5675
    57        enet_free (host -> peers);
    58        enet_free (host);
     76    host -> socket4 = (family & ENET_IPV4) ?
     77      enet_socket_create_bind (address, ENET_IPV4) :
     78      ENET_SOCKET_NULL;
     79    host -> socket6 = (family & ENET_IPV6) ?
     80      enet_socket_create_bind (address, ENET_IPV6) :
     81      ENET_SOCKET_NULL;
    5982
    60        return NULL;
     83    if (host -> socket4 == ENET_SOCKET_NULL && host -> socket6 == ENET_SOCKET_NULL)
     84    {
     85        enet_free (host -> peers);
     86        enet_free (host);
     87        return NULL;
    6188    }
    6289
    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 
    6890    if (address != NULL)
    6991      host -> address = * address;
    7092
    enet_host_destroy (ENetHost * host) 
    133155{
    134156    ENetPeer * currentPeer;
    135157
    136     enet_socket_destroy (host -> socket);
     158    if (host -> socket4 != ENET_SOCKET_NULL)
     159      enet_socket_destroy (host -> socket4);
     160    if (host -> socket6 != ENET_SOCKET_NULL)
     161      enet_socket_destroy (host -> socket6);
    137162
    138163    for (currentPeer = host -> peers;
    139164         currentPeer < & host -> peers [host -> peerCount];
  • include/enet/enet.h

    diff --git a/include/enet/enet.h b/include/enet/enet.h
    index 7f5876f..616fe7f 100644
    a b typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * b 
    335335  */
    336336typedef struct _ENetHost
    337337{
    338    ENetSocket           socket;
     338   ENetSocket           socket4;
     339   ENetSocket           socket6;
    339340   ENetAddress          address;                     /**< Internet address of the host */
    340341   enet_uint32          incomingBandwidth;           /**< downstream bandwidth of the host */
    341342   enet_uint32          outgoingBandwidth;           /**< upstream bandwidth of the host */
    ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFa 
    462463ENET_API int        enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily);
    463464ENET_API int        enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily);
    464465ENET_API int        enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily);
    465 ENET_API int        enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
     466ENET_API int        enet_socket_wait (ENetSocket, ENetSocket, enet_uint32 *, enet_uint32);
    466467ENET_API int        enet_socket_set_option (ENetSocket, ENetSocketOption, int);
    467468ENET_API void       enet_socket_destroy (ENetSocket);
    468469ENET_API int        enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32);
    ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName 
    508509*/
    509510ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address);
    510511
     512/** Returns the Address family of an (IPv4-mapped) IPv6 address.
     513    @param address IPv6 address
     514    @returns address family
     515*/
     516ENET_API ENetAddressFamily enet_get_address_family (const ENetAddress * address);
     517
    511518/** @} */
    512519
    513520ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32);
  • protocol.c

    diff --git a/protocol.c b/protocol.c
    index 4c4850a..505c684 100644
    a b enet_address_map4 (enet_uint32 address) 
    3737    return addr;
    3838}
    3939
     40ENetAddressFamily
     41enet_get_address_family (const ENetAddress * address)
     42{
     43    if (!memcmp(& address->host, & ENET_IPV4MAPPED_PREFIX, ENET_IPV4MAPPED_PREFIX_LEN))
     44        return ENET_IPV4;
     45    return ENET_IPV6;
     46}
     47
    4048size_t
    4149enet_protocol_command_size (enet_uint8 commandNumber)
    4250{
    commandError: 
    10331041}
    10341042 
    10351043static int
    1036 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
     1044enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family)
    10371045{
    10381046    for (;;)
    10391047    {
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10431051       buffer.data = host -> packetData [0];
    10441052       buffer.dataLength = sizeof (host -> packetData [0]);
    10451053
    1046        receivedLength = enet_socket_receive (host -> socket,
     1054       receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6,
    10471055                                             & host -> receivedAddress,
    10481056                                             & buffer,
    10491057                                             1,
    1050                                              ENET_IPV6);
     1058                                             family);
    10511059
    10521060       if (receivedLength < 0)
    10531061         return -1;
    enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10551063       if (receivedLength == 0)
    10561064         return 0;
    10571065
     1066       if (enet_get_address_family (& host -> receivedAddress) != family)
     1067         return -1;
     1068
    10581069       host -> receivedData = host -> packetData [0];
    10591070       host -> receivedDataLength = receivedLength;
    10601071     
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    15101521
    15111522        currentPeer -> lastSendTime = host -> serviceTime;
    15121523
    1513         sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount, ENET_IPV6);
     1524        ENetAddressFamily family = enet_get_address_family (& currentPeer -> address);
     1525        ENetSocket socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6;
     1526        if (socket == ENET_SOCKET_NULL)
     1527          return -1;
     1528        sentLength = enet_socket_send (socket,
     1529                                           & currentPeer -> address,
     1530                                           host -> buffers,
     1531                                           host -> bufferCount,
     1532                                           family);
    15141533
    15151534        enet_protocol_remove_sent_unreliable_commands (currentPeer);
    15161535
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16211640          break;
    16221641       }
    16231642
    1624        switch (enet_protocol_receive_incoming_commands (host, event))
     1643       if (host -> socket4 != ENET_SOCKET_NULL)
     1644         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4))
     1645       {
     1646       case 1:
     1647          return 1;
     1648
     1649       case -1:
     1650          perror ("Error receiving incoming packets");
     1651
     1652          return -1;
     1653
     1654       default:
     1655          break;
     1656       }
     1657
     1658       if (host -> socket6 != ENET_SOCKET_NULL)
     1659         switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6))
    16251660       {
    16261661       case 1:
    16271662          return 1;
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16731708
    16741709       waitCondition = ENET_SOCKET_WAIT_RECEIVE;
    16751710
    1676        if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
     1711       if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
    16771712         return -1;
    16781713       
    16791714       host -> serviceTime = enet_time_get ();
  • unix.c

    diff --git a/unix.c b/unix.c
    index 475c6e3..751cb6f 100644
    a b static int 
    116116enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
    117117{
    118118    memset (sin, 0, enet_sa_size(family));
    119     if (family == ENET_IPV4)
     119    if (family == ENET_IPV4 &&
     120      (enet_get_address_family (address) == ENET_IPV4 ||
     121      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
    120122    {
    121123        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
    122124        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
    enet_socket_create (ENetSocketType type, ENetAddressFamily family) 
    218220#ifdef IPV6_V6ONLY
    219221    if (family == ENET_IPV6)
    220222    {
    221         int value = 0;
     223        int value = 1;
    222224        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
    223225    }
    224226#endif // IPV6_V6ONLY
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    392394}
    393395
    394396int
    395 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     397enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    396398{
    397399#ifdef HAS_POLL
    398     struct pollfd pollSocket;
     400    struct pollfd pollSocket[2];
    399401    int pollCount;
    400    
    401     pollSocket.fd = socket;
    402     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    }
    403415
    404416    if (* condition & ENET_SOCKET_WAIT_SEND)
    405       pollSocket.events |= POLLOUT;
     417    {
     418        pollSocket[0].events |= POLLOUT;
     419        pollSocket[1].events |= POLLOUT;
     420    }
    406421
    407422    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    408       pollSocket.events |= POLLIN;
     423    {
     424        pollSocket[0].events |= POLLIN;
     425        pollSocket[1].events |= POLLIN;
     426    }
    409427
    410     pollCount = poll (& pollSocket, 1, timeout);
     428    pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout);
    411429
    412430    if (pollCount < 0)
    413431      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    417435    if (pollCount == 0)
    418436      return 0;
    419437
    420     if (pollSocket.revents & POLLOUT)
     438    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT)
    421439      * condition |= ENET_SOCKET_WAIT_SEND;
    422440   
    423     if (pollSocket.revents & POLLIN)
     441    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN)
    424442      * condition |= ENET_SOCKET_WAIT_RECEIVE;
    425443
    426444    return 0;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    436454    FD_ZERO (& writeSet);
    437455
    438456    if (* condition & ENET_SOCKET_WAIT_SEND)
    439       FD_SET (socket, & writeSet);
     457    {
     458        if (socket4 != ENET_SOCKET_NULL)
     459            FD_SET (socket4, & writeSet);
     460        if (socket6 != ENET_SOCKET_NULL)
     461            FD_SET (socket6, & writeSet);
     462    }
    440463
    441464    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    442       FD_SET (socket, & readSet);
     465    {
     466        if (socket4 != ENET_SOCKET_NULL)
     467            FD_SET (socket4, & readSet);
     468        if (socket6 != ENET_SOCKET_NULL)
     469            FD_SET (socket6, & readSet);
     470    }
     471
     472    ENetSocket maxSocket = 0;
     473    if (socket4 != ENET_SOCKET_NULL)
     474        maxSocket = socket4;
     475    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
     476        maxSocket = socket6;
    443477
    444     selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
     478    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
    445479
    446480    if (selectCount < 0)
    447481      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    451485    if (selectCount == 0)
    452486      return 0;
    453487
    454     if (FD_ISSET (socket, & writeSet))
    455       * condition |= ENET_SOCKET_WAIT_SEND;
     488    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
     489        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
     490        * condition |= ENET_SOCKET_WAIT_SEND;
    456491
    457     if (FD_ISSET (socket, & readSet))
    458       * condition |= ENET_SOCKET_WAIT_RECEIVE;
     492    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
     493        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
     494        * condition |= ENET_SOCKET_WAIT_RECEIVE;
    459495
    460496    return 0;
    461497#endif
Note: See TracBrowser for help on using the repository browser.