Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7389 was 7389, checked in by adrfried, 15 years ago

fix some stuff

File size: 12.2 KB
  • host.c

    From 9801a6bcd072870248a6b07245fc09a9492f8f51 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              |   53 +++++++++++++++++++++++++++++++++++++-------------
     include/enet/enet.h |   11 ++++++++-
     protocol.c          |   47 +++++++++++++++++++++++++++++++++++++++-----
     unix.c              |   47 ++++++++++++++++++++++++++++++++------------
     4 files changed, 123 insertions(+), 35 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 13a24d8..22ffb76 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 -> host, & 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//#ifdef HAS_POLL
     401    struct pollfd pollSocket[2];
    400402    int pollCount;
    401    
    402     pollSocket.fd = socket;
    403     pollSocket.events = 0;
     403
     404    pollSocket[0].fd = socket4;
     405    pollSocket[1].fd = socket6;
     406    pollSocket[0].events = 0;
     407    pollSocket[1].events = 0;
     408    //pollSocket[0].revents = 0;
     409    pollSocket[1].revents = 0;
     410
     411    if (pollSocket[0].fd == ENET_SOCKET_NULL)
     412    {
     413        pollSocket[0].fd = pollSocket[1].fd;
     414        pollSocket[1].fd = ENET_SOCKET_NULL;
     415    }
    404416
    405417    if (* condition & ENET_SOCKET_WAIT_SEND)
    406       pollSocket.events |= POLLOUT;
     418    {
     419        pollSocket[0].events |= POLLOUT;
     420        pollSocket[1].events |= POLLOUT;
     421    }
    407422
    408423    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
    409       pollSocket.events |= POLLIN;
     424    {
     425        pollSocket[0].events |= POLLIN;
     426        pollSocket[1].events |= POLLIN;
     427    }
    410428
    411     pollCount = poll (& pollSocket, 1, timeout);
     429    pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout);
    412430
    413431    if (pollCount < 0)
    414432      return -1;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    418436    if (pollCount == 0)
    419437      return 0;
    420438
    421     if (pollSocket.revents & POLLOUT)
     439    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT)
    422440      * condition |= ENET_SOCKET_WAIT_SEND;
    423441   
    424     if (pollSocket.revents & POLLIN)
     442    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN)
    425443      * condition |= ENET_SOCKET_WAIT_RECEIVE;
    426444
    427445    return 0;
     446/*
     447FIXME: implement or remove this
    428448#else
    429449    fd_set readSet, writeSet;
    430450    struct timeval timeVal;
    enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 
    460480
    461481    return 0;
    462482#endif
     483*/
    463484}
    464485
    465486#endif
Note: See TracBrowser for help on using the repository browser.