source:
code/branches/ipv6/src/external/enet/patches/0004-using-two-separate-sockets-for-IPv4-and-IPv6.patch
@
7382
Last change on this file since 7382 was 7378, checked in by adrfried, 14 years ago | |
---|---|
File size: 11.9 KB |
-
host.c
From d61448922c08e6801f07c38077623d3bcc513ad4 Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Wed, 8 Sep 2010 12:50:04 +0200 Subject: [PATCH 4/4] using two separate sockets for IPv4 and IPv6 --- host.c | 45 ++++++++++++++++++++++++++++++++++++--------- include/enet/enet.h | 11 +++++++++-- protocol.c | 42 ++++++++++++++++++++++++++++++++++++------ unix.c | 38 ++++++++++++++++++++++++++------------ 4 files changed, 107 insertions(+), 29 deletions(-) diff --git a/host.c b/host.c index 9ccf894..85dfa3c 100644
a b enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 48 48 } 49 49 memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); 50 50 51 host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6); 52 if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address, ENET_IPV6) < 0)) 51 52 // FIXME: check address for ANY_ADRESS if not only bind to specific protocol 53 // FIXME: allow to fail one of the two protocols 54 /* IPv4 */ 55 host -> socket4 = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV4); 56 if (host -> socket4 == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket4, address, ENET_IPV4) < 0)) 53 57 { 54 if (host -> socket != ENET_SOCKET_NULL)55 enet_socket_destroy (host -> socket );58 if (host -> socket4 != ENET_SOCKET_NULL) 59 enet_socket_destroy (host -> socket4); 56 60 57 61 enet_free (host -> peers); 58 62 enet_free (host); … … enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 60 64 return NULL; 61 65 } 62 66 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 enet_socket_set_option (host -> socket4, ENET_SOCKOPT_NONBLOCK, 1); 68 enet_socket_set_option (host -> socket4, ENET_SOCKOPT_BROADCAST, 1); 69 enet_socket_set_option (host -> socket4, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); 70 enet_socket_set_option (host -> socket4, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 71 72 /* IPv6 */ 73 host -> socket6 = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6); 74 if (host -> socket6 == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket6, address, ENET_IPV6) < 0)) 75 { 76 if (host -> socket6 != ENET_SOCKET_NULL) 77 { 78 enet_socket_destroy (host -> socket4); 79 enet_socket_destroy (host -> socket6); 80 } 81 82 enet_free (host -> peers); 83 enet_free (host); 84 85 return NULL; 86 } 87 88 enet_socket_set_option (host -> socket6, ENET_SOCKOPT_NONBLOCK, 1); 89 enet_socket_set_option (host -> socket6, ENET_SOCKOPT_BROADCAST, 1); 90 enet_socket_set_option (host -> socket6, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); 91 enet_socket_set_option (host -> socket6, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 92 67 93 68 94 if (address != NULL) 69 95 host -> address = * address; … … enet_host_destroy (ENetHost * host) 133 159 { 134 160 ENetPeer * currentPeer; 135 161 136 enet_socket_destroy (host -> socket); 162 enet_socket_destroy (host -> socket4); 163 enet_socket_destroy (host -> socket6); 137 164 138 165 for (currentPeer = host -> peers; 139 166 currentPeer < & host -> peers [host -> peerCount]; -
include/enet/enet.h
diff --git a/include/enet/enet.h b/include/enet/enet.h index 97f0556..39cf93e 100644
a b typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * b 335 335 */ 336 336 typedef struct _ENetHost 337 337 { 338 ENetSocket socket; 338 ENetSocket socket4; 339 ENetSocket socket6; 339 340 ENetAddress address; /**< Internet address of the host */ 340 341 enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ 341 342 enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ … … ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFa 462 463 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily); 463 464 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily); 464 465 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily); 465 ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);466 ENET_API int enet_socket_wait (ENetSocket, ENetSocket, enet_uint32 *, enet_uint32); 466 467 ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); 467 468 ENET_API void enet_socket_destroy (ENetSocket); 468 469 ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32); … … ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName 508 509 */ 509 510 ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address); 510 511 512 /** Returns the Address family of an (IPv4-mapped) IPv6 address. 513 @param address IPv6 address 514 @returns address family 515 */ 516 ENET_API ENetAddressFamily enet_get_address_family (const ENetAddress * address); 517 511 518 /** @} */ 512 519 513 520 ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32); -
protocol.c
diff --git a/protocol.c b/protocol.c index 4c4850a..37f6387 100644
a b enet_address_map4 (enet_uint32 address) 37 37 return addr; 38 38 } 39 39 40 ENetAddressFamily 41 enet_get_address_family (const ENetAddress * address) 42 { 43 if (!memcmp(& address->host, & ENET_IPV4MAPPED_PREFIX, ENET_IPV4MAPPED_PREFIX_LEN)) 44 return ENET_IPV4; 45 return ENET_IPV6; 46 } 47 40 48 size_t 41 49 enet_protocol_command_size (enet_uint8 commandNumber) 42 50 { … … commandError: 1033 1041 } 1034 1042 1035 1043 static int 1036 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event )1044 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family) 1037 1045 { 1038 1046 for (;;) 1039 1047 { … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1043 1051 buffer.data = host -> packetData [0]; 1044 1052 buffer.dataLength = sizeof (host -> packetData [0]); 1045 1053 1046 receivedLength = enet_socket_receive ( host -> socket,1054 receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6, 1047 1055 & host -> receivedAddress, 1048 1056 & buffer, 1049 1057 1, 1050 ENET_IPV6);1058 family); 1051 1059 1052 1060 if (receivedLength < 0) 1053 1061 return -1; … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1055 1063 if (receivedLength == 0) 1056 1064 return 0; 1057 1065 1066 if (enet_get_address_family (& host -> receivedAddress) != family) 1067 return -1; 1068 1058 1069 host -> receivedData = host -> packetData [0]; 1059 1070 host -> receivedDataLength = receivedLength; 1060 1071 … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1510 1521 1511 1522 currentPeer -> lastSendTime = host -> serviceTime; 1512 1523 1513 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount, ENET_IPV6); 1524 ENetAddressFamily family = enet_get_address_family (& currentPeer -> address); 1525 sentLength = enet_socket_send (family == ENET_IPV4 ? host -> socket4 : host -> socket6, 1526 & currentPeer -> address, 1527 host -> buffers, 1528 host -> bufferCount, 1529 family); 1514 1530 1515 1531 enet_protocol_remove_sent_unreliable_commands (currentPeer); 1516 1532 … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1621 1637 break; 1622 1638 } 1623 1639 1624 switch (enet_protocol_receive_incoming_commands (host, event)) 1640 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4)) 1641 { 1642 case 1: 1643 return 1; 1644 1645 case -1: 1646 perror ("Error receiving incoming packets"); 1647 1648 return -1; 1649 1650 default: 1651 break; 1652 } 1653 1654 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6)) 1625 1655 { 1626 1656 case 1: 1627 1657 return 1; … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1673 1703 1674 1704 waitCondition = ENET_SOCKET_WAIT_RECEIVE; 1675 1705 1676 if (enet_socket_wait (host -> socket , & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)1706 if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0) 1677 1707 return -1; 1678 1708 1679 1709 host -> serviceTime = enet_time_get (); -
unix.c
diff --git a/unix.c b/unix.c index 13a24d8..96d17f8 100644
a b static int 117 117 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 118 118 { 119 119 memset (sin, 0, enet_sa_size(family)); 120 if (family == ENET_IPV4) 120 if (family == ENET_IPV4 && 121 (enet_get_address_family (address) == ENET_IPV4 || 122 !memcmp (address, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 121 123 { 122 124 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 123 125 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; … … enet_socket_create (ENetSocketType type, ENetAddressFamily family) 219 221 #ifdef IPV6_V6ONLY 220 222 if (family == ENET_IPV6) 221 223 { 222 int value = 0;224 int value = 1; 223 225 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 224 226 } 225 227 #endif // IPV6_V6ONLY … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 393 395 } 394 396 395 397 int 396 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)398 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 397 399 { 398 #ifdef HAS_POLL 399 struct pollfd pollSocket; 400 //FIXME allow only one of the sockets being available 401 //#ifdef HAS_POLL 402 struct pollfd pollSocket[2]; 400 403 int pollCount; 401 404 402 pollSocket.fd = socket; 403 pollSocket.events = 0; 405 pollSocket[0].fd = socket4; 406 pollSocket[1].fd = socket6; 407 pollSocket[0].events = 0; 408 pollSocket[1].events = 0; 404 409 405 410 if (* condition & ENET_SOCKET_WAIT_SEND) 406 pollSocket.events |= POLLOUT; 411 { 412 pollSocket[0].events |= POLLOUT; 413 pollSocket[1].events |= POLLOUT; 414 } 407 415 408 416 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 409 pollSocket.events |= POLLIN; 417 { 418 pollSocket[0].events |= POLLIN; 419 pollSocket[1].events |= POLLIN; 420 } 410 421 411 pollCount = poll ( & pollSocket, 1, timeout);422 pollCount = poll (pollSocket, 2, timeout); 412 423 413 424 if (pollCount < 0) 414 425 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 418 429 if (pollCount == 0) 419 430 return 0; 420 431 421 if ( pollSocket.revents& POLLOUT)432 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT) 422 433 * condition |= ENET_SOCKET_WAIT_SEND; 423 434 424 if ( pollSocket.revents& POLLIN)435 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN) 425 436 * condition |= ENET_SOCKET_WAIT_RECEIVE; 426 437 427 438 return 0; 439 /* 440 FIXME: implement this 428 441 #else 429 442 fd_set readSet, writeSet; 430 443 struct timeval timeVal; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 460 473 461 474 return 0; 462 475 #endif 476 */ 463 477 } 464 478 465 479 #endif
Note: See TracBrowser
for help on using the repository browser.