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.
59 //#include "prototypes.h"
62 * FD_SETSIZE is the size for the maximum number of sockets.
63 * The number of file descriptors is in a variable _nhandle
64 * based on the assertion output. In order the simplify the
65 * code and skip dynamic allocation, used double the socket size.
67 #define MAX_FDS (FD_SETSIZE * 2)
73 FDMAP_IO_ANY /* used for searching only (lookup_handle) */
78 enum fdmap_io_type type;
81 static struct fdmap fdmap[MAX_FDS] = {
89 * Allocate a POSIX equivalent file descriptor.
90 * Note once get_fd() is called either free_fd() or set_fd()
91 * must be called before thread mutex is released as the
92 * allocation/deallocation code is not thread safe.
99 for (fd = 0; fd < nfd && fdmap[fd].type != FDMAP_IO_NOTUSED; fd++) ;
105 fdmap[fd].type = FDMAP_IO_NOTUSED;
112 * Deallocate a POSIX equivalent file descriptor.
117 fdmap[fd].type = FDMAP_IO_NOTUSED;
118 for(; fdmap[nfd - 1].type == FDMAP_IO_NOTUSED; nfd--) ;
122 * Complete the allocation of the file descriptor.
125 set_fd(int fd, enum fdmap_io_type type, int handle)
129 fdmap[fd].handle = handle;
130 fdmap[fd].type = type;
133 * Garbage collection for posix_fileno(), currently not
134 * replacing fclose() and fcloseall() so do not know when
135 * a stream is closed.
137 for (i = 0; i < nfd; i++) {
138 if (i != fd && type == fdmap[i].type && handle == fdmap[i].handle)
144 * Find the windows handle (file or socket) for file descriptor.
145 * Return windows handle and type of handle.
146 * You can search for a specific type (FDMAP_IO_FILE or FDMAP_IO_SOCKET)
147 * or for both search by using FDMAP_IO_ANY.
148 * FDMAP_IO_NOTUSED is not valid type to search with.
151 lookup_handle(int fd, enum fdmap_io_type d_type, int error,
152 enum fdmap_io_type *type_ptr, int *handle_ptr)
155 if (fd < 0 || fd >= MAX_FDS) {
159 } else if ((fdmap[fd].type != d_type && d_type != FDMAP_IO_ANY) ||
160 (fdmap[fd].type == FDMAP_IO_NOTUSED && d_type == FDMAP_IO_ANY)) {
165 if (type_ptr != NULL)
166 *type_ptr = fdmap[fd].type;
167 if (handle_ptr != NULL)
168 *handle_ptr = fdmap[fd].handle;
173 * Find and return the file descriptor associated with windows handle.
174 * You can search for FDMAP_IO_FILE or FDMAP_IO_SOCKET.
175 * FDMAP_IO_ANY or FDMAP_IO_NOTUSED is not considered valid search
179 lookup_fd(int handle, enum fdmap_io_type d_type)
183 for (i = 0; i < nfd; i++)
184 if (fdmap[i].handle == handle && fdmap[i].type == d_type)
190 * Get the window socket handle for POSIX file descriptor.
193 posix_fd2socket(int fd)
196 enum fdmap_io_type type;
198 if (!lookup_handle(fd, FDMAP_IO_SOCKET, WSAENOTSOCK,
200 return INVALID_SOCKET;
204 #define SOCKET_FUNCTION(expr) \
208 if (!lookup_handle(fd, FDMAP_IO_SOCKET, \
209 ENOTSOCK, NULL, &handle)) \
213 if (result == SOCKET_ERROR) { \
214 errno = WSAGetLastError(); \
220 * POSIX equivalent for accept().
224 posix_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
227 int handle, new_handle;
229 if (!lookup_handle(fd, FDMAP_IO_SOCKET, ENOTSOCK, NULL, &handle))
236 new_handle = accept(handle, addr, addrlen);
237 if (new_handle == INVALID_SOCKET) {
239 errno = WSAGetLastError();
242 set_fd(new_fd, FDMAP_IO_SOCKET, new_handle);
247 * POSIX equivalent for bind().
251 posix_bind(int fd, const struct sockaddr *name, socklen_t namelen)
253 SOCKET_FUNCTION(bind(handle, name, namelen))
257 * POSIX equivalent for listen().
261 posix_listen(int fd, int backlog)
263 SOCKET_FUNCTION(listen(handle, backlog))
267 * POSIX equivalent for setsockopt().
271 posix_setsockopt(int fd, int level, int optname,
272 const void *optval, socklen_t optlen)
275 * SO_REUSEADDR requests from tcp_listen.c
276 * to permit another bind even when the
277 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
278 * broken: it makes bind() succeed no matter what, even if
279 * there's another server running on the same port. Luckily,
280 * bind() seems to be broken as well: it seems to succeed while
281 * the port is in state TIME_WAIT by default; thus we get the
282 * behavior we want by not setting SO_REUSEADDR.
284 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
287 SOCKET_FUNCTION(setsockopt(handle, level, optname,
293 * POSIX equivalent for shutdown().
297 posix_shutdown(int fd, int how)
299 SOCKET_FUNCTION(shutdown(handle, how))
303 * POSIX equivalent for socket().
307 posix_socket(int domain, int type, int protocol)
312 if ((new_fd = get_fd()) < 0)
315 handle = socket(domain, type, protocol);
316 if (handle == INVALID_SOCKET) {
318 errno = WSAGetLastError();
321 set_fd(new_fd, FDMAP_IO_SOCKET, handle);
325 #define FILE_FUNCTION(type, expr) \
328 if (!lookup_handle(fd, (type), EBADF, NULL, &handle)) \
334 * POSIX equivalent for close().
341 enum fdmap_io_type type;
343 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
348 case FDMAP_IO_SOCKET:
349 result = closesocket(handle);
350 if (result == SOCKET_ERROR) {
351 errno = WSAGetLastError();
356 return _close(handle);
363 * posix_fsync forces file sync with the disk.
364 * In order for the power report report to accurate timestamp,
365 * the _commit() is to force a sync with disk and therefore
366 * an update for file time.
371 FILE_FUNCTION(FDMAP_IO_FILE, _commit(handle))
375 * POSIX equivalent for fstat().
376 * fstat() is used instead of _fstat(),
377 * otherwise problems with the 32/64 time definitions
382 posix_fstat(int fd, struct stat *buffer)
384 FILE_FUNCTION(FDMAP_IO_ANY, fstat(handle, buffer))
388 * POSIX equivalent for lseek().
391 posix_lseek(int fd, off_t offset, int origin)
393 FILE_FUNCTION(FDMAP_IO_FILE, _lseek(handle, offset, origin))
397 * POSIX equivalent for open().
398 * Implements file locks when opening files to provide equivalent
402 posix_open(const char *fname, int oflag, ...)
405 int pmode = 0, new_fd;
408 if (oflag & O_CREAT) {
410 pmode = va_arg(ap, int);
414 if ((new_fd = get_fd()) < 0)
418 * We don't implement fcntl() for F_SETLK. Instead, we lock *all*
419 * files we open. Not ideal, but it works for Empire.
421 handle = _sopen(fname, oflag,
422 oflag & O_RDONLY ? SH_DENYNO : SH_DENYWR, pmode);
427 set_fd(new_fd, FDMAP_IO_FILE, handle);
431 #define SHARED_FUNCTION(socket_expr, file_expr) \
434 enum fdmap_io_type type; \
436 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle)) \
440 case FDMAP_IO_SOCKET: \
441 result = (socket_expr); \
442 if (result == SOCKET_ERROR) { \
443 errno = WSAGetLastError(); \
447 case FDMAP_IO_FILE: \
448 return (file_expr); \
454 * POSIX equivalent for read().
457 posix_read(int fd, void *buffer, size_t count)
459 SHARED_FUNCTION(recv(handle, buffer, count, 0),
460 _read(handle, buffer, count))
464 * POSIX equivalent for readv
465 * Modelled after the GNU's libc/sysdeps/posix/readv.c
468 readv(int fd, const struct iovec *iov, int iovcnt)
471 unsigned char *buffer, *buffer_location;
472 size_t total_bytes = 0;
476 for (i = 0; i < iovcnt; i++) {
477 total_bytes += iov[i].iov_len;
480 buffer = malloc(total_bytes);
484 bytes_read = posix_read(fd, buffer, total_bytes);
485 if (bytes_read <= 0) {
490 bytes_left = bytes_read;
491 buffer_location = buffer;
492 for (i = 0; i < iovcnt; i++) {
493 size_t copy = MIN(iov[i].iov_len, bytes_left);
495 memcpy(iov[i].iov_base, buffer_location, copy);
497 buffer_location += copy;
509 * POSIX equivalent for write().
512 posix_write(int fd, const void *buffer, size_t count)
514 SHARED_FUNCTION(send(handle, buffer, count, 0),
515 _write(handle, buffer, count))
519 * POSIX equivalent for writev
520 * Modelled after the GNU's libc/sysdeps/posix/writev.c
523 writev(int fd, const struct iovec *iov, int iovcnt)
526 unsigned char *buffer, *buffer_location;
527 size_t total_bytes = 0;
530 for (i = 0; i < iovcnt; i++)
531 total_bytes += iov[i].iov_len;
533 if (total_bytes == 0)
536 buffer = malloc(total_bytes);
540 buffer_location = buffer;
541 for (i = 0; i < iovcnt; i++) {
542 memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
543 buffer_location += iov[i].iov_len;
546 bytes_written = posix_write(fd, buffer, total_bytes);
550 if (bytes_written <= 0)
552 return bytes_written;
556 * POSIX equivalent for fileno().
557 * As fopen/fclose/fcloseall are not implemented as POSIX
558 * equivalent functions, the mapping is done when required
559 * by a call to fileno(). The garbage collection of the
560 * file descriptors allocated is done in set_fd() when the
564 posix_fileno(FILE *stream)
569 if (stream == NULL) {
574 handle = _fileno(stream);
576 fd = lookup_fd(handle, FDMAP_IO_FILE);
580 if ((fd = get_fd()) < 0) {
585 set_fd(fd, FDMAP_IO_FILE, handle);
590 * POSIX equivalent for fcntl().
591 * Currently supports only the F_GETFL/F_SETFL/O_NONBLOCK
592 * Currently ignores F_GETLK/F_SETLK as the file locks are
593 * implement in open()
596 fcntl(int fd, int cmd, ...)
600 unsigned int nonblocking;
604 enum fdmap_io_type type;
606 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
613 * F_GETFL and F_SETFL only support O_NONBLOCK
614 * for sockets currently
616 if (type == FDMAP_IO_SOCKET) {
617 result = WSAIoctl(handle, FIONBIO, NULL, 0,&nonblocking,
618 sizeof (nonblocking), &bytes_returned, NULL, NULL);
621 errno = WSAGetLastError();
632 if (type == FDMAP_IO_SOCKET) {
634 value = va_arg(ap, int);
636 if (value & O_NONBLOCK)
641 result = WSAIoctl(handle, FIONBIO, &nonblocking,
642 sizeof (nonblocking), NULL, 0, &bytes_returned,
646 errno = WSAGetLastError();
654 * The POSIX equivalent is not available in WIN32
655 * That implement the file locking in the file open
656 * by using sopen instead of open.