source:
code/branches/ipv6/src/external/enet/patches/0003-using-address-family-in-functions.patch
@
7382
Last change on this file since 7382 was 7378, checked in by adrfried, 14 years ago | |
---|---|
File size: 11.8 KB |
-
host.c
From 130babc6f69e07830f73f7757222597117eb1cba Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Mon, 6 Sep 2010 14:58:50 +0200 Subject: [PATCH 3/4] using address family in functions --- host.c | 4 +- include/enet/enet.h | 22 +++++++++--- protocol.c | 5 ++- unix.c | 87 ++++++++++++++++++++++++++++++++------------------ 4 files changed, 77 insertions(+), 41 deletions(-) diff --git a/host.c b/host.c index 8bb2433..9ccf894 100644
a b enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 48 48 } 49 49 memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); 50 50 51 host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM );52 if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address ) < 0))51 host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6); 52 if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address, ENET_IPV6) < 0)) 53 53 { 54 54 if (host -> socket != ENET_SOCKET_NULL) 55 55 enet_socket_destroy (host -> socket); -
include/enet/enet.h
diff --git a/include/enet/enet.h b/include/enet/enet.h index d3ca971..97f0556 100644
a b typedef struct _ENetAddress 82 82 } ENetAddress; 83 83 84 84 /** 85 * The address family type. 86 */ 87 typedef enum _ENetAddressFamily 88 { 89 ENET_NO_ADDRESS_FAMILY = 0, 90 ENET_IPV4 = 1, 91 ENET_IPV6 = 2 92 } ENetAddressFamily; 93 94 /** 85 95 * Packet flag bit constants. 86 96 * 87 97 * The host must be specified in network byte-order, and the port must be in … … ENET_API void enet_time_set (enet_uint32); 445 455 /** @defgroup socket ENet socket functions 446 456 @{ 447 457 */ 448 ENET_API ENetSocket enet_socket_create (ENetSocketType );449 ENET_API int enet_socket_bind (ENetSocket, const ENetAddress * );458 ENET_API ENetSocket enet_socket_create (ENetSocketType, ENetAddressFamily); 459 ENET_API int enet_socket_bind (ENetSocket, const ENetAddress *, ENetAddressFamily); 450 460 ENET_API int enet_socket_listen (ENetSocket, int); 451 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress * );452 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress * );453 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t );454 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t );461 ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFamily); 462 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily); 463 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily); 464 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily); 455 465 ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32); 456 466 ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); 457 467 ENET_API void enet_socket_destroy (ENetSocket); -
protocol.c
diff --git a/protocol.c b/protocol.c index 930835e..4c4850a 100644
a b enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1046 1046 receivedLength = enet_socket_receive (host -> socket, 1047 1047 & host -> receivedAddress, 1048 1048 & buffer, 1049 1); 1049 1, 1050 ENET_IPV6); 1050 1051 1051 1052 if (receivedLength < 0) 1052 1053 return -1; … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1509 1510 1510 1511 currentPeer -> lastSendTime = host -> serviceTime; 1511 1512 1512 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount );1513 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount, ENET_IPV6); 1513 1514 1514 1515 enet_protocol_remove_sent_unreliable_commands (currentPeer); 1515 1516 -
unix.c
diff --git a/unix.c b/unix.c index de032bb..13a24d8 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 static int 74 static enet_uint16 75 enet_af (ENetAddressFamily family) 76 { 77 if (family == ENET_IPV4) 78 return AF_INET; 79 if (family == ENET_IPV6) 80 return AF_INET6; 81 return 0; 82 } 83 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 } 93 94 95 static ENetAddressFamily 75 96 enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 76 97 { 77 98 memset (address, 0, sizeof (ENetAddress)); … … enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 80 101 address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); 81 102 //address -> scopeID = 0; 82 103 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); 83 return 0;104 return ENET_IPV4; 84 105 } 85 106 if (sin -> sa_family == AF_INET6) 86 107 { 87 108 address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; 88 109 address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; 89 110 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); 90 return 0;111 return ENET_IPV6; 91 112 } 92 return -1;113 return ENET_NO_ADDRESS_FAMILY; 93 114 } 94 115 95 116 static int 96 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, sa_family_tfamily)117 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 97 118 { 98 if (family == AF_INET) 119 memset (sin, 0, enet_sa_size(family)); 120 if (family == ENET_IPV4) 99 121 { 100 memset (sin, 0, sizeof (struct sockaddr_in));101 122 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 102 123 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; 103 124 ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 104 125 return 0; 105 126 } 106 else if (family == AF_INET6)127 else if (family == ENET_IPV6) 107 128 { 108 memset (sin, 0, sizeof (struct sockaddr_in6));109 129 ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; 110 130 ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; 111 131 ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; … … enet_address_set_host (ENetAddress * address, const char * name) 132 152 133 153 for (res = result; res != NULL; res = res -> ai_next) 134 154 { 135 if ( !enet_address_set_address(address, res -> ai_addr))155 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 136 156 break; 137 157 } 138 158 … … static int 147 167 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 148 168 { 149 169 struct sockaddr_storage sin; 150 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);170 enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); 151 171 152 if ( getnameinfo((struct sockaddr *) & sin, sizeof(struct sockaddr_in6), name, nameLength, NULL, 0, flags))172 if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 153 173 return -1; 154 174 155 175 return 0; … … enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng 168 188 } 169 189 170 190 int 171 enet_socket_bind (ENetSocket socket, const ENetAddress * address )191 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 172 192 { 173 193 struct sockaddr_storage sin; 174 194 175 195 if (address != NULL) 176 196 { 177 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);197 enet_address_set_sin((struct sockaddr *) & sin, address, family); 178 198 } 179 199 else 180 200 { 181 201 ENetAddress address_ = { ENET_HOST_ANY, 0, 0 }; 182 enet_address_set_sin((struct sockaddr *) & sin, & address_, AF_INET6);202 enet_address_set_sin((struct sockaddr *) & sin, & address_, family); 183 203 } 184 204 185 return bind (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));205 return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)); 186 206 } 187 207 188 208 int … … enet_socket_listen (ENetSocket socket, int backlog) 192 212 } 193 213 194 214 ENetSocket 195 enet_socket_create (ENetSocketType type )215 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 196 216 { 197 ENetSocket sock = socket ( AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);217 ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 198 218 199 219 #ifdef IPV6_V6ONLY 200 int value = 0; 201 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 220 if (family == ENET_IPV6) 221 { 222 int value = 0; 223 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 224 } 202 225 #endif // IPV6_V6ONLY 203 226 204 227 return sock; … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 241 264 } 242 265 243 266 int 244 enet_socket_connect (ENetSocket socket, const ENetAddress * address )267 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 245 268 { 246 269 struct sockaddr_storage sin; 247 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);270 enet_address_set_sin((struct sockaddr *) & sin, address, family); 248 271 249 return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));272 return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)); 250 273 } 251 274 252 275 ENetSocket 253 enet_socket_accept (ENetSocket socket, ENetAddress * address )276 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 254 277 { 255 278 int result; 256 279 struct sockaddr_storage sin; 257 socklen_t sinLength = sizeof (struct sockaddr_in6);280 socklen_t sinLength = enet_sa_size (family); 258 281 259 282 result = accept (socket, 260 283 address != NULL ? (struct sockaddr *) & sin : NULL, 261 284 address != NULL ? & sinLength : NULL); 262 285 263 286 if (result == -1) 264 287 return ENET_SOCKET_NULL; 265 288 … … int 281 304 enet_socket_send (ENetSocket socket, 282 305 const ENetAddress * address, 283 306 const ENetBuffer * buffers, 284 size_t bufferCount) 307 size_t bufferCount, 308 ENetAddressFamily family) 285 309 { 286 310 struct msghdr msgHdr; 287 311 struct sockaddr_storage sin; … … enet_socket_send (ENetSocket socket, 291 315 292 316 if (address != NULL) 293 317 { 294 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);318 enet_address_set_sin((struct sockaddr *) & sin, address, family); 295 319 msgHdr.msg_name = & sin; 296 msgHdr.msg_namelen = sizeof (struct sockaddr_in6);320 msgHdr.msg_namelen = enet_sa_size (family); 297 321 } 298 322 299 323 msgHdr.msg_iov = (struct iovec *) buffers; … … int 316 340 enet_socket_receive (ENetSocket socket, 317 341 ENetAddress * address, 318 342 ENetBuffer * buffers, 319 size_t bufferCount) 343 size_t bufferCount, 344 ENetAddressFamily family) 320 345 { 321 346 struct msghdr msgHdr; 322 347 struct sockaddr_storage sin; … … enet_socket_receive (ENetSocket socket, 327 352 if (address != NULL) 328 353 { 329 354 msgHdr.msg_name = & sin; 330 msgHdr.msg_namelen = sizeof (struct sockaddr_in6);355 msgHdr.msg_namelen = enet_sa_size (family); 331 356 } 332 357 333 358 msgHdr.msg_iov = (struct iovec *) buffers;
Note: See TracBrowser
for help on using the repository browser.