source:
code/branches/ipv6/src/external/enet/patches/0003-using-address-family-in-functions.patch
@
7392
Last change on this file since 7392 was 7390, checked in by adrfried, 14 years ago | |
---|---|
File size: 11.8 KB |
-
host.c
From b26e4ef3fa9c0139fc2ec086d2243b809365d683 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/5] using address family in functions --- host.c | 4 +- include/enet/enet.h | 22 +++++++++--- protocol.c | 5 ++- unix.c | 86 ++++++++++++++++++++++++++++++++------------------ 4 files changed, 76 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..7f5876f 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 << 0), 91 ENET_IPV6 = (1 << 1) 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..475c6e3 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 static ENetAddressFamily 75 95 enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 76 96 { 77 97 memset (address, 0, sizeof (ENetAddress)); … … enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) 80 100 address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); 81 101 //address -> scopeID = 0; 82 102 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); 83 return 0;103 return ENET_IPV4; 84 104 } 85 105 if (sin -> sa_family == AF_INET6) 86 106 { 87 107 address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; 88 108 address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; 89 109 address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); 90 return 0;110 return ENET_IPV6; 91 111 } 92 return -1;112 return ENET_NO_ADDRESS_FAMILY; 93 113 } 94 114 95 115 static int 96 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, sa_family_tfamily)116 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 97 117 { 98 if (family == AF_INET) 118 memset (sin, 0, enet_sa_size(family)); 119 if (family == ENET_IPV4) 99 120 { 100 memset (sin, 0, sizeof (struct sockaddr_in));101 121 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 102 122 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; 103 123 ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 104 124 return 0; 105 125 } 106 else if (family == AF_INET6)126 else if (family == ENET_IPV6) 107 127 { 108 memset (sin, 0, sizeof (struct sockaddr_in6));109 128 ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; 110 129 ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; 111 130 ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; … … enet_address_set_host (ENetAddress * address, const char * name) 132 151 133 152 for (res = result; res != NULL; res = res -> ai_next) 134 153 { 135 if ( !enet_address_set_address(address, res -> ai_addr))154 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 136 155 break; 137 156 } 138 157 … … static int 147 166 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 148 167 { 149 168 struct sockaddr_storage sin; 150 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);169 enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); 151 170 152 if ( getnameinfo((struct sockaddr *) & sin, sizeof(struct sockaddr_in6), name, nameLength, NULL, 0, flags))171 if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 153 172 return -1; 154 173 155 174 return 0; … … enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng 168 187 } 169 188 170 189 int 171 enet_socket_bind (ENetSocket socket, const ENetAddress * address )190 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 172 191 { 173 192 struct sockaddr_storage sin; 174 193 175 194 if (address != NULL) 176 195 { 177 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);196 enet_address_set_sin((struct sockaddr *) & sin, address, family); 178 197 } 179 198 else 180 199 { 181 200 ENetAddress address_ = { ENET_HOST_ANY, 0, 0 }; 182 enet_address_set_sin((struct sockaddr *) & sin, & address_, AF_INET6);201 enet_address_set_sin((struct sockaddr *) & sin, & address_, family); 183 202 } 184 203 185 return bind (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));204 return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)); 186 205 } 187 206 188 207 int … … enet_socket_listen (ENetSocket socket, int backlog) 192 211 } 193 212 194 213 ENetSocket 195 enet_socket_create (ENetSocketType type )214 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 196 215 { 197 ENetSocket sock = socket ( AF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);216 ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 198 217 199 218 #ifdef IPV6_V6ONLY 200 int value = 0; 201 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 219 if (family == ENET_IPV6) 220 { 221 int value = 0; 222 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 223 } 202 224 #endif // IPV6_V6ONLY 203 225 204 226 return sock; … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 241 263 } 242 264 243 265 int 244 enet_socket_connect (ENetSocket socket, const ENetAddress * address )266 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 245 267 { 246 268 struct sockaddr_storage sin; 247 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);269 enet_address_set_sin((struct sockaddr *) & sin, address, family); 248 270 249 return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));271 return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)); 250 272 } 251 273 252 274 ENetSocket 253 enet_socket_accept (ENetSocket socket, ENetAddress * address )275 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 254 276 { 255 277 int result; 256 278 struct sockaddr_storage sin; 257 socklen_t sinLength = sizeof (struct sockaddr_in6);279 socklen_t sinLength = enet_sa_size (family); 258 280 259 281 result = accept (socket, 260 282 address != NULL ? (struct sockaddr *) & sin : NULL, 261 283 address != NULL ? & sinLength : NULL); 262 284 263 285 if (result == -1) 264 286 return ENET_SOCKET_NULL; 265 287 … … int 281 303 enet_socket_send (ENetSocket socket, 282 304 const ENetAddress * address, 283 305 const ENetBuffer * buffers, 284 size_t bufferCount) 306 size_t bufferCount, 307 ENetAddressFamily family) 285 308 { 286 309 struct msghdr msgHdr; 287 310 struct sockaddr_storage sin; … … enet_socket_send (ENetSocket socket, 291 314 292 315 if (address != NULL) 293 316 { 294 enet_address_set_sin((struct sockaddr *) & sin, address, AF_INET6);317 enet_address_set_sin((struct sockaddr *) & sin, address, family); 295 318 msgHdr.msg_name = & sin; 296 msgHdr.msg_namelen = sizeof (struct sockaddr_in6);319 msgHdr.msg_namelen = enet_sa_size (family); 297 320 } 298 321 299 322 msgHdr.msg_iov = (struct iovec *) buffers; … … int 316 339 enet_socket_receive (ENetSocket socket, 317 340 ENetAddress * address, 318 341 ENetBuffer * buffers, 319 size_t bufferCount) 342 size_t bufferCount, 343 ENetAddressFamily family) 320 344 { 321 345 struct msghdr msgHdr; 322 346 struct sockaddr_storage sin; … … enet_socket_receive (ENetSocket socket, 327 351 if (address != NULL) 328 352 { 329 353 msgHdr.msg_name = & sin; 330 msgHdr.msg_namelen = sizeof (struct sockaddr_in6);354 msgHdr.msg_namelen = enet_sa_size (family); 331 355 } 332 356 333 357 msgHdr.msg_iov = (struct iovec *) buffers;
Note: See TracBrowser
for help on using the repository browser.