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.
56 #include "sys/socket.h"
61 * FD_SETSIZE is the size for the maximum number of sockets.
62 * The number of file descriptors is in a variable _nhandle
63 * based on the assertion output. In order the simplify the
64 * code and skip dynamic allocation, used double the socket size.
66 #define MAX_FDS (FD_SETSIZE * 2)
72 FDMAP_IO_ANY /* used for searching only (lookup_handle) */
77 enum fdmap_io_type type;
80 static struct fdmap fdmap[MAX_FDS] = {
88 * Allocate a POSIX equivalent file descriptor.
89 * Note once get_fd() is called either free_fd() or set_fd()
90 * must be called before thread mutex is released as the
91 * allocation/deallocation code is not thread safe.
98 for (fd = 0; fd < nfd && fdmap[fd].type != FDMAP_IO_NOTUSED; fd++) ;
104 fdmap[fd].type = FDMAP_IO_NOTUSED;
111 * Deallocate a POSIX equivalent file descriptor.
116 fdmap[fd].type = FDMAP_IO_NOTUSED;
117 for(; fdmap[nfd - 1].type == FDMAP_IO_NOTUSED; nfd--) ;
121 * Complete the allocation of the file descriptor.
124 set_fd(int fd, enum fdmap_io_type type, int handle)
128 fdmap[fd].handle = handle;
129 fdmap[fd].type = type;
132 * Garbage collection for posix_fileno(), currently not
133 * replacing fclose() and fcloseall() so do not know when
134 * a stream is closed.
136 for (i = 0; i < nfd; i++) {
137 if (i != fd && type == fdmap[i].type && handle == fdmap[i].handle)
143 * Find the windows handle (file or socket) for file descriptor.
144 * Return windows handle and type of handle.
145 * You can search for a specific type (FDMAP_IO_FILE or FDMAP_IO_SOCKET)
146 * or for both search by using FDMAP_IO_ANY.
147 * FDMAP_IO_NOTUSED is not valid type to search with.
150 lookup_handle(int fd, enum fdmap_io_type d_type, int error,
151 enum fdmap_io_type *type_ptr, int *handle_ptr)
154 if (fd < 0 || fd >= MAX_FDS) {
158 } else if ((fdmap[fd].type != d_type && d_type != FDMAP_IO_ANY) ||
159 (fdmap[fd].type == FDMAP_IO_NOTUSED && d_type == FDMAP_IO_ANY)) {
164 if (type_ptr != NULL)
165 *type_ptr = fdmap[fd].type;
166 if (handle_ptr != NULL)
167 *handle_ptr = fdmap[fd].handle;
172 * Find and return the file descriptor associated with windows handle.
173 * You can search for FDMAP_IO_FILE or FDMAP_IO_SOCKET.
174 * FDMAP_IO_ANY or FDMAP_IO_NOTUSED is not considered valid search
178 lookup_fd(int handle, enum fdmap_io_type d_type)
182 for (i = 0; i < nfd; i++)
183 if (fdmap[i].handle == handle && fdmap[i].type == d_type)
189 * Get the window socket handle for POSIX file descriptor.
192 posix_fd2socket(int fd)
195 enum fdmap_io_type type;
197 if (!lookup_handle(fd, FDMAP_IO_SOCKET, WSAENOTSOCK,
199 return INVALID_SOCKET;
203 #define SOCKET_FUNCTION(expr) \
207 if (!lookup_handle(fd, FDMAP_IO_SOCKET, \
208 ENOTSOCK, NULL, &handle)) \
212 if (result == SOCKET_ERROR) { \
213 errno = WSAGetLastError(); \
219 * POSIX equivalent for accept().
223 posix_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
226 int handle, new_handle;
228 if (!lookup_handle(fd, FDMAP_IO_SOCKET, ENOTSOCK, NULL, &handle))
235 new_handle = accept(handle, addr, addrlen);
236 if (new_handle == INVALID_SOCKET) {
238 errno = WSAGetLastError();
241 set_fd(new_fd, FDMAP_IO_SOCKET, new_handle);
246 * POSIX equivalent for bind().
250 posix_bind(int fd, const struct sockaddr *name, socklen_t namelen)
252 SOCKET_FUNCTION(bind(handle, name, namelen))
256 * POSIX equivalent for listen().
260 posix_listen(int fd, int backlog)
262 SOCKET_FUNCTION(listen(handle, backlog))
266 * POSIX equivalent for setsockopt().
270 posix_setsockopt(int fd, int level, int optname,
271 const void *optval, socklen_t optlen)
274 * SO_REUSEADDR requests to permit another bind even when the
275 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
276 * broken: it makes bind() succeed no matter what, even if
277 * there's another server running on the same port. Luckily,
278 * bind() seems to be broken as well: it seems to succeed while
279 * the port is in state TIME_WAIT by default; thus we get the
280 * behavior we want by not setting SO_REUSEADDR.
282 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
285 SOCKET_FUNCTION(setsockopt(handle, level, optname,
291 * POSIX equivalent for shutdown().
295 posix_shutdown(int fd, int how)
297 SOCKET_FUNCTION(shutdown(handle, how))
301 * POSIX equivalent for socket().
305 posix_socket(int domain, int type, int protocol)
310 if ((new_fd = get_fd()) < 0)
313 handle = socket(domain, type, protocol);
314 if (handle == INVALID_SOCKET) {
316 errno = WSAGetLastError();
319 set_fd(new_fd, FDMAP_IO_SOCKET, handle);
323 #ifdef HAVE_GETADDRINFO
325 inet_ntop(int af, const void *source, char *dest, socklen_t len)
329 struct sockaddr_in in;
330 memset(&in, 0, sizeof(in));
331 in.sin_family = AF_INET;
332 memcpy(&in.sin_addr, source, sizeof(struct in_addr));
333 getnameinfo((struct sockaddr *)&in,
334 sizeof(struct sockaddr_in), dest, len,
335 NULL, 0, NI_NUMERICHOST);
338 else if (af == AF_INET6)
340 struct sockaddr_in6 in;
341 memset(&in, 0, sizeof(in));
342 in.sin6_family = AF_INET6;
343 memcpy(&in.sin6_addr, source, sizeof(struct in_addr6));
344 getnameinfo((struct sockaddr *)&in,
345 sizeof(struct sockaddr_in6), dest, len,
346 NULL, 0, NI_NUMERICHOST);
353 #define FILE_FUNCTION(type, expr) \
356 if (!lookup_handle(fd, (type), EBADF, NULL, &handle)) \
362 * POSIX equivalent for close().
369 enum fdmap_io_type type;
371 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
376 case FDMAP_IO_SOCKET:
377 result = closesocket(handle);
378 if (result == SOCKET_ERROR) {
379 errno = WSAGetLastError();
384 return _close(handle);
391 * posix_fsync forces file sync with the disk.
392 * In order for the power report report to accurate timestamp,
393 * the _commit() is to force a sync with disk and therefore
394 * an update for file time.
399 FILE_FUNCTION(FDMAP_IO_FILE, _commit(handle))
403 * POSIX equivalent for fstat().
404 * fstat() is used instead of _fstat(),
405 * otherwise problems with the 32/64 time definitions
410 posix_fstat(int fd, struct stat *buffer)
412 FILE_FUNCTION(FDMAP_IO_ANY, fstat(handle, buffer))
416 * POSIX equivalent for lseek().
419 posix_lseek(int fd, off_t offset, int origin)
421 FILE_FUNCTION(FDMAP_IO_FILE, _lseek(handle, offset, origin))
425 * POSIX equivalent for open().
426 * Implements file locks when opening files to provide equivalent
430 posix_open(const char *fname, int oflag, ...)
433 int pmode = 0, new_fd;
436 if (oflag & O_CREAT) {
438 pmode = va_arg(ap, int);
442 if ((new_fd = get_fd()) < 0)
446 * We don't implement fcntl() for F_SETLK. Instead, we lock *all*
447 * files we open. Not ideal, but it works for Empire.
449 handle = _sopen(fname, oflag,
450 oflag & O_RDONLY ? SH_DENYNO : SH_DENYWR, pmode);
455 set_fd(new_fd, FDMAP_IO_FILE, handle);
459 #define SHARED_FUNCTION(socket_expr, file_expr) \
462 enum fdmap_io_type type; \
464 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle)) \
468 case FDMAP_IO_SOCKET: \
469 result = (socket_expr); \
470 if (result == SOCKET_ERROR) { \
471 errno = WSAGetLastError(); \
475 case FDMAP_IO_FILE: \
476 return (file_expr); \
482 * POSIX equivalent for read().
485 posix_read(int fd, void *buffer, size_t count)
487 SHARED_FUNCTION(recv(handle, buffer, count, 0),
488 _read(handle, buffer, count))
492 * POSIX equivalent for readv
493 * Modelled after the GNU's libc/sysdeps/posix/readv.c
496 readv(int fd, const struct iovec *iov, int iovcnt)
499 unsigned char *buffer, *buffer_location;
500 size_t total_bytes = 0;
504 for (i = 0; i < iovcnt; i++) {
505 total_bytes += iov[i].iov_len;
508 buffer = malloc(total_bytes);
509 if (buffer == NULL && total_bytes != 0)
512 bytes_read = posix_read(fd, buffer, total_bytes);
513 if (bytes_read <= 0) {
518 bytes_left = bytes_read;
519 buffer_location = buffer;
520 for (i = 0; i < iovcnt; i++) {
521 size_t copy = MIN(iov[i].iov_len, bytes_left);
523 memcpy(iov[i].iov_base, buffer_location, copy);
525 buffer_location += copy;
537 * POSIX equivalent for write().
540 posix_write(int fd, const void *buffer, size_t count)
542 SHARED_FUNCTION(send(handle, buffer, count, 0),
543 _write(handle, buffer, count))
547 * POSIX equivalent for writev
548 * Modelled after the GNU's libc/sysdeps/posix/writev.c
551 writev(int fd, const struct iovec *iov, int iovcnt)
554 unsigned char *buffer, *buffer_location;
555 size_t total_bytes = 0;
558 for (i = 0; i < iovcnt; i++)
559 total_bytes += iov[i].iov_len;
561 buffer = malloc(total_bytes);
562 if (buffer == NULL && total_bytes != 0)
565 buffer_location = buffer;
566 for (i = 0; i < iovcnt; i++) {
567 memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len);
568 buffer_location += iov[i].iov_len;
571 bytes_written = posix_write(fd, buffer, total_bytes);
575 if (bytes_written <= 0)
577 return bytes_written;
581 * POSIX equivalent for fileno().
582 * As fopen/fclose/fcloseall are not implemented as POSIX
583 * equivalent functions, the mapping is done when required
584 * by a call to fileno(). The garbage collection of the
585 * file descriptors allocated is done in set_fd() when the
589 posix_fileno(FILE *stream)
594 if (stream == NULL) {
599 handle = _fileno(stream);
601 fd = lookup_fd(handle, FDMAP_IO_FILE);
605 if ((fd = get_fd()) < 0) {
610 set_fd(fd, FDMAP_IO_FILE, handle);
615 * POSIX equivalent for fcntl().
616 * Currently supports only the F_GETFL/F_SETFL/O_NONBLOCK
617 * Currently ignores F_GETLK/F_SETLK as the file locks are
618 * implement in open()
621 fcntl(int fd, int cmd, ...)
625 unsigned int nonblocking;
629 enum fdmap_io_type type;
631 if (!lookup_handle(fd, FDMAP_IO_ANY, EBADF, &type, &handle))
638 * F_GETFL and F_SETFL only support O_NONBLOCK
639 * for sockets currently
641 if (type == FDMAP_IO_SOCKET) {
642 result = WSAIoctl(handle, FIONBIO, NULL, 0,&nonblocking,
643 sizeof (nonblocking), &bytes_returned, NULL, NULL);
646 errno = WSAGetLastError();
657 if (type == FDMAP_IO_SOCKET) {
659 value = va_arg(ap, int);
661 if (value & O_NONBLOCK)
666 result = WSAIoctl(handle, FIONBIO, &nonblocking,
667 sizeof (nonblocking), NULL, 0, &bytes_returned,
671 errno = WSAGetLastError();
679 * The POSIX equivalent is not available in WIN32
680 * That implement the file locking in the file open
681 * by using sopen instead of open.