source:
code/trunk/src/external/enet/patches/0001-Add-IPv6-support-to-Enet.patch
@
9382
Last change on this file since 9382 was 8351, checked in by rgrieder, 14 years ago | |
---|---|
|
|
File size: 44.9 KB |
-
host.c
From 7a7274dad3f90d206e91c0712ea0d2599850b24e Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Thu, 2 Sep 2010 14:26:42 +0200 Subject: [PATCH] Add IPv6 support to Enet --- host.c | 57 +++++++--- include/enet/enet.h | 64 ++++++++--- include/enet/win32.h | 4 + protocol.c | 73 ++++++++++-- unix.c | 312 ++++++++++++++++++++++++++++++-------------------- win32.c | 247 ++++++++++++++++++++++++++-------------- 6 files changed, 507 insertions(+), 250 deletions(-) mode change 100755 => 100644 Doxyfile mode change 100755 => 100644 docs/FAQ.dox mode change 100755 => 100644 docs/design.dox mode change 100755 => 100644 docs/install.dox mode change 100755 => 100644 docs/license.dox mode change 100755 => 100644 docs/mainpage.dox mode change 100755 => 100644 docs/tutorial.dox mode change 100755 => 100644 enet.dsp diff --git a/Doxyfile b/Doxyfile old mode 100755 new mode 100644 diff --git a/docs/FAQ.dox b/docs/FAQ.dox old mode 100755 new mode 100644 diff --git a/docs/design.dox b/docs/design.dox old mode 100755 new mode 100644 diff --git a/docs/install.dox b/docs/install.dox old mode 100755 new mode 100644 diff --git a/docs/license.dox b/docs/license.dox old mode 100755 new mode 100644 diff --git a/docs/mainpage.dox b/docs/mainpage.dox old mode 100755 new mode 100644 diff --git a/docs/tutorial.dox b/docs/tutorial.dox old mode 100755 new mode 100644 diff --git a/enet.dsp b/enet.dsp old mode 100755 new mode 100644 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 b572590..8351166 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..6145851 100644
a b typedef fd_set ENetSocketSet; 53 53 #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset)) 54 54 #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset)) 55 55 56 #ifndef AI_ADDRCONFIG 57 #define AI_ADDRCONFIG 0x0400 /* AI_ADDRCONFIG is not defined everywhere */ 58 #endif 59 56 60 #endif /* __ENET_WIN32_H__ */ 57 61 58 62 -
protocol.c
diff --git a/protocol.c b/protocol.c index 2237ba8..7205d1b 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 266 282 ++ currentPeer) 267 283 { 268 284 if (currentPeer -> state != ENET_PEER_STATE_DISCONNECTED && 269 currentPeer -> address.host == host -> receivedAddress.host &&270 285 currentPeer -> address.port == host -> receivedAddress.port && 271 currentPeer -> connectID == command -> connect.connectID) 286 currentPeer -> connectID == command -> connect.connectID && 287 !memcmp(& currentPeer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress))) 272 288 return NULL; 273 289 } 274 290 … … enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 852 868 853 869 if (peer -> state == ENET_PEER_STATE_DISCONNECTED || 854 870 peer -> state == ENET_PEER_STATE_ZOMBIE || 855 (host -> receivedAddress.host != peer -> address.host &&856 peer -> address.host != ENET_HOST_BROADCAST) ||857 871 (peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID && 858 sessionID != peer -> incomingSessionID)) 872 sessionID != peer -> incomingSessionID) || 873 ( memcmp(& peer -> address.host, & host -> receivedAddress.host, sizeof (ENetHostAddress)) && 874 memcmp(& peer -> address.host, & ENET_HOST_BROADCAST, sizeof (ENetHostAddress)) && 875 peer -> address.host.addr[0] != 0xff ) ) 859 876 return 0; 860 877 } 861 878 … … enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event) 895 912 896 913 if (peer != NULL) 897 914 { 898 peer -> address.host = host -> receivedAddress.host; 899 peer -> address.port = host -> receivedAddress.port; 915 peer -> address = host -> receivedAddress; 900 916 peer -> incomingDataTotal += host -> receivedDataLength; 901 917 } 902 918 … … commandError: 1025 1041 } 1026 1042 1027 1043 static int 1028 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event )1044 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family) 1029 1045 { 1030 1046 for (;;) 1031 1047 { … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1035 1051 buffer.data = host -> packetData [0]; 1036 1052 buffer.dataLength = sizeof (host -> packetData [0]); 1037 1053 1038 receivedLength = enet_socket_receive ( host -> socket,1054 receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6, 1039 1055 & host -> receivedAddress, 1040 1056 & buffer, 1041 1); 1057 1, 1058 family); 1042 1059 1043 1060 if (receivedLength < 0) 1044 1061 return -1; … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1046 1063 if (receivedLength == 0) 1047 1064 return 0; 1048 1065 1066 if (enet_get_address_family (& host -> receivedAddress) != family) 1067 return -1; 1068 1049 1069 host -> receivedData = host -> packetData [0]; 1050 1070 host -> receivedDataLength = receivedLength; 1051 1071 … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1397 1417 currentPeer < & host -> peers [host -> peerCount]; 1398 1418 ++ currentPeer) 1399 1419 { 1420 ENetAddressFamily family; 1421 ENetSocket socket; 1422 1400 1423 if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED || 1401 1424 currentPeer -> state == ENET_PEER_STATE_ZOMBIE) 1402 1425 continue; … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1520 1543 1521 1544 currentPeer -> lastSendTime = host -> serviceTime; 1522 1545 1523 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount); 1546 family = enet_get_address_family (& currentPeer -> address); 1547 socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6; 1548 if (socket == ENET_SOCKET_NULL) 1549 return -1; 1550 sentLength = enet_socket_send (socket, 1551 & currentPeer -> address, 1552 host -> buffers, 1553 host -> bufferCount, 1554 family); 1524 1555 1525 1556 enet_protocol_remove_sent_unreliable_commands (currentPeer); 1526 1557 … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1631 1662 break; 1632 1663 } 1633 1664 1634 switch (enet_protocol_receive_incoming_commands (host, event)) 1665 if (host -> socket4 != ENET_SOCKET_NULL) 1666 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4)) 1667 { 1668 case 1: 1669 return 1; 1670 1671 case -1: 1672 perror ("Error receiving incoming packets"); 1673 1674 return -1; 1675 1676 default: 1677 break; 1678 } 1679 1680 if (host -> socket6 != ENET_SOCKET_NULL) 1681 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6)) 1635 1682 { 1636 1683 case 1: 1637 1684 return 1; … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1683 1730 1684 1731 waitCondition = ENET_SOCKET_WAIT_RECEIVE; 1685 1732 1686 if (enet_socket_wait (host -> socket , & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)1733 if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0) 1687 1734 return -1; 1688 1735 1689 1736 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..15b9888 100644
a b 4 4 */ 5 5 #ifdef WIN32 6 6 7 #define _WIN32_WINNT 0x0501 8 7 9 #include <time.h> 10 8 11 #define ENET_BUILDING_LIB 1 9 12 #include "enet/enet.h" 13 #include <ws2tcpip.h> 10 14 11 15 static enet_uint32 timeBase = 0; 12 16 13 17 int 14 18 enet_initialize (void) 15 19 { 16 WORD versionRequested = MAKEWORD ( 1, 1);20 WORD versionRequested = MAKEWORD (2, 2); 17 21 WSADATA wsaData; 18 22 19 23 if (WSAStartup (versionRequested, & wsaData)) 20 24 return -1; 21 25 22 if (LOBYTE (wsaData.wVersion) != 1||23 HIBYTE (wsaData.wVersion) != 1)26 if (LOBYTE (wsaData.wVersion) != 2|| 27 HIBYTE (wsaData.wVersion) != 2) 24 28 { 25 29 WSACleanup (); 26 30 … … enet_time_set (enet_uint32 newTimeBase) 52 56 timeBase = (enet_uint32) timeGetTime () - newTimeBase; 53 57 } 54 58 55 int 56 enet_a ddress_set_host (ENetAddress * address, const char * name)59 static enet_uint16 60 enet_af (ENetAddressFamily family) 57 61 { 58 struct hostent * hostEntry; 62 if (family == ENET_IPV4) 63 return AF_INET; 64 if (family == ENET_IPV6) 65 return AF_INET6; 66 return 0; 67 } 59 68 60 hostEntry = gethostbyname (name); 61 if (hostEntry == NULL || 62 hostEntry -> h_addrtype != AF_INET) 69 static socklen_t 70 enet_sa_size (ENetAddressFamily family) 71 { 72 if (family == ENET_IPV4) 73 return sizeof (struct sockaddr_in); 74 if (family == ENET_IPV6) 75 return sizeof (struct sockaddr_in6); 76 return 0; 77 } 78 79 static ENetAddressFamily 80 enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 81 { 82 memset (address, 0, sizeof (ENetAddress)); 83 if (sin -> sa_family == AF_INET) 63 84 { 64 unsigned long host = inet_addr (name); 65 if (host == INADDR_NONE) 66 return -1; 67 address -> host = host; 68 return 0; 85 address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); 86 /* address -> scopeID = 0; */ 87 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); 88 return ENET_IPV4; 69 89 } 90 if (sin -> sa_family == AF_INET6) 91 { 92 address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; 93 address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; 94 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); 95 return ENET_IPV6; 96 } 97 return ENET_NO_ADDRESS_FAMILY; 98 } 70 99 71 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 72 73 return 0; 100 static int 101 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 102 { 103 memset (sin, 0, enet_sa_size(family)); 104 if (family == ENET_IPV4 && 105 (enet_get_address_family (address) == ENET_IPV4 || 106 !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 107 { 108 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 109 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; 110 ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 111 return 0; 112 } 113 else if (family == ENET_IPV6) 114 { 115 ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; 116 ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; 117 ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; 118 ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); 119 return 0; 120 } 121 return -1; 74 122 } 75 123 76 124 int 77 enet_address_ get_host_ip (const ENetAddress * address, char * name, size_t nameLength)125 enet_address_set_host (ENetAddress * address, const char * name) 78 126 { 79 char * addr = inet_ntoa (* (struct in_addr *) & address -> host); 80 if (addr == NULL) 127 enet_uint16 port = address -> port; 128 struct addrinfo hints; 129 struct addrinfo * result; 130 struct addrinfo * res; 131 132 memset(& hints, 0, sizeof (hints)); 133 hints.ai_flags = AI_ADDRCONFIG; 134 hints.ai_family = AF_UNSPEC; 135 136 if ( getaddrinfo(name, NULL, &hints, &result) ) 81 137 return -1; 82 strncpy (name, addr, nameLength); 138 139 for (res = result; res != NULL; res = res -> ai_next) 140 { 141 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 142 break; 143 } 144 145 address -> port = port; 146 freeaddrinfo(result); 147 if (res == NULL) return -1; 148 83 149 return 0; 84 150 } 85 151 86 int87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)152 static int 153 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 88 154 { 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); 155 struct sockaddr_storage sin; 156 enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); 97 157 98 strncpy (name, hostEntry -> h_name, nameLength); 158 if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 159 return -1; 99 160 100 161 return 0; 101 162 } 102 163 103 164 int 104 enet_ socket_bind (ENetSocket socket, const ENetAddress * address)165 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 105 166 { 106 struct sockaddr_in sin; 167 return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); 168 } 107 169 108 memset (& sin, 0, sizeof (struct sockaddr_in)); 170 int 171 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 172 { 173 return enet_address_get_host_x(address, name, nameLength, 0); 174 } 109 175 110 sin.sin_family = AF_INET; 176 int 177 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 178 { 179 struct sockaddr_storage sin; 111 180 112 181 if (address != NULL) 113 182 { 114 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 115 sin.sin_addr.s_addr = address -> host; 183 enet_address_set_sin((struct sockaddr *) & sin, address, family); 116 184 } 117 185 else 118 186 { 119 sin.sin_port = 0;120 sin.sin_addr.s_addr = INADDR_ANY;187 ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; 188 enet_address_set_sin((struct sockaddr *) & sin, & address_, family); 121 189 } 122 190 123 return bind (socket, 124 (struct sockaddr *) & sin, 125 sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; 191 return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; 126 192 } 127 193 128 194 int … … enet_socket_listen (ENetSocket socket, int backlog) 132 198 } 133 199 134 200 ENetSocket 135 enet_socket_create (ENetSocketType type )201 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 136 202 { 137 return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 203 ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 204 return sock; 138 205 } 139 206 140 207 int … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 173 240 } 174 241 175 242 int 176 enet_socket_connect (ENetSocket socket, const ENetAddress * address )243 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 177 244 { 178 struct sockaddr_in sin; 179 180 memset (& sin, 0, sizeof (struct sockaddr_in)); 245 struct sockaddr_storage sin; 246 enet_address_set_sin((struct sockaddr *) & sin, address, family); 181 247 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; 248 return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0; 187 249 } 188 250 189 251 ENetSocket 190 enet_socket_accept (ENetSocket socket, ENetAddress * address )252 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 191 253 { 192 254 SOCKET result; 193 struct sockaddr_ insin;194 int sinLength = sizeof (struct sockaddr_in);255 struct sockaddr_storage sin; 256 socklen_t sinLength = enet_sa_size (family); 195 257 196 258 result = accept (socket, 197 address != NULL ? (struct sockaddr *) & sin : NULL, 259 address != NULL ? (struct sockaddr *) & sin : NULL, 198 260 address != NULL ? & sinLength : NULL); 199 261 200 262 if (result == INVALID_SOCKET) … … enet_socket_accept (ENetSocket socket, ENetAddress * address) 202 264 203 265 if (address != NULL) 204 266 { 205 address -> host = (enet_uint32) sin.sin_addr.s_addr; 206 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 267 enet_address_set_address(address, (struct sockaddr *) & sin); 207 268 } 208 269 209 270 return result; … … int 219 280 enet_socket_send (ENetSocket socket, 220 281 const ENetAddress * address, 221 282 const ENetBuffer * buffers, 222 size_t bufferCount) 283 size_t bufferCount, 284 ENetAddressFamily family) 223 285 { 224 struct sockaddr_ insin;286 struct sockaddr_storage sin; 225 287 DWORD sentLength; 226 288 227 289 if (address != NULL) 228 290 { 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; 291 enet_address_set_sin((struct sockaddr *) & sin, address, family); 234 292 } 235 293 236 294 if (WSASendTo (socket, … … enet_socket_send (ENetSocket socket, 239 297 & sentLength, 240 298 0, 241 299 address != NULL ? (struct sockaddr *) & sin : 0, 242 address != NULL ? sizeof (struct sockaddr_in) : 0,300 address != NULL ? enet_sa_size (family) : 0, 243 301 NULL, 244 302 NULL) == SOCKET_ERROR) 245 303 { … … int 256 314 enet_socket_receive (ENetSocket socket, 257 315 ENetAddress * address, 258 316 ENetBuffer * buffers, 259 size_t bufferCount) 317 size_t bufferCount, 318 ENetAddressFamily family) 260 319 { 261 INT sinLength = sizeof (struct sockaddr_in);320 INT sinLength = enet_sa_size (family); 262 321 DWORD flags = 0, 263 322 recvLength; 264 struct sockaddr_ insin;323 struct sockaddr_storage sin; 265 324 266 325 if (WSARecvFrom (socket, 267 326 (LPWSABUF) buffers, … … enet_socket_receive (ENetSocket socket, 288 347 289 348 if (address != NULL) 290 349 { 291 address -> host = (enet_uint32) sin.sin_addr.s_addr; 292 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 350 enet_address_set_address(address, (struct sockaddr *) & sin); 293 351 } 294 352 295 353 return (int) recvLength; … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 307 365 } 308 366 309 367 int 310 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)368 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 311 369 { 312 370 fd_set readSet, writeSet; 313 371 struct timeval timeVal; 314 372 int selectCount; 315 373 ENetSocket maxSocket; 374 316 375 timeVal.tv_sec = timeout / 1000; 317 376 timeVal.tv_usec = (timeout % 1000) * 1000; 318 377 319 378 FD_ZERO (& readSet); 320 379 FD_ZERO (& writeSet); 321 380 322 381 if (* condition & ENET_SOCKET_WAIT_SEND) 323 FD_SET (socket, & writeSet); 382 { 383 if (socket4 != ENET_SOCKET_NULL) 384 FD_SET (socket4, & writeSet); 385 if (socket6 != ENET_SOCKET_NULL) 386 FD_SET (socket6, & writeSet); 387 } 324 388 325 389 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 326 FD_SET (socket, & readSet); 390 { 391 if (socket4 != ENET_SOCKET_NULL) 392 FD_SET (socket4, & readSet); 393 if (socket6 != ENET_SOCKET_NULL) 394 FD_SET (socket6, & readSet); 395 } 327 396 328 selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal); 397 maxSocket = 0; 398 if (socket4 != ENET_SOCKET_NULL) 399 maxSocket = socket4; 400 if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) 401 maxSocket = socket6; 402 403 selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); 329 404 330 405 if (selectCount < 0) 331 406 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 335 410 if (selectCount == 0) 336 411 return 0; 337 412 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; 413 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || 414 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) 415 * condition |= ENET_SOCKET_WAIT_SEND; 416 417 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || 418 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) 419 * condition |= ENET_SOCKET_WAIT_RECEIVE; 343 420 344 421 return 0; 345 } 422 } 346 423 347 424 #endif 348 425
Note: See TracBrowser
for help on using the repository browser.