2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * posixio.c: POSIX IO emulation layer for WIN32
30 * Known contributors to this file:
31 * Ron Koenderink, 2007
35 * POSIX has just one kind of file descriptors, while Windows has (at
36 * least) two: one for sockets and one for files, with separate
37 * functions to operate on them. To present a more POSIX-like
38 * interface to our application code, we provide a compatibility layer
39 * that maps POSIX file descriptors to sockets and file handles behind
40 * the scenes. This actual mapping is done by the fdmap. It doesn't
41 * implement the finer points of POSIX correctly. In particular, the
42 * actual values of the file descriptors usually differ.
54 * Need to include winsock2.h before ws2tcpip.h.
55 * Use sys/socket.h to ensure the #undef NS_ALL
56 * is not missed after including winsock2.h.
58 #include "sys/socket.h"
67 * FD_SETSIZE is the size for the maximum number of sockets.
68 * The number of file descriptors is in a variable _nhandle
69 * based on the assertion output. In order the simplify the
70 * code and skip dynamic allocation, used double the socket size.
72 #define MAX_FDS (FD_SETSIZE * 2)
78 FDMAP_IO_ANY /* used for searching only (lookup_handle) */
83 enum fdmap_io_type type;
86 static struct fdmap fdmap[MAX_FDS] = {
94 * Allocate a POSIX equivalent file descriptor.
95 * Note once get_fd() is called either free_fd() or set_fd()
96 * must be called before thread mutex is released as the
97 * allocation/deallocation code is not thread safe.
104 for (fd = 0; fd < nfd && fdmap[fd].type != FDMAP_IO_NOTUSED; fd++) ;
110 fdmap[fd].type = FDMAP_IO_NOTUSED;
117 * Deallocate a POSIX equivalent file descriptor.
122 fdmap[fd].type = FDMAP_IO_NOTUSED;
123 for(; fdmap[nfd - 1].type == FDMAP_IO_NOTUSED; nfd--) ;
127 * Complete the allocation of the file descriptor.
130 set_fd(int fd, enum fdmap_io_type type, int handle)
134 fdmap[fd].handle = handle;
135 fdmap[fd].type = type;
138 * Garbage collection for fileno(), currently not
139 * replacing fclose() and fcloseall() so do not know when
140 * a stream is closed.
142 for (i = 0; i < nfd; i++) {
143 if (i != fd && type == fdmap[i].type && handle == fdmap[i].handle)
149 * Find the windows handle (file or socket) for file descriptor.
150 * Return windows handle and type of handle.
151 * You can search for a specific type (FDMAP_IO_FILE or FDMAP_IO_SOCKET)
152 * or for both search by using FDMAP_IO_ANY.
153 * FDMAP_IO_NOTUSED is not valid type to search with.
156 lookup_handle(int fd, enum fdmap_io_type d_type, int error,
157 enum fdmap_io_type *type_ptr, int *handle_ptr)
160 if (fd < 0 || fd >= MAX_FDS) {
164 } else if ((fdmap[fd].type != d_type && d_type != FDMAP_IO_ANY) ||
165 (fdmap[fd].type == FDMAP_IO_NOTUSED && d_type == FDMAP_IO_ANY)) {
170 if (type_ptr != NULL)
171 *type_ptr = fdmap[fd].type;
172 if (handle_ptr != NULL)
173 *handle_ptr = fdmap[fd].handle;
178 * Find and return the file descriptor associated with windows handle.
179 * You can search for FDMAP_IO_FILE or FDMAP_IO_SOCKET.
180 * FDMAP_IO_ANY or FDMAP_IO_NOTUSED is not considered valid search
184 lookup_fd(int handle, enum fdmap_io_type d_type)
188 for (i = 0; i < nfd; i++)
189 if (fdmap[i].handle == handle && fdmap[i].type == d_type)
195 * Get the window socket handle for POSIX file descriptor.
198 posix_fd2socket(int fd)
201 enum fdmap_io_type type;
203 if (!lookup_handle(fd, FDMAP_IO_SOCKET, WSAENOTSOCK,
205 return INVALID_SOCKET;
209 #define SOCKET_FUNCTION(expr) \
213 if (!lookup_handle(fd, FDMAP_IO_SOCKET, \
214 ENOTSOCK, NULL, &handle)) \
218 if (result == SOCKET_ERROR) { \
219 errno = WSAGetLastError(); \
225 * POSIX equivalent for accept().
229 posix_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
235 if (!lookup_handle(fd, FDMAP_IO_SOCKET, ENOTSOCK, NULL, &handle))
242 new_handle = accept(handle, addr, addrlen);
243 if (new_handle == INVALID_SOCKET) {
245 errno = WSAGetLastError();
248 set_fd(new_fd, FDMAP_IO_SOCKET, (int)new_handle);
253 * POSIX equivalent for bind().
257 posix_bind(int fd, const struct sockaddr *name, socklen_t namelen)
259 SOCKET_FUNCTION(bind(handle, name, namelen))
263 * POSIX equivalent for listen().
267 posix_listen(int fd, int backlog)
269 SOCKET_FUNCTION(listen(handle, backlog))
273 * POSIX equivalent for setsockopt().
277 posix_setsockopt(int fd, int level, int optname,
278 const void *optval, socklen_t optlen)
281 * SO_REUSEADDR requests to permit another bind even when the
282 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
283 * broken: it makes bind() succeed no matter what, even if
284 * there's another server running on the same port. Luckily,
285 * bind() seems to be broken as well: it seems to succeed while
286 * the port is in state TIME_WAIT by default; thus we get the
287 * behavior we want by not setting SO_REUSEADDR.
289 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
292 SOCKET_FUNCTION(setsockopt(handle, level, optname,
298 * POSIX equivalent for shutdown().
302 posix_shutdown(int fd, int how)
304 SOCKET_FUNCTION(shutdown(handle, how))
308 * POSIX equivalent for socket().
312 posix_socket(int domain, int type, int protocol)
317 if ((new_fd = get_fd()) < 0)
320 handle = socket(domain, type, protocol);
321 if (handle == INVALID_SOCKET) {
323 errno = WSAGetLastError();
326 set_fd(new_fd, FDMAP_IO_SOCKET, (int)handle);
330 #ifdef HAVE_GETADDRINFO
332 inet_ntop(int af, const void *src, char *dst, socklen_t len)
335 struct sockaddr_in sin;
336 struct sockaddr_in6 sin6;
340 memset(&sin, 0, sizeof(sin));
342 memcpy(&sin.sin_addr, src, sizeof(sin.sin_addr));
343 sa = (struct sockaddr *)&sin;
345 } else if (af == AF_INET6) {
346 memset(&sin6, 0, sizeof(sin6));
347 sin6.sin6_family = af;
348 memcpy(&sin6.sin6_addr, src, sizeof(sin6.sin6_addr));
349 sa = (struct sockaddr *)&sin6;
350 salen = sizeof(sin6);
352 errno = EAFNOSUPPORT;
356 if (getnameinfo(sa, salen, dst, len, NULL, 0, NI_NUMERICHOST)) {
357 errno = EAFNOSUPPORT;
365 #define FILE_FUNCTION(type, expr) \
368 if (!lookup_handle(fd, (type), EBADF, NULL, &handle)) \
374 * POSIX equivalent for close().
381 enum fdmap_io_type type;
383 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
388 case FDMAP_IO_SOCKET:
389 result = closesocket(handle);
390 if (result == SOCKET_ERROR) {
391 errno = WSAGetLastError();
396 return _close(handle);
404 * posix_fsync forces file sync with the disk.
405 * In order for the power report report to accurate timestamp,
406 * the _commit() is to force a sync with disk and therefore
407 * an update for file time.
412 FILE_FUNCTION(FDMAP_IO_FILE, _commit(handle))
416 * POSIX equivalent for fstat().
417 * fstat() is used instead of _fstat(),
418 * otherwise problems with the 32/64 time definitions
423 posix_fstat(int fd, struct stat *buffer)
425 FILE_FUNCTION(FDMAP_IO_ANY, fstat(handle, buffer))
429 * POSIX equivalent for lseek().
432 posix_lseek(int fd, off_t offset, int origin)
434 FILE_FUNCTION(FDMAP_IO_FILE, _lseek(handle, offset, origin))
438 * POSIX equivalent for open().
439 * Implements file locks when opening files to provide equivalent
443 posix_open(const char *fname, int oflag, ...)
446 int pmode = 0, new_fd;
449 if (oflag & O_CREAT) {
451 pmode = va_arg(ap, int);
455 if ((new_fd = get_fd()) < 0)
459 * We don't implement fcntl() for F_SETLK. Instead, we lock *all*
460 * files we open. Not ideal, but it works for Empire.
462 handle = _sopen(fname, oflag,
463 oflag & O_RDONLY ? SH_DENYNO : SH_DENYWR, pmode);
468 set_fd(new_fd, FDMAP_IO_FILE, handle);
472 #define SHARED_FUNCTION(socket_expr, file_expr) \
475 enum fdmap_io_type type; \
477 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle)) \
481 case FDMAP_IO_SOCKET: \
482 result = (socket_expr); \
483 if (result == SOCKET_ERROR) { \
484 errno = WSAGetLastError(); \
488 case FDMAP_IO_FILE: \
489 return (file_expr); \
496 * POSIX equivalent for read().
499 posix_read(int fd, void *buffer, size_t count)
501 SHARED_FUNCTION(recv(handle, buffer, count, 0),
502 _read(handle, buffer, count))
506 * POSIX equivalent for readv
507 * Modelled after the GNU's libc/sysdeps/posix/readv.c
510 readv(int fd, const struct iovec *iov, int iovcnt)
513 unsigned char *buffer, *buffer_location;
514 size_t total_bytes = 0;
518 for (i = 0; i < iovcnt; i++) {
519 total_bytes += iov[i].iov_len;
522 buffer = malloc(total_bytes);
523 if (buffer == NULL && total_bytes != 0) {
528 bytes_read = posix_read(fd, buffer, total_bytes);
529 if (bytes_read <= 0) {
534 bytes_left = bytes_read;
535 buffer_location = buffer;
536 for (i = 0; i < iovcnt; i++) {
537 size_t copy = MIN(iov[i].iov_len, bytes_left);
539 memcpy(iov[i].iov_base, buffer_location, copy);
541 buffer_location += copy;
553 * POSIX equivalent for write().
556 posix_write(int fd, const void *buffer, size_t count)
558 SHARED_FUNCTION(send(handle, buffer, count, 0),
559 _write(handle, buffer, count))
563 * POSIX equivalent for writev
564 * Modelled after the GNU's libc/sysdeps/posix/writev.c
567 writev(int fd, const struct iovec *iov, int iovcnt)
570 unsigned char *buffer, *buffer_location;
571 size_t total_bytes = 0;
574 for (i = 0; i < iovcnt; i++)
575 total_bytes += iov[i].iov_len;
577 buffer = malloc(total_bytes);
578 if (buffer == NULL && total_bytes != 0) {
583 buffer_location = buffer;
584 for (i = 0; i < iovcnt; i++) {
585 memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
586 buffer_location += iov[i].iov_len;
589 bytes_written = posix_write(fd, buffer, total_bytes);
593 if (bytes_written <= 0)
595 return bytes_written;
599 * POSIX equivalent for fileno().
600 * As fopen/fclose/fcloseall are not implemented as POSIX
601 * equivalent functions, the mapping is done when required
602 * by a call to fileno(). The garbage collection of the
603 * file descriptors allocated is done in set_fd() when the
612 if (stream == NULL) {
617 handle = _fileno(stream);
619 fd = lookup_fd(handle, FDMAP_IO_FILE);
623 if ((fd = get_fd()) < 0) {
628 set_fd(fd, FDMAP_IO_FILE, handle);
633 * POSIX equivalent for fcntl().
634 * Currently supports only the F_GETFL/F_SETFL/O_NONBLOCK
635 * Currently ignores F_GETLK/F_SETLK as the file locks are
636 * implement in open()
639 fcntl(int fd, int cmd, ...)
643 unsigned int nonblocking;
647 enum fdmap_io_type type;
649 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
656 * F_GETFL and F_SETFL only support O_NONBLOCK
657 * for sockets currently
659 if (type == FDMAP_IO_SOCKET) {
660 result = WSAIoctl(handle, FIONBIO, NULL, 0,&nonblocking,
661 sizeof (nonblocking), &bytes_returned, NULL, NULL);
664 errno = WSAGetLastError();
675 if (type == FDMAP_IO_SOCKET) {
677 value = va_arg(ap, int);
679 if (value & O_NONBLOCK)
684 result = WSAIoctl(handle, FIONBIO, &nonblocking,
685 sizeof (nonblocking), NULL, 0, &bytes_returned,
689 errno = WSAGetLastError();
697 * The POSIX equivalent is not available in WIN32
698 * That implement the file locking in the file open
699 * by using sopen instead of open.