Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/external/enet/patches/0003-using-address-family-in-functions.patch @ 7382

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

split libenet patch into smaller ones

File size: 11.8 KB
  • host.c

    From 130babc6f69e07830f73f7757222597117eb1cba Mon Sep 17 00:00:00 2001
    From: Adrian Friedli <adi@koalatux.ch>
    Date: Mon, 6 Sep 2010 14:58:50 +0200
    Subject: [PATCH 3/4] using address family in functions
    
    ---
     host.c              |    4 +-
     include/enet/enet.h |   22 +++++++++---
     protocol.c          |    5 ++-
     unix.c              |   87 ++++++++++++++++++++++++++++++++------------------
     4 files changed, 77 insertions(+), 41 deletions(-)
    
    diff --git a/host.c b/host.c
    index 8bb2433..9ccf894 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);
    52     if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0))
     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))
    5353    {
    5454       if (host -> socket != ENET_SOCKET_NULL)
    5555         enet_socket_destroy (host -> socket);
  • include/enet/enet.h

    diff --git a/include/enet/enet.h b/include/enet/enet.h
    index d3ca971..97f0556 100644
    a b typedef struct _ENetAddress 
    8282} ENetAddress;
    8383
    8484/**
     85 * The address family type.
     86 */
     87typedef enum _ENetAddressFamily
     88{
     89    ENET_NO_ADDRESS_FAMILY = 0,
     90    ENET_IPV4 = 1,
     91    ENET_IPV6 = 2
     92} ENetAddressFamily;
     93
     94/**
    8595 * Packet flag bit constants.
    8696 *
    8797 * The host must be specified in network byte-order, and the port must be in
    ENET_API void enet_time_set (enet_uint32); 
    445455/** @defgroup socket ENet socket functions
    446456    @{
    447457*/
    448 ENET_API ENetSocket enet_socket_create (ENetSocketType);
    449 ENET_API int        enet_socket_bind (ENetSocket, const ENetAddress *);
     458ENET_API ENetSocket enet_socket_create (ENetSocketType, ENetAddressFamily);
     459ENET_API int        enet_socket_bind (ENetSocket, const ENetAddress *, ENetAddressFamily);
    450460ENET_API int        enet_socket_listen (ENetSocket, int);
    451 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
    452 ENET_API int        enet_socket_connect (ENetSocket, const ENetAddress *);
    453 ENET_API int        enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t);
    454 ENET_API int        enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t);
     461ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFamily);
     462ENET_API int        enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily);
     463ENET_API int        enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily);
     464ENET_API int        enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily);
    455465ENET_API int        enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
    456466ENET_API int        enet_socket_set_option (ENetSocket, ENetSocketOption, int);
    457467ENET_API void       enet_socket_destroy (ENetSocket);
  • protocol.c

    diff --git a/protocol.c b/protocol.c
    index 930835e..4c4850a 100644
    a b enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 
    10461046       receivedLength = enet_socket_receive (host -> socket,
    10471047                                             & host -> receivedAddress,
    10481048                                             & buffer,
    1049                                              1);
     1049                                             1,
     1050                                             ENET_IPV6);
    10501051
    10511052       if (receivedLength < 0)
    10521053         return -1;
    enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 
    15091510
    15101511        currentPeer -> lastSendTime = host -> serviceTime;
    15111512
    1512         sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount);
     1513        sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount, ENET_IPV6);
    15131514
    15141515        enet_protocol_remove_sent_unreliable_commands (currentPeer);
    15151516
  • unix.c

    diff --git a/unix.c b/unix.c
    index de032bb..13a24d8 100644
    a b enet_time_set (enet_uint32 newTimeBase) 
    7171    timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
    7272}
    7373
    74 static int
     74static enet_uint16
     75enet_af (ENetAddressFamily family)
     76{
     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}
     93
     94
     95static ENetAddressFamily
    7596enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
    7697{
    7798    memset (address, 0, sizeof (ENetAddress));
    enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 
    80101        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
    81102        //address -> scopeID = 0;
    82103        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
    83         return 0;
     104        return ENET_IPV4;
    84105    }
    85106    if (sin -> sa_family == AF_INET6)
    86107    {
    87108        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
    88109        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
    89110        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
    90         return 0;
     111        return ENET_IPV6;
    91112    }
    92     return -1;
     113    return ENET_NO_ADDRESS_FAMILY;
    93114}
    94115
    95116static int
    96 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, sa_family_t family)
     117enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
    97118{
    98     if (family == AF_INET)
     119    memset (sin, 0, enet_sa_size(family));
     120    if (family == ENET_IPV4)
    99121    {
    100         memset (sin, 0, sizeof (struct sockaddr_in));
    101122        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
    102123        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
    103124        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
    104125        return 0;
    105126    }
    106     else if (family == AF_INET6)
     127    else if (family == ENET_IPV6)
    107128    {
    108         memset (sin, 0, sizeof (struct sockaddr_in6));
    109129        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
    110130        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
    111131        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
    enet_address_set_host (ENetAddress * address, const char * name) 
    132152
    133153    for (res = result; res != NULL; res = res -> ai_next)
    134154    {
    135         if ( !enet_address_set_address(address, res -> ai_addr) )
     155        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
    136156            break;
    137157    }
    138158
    static int 
    147167enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    148168{
    149169    struct sockaddr_storage sin;
    150     enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     170    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
    151171
    152     if ( getnameinfo((struct sockaddr *) & sin, sizeof(struct sockaddr_in6), name, nameLength, NULL, 0, flags))
     172    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
    153173        return -1;
    154174
    155175    return 0;
    enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng 
    168188}
    169189
    170190int
    171 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     191enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    172192{
    173193    struct sockaddr_storage sin;
    174194
    175195    if (address != NULL)
    176196    {
    177         enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     197        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    178198    }
    179199    else
    180200    {
    181201        ENetAddress address_ = { ENET_HOST_ANY, 0, 0 };
    182         enet_address_set_sin((struct sockaddr *) & sin, & address_, AF_INET6);
     202        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
    183203    }
    184204
    185     return bind (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
     205    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family));
    186206}
    187207
    188208int
    enet_socket_listen (ENetSocket socket, int backlog) 
    192212}
    193213
    194214ENetSocket
    195 enet_socket_create (ENetSocketType type)
     215enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    196216{
    197     ENetSocket sock = socket (AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     217    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
    198218
    199219#ifdef IPV6_V6ONLY
    200     int value = 0;
    201     setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
     220    if (family == ENET_IPV6)
     221    {
     222        int value = 0;
     223        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
     224    }
    202225#endif // IPV6_V6ONLY
    203226
    204227    return sock;
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    241264}
    242265
    243266int
    244 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     267enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    245268{
    246269    struct sockaddr_storage sin;
    247     enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     270    enet_address_set_sin((struct sockaddr *) & sin, address, family);
    248271
    249     return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
     272    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family));
    250273}
    251274
    252275ENetSocket
    253 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     276enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    254277{
    255278    int result;
    256279    struct sockaddr_storage sin;
    257     socklen_t sinLength = sizeof (struct sockaddr_in6);
     280    socklen_t sinLength = enet_sa_size (family);
    258281
    259282    result = accept (socket,
    260283                     address != NULL ? (struct sockaddr *) & sin : NULL,
    261284                     address != NULL ? & sinLength : NULL);
    262    
     285
    263286    if (result == -1)
    264287      return ENET_SOCKET_NULL;
    265288
    int 
    281304enet_socket_send (ENetSocket socket,
    282305                  const ENetAddress * address,
    283306                  const ENetBuffer * buffers,
    284                   size_t bufferCount)
     307                  size_t bufferCount,
     308                  ENetAddressFamily family)
    285309{
    286310    struct msghdr msgHdr;
    287311    struct sockaddr_storage sin;
    enet_socket_send (ENetSocket socket, 
    291315
    292316    if (address != NULL)
    293317    {
    294         enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     318        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    295319        msgHdr.msg_name = & sin;
    296         msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     320        msgHdr.msg_namelen = enet_sa_size (family);
    297321    }
    298322
    299323    msgHdr.msg_iov = (struct iovec *) buffers;
    int 
    316340enet_socket_receive (ENetSocket socket,
    317341                     ENetAddress * address,
    318342                     ENetBuffer * buffers,
    319                      size_t bufferCount)
     343                     size_t bufferCount,
     344                     ENetAddressFamily family)
    320345{
    321346    struct msghdr msgHdr;
    322347    struct sockaddr_storage sin;
    enet_socket_receive (ENetSocket socket, 
    327352    if (address != NULL)
    328353    {
    329354        msgHdr.msg_name = & sin;
    330         msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     355        msgHdr.msg_namelen = enet_sa_size (family);
    331356    }
    332357
    333358    msgHdr.msg_iov = (struct iovec *) buffers;
Note: See TracBrowser for help on using the repository browser.