2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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.
60 #include "prototypes.h"
63 * FD_SETSIZE is the size for the maximum number of sockets.
64 * The number of file descriptors is in a variable _nhandle
65 * based on the assertion output. In order the simplify the
66 * code and skip dynamic allocation, used double the socket size.
68 #define MAX_FDS (FD_SETSIZE * 2)
74 FDMAP_IO_ANY /* used for searching only (lookup_handle) */
79 enum fdmap_io_type type;
82 static struct fdmap fdmap[MAX_FDS] = {
90 * Allocate a POSIX equivalent file descriptor.
91 * Note once get_fd() is called either free_fd() or set_fd()
92 * must be called before thread mutex is released as the
93 * allocation/deallocation code is not thread safe.
100 for (fd = 0; fd < nfd && fdmap[fd].type != FDMAP_IO_NOTUSED; fd++) ;
106 fdmap[fd].type = FDMAP_IO_NOTUSED;
113 * Deallocate a POSIX equivalent file descriptor.
118 fdmap[fd].type = FDMAP_IO_NOTUSED;
119 for(; fdmap[nfd - 1].type == FDMAP_IO_NOTUSED; nfd--) ;
123 * Complete the allocation of the file descriptor.
126 set_fd(int fd, enum fdmap_io_type type, int handle)
130 fdmap[fd].handle = handle;
131 fdmap[fd].type = type;
134 * Garbage collection for posix_fileno(), currently not
135 * replacing fclose() and fcloseall() so do not know when
136 * a stream is closed.
138 for (i = 0; i < nfd; i++) {
139 if (i != fd && type == fdmap[i].type && handle == fdmap[i].handle)
145 * Find the windows handle (file or socket) for file descriptor.
146 * Return windows handle and type of handle.
147 * You can search for a specific type (FDMAP_IO_FILE or FDMAP_IO_SOCKET)
148 * or for both search by using FDMAP_IO_ANY.
149 * FDMAP_IO_NOTUSED is not valid type to search with.
152 lookup_handle(int fd, enum fdmap_io_type d_type, int error,
153 enum fdmap_io_type *type_ptr, int *handle_ptr)
156 if (fd < 0 || fd >= MAX_FDS) {
160 } else if ((fdmap[fd].type != d_type && d_type != FDMAP_IO_ANY) ||
161 (fdmap[fd].type == FDMAP_IO_NOTUSED && d_type == FDMAP_IO_ANY)) {
166 if (type_ptr != NULL)
167 *type_ptr = fdmap[fd].type;
168 if (handle_ptr != NULL)
169 *handle_ptr = fdmap[fd].handle;
174 * Find and return the file descriptor associated with windows handle.
175 * You can search for FDMAP_IO_FILE or FDMAP_IO_SOCKET.
176 * FDMAP_IO_ANY or FDMAP_IO_NOTUSED is not considered valid search
180 lookup_fd(int handle, enum fdmap_io_type d_type)
184 for (i = 0; i < nfd; i++)
185 if (fdmap[i].handle == handle && fdmap[i].type == d_type)
191 * Get the window socket handle for POSIX file descriptor.
194 posix_fd2socket(int fd)
197 enum fdmap_io_type type;
199 if (!lookup_handle(fd, FDMAP_IO_SOCKET, WSAENOTSOCK,
201 return INVALID_SOCKET;
205 #define SOCKET_FUNCTION(expr) \
209 if (!lookup_handle(fd, FDMAP_IO_SOCKET, \
210 ENOTSOCK, NULL, &handle)) \
214 if (result == SOCKET_ERROR) { \
215 errno = WSAGetLastError(); \
221 * POSIX equivalent for accept().
225 posix_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
228 int handle, new_handle;
230 if (!lookup_handle(fd, FDMAP_IO_SOCKET, ENOTSOCK, NULL, &handle))
237 new_handle = accept(handle, addr, addrlen);
238 if (new_handle == INVALID_SOCKET) {
240 errno = WSAGetLastError();
243 set_fd(new_fd, FDMAP_IO_SOCKET, new_handle);
248 * POSIX equivalent for bind().
252 posix_bind(int fd, const struct sockaddr *name, socklen_t namelen)
254 SOCKET_FUNCTION(bind(handle, name, namelen))
258 * POSIX equivalent for listen().
262 posix_listen(int fd, int backlog)
264 SOCKET_FUNCTION(listen(handle, backlog))
268 * POSIX equivalent for setsockopt().
272 posix_setsockopt(int fd, int level, int optname,
273 const void *optval, socklen_t optlen)
276 * SO_REUSEADDR requests from tcp_listen.c
277 * to permit another bind even when the
278 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
279 * broken: it makes bind() succeed no matter what, even if
280 * there's another server running on the same port. Luckily,
281 * bind() seems to be broken as well: it seems to succeed while
282 * the port is in state TIME_WAIT by default; thus we get the
283 * behavior we want by not setting SO_REUSEADDR.
285 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
288 SOCKET_FUNCTION(setsockopt(handle, level, optname,
294 * POSIX equivalent for shutdown().
298 posix_shutdown(int fd, int how)
300 SOCKET_FUNCTION(shutdown(handle, how))
304 * POSIX equivalent for socket().
308 posix_socket(int domain, int type, int protocol)
313 if ((new_fd = get_fd()) < 0)
316 handle = socket(domain, type, protocol);
317 if (handle == INVALID_SOCKET) {
319 errno = WSAGetLastError();
322 set_fd(new_fd, FDMAP_IO_SOCKET, handle);
326 #define FILE_FUNCTION(type, expr) \
329 if (!lookup_handle(fd, (type), EBADF, NULL, &handle)) \
335 * POSIX equivalent for close().
342 enum fdmap_io_type type;
344 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
349 case FDMAP_IO_SOCKET:
350 result = closesocket(handle);
351 if (result == SOCKET_ERROR) {
352 errno = WSAGetLastError();
357 return _close(handle);
364 * posix_fsync forces file sync with the disk.
365 * In order for the power report report to accurate timestamp,
366 * the _commit() is to force a sync with disk and therefore
367 * an update for file time.
372 FILE_FUNCTION(FDMAP_IO_FILE, _commit(handle))
376 * POSIX equivalent for fstat().
377 * fstat() is used instead of _fstat(),
378 * otherwise problems with the 32/64 time definitions
383 posix_fstat(int fd, struct stat *buffer)
385 FILE_FUNCTION(FDMAP_IO_ANY, fstat(handle, buffer))
389 * POSIX equivalent for lseek().
392 posix_lseek(int fd, off_t offset, int origin)
394 FILE_FUNCTION(FDMAP_IO_FILE, _lseek(handle, offset, origin))
398 * POSIX equivalent for open().
399 * Implements file locks when opening files to provide equivalent
403 posix_open(const char *fname, int oflag, ...)
406 int pmode = 0, new_fd;
409 if (oflag & O_CREAT) {
411 pmode = va_arg(ap, int);
415 if ((new_fd = get_fd()) < 0)
419 * We don't implement fcntl() for F_SETLK. Instead, we lock *all*
420 * files we open. Not ideal, but it works for Empire.
422 handle = _sopen(fname, oflag,
423 oflag & O_RDONLY ? SH_DENYNO : SH_DENYWR, pmode);
428 set_fd(new_fd, FDMAP_IO_FILE, handle);
432 #define SHARED_FUNCTION(socket_expr, file_expr) \
435 enum fdmap_io_type type; \
437 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle)) \
441 case FDMAP_IO_SOCKET: \
442 result = (socket_expr); \
443 if (result == SOCKET_ERROR) { \
444 errno = WSAGetLastError(); \
448 case FDMAP_IO_FILE: \
449 return (file_expr); \
455 * POSIX equivalent for read().
458 posix_read(int fd, void *buffer, size_t count)
460 SHARED_FUNCTION(recv(handle, buffer, count, 0),
461 _read(handle, buffer, count))
465 * POSIX equivalent for readv
466 * Modelled after the GNU's libc/sysdeps/posix/readv.c
469 readv(int fd, const struct iovec *iov, int iovcnt)
472 unsigned char *buffer, *buffer_location;
473 size_t total_bytes = 0;
477 for (i = 0; i < iovcnt; i++) {
478 total_bytes += iov[i].iov_len;
481 buffer = malloc(total_bytes);
485 bytes_read = posix_read(fd, buffer, total_bytes);
486 if (bytes_read <= 0) {
491 bytes_left = bytes_read;
492 buffer_location = buffer;
493 for (i = 0; i < iovcnt; i++) {
494 size_t copy = MIN(iov[i].iov_len, bytes_left);
496 memcpy(iov[i].iov_base, buffer_location, copy);
498 buffer_location += copy;
510 * POSIX equivalent for write().
513 posix_write(int fd, const void *buffer, size_t count)
515 SHARED_FUNCTION(send(handle, buffer, count, 0),
516 _write(handle, buffer, count))
520 * POSIX equivalent for writev
521 * Modelled after the GNU's libc/sysdeps/posix/writev.c
524 writev(int fd, const struct iovec *iov, int iovcnt)
527 unsigned char *buffer, *buffer_location;
528 size_t total_bytes = 0;
531 for (i = 0; i < iovcnt; i++)
532 total_bytes += iov[i].iov_len;
534 if (total_bytes == 0)
537 buffer = malloc(total_bytes);
541 buffer_location = buffer;
542 for (i = 0; i < iovcnt; i++) {
543 memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
544 buffer_location += iov[i].iov_len;
547 bytes_written = posix_write(fd, buffer, total_bytes);
551 if (bytes_written <= 0)
553 return bytes_written;
557 * POSIX equivalent for fileno().
558 * As fopen/fclose/fcloseall are not implemented as POSIX
559 * equivalent functions, the mapping is done when required
560 * by a call to fileno(). The garbage collection of the
561 * file descriptors allocated is done in set_fd() when the
565 posix_fileno(FILE *stream)
570 if (stream == NULL) {
575 handle = _fileno(stream);
577 fd = lookup_fd(handle, FDMAP_IO_FILE);
581 if ((fd = get_fd()) < 0) {
586 set_fd(fd, FDMAP_IO_FILE, handle);
591 * POSIX equivalent for fcntl().
592 * Currently supports only the F_GETFL/F_SETFL/O_NONBLOCK
593 * Currently ignores F_GETLK/F_SETLK as the file locks are
594 * implement in open()
597 fcntl(int fd, int cmd, ...)
601 unsigned int nonblocking;
605 enum fdmap_io_type type;
607 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
614 * F_GETFL and F_SETFL only support O_NONBLOCK
615 * for sockets currently
617 if (type == FDMAP_IO_SOCKET) {
618 result = WSAIoctl(handle, FIONBIO, NULL, 0,&nonblocking,
619 sizeof (nonblocking), &bytes_returned, NULL, NULL);
622 errno = WSAGetLastError();
633 if (type == FDMAP_IO_SOCKET) {
635 value = va_arg(ap, int);
637 if (value & O_NONBLOCK)
642 result = WSAIoctl(handle, FIONBIO, &nonblocking,
643 sizeof (nonblocking), NULL, 0, &bytes_returned,
647 errno = WSAGetLastError();
655 * The POSIX equivalent is not available in WIN32
656 * That implement the file locking in the file open
657 * by using sopen instead of open.