Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 3, 2010, 12:28:21 AM (14 years ago)
Author:
adrfried
Message:

patch libenet to support ipv6

This is only done for Linux so far.

Location:
code/branches/ipv6/src/external/enet
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ipv6/src/external/enet/host.c

    r7328 r7330  
    5050
    5151    host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM);
     52    enet_socket_set_option (host -> socket, ENET_SOCKOPT_V6ONLY, 0); // Needs to be set before bind.
     53
    5254    if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0))
    5355    {
  • code/branches/ipv6/src/external/enet/include/enet/enet.h

    r7328 r7330  
    5151   ENET_SOCKOPT_RCVBUF    = 3,
    5252   ENET_SOCKOPT_SNDBUF    = 4,
    53    ENET_SOCKOPT_REUSEADDR = 5
     53   ENET_SOCKOPT_REUSEADDR = 5,
     54   ENET_SOCKOPT_V6ONLY = 6
    5455} ENetSocketOption;
    5556
    56 enum
    57 {
    58    ENET_HOST_ANY       = 0,            /**< specifies the default server host */
    59    ENET_HOST_BROADCAST = 0xFFFFFFFF,   /**< specifies a subnet-wide broadcast */
    60 
    61    ENET_PORT_ANY       = 0             /**< specifies that a port should be automatically chosen */
    62 };
     57typedef struct _ENetHostAddress
     58{
     59   enet_uint8 addr[16];
     60} ENetHostAddress;
     61
     62extern const ENetHostAddress ENET_HOST_ANY;       /**< specifies the default server host */
     63extern const ENetHostAddress ENET_HOST_BROADCAST; /**< specifies a IPv4 subnet-wide broadcast */
     64#define ENET_PORT_ANY 0                           /**< specifies that a port should be automatically chosen */
    6365
    6466/**
     
    7476typedef struct _ENetAddress
    7577{
    76    enet_uint32 host;
     78   ENetHostAddress host;
     79   enet_uint32 scopeID; //FIXME: this is of different size on Windows
    7780   enet_uint16 port;
    7881} ENetAddress;
     
    489492ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength);
    490493
     494/** Maps an IPv4 Address to an IPv6 address.
     495    @param address IPv4 address in network byte order
     496    @returns the IPv4-mapped IPv6 address in network byte order
     497*/
     498static inline ENetHostAddress enet_address_map4 (enet_uint32 address)
     499{
     500   ENetHostAddress addr = ENET_HOST_ANY;
     501   ((enet_uint16 *)addr.addr)[5] = 0xffff;
     502   ((enet_uint32 *)addr.addr)[3] = address;
     503   return addr;
     504}
     505
    491506/** @} */
    492507
  • code/branches/ipv6/src/external/enet/protocol.c

    r7328 r7330  
    99#include "enet/time.h"
    1010#include "enet/enet.h"
     11
     12const ENetHostAddress ENET_HOST_ANY = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } };
     13const ENetHostAddress ENET_HOST_BROADCAST = { { 0,0,0,0,0,0,0,0,0,0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
    1114
    1215static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] =
     
    263266    {
    264267        if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED &&
    265             currentPeer -> address.host == host -> receivedAddress.host &&
    266268            currentPeer -> address.port == host -> receivedAddress.port &&
    267             currentPeer -> connectID == command -> connect.connectID)
     269            currentPeer -> connectID == command -> connect.connectID &&
     270            !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)))
    268271          return NULL;
    269272    }
     
    849852       if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
    850853           peer -> state == ENET_PEER_STATE_ZOMBIE ||
    851            (host -> receivedAddress.host != peer -> address.host &&
    852              peer -> address.host != ENET_HOST_BROADCAST) ||
    853854           (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
    854             sessionID != peer -> incomingSessionID))
     855            sessionID != peer -> incomingSessionID) ||
     856           ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) &&
     857             memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) &&
     858             peer -> address.host.addr[0] != 0xff ) )
    855859         return 0;
    856860    }
     
    892896    if (peer != NULL)
    893897    {
    894        peer -> address.host = host -> receivedAddress.host;
    895        peer -> address.port = host -> receivedAddress.port;
     898       peer -> address = host -> receivedAddress;
    896899       peer -> incomingDataTotal += host -> receivedDataLength;
    897900    }
  • code/branches/ipv6/src/external/enet/unix.c

    r7328 r7330  
    7575enet_address_set_host (ENetAddress * address, const char * name)
    7676{
    77     struct hostent * hostEntry = NULL;
    78 #ifdef HAS_GETHOSTBYNAME_R
    79     struct hostent hostData;
    80     char buffer [2048];
    81     int errnum;
    82 
    83 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
    84     gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
    85 #else
    86     hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
    87 #endif
    88 #else
    89     hostEntry = gethostbyname (name);
    90 #endif
    91 
    92     if (hostEntry == NULL ||
    93         hostEntry -> h_addrtype != AF_INET)
    94     {
    95 #ifdef HAS_INET_PTON
    96         if (! inet_pton (AF_INET, name, & address -> host))
    97 #else
    98         if (! inet_aton (name, (struct in_addr *) & address -> host))
    99 #endif
    100             return -1;
    101         return 0;
    102     }
    103 
    104     address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
     77    struct addrinfo hints;
     78    struct addrinfo * result;
     79    struct addrinfo * res;
     80
     81    memset(& hints, 0, sizeof (hints));
     82    hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;
     83    hints.ai_family = AF_UNSPEC;
     84
     85    if ( getaddrinfo(name, NULL, &hints, &result) )
     86    {
     87        return -1;
     88    }
     89
     90    for (res = result; res != NULL; res = res -> ai_next)
     91    {
     92        if (res -> ai_family == AF_INET)
     93        {
     94            address -> host = enet_address_map4( ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr );
     95            address -> scopeID = 0;
     96            break;
     97        }
     98        if (res -> ai_family == AF_INET6)
     99        {
     100            address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) res -> ai_addr ) -> sin6_addr;
     101            address -> scopeID = ((struct sockaddr_in6 *) res -> ai_addr ) -> sin6_scope_id;
     102            break;
     103        }
     104    }
     105    freeaddrinfo(result);
     106    if (res == NULL) return -1;
    105107
    106108    return 0;
    107109}
    108110
     111static int
     112enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
     113{
     114    struct sockaddr_in6 sin;
     115
     116    memset (& sin, 0, sizeof (struct sockaddr_in6));
     117
     118    sin.sin6_family = AF_INET6;
     119    sin.sin6_addr = * (struct in6_addr *) & address -> host;
     120    sin.sin6_scope_id = address -> scopeID;
     121
     122    if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags))
     123    {
     124        return -1;
     125    }
     126
     127    return 0;
     128}
     129
    109130int
    110131enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
    111132{
    112 #ifdef HAS_INET_NTOP
    113     if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
    114 #else
    115     char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
    116     if (addr != NULL)
    117         strncpy (name, addr, nameLength);
     133    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
     134}
     135
     136int
     137enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
     138{
     139    return enet_address_get_host_x(address, name, nameLength, 0);
     140}
     141
     142int
     143enet_socket_bind (ENetSocket socket, const ENetAddress * address)
     144{
     145    struct sockaddr_in6 sin;
     146
     147    memset (& sin, 0, sizeof (struct sockaddr_in6));
     148
     149    sin.sin6_family = AF_INET6;
     150
     151    if (address != NULL)
     152    {
     153       sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     154       sin.sin6_addr = * (struct in6_addr *) & address -> host;
     155       sin.sin6_scope_id = address -> scopeID;
     156    }
    118157    else
    119 #endif
    120         return -1;
    121     return 0;
    122 }
    123 
    124 int
    125 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
    126 {
    127     struct in_addr in;
    128     struct hostent * hostEntry = NULL;
    129 #ifdef HAS_GETHOSTBYADDR_R
    130     struct hostent hostData;
    131     char buffer [2048];
    132     int errnum;
    133 
    134     in.s_addr = address -> host;
    135 
    136 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
    137     gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
    138 #else
    139     hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
    140 #endif
    141 #else
    142     in.s_addr = address -> host;
    143 
    144     hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
    145 #endif
    146 
    147     if (hostEntry == NULL)
    148       return enet_address_get_host_ip (address, name, nameLength);
    149 
    150     strncpy (name, hostEntry -> h_name, nameLength);
    151 
    152     return 0;
    153 }
    154 
    155 int
    156 enet_socket_bind (ENetSocket socket, const ENetAddress * address)
    157 {
    158     struct sockaddr_in sin;
    159 
    160     memset (& sin, 0, sizeof (struct sockaddr_in));
    161 
    162     sin.sin_family = AF_INET;
    163 
    164     if (address != NULL)
    165     {
    166        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    167        sin.sin_addr.s_addr = address -> host;
    168     }
    169     else
    170     {
    171        sin.sin_port = 0;
    172        sin.sin_addr.s_addr = INADDR_ANY;
     158    {
     159       sin.sin6_port = 0;
     160       sin.sin6_addr = in6addr_any;
     161       sin.sin6_scope_id = 0;
    173162    }
    174163
    175164    return bind (socket,
    176165                 (struct sockaddr *) & sin,
    177                  sizeof (struct sockaddr_in));
     166                 sizeof (struct sockaddr_in6));
    178167}
    179168
     
    187176enet_socket_create (ENetSocketType type)
    188177{
    189     return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
     178    return socket (AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
    190179}
    191180
     
    220209            break;
    221210
     211        case ENET_SOCKOPT_V6ONLY:
     212            result = setsockopt (socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) & value, sizeof (int));
     213            break;
     214
    222215        default:
    223216            break;
     
    229222enet_socket_connect (ENetSocket socket, const ENetAddress * address)
    230223{
    231     struct sockaddr_in sin;
    232 
    233     memset (& sin, 0, sizeof (struct sockaddr_in));
    234 
    235     sin.sin_family = AF_INET;
    236     sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    237     sin.sin_addr.s_addr = address -> host;
    238 
    239     return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
     224    struct sockaddr_in6 sin;
     225
     226    memset (& sin, 0, sizeof (struct sockaddr_in6));
     227
     228    sin.sin6_family = AF_INET6;
     229    sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     230    sin.sin6_addr = * (struct in6_addr *) & address -> host;
     231    sin.sin6_scope_id = address -> scopeID;
     232
     233    return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
    240234}
    241235
     
    244238{
    245239    int result;
    246     struct sockaddr_in sin;
    247     socklen_t sinLength = sizeof (struct sockaddr_in);
     240    struct sockaddr_in6 sin;
     241    socklen_t sinLength = sizeof (struct sockaddr_in6);
    248242
    249243    result = accept (socket,
     
    256250    if (address != NULL)
    257251    {
    258         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    259         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     252        address -> host = * (ENetHostAddress *) & sin.sin6_addr;
     253        address -> scopeID = sin.sin6_scope_id;
     254        address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
    260255    }
    261256
     
    276271{
    277272    struct msghdr msgHdr;
    278     struct sockaddr_in sin;
     273    struct sockaddr_in6 sin;
    279274    int sentLength;
    280275
     
    283278    if (address != NULL)
    284279    {
    285         memset (& sin, 0, sizeof (struct sockaddr_in));
    286 
    287         sin.sin_family = AF_INET;
    288         sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
    289         sin.sin_addr.s_addr = address -> host;
     280        memset (& sin, 0, sizeof (struct sockaddr_in6));
     281
     282        sin.sin6_family = AF_INET6;
     283        sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
     284        sin.sin6_addr = * (struct in6_addr *) & address -> host;
     285        sin.sin6_scope_id = address -> scopeID;
    290286
    291287        msgHdr.msg_name = & sin;
    292         msgHdr.msg_namelen = sizeof (struct sockaddr_in);
     288        msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
    293289    }
    294290
     
    316312{
    317313    struct msghdr msgHdr;
    318     struct sockaddr_in sin;
     314    struct sockaddr_in6 sin;
    319315    int recvLength;
    320316
     
    324320    {
    325321        msgHdr.msg_name = & sin;
    326         msgHdr.msg_namelen = sizeof (struct sockaddr_in);
     322        msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
    327323    }
    328324
     
    347343    if (address != NULL)
    348344    {
    349         address -> host = (enet_uint32) sin.sin_addr.s_addr;
    350         address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
     345        address -> host = * (ENetHostAddress *) & sin.sin6_addr;
     346        address -> scopeID = sin.sin6_scope_id;
     347        address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
    351348    }
    352349
Note: See TracChangeset for help on using the changeset viewer.