Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

split libenet patch into smaller ones

File size: 11.9 KB
  • host.c

    From d61448922c08e6801f07c38077623d3bcc513ad4 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/4] using two separate sockets for IPv4 and IPv6
    
    ---
     host.c              |   45 ++++++++++++++++++++++++++++++++++++---------
     include/enet/enet.h |   11 +++++++++--
     protocol.c          |   42 ++++++++++++++++++++++++++++++++++++------
     unix.c              |   38 ++++++++++++++++++++++++++------------
     4 files changed, 107 insertions(+), 29 deletions(-)
    
    diff --git a/host.c b/host.c
    index 9ccf894..85dfa3c 100644
    a b enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 
    4848    }
    4949    memset (host -> peers, 0, peerCount * sizeof (ENetPeer));
    5050
    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))
     51
     52    // FIXME: check address for ANY_ADRESS if not only bind to specific protocol
     53    // FIXME: allow to fail one of the two protocols
     54    /* IPv4 */
     55    host -> socket4 = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV4);
     56    if (host -> socket4 == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket4, address, ENET_IPV4) < 0))
    5357    {
    54        if (host -> socket != ENET_SOCKET_NULL)
    55          enet_socket_destroy (host -> socket);
     58       if (host -> socket4 != ENET_SOCKET_NULL)
     59         enet_socket_destroy (host -> socket4);
    5660
    5761       enet_free (host -> peers);
    5862       enet_free (host);
    enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 
    6064       return NULL;
    6165    }
    6266
    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    enet_socket_set_option (host -> socket4, ENET_SOCKOPT_NONBLOCK, 1);
     68    enet_socket_set_option (host -> socket4, ENET_SOCKOPT_BROADCAST, 1);
     69    enet_socket_set_option (host -> socket4, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
     70    enet_socket_set_option (host -> socket4, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
     71
     72    /* IPv6 */
     73    host -> socket6 = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6);
     74    if (host -> socket6 == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket6, address, ENET_IPV6) < 0))
     75    {
     76       if (host -> socket6 != ENET_SOCKET_NULL)
     77       {
     78           enet_socket_destroy (host -> socket4);
     79           enet_socket_destroy (host -> socket6);
     80       }
     81
     82       enet_free (host -> peers);
     83       enet_free (host);
     84
     85       return NULL;
     86    }
     87
     88    enet_socket_set_option (host -> socket6, ENET_SOCKOPT_NONBLOCK, 1);
     89    enet_socket_set_option (host -> socket6, ENET_SOCKOPT_BROADCAST, 1);
     90    enet_socket_set_option (host -> socket6, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);
     91    enet_socket_set_option (host -> socket6, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);
     92
    6793
    6894    if (address != NULL)
    6995      host -> address = * address;
    enet_host_destroy (ENetHost * host) 
    133159{
    134160    ENetPeer * currentPeer;
    135161
    136     enet_socket_destroy (host -> socket);
     162    enet_socket_destroy (host -> socket4);
     163    enet_socket_destroy (host -> socket6);
    137164
    138165    for (currentPeer = host -> peers;
    139166         currentPeer < & host -> peers [host -> peerCount];
  • include/enet/enet.h

    diff --git a/include/enet/enet.h b/include/enet/enet.h
    index 97f0556..39cf93e 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..37f6387 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        sentLength = enet_socket_send (family == ENET_IPV4 ? host -> socket4 : host -> socket6,
     1526                                           & currentPeer -> address,
     1527                                           host -> buffers,
     1528                                           host -> bufferCount,
     1529                                           family);
    15141530
    15151531        enet_protocol_remove_sent_unreliable_commands (currentPeer);
    15161532
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16211637          break;
    16221638       }
    16231639
    1624        switch (enet_protocol_receive_incoming_commands (host, event))
     1640       switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4))
     1641       {
     1642       case 1:
     1643          return 1;
     1644
     1645       case -1:
     1646          perror ("Error receiving incoming packets");
     1647
     1648          return -1;
     1649
     1650       default:
     1651          break;
     1652       }
     1653
     1654       switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6))
    16251655       {
    16261656       case 1:
    16271657          return 1;
    enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 
    16731703
    16741704       waitCondition = ENET_SOCKET_WAIT_RECEIVE;
    16751705
    1676        if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
     1706       if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)
    16771707         return -1;
    16781708       
    16791709       host -> serviceTime = enet_time_get ();
  • unix.c

    diff --git a/unix.c b/unix.c
    index 13a24d8..96d17f8 100644
    a b static int 
    117117enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
    118118{
    119119    memset (sin, 0, enet_sa_size(family));
    120     if (family == ENET_IPV4)
     120    if (family == ENET_IPV4 &&
     121      (enet_get_address_family (address) == ENET_IPV4 ||
     122      !memcmp (address, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
    121123    {
    122124        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
    123125        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
    enet_socket_create (ENetSocketType type, ENetAddressFamily family) 
    219221#ifdef IPV6_V6ONLY
    220222    if (family == ENET_IPV6)
    221223    {
    222         int value = 0;
     224        int value = 1;
    223225        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
    224226    }
    225227#endif // IPV6_V6ONLY
    enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 
    393395}
    394396
    395397int
    396 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
     398enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
    397399{
    398 #ifdef HAS_POLL
    399     struct pollfd pollSocket;
     400    //FIXME allow only one of the sockets being available
     401//#ifdef HAS_POLL
     402    struct pollfd pollSocket[2];
    400403    int pollCount;
    401404   
    402     pollSocket.fd = socket;
    403     pollSocket.events = 0;
     405    pollSocket[0].fd = socket4;
     406    pollSocket[1].fd = socket6;
     407    pollSocket[0].events = 0;
     408    pollSocket[1].events = 0;
    404409
    405410    if (* condition & ENET_SOCKET_WAIT_SEND)
    406       pollSocket.events |= POLLOUT;
     411    {
     412        pollSocket[0].events |= POLLOUT;
     413        pollSocket[1].events |= POLLOUT;
     414    }
    407415
    408416    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    409       pollSocket.events |= POLLIN;
     417    {
     418        pollSocket[0].events |= POLLIN;
     419        pollSocket[1].events |= POLLIN;
     420    }
    410421
    411     pollCount = poll (& pollSocket, 1, timeout);
     422    pollCount = poll (pollSocket, 2, timeout);
    412423
    413424    if (pollCount < 0)
    414425      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    418429    if (pollCount == 0)
    419430      return 0;
    420431
    421     if (pollSocket.revents & POLLOUT)
     432    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT)
    422433      * condition |= ENET_SOCKET_WAIT_SEND;
    423434   
    424     if (pollSocket.revents & POLLIN)
     435    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN)
    425436      * condition |= ENET_SOCKET_WAIT_RECEIVE;
    426437
    427438    return 0;
     439/*
     440FIXME: implement this
    428441#else
    429442    fd_set readSet, writeSet;
    430443    struct timeval timeVal;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    460473
    461474    return 0;
    462475#endif
     476*/
    463477}
    464478
    465479#endif
Note: See TracBrowser for help on using the repository browser.