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