[7328] | 1 | /** |
---|
| 2 | @file win32.c |
---|
| 3 | @brief ENet Win32 system specific functions |
---|
| 4 | */ |
---|
| 5 | #ifdef WIN32 |
---|
| 6 | |
---|
[7438] | 7 | #define _WIN32_WINNT 0x0501 |
---|
| 8 | |
---|
[7328] | 9 | #include <time.h> |
---|
[7954] | 10 | #include <ws2tcpip.h> |
---|
[7328] | 11 | #define ENET_BUILDING_LIB 1 |
---|
| 12 | #include "enet/enet.h" |
---|
[8079] | 13 | #include <ws2tcpip.h> |
---|
[7328] | 14 | |
---|
| 15 | static enet_uint32 timeBase = 0; |
---|
| 16 | |
---|
| 17 | int |
---|
| 18 | enet_initialize (void) |
---|
| 19 | { |
---|
[7394] | 20 | WORD versionRequested = MAKEWORD (2, 2); |
---|
[7328] | 21 | WSADATA wsaData; |
---|
| 22 | |
---|
| 23 | if (WSAStartup (versionRequested, & wsaData)) |
---|
| 24 | return -1; |
---|
| 25 | |
---|
[7394] | 26 | if (LOBYTE (wsaData.wVersion) != 2|| |
---|
| 27 | HIBYTE (wsaData.wVersion) != 2) |
---|
[7328] | 28 | { |
---|
| 29 | WSACleanup (); |
---|
| 30 | |
---|
| 31 | return -1; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | timeBeginPeriod (1); |
---|
| 35 | |
---|
| 36 | return 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void |
---|
| 40 | enet_deinitialize (void) |
---|
| 41 | { |
---|
| 42 | timeEndPeriod (1); |
---|
| 43 | |
---|
| 44 | WSACleanup (); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | enet_uint32 |
---|
| 48 | enet_time_get (void) |
---|
| 49 | { |
---|
| 50 | return (enet_uint32) timeGetTime () - timeBase; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void |
---|
| 54 | enet_time_set (enet_uint32 newTimeBase) |
---|
| 55 | { |
---|
| 56 | timeBase = (enet_uint32) timeGetTime () - newTimeBase; |
---|
| 57 | } |
---|
| 58 | |
---|
[7390] | 59 | static enet_uint16 |
---|
| 60 | enet_af (ENetAddressFamily family) |
---|
| 61 | { |
---|
| 62 | if (family == ENET_IPV4) |
---|
| 63 | return AF_INET; |
---|
| 64 | if (family == ENET_IPV6) |
---|
| 65 | return AF_INET6; |
---|
| 66 | return 0; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | static socklen_t |
---|
| 70 | enet_sa_size (ENetAddressFamily family) |
---|
| 71 | { |
---|
| 72 | if (family == ENET_IPV4) |
---|
[7433] | 73 | return sizeof (struct sockaddr_in); |
---|
[7390] | 74 | if (family == ENET_IPV6) |
---|
[7433] | 75 | return sizeof (struct sockaddr_in6); |
---|
[7390] | 76 | return 0; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | static ENetAddressFamily |
---|
[7433] | 80 | enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) |
---|
[7390] | 81 | { |
---|
| 82 | memset (address, 0, sizeof (ENetAddress)); |
---|
| 83 | if (sin -> sa_family == AF_INET) |
---|
| 84 | { |
---|
[7433] | 85 | address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); |
---|
[7392] | 86 | /* address -> scopeID = 0; */ |
---|
[7433] | 87 | address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); |
---|
[7390] | 88 | return ENET_IPV4; |
---|
| 89 | } |
---|
| 90 | if (sin -> sa_family == AF_INET6) |
---|
| 91 | { |
---|
[7433] | 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); |
---|
[7390] | 95 | return ENET_IPV6; |
---|
| 96 | } |
---|
| 97 | return ENET_NO_ADDRESS_FAMILY; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static int |
---|
[7433] | 101 | enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) |
---|
[7390] | 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 | { |
---|
[7433] | 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); |
---|
[7390] | 111 | return 0; |
---|
| 112 | } |
---|
| 113 | else if (family == ENET_IPV6) |
---|
| 114 | { |
---|
[7433] | 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); |
---|
[7390] | 119 | return 0; |
---|
| 120 | } |
---|
| 121 | return -1; |
---|
| 122 | } |
---|
| 123 | |
---|
[7328] | 124 | int |
---|
| 125 | enet_address_set_host (ENetAddress * address, const char * name) |
---|
| 126 | { |
---|
[7390] | 127 | enet_uint16 port = address -> port; |
---|
[7433] | 128 | struct addrinfo hints; |
---|
| 129 | struct addrinfo * result; |
---|
| 130 | struct addrinfo * res; |
---|
[7328] | 131 | |
---|
[7438] | 132 | memset(& hints, 0, sizeof (hints)); |
---|
| 133 | hints.ai_flags = AI_ADDRCONFIG; |
---|
[7390] | 134 | hints.ai_family = AF_UNSPEC; |
---|
| 135 | |
---|
| 136 | if ( getaddrinfo(name, NULL, &hints, &result) ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | for (res = result; res != NULL; res = res -> ai_next) |
---|
[7328] | 140 | { |
---|
[7390] | 141 | if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) |
---|
| 142 | break; |
---|
[7328] | 143 | } |
---|
| 144 | |
---|
[7390] | 145 | address -> port = port; |
---|
| 146 | freeaddrinfo(result); |
---|
| 147 | if (res == NULL) return -1; |
---|
[7328] | 148 | |
---|
| 149 | return 0; |
---|
| 150 | } |
---|
| 151 | |
---|
[7390] | 152 | static int |
---|
| 153 | enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) |
---|
[7328] | 154 | { |
---|
[7433] | 155 | struct sockaddr_storage sin; |
---|
| 156 | enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); |
---|
[7390] | 157 | |
---|
[7433] | 158 | if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) |
---|
[7328] | 159 | return -1; |
---|
[7390] | 160 | |
---|
[7328] | 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | int |
---|
[7390] | 165 | enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) |
---|
| 166 | { |
---|
| 167 | return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | int |
---|
[7328] | 171 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
---|
| 172 | { |
---|
[7390] | 173 | return enet_address_get_host_x(address, name, nameLength, 0); |
---|
[7328] | 174 | } |
---|
| 175 | |
---|
| 176 | int |
---|
[7390] | 177 | enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 178 | { |
---|
[7433] | 179 | struct sockaddr_storage sin; |
---|
[7328] | 180 | |
---|
| 181 | if (address != NULL) |
---|
| 182 | { |
---|
[7433] | 183 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
[7397] | 187 | ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; |
---|
[7433] | 188 | enet_address_set_sin((struct sockaddr *) & sin, & address_, family); |
---|
[7328] | 189 | } |
---|
| 190 | |
---|
[7433] | 191 | return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 192 | } |
---|
| 193 | |
---|
| 194 | int |
---|
| 195 | enet_socket_listen (ENetSocket socket, int backlog) |
---|
| 196 | { |
---|
| 197 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | ENetSocket |
---|
[7390] | 201 | enet_socket_create (ENetSocketType type, ENetAddressFamily family) |
---|
[7328] | 202 | { |
---|
[7433] | 203 | ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); |
---|
| 204 | return sock; |
---|
[7328] | 205 | } |
---|
| 206 | |
---|
| 207 | int |
---|
| 208 | enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) |
---|
| 209 | { |
---|
| 210 | int result = SOCKET_ERROR; |
---|
| 211 | switch (option) |
---|
| 212 | { |
---|
| 213 | case ENET_SOCKOPT_NONBLOCK: |
---|
| 214 | { |
---|
| 215 | u_long nonBlocking = (u_long) value; |
---|
| 216 | result = ioctlsocket (socket, FIONBIO, & nonBlocking); |
---|
| 217 | break; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | case ENET_SOCKOPT_BROADCAST: |
---|
| 221 | result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); |
---|
| 222 | break; |
---|
| 223 | |
---|
| 224 | case ENET_SOCKOPT_REUSEADDR: |
---|
| 225 | result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int)); |
---|
| 226 | break; |
---|
| 227 | |
---|
| 228 | case ENET_SOCKOPT_RCVBUF: |
---|
| 229 | result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); |
---|
| 230 | break; |
---|
| 231 | |
---|
| 232 | case ENET_SOCKOPT_SNDBUF: |
---|
| 233 | result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); |
---|
| 234 | break; |
---|
| 235 | |
---|
| 236 | default: |
---|
| 237 | break; |
---|
| 238 | } |
---|
| 239 | return result == SOCKET_ERROR ? -1 : 0; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | int |
---|
[7390] | 243 | enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 244 | { |
---|
[7433] | 245 | struct sockaddr_storage sin; |
---|
| 246 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 247 | |
---|
[7433] | 248 | return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 249 | } |
---|
| 250 | |
---|
| 251 | ENetSocket |
---|
[7390] | 252 | enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 253 | { |
---|
| 254 | SOCKET result; |
---|
[7433] | 255 | struct sockaddr_storage sin; |
---|
| 256 | socklen_t sinLength = enet_sa_size (family); |
---|
[7328] | 257 | |
---|
| 258 | result = accept (socket, |
---|
[7433] | 259 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
[7328] | 260 | address != NULL ? & sinLength : NULL); |
---|
| 261 | |
---|
| 262 | if (result == INVALID_SOCKET) |
---|
| 263 | return ENET_SOCKET_NULL; |
---|
| 264 | |
---|
| 265 | if (address != NULL) |
---|
| 266 | { |
---|
[7433] | 267 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
[7328] | 268 | } |
---|
| 269 | |
---|
| 270 | return result; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | void |
---|
| 274 | enet_socket_destroy (ENetSocket socket) |
---|
| 275 | { |
---|
| 276 | closesocket (socket); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | int |
---|
| 280 | enet_socket_send (ENetSocket socket, |
---|
| 281 | const ENetAddress * address, |
---|
| 282 | const ENetBuffer * buffers, |
---|
[7390] | 283 | size_t bufferCount, |
---|
| 284 | ENetAddressFamily family) |
---|
[7328] | 285 | { |
---|
[7433] | 286 | struct sockaddr_storage sin; |
---|
[7328] | 287 | DWORD sentLength; |
---|
| 288 | |
---|
| 289 | if (address != NULL) |
---|
| 290 | { |
---|
[7433] | 291 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 292 | } |
---|
| 293 | |
---|
| 294 | if (WSASendTo (socket, |
---|
| 295 | (LPWSABUF) buffers, |
---|
| 296 | (DWORD) bufferCount, |
---|
| 297 | & sentLength, |
---|
| 298 | 0, |
---|
[7433] | 299 | address != NULL ? (struct sockaddr *) & sin : 0, |
---|
[7390] | 300 | address != NULL ? enet_sa_size (family) : 0, |
---|
[7328] | 301 | NULL, |
---|
| 302 | NULL) == SOCKET_ERROR) |
---|
| 303 | { |
---|
| 304 | if (WSAGetLastError () == WSAEWOULDBLOCK) |
---|
| 305 | return 0; |
---|
| 306 | |
---|
| 307 | return -1; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | return (int) sentLength; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | int |
---|
| 314 | enet_socket_receive (ENetSocket socket, |
---|
| 315 | ENetAddress * address, |
---|
| 316 | ENetBuffer * buffers, |
---|
[7390] | 317 | size_t bufferCount, |
---|
| 318 | ENetAddressFamily family) |
---|
[7328] | 319 | { |
---|
[7390] | 320 | INT sinLength = enet_sa_size (family); |
---|
[7328] | 321 | DWORD flags = 0, |
---|
| 322 | recvLength; |
---|
[7433] | 323 | struct sockaddr_storage sin; |
---|
[7328] | 324 | |
---|
| 325 | if (WSARecvFrom (socket, |
---|
| 326 | (LPWSABUF) buffers, |
---|
| 327 | (DWORD) bufferCount, |
---|
| 328 | & recvLength, |
---|
| 329 | & flags, |
---|
[7433] | 330 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
[7328] | 331 | address != NULL ? & sinLength : NULL, |
---|
| 332 | NULL, |
---|
| 333 | NULL) == SOCKET_ERROR) |
---|
| 334 | { |
---|
| 335 | switch (WSAGetLastError ()) |
---|
| 336 | { |
---|
| 337 | case WSAEWOULDBLOCK: |
---|
| 338 | case WSAECONNRESET: |
---|
| 339 | return 0; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | return -1; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if (flags & MSG_PARTIAL) |
---|
| 346 | return -1; |
---|
| 347 | |
---|
| 348 | if (address != NULL) |
---|
| 349 | { |
---|
[7433] | 350 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
[7328] | 351 | } |
---|
| 352 | |
---|
| 353 | return (int) recvLength; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | int |
---|
| 357 | enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout) |
---|
| 358 | { |
---|
| 359 | struct timeval timeVal; |
---|
| 360 | |
---|
| 361 | timeVal.tv_sec = timeout / 1000; |
---|
| 362 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
| 363 | |
---|
| 364 | return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | int |
---|
[7390] | 368 | enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) |
---|
[7328] | 369 | { |
---|
| 370 | fd_set readSet, writeSet; |
---|
| 371 | struct timeval timeVal; |
---|
| 372 | int selectCount; |
---|
[7391] | 373 | ENetSocket maxSocket; |
---|
[7390] | 374 | |
---|
[7328] | 375 | timeVal.tv_sec = timeout / 1000; |
---|
| 376 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
[7390] | 377 | |
---|
[7328] | 378 | FD_ZERO (& readSet); |
---|
| 379 | FD_ZERO (& writeSet); |
---|
| 380 | |
---|
| 381 | if (* condition & ENET_SOCKET_WAIT_SEND) |
---|
[7390] | 382 | { |
---|
| 383 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 384 | FD_SET (socket4, & writeSet); |
---|
| 385 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 386 | FD_SET (socket6, & writeSet); |
---|
| 387 | } |
---|
[7328] | 388 | |
---|
| 389 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) |
---|
[7390] | 390 | { |
---|
| 391 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 392 | FD_SET (socket4, & readSet); |
---|
| 393 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 394 | FD_SET (socket6, & readSet); |
---|
| 395 | } |
---|
[7328] | 396 | |
---|
[7391] | 397 | maxSocket = 0; |
---|
[7390] | 398 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 399 | maxSocket = socket4; |
---|
| 400 | if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) |
---|
| 401 | maxSocket = socket6; |
---|
[7328] | 402 | |
---|
[7390] | 403 | selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); |
---|
| 404 | |
---|
[7328] | 405 | if (selectCount < 0) |
---|
| 406 | return -1; |
---|
| 407 | |
---|
| 408 | * condition = ENET_SOCKET_WAIT_NONE; |
---|
| 409 | |
---|
| 410 | if (selectCount == 0) |
---|
| 411 | return 0; |
---|
| 412 | |
---|
[7390] | 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; |
---|
[7328] | 416 | |
---|
[7390] | 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; |
---|
| 420 | |
---|
[7328] | 421 | return 0; |
---|
[7390] | 422 | } |
---|
[7328] | 423 | |
---|
| 424 | #endif |
---|
| 425 | |
---|