source:
code/trunk/src/external/enet/patches/0001-IPv6-support.patch
@
7656
Last change on this file since 7656 was 7459, checked in by adrfried, 14 years ago | |
---|---|
|
|
File size: 44.1 KB |
-
host.c
From 68e3ac1c2876aedb62a77fdc7ba71e4983a6b99d Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Thu, 2 Sep 2010 14:26:42 +0200 Subject: [PATCH] IPv6 support --- host.c | 57 +++++++--- include/enet/enet.h | 64 ++++++++--- include/enet/win32.h | 5 + protocol.c | 73 ++++++++++-- unix.c | 312 ++++++++++++++++++++++++++++++-------------------- win32.c | 245 ++++++++++++++++++++++++++-------------- 6 files changed, 506 insertions(+), 250 deletions(-) diff --git a/host.c b/host.c index 8bb2433..a9d157b 100644
a b 7 7 #include <time.h> 8 8 #include "enet/enet.h" 9 9 10 static ENetSocket 11 enet_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 /* This is not a conditional bind anymore, 18 * because WSARecvFrom returned WSAEINVAL on the IPv6 socket. 19 * TODO: Check for it's consequences. */ 20 if (enet_socket_bind (socket, address, family) < 0) 21 { 22 enet_socket_destroy (socket); 23 return ENET_SOCKET_NULL; 24 } 25 26 enet_socket_set_option (socket, ENET_SOCKOPT_NONBLOCK, 1); 27 enet_socket_set_option (socket, ENET_SOCKOPT_BROADCAST, 1); 28 enet_socket_set_option (socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); 29 enet_socket_set_option (socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 30 31 return socket; 32 } 33 10 34 /** @defgroup host ENet host functions 11 35 @{ 12 36 */ … … enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 31 55 { 32 56 ENetHost * host; 33 57 ENetPeer * currentPeer; 58 int family; 34 59 35 60 if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID) 36 61 return NULL; … … enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 48 73 } 49 74 memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); 50 75 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)) 53 { 54 if (host -> socket != ENET_SOCKET_NULL) 55 enet_socket_destroy (host -> socket); 76 family = (address == NULL || !memcmp (& address -> host, & ENET_HOST_ANY, sizeof (ENetHostAddress))) ? 77 ENET_IPV4 | ENET_IPV6 : 78 enet_get_address_family (address); 56 79 57 enet_free (host -> peers); 58 enet_free (host); 80 host -> socket4 = (family & ENET_IPV4) ? 81 enet_socket_create_bind (address, ENET_IPV4) : 82 ENET_SOCKET_NULL; 83 host -> socket6 = (family & ENET_IPV6) ? 84 enet_socket_create_bind (address, ENET_IPV6) : 85 ENET_SOCKET_NULL; 59 86 60 return NULL; 87 if (host -> socket4 == ENET_SOCKET_NULL && host -> socket6 == ENET_SOCKET_NULL) 88 { 89 enet_free (host -> peers); 90 enet_free (host); 91 return NULL; 61 92 } 62 93 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 68 94 if (address != NULL) 69 95 host -> address = * address; 70 96 … … enet_host_destroy (ENetHost * host) 133 159 { 134 160 ENetPeer * currentPeer; 135 161 136 enet_socket_destroy (host -> socket); 162 if (host -> socket4 != ENET_SOCKET_NULL) 163 enet_socket_destroy (host -> socket4); 164 if (host -> socket6 != ENET_SOCKET_NULL) 165 enet_socket_destroy (host -> socket6); 137 166 138 167 for (currentPeer = host -> peers; 139 168 currentPeer < & host -> peers [host -> peerCount]; -
include/enet/enet.h
diff --git a/include/enet/enet.h b/include/enet/enet.h index 2f656d6..7b468a0 100644
a b typedef enum _ENetSocketOption 53 53 ENET_SOCKOPT_REUSEADDR = 5 54 54 } ENetSocketOption; 55 55 56 enum 56 typedef struct _ENetHostAddress 57 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 */ 58 enet_uint8 addr[16]; 59 } ENetHostAddress; 60 61 #define ENET_HOST_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } /**< specifies the default server host (macro for variable initialization) */ 62 static const ENetHostAddress ENET_HOST_ANY = ENET_HOST_ANY_INIT; /**< specifies the default server host (global constant variable) */ 63 #define ENET_IPV4MAPPED_PREFIX_INIT { { 0,0,0,0,0,0,0,0,0,0,0xff,0xff,0,0,0,0 } } /**< specifies the IPv4-mapped IPv6 prefix (macro for variable initialization) */ 64 static const ENetHostAddress ENET_IPV4MAPPED_PREFIX = ENET_IPV4MAPPED_PREFIX_INIT; /**< specifies the IPv4-mapped IPv6 prefix (global constant variable) */ 65 #define ENET_HOST_BROADCAST_INIT { { 0,0,0,0,0,0,0,0,0,0,0xff,0xff,0xff,0xff,0xff,0xff } } /**< specifies a IPv4 subnet-wide broadcast (macro for variable initialization) */ 66 static const ENetHostAddress ENET_HOST_BROADCAST = ENET_HOST_BROADCAST_INIT; /**< specifies a IPv4 subnet-wide broadcast (global constant variable) */ 67 enum { 68 ENET_IPV4MAPPED_PREFIX_LEN = 12, /**< specifies the length of the IPv4-mapped IPv6 prefix */ 69 ENET_PORT_ANY = 0 /**< specifies that a port should be automatically chosen */ 62 70 }; 63 71 64 72 /** … … enum 73 81 */ 74 82 typedef struct _ENetAddress 75 83 { 76 enet_uint32 host; 84 ENetHostAddress host; 85 #ifdef WIN32 86 u_long scopeID; 87 #else 88 uint32_t scopeID; 89 #endif 77 90 enet_uint16 port; 78 91 } ENetAddress; 79 92 80 93 /** 94 * The address family type. 95 */ 96 typedef enum _ENetAddressFamily 97 { 98 ENET_NO_ADDRESS_FAMILY = 0, 99 ENET_IPV4 = (1 << 0), 100 ENET_IPV6 = (1 << 1) 101 } ENetAddressFamily; 102 103 /** 81 104 * Packet flag bit constants. 82 105 * 83 106 * The host must be specified in network byte-order, and the port must be in … … typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * b 321 344 */ 322 345 typedef struct _ENetHost 323 346 { 324 ENetSocket socket; 347 ENetSocket socket4; 348 ENetSocket socket6; 325 349 ENetAddress address; /**< Internet address of the host */ 326 350 enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ 327 351 enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ … … ENET_API void enet_time_set (enet_uint32); 441 465 /** @defgroup socket ENet socket functions 442 466 @{ 443 467 */ 444 ENET_API ENetSocket enet_socket_create (ENetSocketType );445 ENET_API int enet_socket_bind (ENetSocket, const ENetAddress * );468 ENET_API ENetSocket enet_socket_create (ENetSocketType, ENetAddressFamily); 469 ENET_API int enet_socket_bind (ENetSocket, const ENetAddress *, ENetAddressFamily); 446 470 ENET_API int enet_socket_listen (ENetSocket, int); 447 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress * );448 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress * );449 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t );450 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t );451 ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);471 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFamily); 472 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily); 473 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily); 474 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily); 475 ENET_API int enet_socket_wait (ENetSocket, ENetSocket, enet_uint32 *, enet_uint32); 452 476 ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); 453 477 ENET_API void enet_socket_destroy (ENetSocket); 454 478 ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32); … … ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostN 488 512 */ 489 513 ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength); 490 514 515 /** Maps an IPv4 Address to an IPv6 address. 516 @param address IPv4 address in network byte order 517 @returns the IPv4-mapped IPv6 address in network byte order 518 */ 519 ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address); 520 521 /** Returns the Address family of an (IPv4-mapped) IPv6 address. 522 @param address IPv6 address 523 @returns address family 524 */ 525 ENET_API ENetAddressFamily enet_get_address_family (const ENetAddress * address); 526 491 527 /** @} */ 492 528 493 529 ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32); -
include/enet/win32.h
diff --git a/include/enet/win32.h b/include/enet/win32.h index 0e1cf0c..53a7ff7 100644
a b 14 14 15 15 #include <stdlib.h> 16 16 #include <winsock2.h> 17 #include <ws2tcpip.h> 17 18 18 19 typedef SOCKET ENetSocket; 19 20 … … typedef fd_set ENetSocketSet; 53 54 #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset)) 54 55 #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset)) 55 56 57 #ifndef AI_ADDRCONFIG 58 #define AI_ADDRCONFIG 0x0400 /* AI_ADDRCONFIG is not defined everywhere */ 59 #endif 60 56 61 #endif /* __ENET_WIN32_H__ */ 57 62 58 63 -
protocol.c
diff --git a/protocol.c b/protocol.c index 8e26dfb..cfef646 100644
a b static size_t commandSizes [ENET_PROTOCOL_COMMAND_COUNT] = 25 25 sizeof (ENetProtocolThrottleConfigure), 26 26 }; 27 27 28 ENetHostAddress 29 enet_address_map4 (enet_uint32 address) 30 { 31 ENetHostAddress addr = ENET_IPV4MAPPED_PREFIX_INIT; 32 ((enet_uint32 *)addr.addr)[3] = address; 33 return addr; 34 } 35 36 ENetAddressFamily 37 enet_get_address_family (const ENetAddress * address) 38 { 39 if (!memcmp(& address->host, & ENET_IPV4MAPPED_PREFIX, ENET_IPV4MAPPED_PREFIX_LEN)) 40 return ENET_IPV4; 41 return ENET_IPV6; 42 } 43 28 44 size_t 29 45 enet_protocol_command_size (enet_uint8 commandNumber) 30 46 { … … enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet 262 278 ++ currentPeer) 263 279 { 264 280 if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED && 265 currentPeer -> address.host == host -> receivedAddress.host &&266 281 currentPeer -> address.port == host -> receivedAddress.port && 267 currentPeer -> connectID == command -> connect.connectID) 282 currentPeer -> connectID == command -> connect.connectID && 283 !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress))) 268 284 return NULL; 269 285 } 270 286 … … enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 848 864 849 865 if (peer -> state == ENET_PEER_STATE_DISCONNECTED || 850 866 peer -> state == ENET_PEER_STATE_ZOMBIE || 851 (host -> receivedAddress.host != peer -> address.host &&852 peer -> address.host != ENET_HOST_BROADCAST) ||853 867 (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID && 854 sessionID != peer -> incomingSessionID)) 868 sessionID != peer -> incomingSessionID) || 869 ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) && 870 memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) && 871 peer -> address.host.addr[0] != 0xff ) ) 855 872 return 0; 856 873 } 857 874 … … enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 891 908 892 909 if (peer != NULL) 893 910 { 894 peer -> address.host = host -> receivedAddress.host; 895 peer -> address.port = host -> receivedAddress.port; 911 peer -> address = host -> receivedAddress; 896 912 peer -> incomingDataTotal += host -> receivedDataLength; 897 913 } 898 914 … … commandError: 1021 1037 } 1022 1038 1023 1039 static int 1024 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event )1040 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family) 1025 1041 { 1026 1042 for (;;) 1027 1043 { … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1031 1047 buffer.data = host -> packetData [0]; 1032 1048 buffer.dataLength = sizeof (host -> packetData [0]); 1033 1049 1034 receivedLength = enet_socket_receive ( host -> socket,1050 receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6, 1035 1051 & host -> receivedAddress, 1036 1052 & buffer, 1037 1); 1053 1, 1054 family); 1038 1055 1039 1056 if (receivedLength < 0) 1040 1057 return -1; … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1042 1059 if (receivedLength == 0) 1043 1060 return 0; 1044 1061 1062 if (enet_get_address_family (& host -> receivedAddress) != family) 1063 return -1; 1064 1045 1065 host -> receivedData = host -> packetData [0]; 1046 1066 host -> receivedDataLength = receivedLength; 1047 1067 … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1373 1393 currentPeer < & host -> peers [host -> peerCount]; 1374 1394 ++ currentPeer) 1375 1395 { 1396 ENetAddressFamily family; 1397 ENetSocket socket; 1398 1376 1399 if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED || 1377 1400 currentPeer -> state == ENET_PEER_STATE_ZOMBIE) 1378 1401 continue; … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1497 1520 1498 1521 currentPeer -> lastSendTime = host -> serviceTime; 1499 1522 1500 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount); 1523 family = enet_get_address_family (& currentPeer -> address); 1524 socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6; 1525 if (socket == ENET_SOCKET_NULL) 1526 return -1; 1527 sentLength = enet_socket_send (socket, 1528 & currentPeer -> address, 1529 host -> buffers, 1530 host -> bufferCount, 1531 family); 1501 1532 1502 1533 enet_protocol_remove_sent_unreliable_commands (currentPeer); 1503 1534 … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1608 1639 break; 1609 1640 } 1610 1641 1611 switch (enet_protocol_receive_incoming_commands (host, event)) 1642 if (host -> socket4 != ENET_SOCKET_NULL) 1643 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4)) 1644 { 1645 case 1: 1646 return 1; 1647 1648 case -1: 1649 perror ("Error receiving incoming packets"); 1650 1651 return -1; 1652 1653 default: 1654 break; 1655 } 1656 1657 if (host -> socket6 != ENET_SOCKET_NULL) 1658 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6)) 1612 1659 { 1613 1660 case 1: 1614 1661 return 1; … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1660 1707 1661 1708 waitCondition = ENET_SOCKET_WAIT_RECEIVE; 1662 1709 1663 if (enet_socket_wait (host -> socket , & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)1710 if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0) 1664 1711 return -1; 1665 1712 1666 1713 host -> serviceTime = enet_time_get (); -
unix.c
diff --git a/unix.c b/unix.c index 6971541..992ecd3 100644
a b enet_time_set (enet_uint32 newTimeBase) 71 71 timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase; 72 72 } 73 73 74 int 75 enet_a ddress_set_host (ENetAddress * address, const char * name)74 static enet_uint16 75 enet_af (ENetAddressFamily family) 76 76 { 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 77 if (family == ENET_IPV4) 78 return AF_INET; 79 if (family == ENET_IPV6) 80 return AF_INET6; 81 return 0; 82 } 83 84 static socklen_t 85 enet_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 } 91 93 92 if (hostEntry == NULL || 93 hostEntry -> h_addrtype != AF_INET) 94 static ENetAddressFamily 95 enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 96 { 97 memset (address, 0, sizeof (ENetAddress)); 98 if (sin -> sa_family == AF_INET) 94 99 { 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; 100 address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); 101 /* address -> scopeID = 0; */ 102 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); 103 return ENET_IPV4; 102 104 } 103 104 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 105 106 return 0; 105 if (sin -> sa_family == AF_INET6) 106 { 107 address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; 108 address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; 109 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); 110 return ENET_IPV6; 111 } 112 return ENET_NO_ADDRESS_FAMILY; 107 113 } 108 114 109 int110 enet_address_ get_host_ip (const ENetAddress * address, char * name, size_t nameLength)115 static int 116 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 111 117 { 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); 118 else 119 #endif 120 return -1; 121 return 0; 118 memset (sin, 0, enet_sa_size(family)); 119 if (family == ENET_IPV4 && 120 (enet_get_address_family (address) == ENET_IPV4 || 121 !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 122 { 123 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 124 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; 125 ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 126 return 0; 127 } 128 else if (family == ENET_IPV6) 129 { 130 ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; 131 ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; 132 ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; 133 ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); 134 return 0; 135 } 136 return -1; 122 137 } 123 138 124 139 int 125 enet_address_ get_host (const ENetAddress * address, char * name, size_t nameLength)140 enet_address_set_host (ENetAddress * address, const char * name) 126 141 { 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; 142 enet_uint16 port = address -> port; 143 struct addrinfo hints; 144 struct addrinfo * result; 145 struct addrinfo * res; 133 146 134 in.s_addr = address -> host; 147 memset(& hints, 0, sizeof (hints)); 148 hints.ai_flags = AI_ADDRCONFIG; 149 hints.ai_family = AF_UNSPEC; 135 150 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; 151 if ( getaddrinfo(name, NULL, &hints, &result) ) 152 return -1; 143 153 144 hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); 145 #endif 154 for (res = result; res != NULL; res = res -> ai_next) 155 { 156 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 157 break; 158 } 146 159 147 if (hostEntry == NULL) 148 return enet_address_get_host_ip (address, name, nameLength); 160 address -> port = port; 161 freeaddrinfo(result); 162 if (res == NULL) return -1; 149 163 150 strncpy (name, hostEntry -> h_name, nameLength); 164 return 0; 165 } 166 167 static int 168 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 169 { 170 struct sockaddr_storage sin; 171 enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); 172 173 if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 174 return -1; 151 175 152 176 return 0; 153 177 } 154 178 155 179 int 156 enet_ socket_bind (ENetSocket socket, const ENetAddress * address)180 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 157 181 { 158 struct sockaddr_in sin; 182 return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); 183 } 159 184 160 memset (& sin, 0, sizeof (struct sockaddr_in)); 185 int 186 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 187 { 188 return enet_address_get_host_x(address, name, nameLength, 0); 189 } 161 190 162 sin.sin_family = AF_INET; 191 int 192 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 193 { 194 struct sockaddr_storage sin; 163 195 164 196 if (address != NULL) 165 197 { 166 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 167 sin.sin_addr.s_addr = address -> host; 198 enet_address_set_sin((struct sockaddr *) & sin, address, family); 168 199 } 169 200 else 170 201 { 171 sin.sin_port = 0;172 sin.sin_addr.s_addr = INADDR_ANY;202 ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; 203 enet_address_set_sin((struct sockaddr *) & sin, & address_, family); 173 204 } 174 205 175 return bind (socket, 176 (struct sockaddr *) & sin, 177 sizeof (struct sockaddr_in)); 206 return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)); 178 207 } 179 208 180 int 209 int 181 210 enet_socket_listen (ENetSocket socket, int backlog) 182 211 { 183 212 return listen (socket, backlog < 0 ? SOMAXCONN : backlog); 184 213 } 185 214 186 215 ENetSocket 187 enet_socket_create (ENetSocketType type )216 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 188 217 { 189 return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 218 ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 219 220 #ifdef IPV6_V6ONLY 221 if (family == ENET_IPV6) 222 { 223 int value = 1; 224 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 225 } 226 #endif /* IPV6_V6ONLY */ 227 228 return sock; 190 229 } 191 230 192 231 int … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 226 265 } 227 266 228 267 int 229 enet_socket_connect (ENetSocket socket, const ENetAddress * address )268 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 230 269 { 231 struct sockaddr_in sin; 232 233 memset (& sin, 0, sizeof (struct sockaddr_in)); 270 struct sockaddr_storage sin; 271 enet_address_set_sin((struct sockaddr *) & sin, address, family); 234 272 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)); 273 return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)); 240 274 } 241 275 242 276 ENetSocket 243 enet_socket_accept (ENetSocket socket, ENetAddress * address )277 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 244 278 { 245 279 int result; 246 struct sockaddr_ insin;247 socklen_t sinLength = sizeof (struct sockaddr_in);280 struct sockaddr_storage sin; 281 socklen_t sinLength = enet_sa_size (family); 248 282 249 283 result = accept (socket, 250 address != NULL ? (struct sockaddr *) & sin : NULL, 284 address != NULL ? (struct sockaddr *) & sin : NULL, 251 285 address != NULL ? & sinLength : NULL); 252 286 253 287 if (result == -1) 254 288 return ENET_SOCKET_NULL; 255 289 256 290 if (address != NULL) 257 291 { 258 address -> host = (enet_uint32) sin.sin_addr.s_addr; 259 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 292 enet_address_set_address(address, (struct sockaddr *) & sin); 260 293 } 261 294 262 295 return result; 263 } 264 296 } 297 265 298 void 266 299 enet_socket_destroy (ENetSocket socket) 267 300 { … … int 272 305 enet_socket_send (ENetSocket socket, 273 306 const ENetAddress * address, 274 307 const ENetBuffer * buffers, 275 size_t bufferCount) 308 size_t bufferCount, 309 ENetAddressFamily family) 276 310 { 277 311 struct msghdr msgHdr; 278 struct sockaddr_ insin;312 struct sockaddr_storage sin; 279 313 int sentLength; 280 314 281 315 memset (& msgHdr, 0, sizeof (struct msghdr)); 282 316 283 317 if (address != NULL) 284 318 { 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; 290 319 enet_address_set_sin((struct sockaddr *) & sin, address, family); 291 320 msgHdr.msg_name = & sin; 292 msgHdr.msg_namelen = sizeof (struct sockaddr_in);321 msgHdr.msg_namelen = enet_sa_size (family); 293 322 } 294 323 295 324 msgHdr.msg_iov = (struct iovec *) buffers; … … int 312 341 enet_socket_receive (ENetSocket socket, 313 342 ENetAddress * address, 314 343 ENetBuffer * buffers, 315 size_t bufferCount) 344 size_t bufferCount, 345 ENetAddressFamily family) 316 346 { 317 347 struct msghdr msgHdr; 318 struct sockaddr_ insin;348 struct sockaddr_storage sin; 319 349 int recvLength; 320 350 321 351 memset (& msgHdr, 0, sizeof (struct msghdr)); … … enet_socket_receive (ENetSocket socket, 323 353 if (address != NULL) 324 354 { 325 355 msgHdr.msg_name = & sin; 326 msgHdr.msg_namelen = sizeof (struct sockaddr_in);356 msgHdr.msg_namelen = enet_sa_size (family); 327 357 } 328 358 329 359 msgHdr.msg_iov = (struct iovec *) buffers; … … enet_socket_receive (ENetSocket socket, 346 376 347 377 if (address != NULL) 348 378 { 349 address -> host = (enet_uint32) sin.sin_addr.s_addr; 350 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 379 enet_address_set_address(address, (struct sockaddr *) & sin); 351 380 } 352 381 353 382 return recvLength; … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 365 394 } 366 395 367 396 int 368 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)397 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 369 398 { 370 399 #ifdef HAS_POLL 371 struct pollfd pollSocket ;400 struct pollfd pollSocket[2]; 372 401 int pollCount; 373 374 pollSocket.fd = socket; 375 pollSocket.events = 0; 402 403 pollSocket[0].fd = socket4; 404 pollSocket[1].fd = socket6; 405 pollSocket[0].events = 0; 406 pollSocket[1].events = 0; 407 /* pollSocket[0].revents = 0; */ 408 pollSocket[1].revents = 0; 409 410 if (pollSocket[0].fd == ENET_SOCKET_NULL) 411 { 412 pollSocket[0].fd = pollSocket[1].fd; 413 pollSocket[1].fd = ENET_SOCKET_NULL; 414 } 376 415 377 416 if (* condition & ENET_SOCKET_WAIT_SEND) 378 pollSocket.events |= POLLOUT; 417 { 418 pollSocket[0].events |= POLLOUT; 419 pollSocket[1].events |= POLLOUT; 420 } 379 421 380 422 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 381 pollSocket.events |= POLLIN; 423 { 424 pollSocket[0].events |= POLLIN; 425 pollSocket[1].events |= POLLIN; 426 } 382 427 383 pollCount = poll ( & pollSocket,1, timeout);428 pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout); 384 429 385 430 if (pollCount < 0) 386 431 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 390 435 if (pollCount == 0) 391 436 return 0; 392 437 393 if ( pollSocket.revents& POLLOUT)438 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT) 394 439 * condition |= ENET_SOCKET_WAIT_SEND; 395 440 396 if ( pollSocket.revents& POLLIN)441 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN) 397 442 * condition |= ENET_SOCKET_WAIT_RECEIVE; 398 443 399 444 return 0; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 401 446 fd_set readSet, writeSet; 402 447 struct timeval timeVal; 403 448 int selectCount; 449 ENetSocket maxSocket; 404 450 405 451 timeVal.tv_sec = timeout / 1000; 406 452 timeVal.tv_usec = (timeout % 1000) * 1000; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 409 455 FD_ZERO (& writeSet); 410 456 411 457 if (* condition & ENET_SOCKET_WAIT_SEND) 412 FD_SET (socket, & writeSet); 458 { 459 if (socket4 != ENET_SOCKET_NULL) 460 FD_SET (socket4, & writeSet); 461 if (socket6 != ENET_SOCKET_NULL) 462 FD_SET (socket6, & writeSet); 463 } 413 464 414 465 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 415 FD_SET (socket, & readSet); 466 { 467 if (socket4 != ENET_SOCKET_NULL) 468 FD_SET (socket4, & readSet); 469 if (socket6 != ENET_SOCKET_NULL) 470 FD_SET (socket6, & readSet); 471 } 472 473 maxSocket = 0; 474 if (socket4 != ENET_SOCKET_NULL) 475 maxSocket = socket4; 476 if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) 477 maxSocket = socket6; 416 478 417 selectCount = select ( socket + 1, & readSet, & writeSet, NULL, & timeVal);479 selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); 418 480 419 481 if (selectCount < 0) 420 482 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 424 486 if (selectCount == 0) 425 487 return 0; 426 488 427 if (FD_ISSET (socket, & writeSet)) 428 * condition |= ENET_SOCKET_WAIT_SEND; 489 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || 490 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) 491 * condition |= ENET_SOCKET_WAIT_SEND; 429 492 430 if (FD_ISSET (socket, & readSet)) 431 * condition |= ENET_SOCKET_WAIT_RECEIVE; 493 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || 494 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) 495 * condition |= ENET_SOCKET_WAIT_RECEIVE; 432 496 433 497 return 0; 434 498 #endif -
win32.c
diff --git a/win32.c b/win32.c index e1fae23..2607160 100644
a b 4 4 */ 5 5 #ifdef WIN32 6 6 7 #define _WIN32_WINNT 0x0501 8 7 9 #include <time.h> 8 10 #define ENET_BUILDING_LIB 1 9 11 #include "enet/enet.h" … … static enet_uint32 timeBase = 0; 13 15 int 14 16 enet_initialize (void) 15 17 { 16 WORD versionRequested = MAKEWORD ( 1, 1);18 WORD versionRequested = MAKEWORD (2, 2); 17 19 WSADATA wsaData; 18 20 19 21 if (WSAStartup (versionRequested, & wsaData)) 20 22 return -1; 21 23 22 if (LOBYTE (wsaData.wVersion) != 1||23 HIBYTE (wsaData.wVersion) != 1)24 if (LOBYTE (wsaData.wVersion) != 2|| 25 HIBYTE (wsaData.wVersion) != 2) 24 26 { 25 27 WSACleanup (); 26 28 … … enet_time_set (enet_uint32 newTimeBase) 52 54 timeBase = (enet_uint32) timeGetTime () - newTimeBase; 53 55 } 54 56 55 int 56 enet_a ddress_set_host (ENetAddress * address, const char * name)57 static enet_uint16 58 enet_af (ENetAddressFamily family) 57 59 { 58 struct hostent * hostEntry; 60 if (family == ENET_IPV4) 61 return AF_INET; 62 if (family == ENET_IPV6) 63 return AF_INET6; 64 return 0; 65 } 59 66 60 hostEntry = gethostbyname (name); 61 if (hostEntry == NULL || 62 hostEntry -> h_addrtype != AF_INET) 67 static socklen_t 68 enet_sa_size (ENetAddressFamily family) 69 { 70 if (family == ENET_IPV4) 71 return sizeof (struct sockaddr_in); 72 if (family == ENET_IPV6) 73 return sizeof (struct sockaddr_in6); 74 return 0; 75 } 76 77 static ENetAddressFamily 78 enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 79 { 80 memset (address, 0, sizeof (ENetAddress)); 81 if (sin -> sa_family == AF_INET) 63 82 { 64 unsigned long host = inet_addr (name); 65 if (host == INADDR_NONE) 66 return -1; 67 address -> host = host; 68 return 0; 83 address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); 84 /* address -> scopeID = 0; */ 85 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); 86 return ENET_IPV4; 69 87 } 88 if (sin -> sa_family == AF_INET6) 89 { 90 address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; 91 address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; 92 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); 93 return ENET_IPV6; 94 } 95 return ENET_NO_ADDRESS_FAMILY; 96 } 70 97 71 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 72 73 return 0; 98 static int 99 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 100 { 101 memset (sin, 0, enet_sa_size(family)); 102 if (family == ENET_IPV4 && 103 (enet_get_address_family (address) == ENET_IPV4 || 104 !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 105 { 106 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 107 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; 108 ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 109 return 0; 110 } 111 else if (family == ENET_IPV6) 112 { 113 ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; 114 ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; 115 ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; 116 ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); 117 return 0; 118 } 119 return -1; 74 120 } 75 121 76 122 int 77 enet_address_ get_host_ip (const ENetAddress * address, char * name, size_t nameLength)123 enet_address_set_host (ENetAddress * address, const char * name) 78 124 { 79 char * addr = inet_ntoa (* (struct in_addr *) & address -> host); 80 if (addr == NULL) 125 enet_uint16 port = address -> port; 126 struct addrinfo hints; 127 struct addrinfo * result; 128 struct addrinfo * res; 129 130 memset(& hints, 0, sizeof (hints)); 131 hints.ai_flags = AI_ADDRCONFIG; 132 hints.ai_family = AF_UNSPEC; 133 134 if ( getaddrinfo(name, NULL, &hints, &result) ) 81 135 return -1; 82 strncpy (name, addr, nameLength); 136 137 for (res = result; res != NULL; res = res -> ai_next) 138 { 139 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 140 break; 141 } 142 143 address -> port = port; 144 freeaddrinfo(result); 145 if (res == NULL) return -1; 146 83 147 return 0; 84 148 } 85 149 86 int87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)150 static int 151 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 88 152 { 89 struct in_addr in; 90 struct hostent * hostEntry; 91 92 in.s_addr = address -> host; 93 94 hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); 95 if (hostEntry == NULL) 96 return enet_address_get_host_ip (address, name, nameLength); 153 struct sockaddr_storage sin; 154 enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); 97 155 98 strncpy (name, hostEntry -> h_name, nameLength); 156 if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 157 return -1; 99 158 100 159 return 0; 101 160 } 102 161 103 162 int 104 enet_ socket_bind (ENetSocket socket, const ENetAddress * address)163 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 105 164 { 106 struct sockaddr_in sin; 165 return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); 166 } 107 167 108 memset (& sin, 0, sizeof (struct sockaddr_in)); 168 int 169 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 170 { 171 return enet_address_get_host_x(address, name, nameLength, 0); 172 } 109 173 110 sin.sin_family = AF_INET; 174 int 175 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 176 { 177 struct sockaddr_storage sin; 111 178 112 179 if (address != NULL) 113 180 { 114 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 115 sin.sin_addr.s_addr = address -> host; 181 enet_address_set_sin((struct sockaddr *) & sin, address, family); 116 182 } 117 183 else 118 184 { 119 sin.sin_port = 0;120 sin.sin_addr.s_addr = INADDR_ANY;185 ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; 186 enet_address_set_sin((struct sockaddr *) & sin, & address_, family); 121 187 } 122 188 123 return bind (socket, 124 (struct sockaddr *) & sin, 125 sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; 189 return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; 126 190 } 127 191 128 192 int … … enet_socket_listen (ENetSocket socket, int backlog) 132 196 } 133 197 134 198 ENetSocket 135 enet_socket_create (ENetSocketType type )199 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 136 200 { 137 return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 201 ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 202 return sock; 138 203 } 139 204 140 205 int … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 173 238 } 174 239 175 240 int 176 enet_socket_connect (ENetSocket socket, const ENetAddress * address )241 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 177 242 { 178 struct sockaddr_in sin; 179 180 memset (& sin, 0, sizeof (struct sockaddr_in)); 243 struct sockaddr_storage sin; 244 enet_address_set_sin((struct sockaddr *) & sin, address, family); 181 245 182 sin.sin_family = AF_INET; 183 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 184 sin.sin_addr.s_addr = address -> host; 185 186 return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; 246 return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0; 187 247 } 188 248 189 249 ENetSocket 190 enet_socket_accept (ENetSocket socket, ENetAddress * address )250 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 191 251 { 192 252 SOCKET result; 193 struct sockaddr_ insin;194 int sinLength = sizeof (struct sockaddr_in);253 struct sockaddr_storage sin; 254 socklen_t sinLength = enet_sa_size (family); 195 255 196 256 result = accept (socket, 197 address != NULL ? (struct sockaddr *) & sin : NULL, 257 address != NULL ? (struct sockaddr *) & sin : NULL, 198 258 address != NULL ? & sinLength : NULL); 199 259 200 260 if (result == INVALID_SOCKET) … … enet_socket_accept (ENetSocket socket, ENetAddress * address) 202 262 203 263 if (address != NULL) 204 264 { 205 address -> host = (enet_uint32) sin.sin_addr.s_addr; 206 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 265 enet_address_set_address(address, (struct sockaddr *) & sin); 207 266 } 208 267 209 268 return result; … … int 219 278 enet_socket_send (ENetSocket socket, 220 279 const ENetAddress * address, 221 280 const ENetBuffer * buffers, 222 size_t bufferCount) 281 size_t bufferCount, 282 ENetAddressFamily family) 223 283 { 224 struct sockaddr_ insin;284 struct sockaddr_storage sin; 225 285 DWORD sentLength; 226 286 227 287 if (address != NULL) 228 288 { 229 memset (& sin, 0, sizeof (struct sockaddr_in)); 230 231 sin.sin_family = AF_INET; 232 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 233 sin.sin_addr.s_addr = address -> host; 289 enet_address_set_sin((struct sockaddr *) & sin, address, family); 234 290 } 235 291 236 292 if (WSASendTo (socket, … … enet_socket_send (ENetSocket socket, 239 295 & sentLength, 240 296 0, 241 297 address != NULL ? (struct sockaddr *) & sin : 0, 242 address != NULL ? sizeof (struct sockaddr_in) : 0,298 address != NULL ? enet_sa_size (family) : 0, 243 299 NULL, 244 300 NULL) == SOCKET_ERROR) 245 301 { … … int 256 312 enet_socket_receive (ENetSocket socket, 257 313 ENetAddress * address, 258 314 ENetBuffer * buffers, 259 size_t bufferCount) 315 size_t bufferCount, 316 ENetAddressFamily family) 260 317 { 261 INT sinLength = sizeof (struct sockaddr_in);318 INT sinLength = enet_sa_size (family); 262 319 DWORD flags = 0, 263 320 recvLength; 264 struct sockaddr_ insin;321 struct sockaddr_storage sin; 265 322 266 323 if (WSARecvFrom (socket, 267 324 (LPWSABUF) buffers, … … enet_socket_receive (ENetSocket socket, 288 345 289 346 if (address != NULL) 290 347 { 291 address -> host = (enet_uint32) sin.sin_addr.s_addr; 292 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 348 enet_address_set_address(address, (struct sockaddr *) & sin); 293 349 } 294 350 295 351 return (int) recvLength; … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 307 363 } 308 364 309 365 int 310 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)366 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 311 367 { 312 368 fd_set readSet, writeSet; 313 369 struct timeval timeVal; 314 370 int selectCount; 315 371 ENetSocket maxSocket; 372 316 373 timeVal.tv_sec = timeout / 1000; 317 374 timeVal.tv_usec = (timeout % 1000) * 1000; 318 375 319 376 FD_ZERO (& readSet); 320 377 FD_ZERO (& writeSet); 321 378 322 379 if (* condition & ENET_SOCKET_WAIT_SEND) 323 FD_SET (socket, & writeSet); 380 { 381 if (socket4 != ENET_SOCKET_NULL) 382 FD_SET (socket4, & writeSet); 383 if (socket6 != ENET_SOCKET_NULL) 384 FD_SET (socket6, & writeSet); 385 } 324 386 325 387 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 326 FD_SET (socket, & readSet); 388 { 389 if (socket4 != ENET_SOCKET_NULL) 390 FD_SET (socket4, & readSet); 391 if (socket6 != ENET_SOCKET_NULL) 392 FD_SET (socket6, & readSet); 393 } 327 394 328 selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal); 395 maxSocket = 0; 396 if (socket4 != ENET_SOCKET_NULL) 397 maxSocket = socket4; 398 if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) 399 maxSocket = socket6; 400 401 selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); 329 402 330 403 if (selectCount < 0) 331 404 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 335 408 if (selectCount == 0) 336 409 return 0; 337 410 338 if (FD_ISSET (socket, & writeSet)) 339 * condition |= ENET_SOCKET_WAIT_SEND; 340 341 if (FD_ISSET (socket, & readSet)) 342 * condition |= ENET_SOCKET_WAIT_RECEIVE; 411 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || 412 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) 413 * condition |= ENET_SOCKET_WAIT_SEND; 414 415 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || 416 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) 417 * condition |= ENET_SOCKET_WAIT_RECEIVE; 343 418 344 419 return 0; 345 } 420 } 346 421 347 422 #endif 348 423
Note: See TracBrowser
for help on using the repository browser.