Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

IPv6 for Windows

I have not tested this, please test this and report issues to me.

File size: 11.8 KB
  • host.c

    From b26e4ef3fa9c0139fc2ec086d2243b809365d683 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/5] using address family in functions
    
    ---
     host.c              |    4 +-
     include/enet/enet.h |   22 +++++++++---
     protocol.c          |    5 ++-
     unix.c              |   86 ++++++++++++++++++++++++++++++++------------------
     4 files changed, 76 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..7f5876f 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 << 0),
     91    ENET_IPV6 = (1 << 1)
     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..475c6e3 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
     94static ENetAddressFamily
    7595enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
    7696{
    7797    memset (address, 0, sizeof (ENetAddress));
    enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 
    80100        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
    81101        //address -> scopeID = 0;
    82102        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
    83         return 0;
     103        return ENET_IPV4;
    84104    }
    85105    if (sin -> sa_family == AF_INET6)
    86106    {
    87107        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
    88108        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
    89109        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
    90         return 0;
     110        return ENET_IPV6;
    91111    }
    92     return -1;
     112    return ENET_NO_ADDRESS_FAMILY;
    93113}
    94114
    95115static int
    96 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, sa_family_t family)
     116enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
    97117{
    98     if (family == AF_INET)
     118    memset (sin, 0, enet_sa_size(family));
     119    if (family == ENET_IPV4)
    99120    {
    100         memset (sin, 0, sizeof (struct sockaddr_in));
    101121        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
    102122        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
    103123        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
    104124        return 0;
    105125    }
    106     else if (family == AF_INET6)
     126    else if (family == ENET_IPV6)
    107127    {
    108         memset (sin, 0, sizeof (struct sockaddr_in6));
    109128        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
    110129        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
    111130        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
    enet_address_set_host (ENetAddress * address, const char * name) 
    132151
    133152    for (res = result; res != NULL; res = res -> ai_next)
    134153    {
    135         if ( !enet_address_set_address(address, res -> ai_addr) )
     154        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
    136155            break;
    137156    }
    138157
    static int 
    147166enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
    148167{
    149168    struct sockaddr_storage sin;
    150     enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     169    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
    151170
    152     if ( getnameinfo((struct sockaddr *) & sin, sizeof(struct sockaddr_in6), name, nameLength, NULL, 0, flags))
     171    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
    153172        return -1;
    154173
    155174    return 0;
    enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng 
    168187}
    169188
    170189int
    171 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     190enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    172191{
    173192    struct sockaddr_storage sin;
    174193
    175194    if (address != NULL)
    176195    {
    177         enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     196        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    178197    }
    179198    else
    180199    {
    181200        ENetAddress address_ = { ENET_HOST_ANY, 0, 0 };
    182         enet_address_set_sin((struct sockaddr *) & sin, & address_, AF_INET6);
     201        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
    183202    }
    184203
    185     return bind (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
     204    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family));
    186205}
    187206
    188207int
    enet_socket_listen (ENetSocket socket, int backlog) 
    192211}
    193212
    194213ENetSocket
    195 enet_socket_create (ENetSocketType type)
     214enet_socket_create (ENetSocketType type, ENetAddressFamily family)
    196215{
    197     ENetSocket sock = socket (AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     216    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
    198217
    199218#ifdef IPV6_V6ONLY
    200     int value = 0;
    201     setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
     219    if (family == ENET_IPV6)
     220    {
     221        int value = 0;
     222        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
     223    }
    202224#endif // IPV6_V6ONLY
    203225
    204226    return sock;
    enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 
    241263}
    242264
    243265int
    244 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
     266enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
    245267{
    246268    struct sockaddr_storage sin;
    247     enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     269    enet_address_set_sin((struct sockaddr *) & sin, address, family);
    248270
    249     return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
     271    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family));
    250272}
    251273
    252274ENetSocket
    253 enet_socket_accept (ENetSocket socket, ENetAddress * address)
     275enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
    254276{
    255277    int result;
    256278    struct sockaddr_storage sin;
    257     socklen_t sinLength = sizeof (struct sockaddr_in6);
     279    socklen_t sinLength = enet_sa_size (family);
    258280
    259281    result = accept (socket,
    260282                     address != NULL ? (struct sockaddr *) & sin : NULL,
    261283                     address != NULL ? & sinLength : NULL);
    262    
     284
    263285    if (result == -1)
    264286      return ENET_SOCKET_NULL;
    265287
    int 
    281303enet_socket_send (ENetSocket socket,
    282304                  const ENetAddress * address,
    283305                  const ENetBuffer * buffers,
    284                   size_t bufferCount)
     306                  size_t bufferCount,
     307                  ENetAddressFamily family)
    285308{
    286309    struct msghdr msgHdr;
    287310    struct sockaddr_storage sin;
    enet_socket_send (ENetSocket socket, 
    291314
    292315    if (address != NULL)
    293316    {
    294         enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);
     317        enet_address_set_sin((struct sockaddr *) & sin, address, family);
    295318        msgHdr.msg_name = & sin;
    296         msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     319        msgHdr.msg_namelen = enet_sa_size (family);
    297320    }
    298321
    299322    msgHdr.msg_iov = (struct iovec *) buffers;
    int 
    316339enet_socket_receive (ENetSocket socket,
    317340                     ENetAddress * address,
    318341                     ENetBuffer * buffers,
    319                      size_t bufferCount)
     342                     size_t bufferCount,
     343                     ENetAddressFamily family)
    320344{
    321345    struct msghdr msgHdr;
    322346    struct sockaddr_storage sin;
    enet_socket_receive (ENetSocket socket, 
    327351    if (address != NULL)
    328352    {
    329353        msgHdr.msg_name = & sin;
    330         msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
     354        msgHdr.msg_namelen = enet_sa_size (family);
    331355    }
    332356
    333357    msgHdr.msg_iov = (struct iovec *) buffers;
Note: See TracBrowser for help on using the repository browser.