source:
code/branches/ipv6/src/external/enet/patches/0005-IPv6-for-Windows.patch
@
7397
Last change on this file since 7397 was 7390, checked in by adrfried, 14 years ago | |
---|---|
File size: 13.5 KB |
-
include/enet/enet.h
From 93248e1cd75da47980e0d69428747ff6471be1ed Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Thu, 9 Sep 2010 16:02:21 +0200 Subject: [PATCH 5/5] IPv6 for Windows --- include/enet/enet.h | 6 +- include/enet/win32.h | 1 + win32.c | 237 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 160 insertions(+), 84 deletions(-) diff --git a/include/enet/enet.h b/include/enet/enet.h index 616fe7f..54e3b3b 100644
a b extern const ENetHostAddress ENET_HOST_BROADCAST; /**< specifies a IPv4 subne 77 77 typedef struct _ENetAddress 78 78 { 79 79 ENetHostAddress host; 80 enet_uint32 scopeID; //FIXME: this is of different size on Windows 80 #ifdef WIN32 81 u_long scopeID; 82 #else 83 uint32_t scopeID; 84 #endif 81 85 enet_uint16 port; 82 86 } ENetAddress; 83 87 -
include/enet/win32.h
diff --git a/include/enet/win32.h b/include/enet/win32.h index 0e1cf0c..9f3f6e5 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 -
win32.c
diff --git a/win32.c b/win32.c index e1fae23..dbbe85a 100644
a b enet_time_set (enet_uint32 newTimeBase) 52 52 timeBase = (enet_uint32) timeGetTime () - newTimeBase; 53 53 } 54 54 55 int 56 enet_a ddress_set_host (ENetAddress * address, const char * name)55 static enet_uint16 56 enet_af (ENetAddressFamily family) 57 57 { 58 struct hostent * hostEntry; 58 if (family == ENET_IPV4) 59 return AF_INET; 60 if (family == ENET_IPV6) 61 return AF_INET6; 62 return 0; 63 } 64 65 static socklen_t 66 enet_sa_size (ENetAddressFamily family) 67 { 68 if (family == ENET_IPV4) 69 return sizeof (SOCKADDR_IN); 70 if (family == ENET_IPV6) 71 return sizeof (SOCKADDR_IN6); 72 return 0; 73 } 59 74 60 hostEntry = gethostbyname (name); 61 if (hostEntry == NULL || 62 hostEntry -> h_addrtype != AF_INET) 75 static ENetAddressFamily 76 enet_address_set_address (ENetAddress * address, const SOCKADDR * sin) 77 { 78 memset (address, 0, sizeof (ENetAddress)); 79 if (sin -> sa_family == AF_INET) 63 80 { 64 unsigned long host = inet_addr (name); 65 if (host == INADDR_NONE) 66 return -1; 67 address -> host = host; 68 return 0; 81 address -> host = enet_address_map4 ((((SOCKADDR_IN *) sin) -> sin_addr.s_addr)); 82 //address -> scopeID = 0; 83 address -> port = ENET_NET_TO_HOST_16 (((SOCKADDR_IN *) sin) -> sin_port); 84 return ENET_IPV4; 69 85 } 86 if (sin -> sa_family == AF_INET6) 87 { 88 address -> host = * (ENetHostAddress *) & ((SOCKADDR_IN6 *) sin) -> sin6_addr; 89 address -> scopeID = ((SOCKADDR_IN6 *) sin) -> sin6_scope_id; 90 address -> port = ENET_NET_TO_HOST_16 (((SOCKADDR_IN6 *) sin) -> sin6_port); 91 return ENET_IPV6; 92 } 93 return ENET_NO_ADDRESS_FAMILY; 94 } 70 95 71 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 72 73 return 0; 96 static int 97 enet_address_set_sin (SOCKADDR * sin, const ENetAddress * address, ENetAddressFamily family) 98 { 99 memset (sin, 0, enet_sa_size(family)); 100 if (family == ENET_IPV4 && 101 (enet_get_address_family (address) == ENET_IPV4 || 102 !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 103 { 104 ((SOCKADDR_IN *) sin) -> sin_family = AF_INET; 105 ((SOCKADDR_IN *) sin) -> sin_addr = * (IN_ADDR *) & address -> host.addr[12]; 106 ((SOCKADDR_IN *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); 107 return 0; 108 } 109 else if (family == ENET_IPV6) 110 { 111 ((SOCKADDR_IN6 *) sin) -> sin6_family = AF_INET6; 112 ((SOCKADDR_IN6 *) sin) -> sin6_addr = * (IN6_ADDR *) & address -> host; 113 ((SOCKADDR_IN6 *) sin) -> sin6_scope_id = address -> scopeID; 114 ((SOCKADDR_IN6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); 115 return 0; 116 } 117 return -1; 74 118 } 75 119 76 120 int 77 enet_address_ get_host_ip (const ENetAddress * address, char * name, size_t nameLength)121 enet_address_set_host (ENetAddress * address, const char * name) 78 122 { 79 char * addr = inet_ntoa (* (struct in_addr *) & address -> host); 80 if (addr == NULL) 123 enet_uint16 port = address -> port; 124 ADDRINFO hints; 125 ADDRINFO * result; 126 ADDRINFO * res; 127 128 memset(& hints, 0, sizeof (hints)); 129 hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG; 130 hints.ai_family = AF_UNSPEC; 131 132 if ( getaddrinfo(name, NULL, &hints, &result) ) 81 133 return -1; 82 strncpy (name, addr, nameLength); 134 135 for (res = result; res != NULL; res = res -> ai_next) 136 { 137 if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) 138 break; 139 } 140 141 address -> port = port; 142 freeaddrinfo(result); 143 if (res == NULL) return -1; 144 83 145 return 0; 84 146 } 85 147 86 int87 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)148 static int 149 enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) 88 150 { 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); 151 SOCKADDR_STORAGE sin; 152 enet_address_set_sin((SOCKADDR *) & sin, address, ENET_IPV6); 97 153 98 strncpy (name, hostEntry -> h_name, nameLength); 154 if ( getnameinfo((SOCKADDR *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) 155 return -1; 99 156 100 157 return 0; 101 158 } 102 159 103 160 int 104 enet_ socket_bind (ENetSocket socket, const ENetAddress * address)161 enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 105 162 { 106 struct sockaddr_in sin; 163 return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); 164 } 107 165 108 memset (& sin, 0, sizeof (struct sockaddr_in)); 166 int 167 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 168 { 169 return enet_address_get_host_x(address, name, nameLength, 0); 170 } 109 171 110 sin.sin_family = AF_INET; 172 int 173 enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 174 { 175 SOCKADDR_STORAGE sin; 111 176 112 177 if (address != NULL) 113 178 { 114 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 115 sin.sin_addr.s_addr = address -> host; 179 enet_address_set_sin((SOCKADDR *) & sin, address, family); 116 180 } 117 181 else 118 182 { 119 sin.sin_port = 0;120 sin.sin_addr.s_addr = INADDR_ANY;183 ENetAddress address_ = { ENET_HOST_ANY, 0, 0 }; 184 enet_address_set_sin((SOCKADDR *) & sin, & address_, family); 121 185 } 122 186 123 return bind (socket, 124 (struct sockaddr *) & sin, 125 sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; 187 return bind (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; 126 188 } 127 189 128 190 int … … enet_socket_listen (ENetSocket socket, int backlog) 132 194 } 133 195 134 196 ENetSocket 135 enet_socket_create (ENetSocketType type )197 enet_socket_create (ENetSocketType type, ENetAddressFamily family) 136 198 { 137 199 return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 138 200 } … … enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 173 235 } 174 236 175 237 int 176 enet_socket_connect (ENetSocket socket, const ENetAddress * address )238 enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) 177 239 { 178 struct sockaddr_in sin; 179 180 memset (& sin, 0, sizeof (struct sockaddr_in)); 240 SOCKADDR_STORAGE sin; 241 enet_address_set_sin((SOCKADDR *) & sin, address, family); 181 242 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; 243 return connect (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; 187 244 } 188 245 189 246 ENetSocket 190 enet_socket_accept (ENetSocket socket, ENetAddress * address )247 enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) 191 248 { 192 249 SOCKET result; 193 struct sockaddr_insin;194 int sinLength = sizeof (struct sockaddr_in);250 SOCKADDR_STORAGE sin; 251 int sinLength = enet_sa_size (family); 195 252 196 253 result = accept (socket, 197 address != NULL ? ( struct sockaddr *) & sin : NULL,254 address != NULL ? (SOCKADDR *) & sin : NULL, 198 255 address != NULL ? & sinLength : NULL); 199 256 200 257 if (result == INVALID_SOCKET) … … enet_socket_accept (ENetSocket socket, ENetAddress * address) 202 259 203 260 if (address != NULL) 204 261 { 205 address -> host = (enet_uint32) sin.sin_addr.s_addr; 206 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 262 enet_address_set_address(address, (SOCKADDR *) & sin); 207 263 } 208 264 209 265 return result; … … int 219 275 enet_socket_send (ENetSocket socket, 220 276 const ENetAddress * address, 221 277 const ENetBuffer * buffers, 222 size_t bufferCount) 278 size_t bufferCount, 279 ENetAddressFamily family) 223 280 { 224 struct sockaddr_insin;281 SOCKADDR_STORAGE sin; 225 282 DWORD sentLength; 226 283 227 284 if (address != NULL) 228 285 { 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; 286 enet_address_set_sin((SOCKADDR *) & sin, address, family); 234 287 } 235 288 236 289 if (WSASendTo (socket, … … enet_socket_send (ENetSocket socket, 238 291 (DWORD) bufferCount, 239 292 & sentLength, 240 293 0, 241 address != NULL ? ( struct sockaddr*) & sin : 0,242 address != NULL ? sizeof (struct sockaddr_in) : 0,294 address != NULL ? (SOCKADDR *) & sin : 0, 295 address != NULL ? enet_sa_size (family) : 0, 243 296 NULL, 244 297 NULL) == SOCKET_ERROR) 245 298 { … … int 256 309 enet_socket_receive (ENetSocket socket, 257 310 ENetAddress * address, 258 311 ENetBuffer * buffers, 259 size_t bufferCount) 312 size_t bufferCount, 313 ENetAddressFamily family) 260 314 { 261 INT sinLength = sizeof (struct sockaddr_in);315 INT sinLength = enet_sa_size (family); 262 316 DWORD flags = 0, 263 317 recvLength; 264 struct sockaddr_insin;318 SOCKADDR_STORAGE sin; 265 319 266 320 if (WSARecvFrom (socket, 267 321 (LPWSABUF) buffers, 268 322 (DWORD) bufferCount, 269 323 & recvLength, 270 324 & flags, 271 address != NULL ? ( struct sockaddr*) & sin : NULL,325 address != NULL ? (SOCKADDR *) & sin : NULL, 272 326 address != NULL ? & sinLength : NULL, 273 327 NULL, 274 328 NULL) == SOCKET_ERROR) … … enet_socket_receive (ENetSocket socket, 288 342 289 343 if (address != NULL) 290 344 { 291 address -> host = (enet_uint32) sin.sin_addr.s_addr; 292 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 345 enet_address_set_address(address, (SOCKADDR *) & sin); 293 346 } 294 347 295 348 return (int) recvLength; … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 307 360 } 308 361 309 362 int 310 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)363 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 311 364 { 312 365 fd_set readSet, writeSet; 313 366 struct timeval timeVal; 314 367 int selectCount; 315 368 316 369 timeVal.tv_sec = timeout / 1000; 317 370 timeVal.tv_usec = (timeout % 1000) * 1000; 318 371 319 372 FD_ZERO (& readSet); 320 373 FD_ZERO (& writeSet); 321 374 322 375 if (* condition & ENET_SOCKET_WAIT_SEND) 323 FD_SET (socket, & writeSet); 376 { 377 if (socket4 != ENET_SOCKET_NULL) 378 FD_SET (socket4, & writeSet); 379 if (socket6 != ENET_SOCKET_NULL) 380 FD_SET (socket6, & writeSet); 381 } 324 382 325 383 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 326 FD_SET (socket, & readSet); 384 { 385 if (socket4 != ENET_SOCKET_NULL) 386 FD_SET (socket4, & readSet); 387 if (socket6 != ENET_SOCKET_NULL) 388 FD_SET (socket6, & readSet); 389 } 327 390 328 selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal); 391 ENetSocket maxSocket = 0; 392 if (socket4 != ENET_SOCKET_NULL) 393 maxSocket = socket4; 394 if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) 395 maxSocket = socket6; 396 397 selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); 329 398 330 399 if (selectCount < 0) 331 400 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 335 404 if (selectCount == 0) 336 405 return 0; 337 406 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; 407 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || 408 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) 409 * condition |= ENET_SOCKET_WAIT_SEND; 410 411 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || 412 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) 413 * condition |= ENET_SOCKET_WAIT_RECEIVE; 343 414 344 415 return 0; 345 } 416 } 346 417 347 418 #endif 348 419
Note: See TracBrowser
for help on using the repository browser.