2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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 * w32sockets.c: POSIX socket emulation layer for Windows
30 * Known contributors to this file:
31 * Ron Koenderink, 2007
32 * Markus Armbruster, 2007-2009
36 * POSIX sockets are file descriptors. Windows sockets are something
37 * else, with a separate set of functions to operate on them. To
38 * present a more POSIX-like interface to our application code, we
39 * provide a compatibility layer that wraps file descriptors around
49 * Need to include winsock2.h before ws2tcpip.h.
50 * Use sys/socket.h to ensure the #undef NS_ALL
51 * is not missed after including winsock2.h.
53 #include "sys/socket.h"
58 #define W32_FD_TO_SOCKET(fd) ((SOCKET)_get_osfhandle((fd)))
59 #define W32_SOCKET_TO_FD(fh) (_open_osfhandle((long)(fh), O_RDWR | O_BINARY))
64 return W32_FD_TO_SOCKET(fd);
68 fd_is_socket(int fd, SOCKET *sockp)
73 sock = W32_FD_TO_SOCKET(fd);
76 return WSAEnumNetworkEvents(sock, NULL, &ev) == 0;
80 w32_set_winsock_errno(void)
82 int err = WSAGetLastError();
85 /* Map some WSAE* errors to the runtime library's error codes. */
88 case WSA_INVALID_HANDLE:
91 case WSA_NOT_ENOUGH_MEMORY:
94 case WSA_INVALID_PARAMETER:
100 case WSAENAMETOOLONG:
101 errno = ENAMETOOLONG;
107 errno = (err > 10000 && err < 10025) ? err - 10000 : err;
112 #define SOCKET_FUNCTION(expr) do { \
113 SOCKET sock = W32_FD_TO_SOCKET(fd); \
115 if (res == SOCKET_ERROR) { \
116 w32_set_winsock_errno(); \
123 * POSIX equivalent for accept().
127 w32_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
131 sock = accept(W32_FD_TO_SOCKET(fd), addr, addrlen);
132 if (sock == INVALID_SOCKET) {
133 w32_set_winsock_errno();
137 return W32_SOCKET_TO_FD(sock);
141 * POSIX equivalent for bind().
145 w32_bind(int fd, const struct sockaddr *name, socklen_t namelen)
147 SOCKET_FUNCTION(bind(sock, name, namelen));
151 * POSIX equivalent for listen().
155 w32_listen(int fd, int backlog)
157 SOCKET_FUNCTION(listen(sock, backlog));
161 * POSIX equivalent for setsockopt().
165 w32_setsockopt(int fd, int level, int optname,
166 const void *optval, socklen_t optlen)
169 * SO_REUSEADDR requests to permit another bind even when the
170 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
171 * broken: it makes bind() succeed no matter what, even if
172 * there's another server running on the same port. Luckily,
173 * bind() seems to be broken as well: it seems to succeed while
174 * the port is in state TIME_WAIT by default; thus we get the
175 * behavior we want by not setting SO_REUSEADDR.
177 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
180 SOCKET_FUNCTION(setsockopt(sock, level, optname, optval, optlen));
185 * POSIX equivalent for shutdown().
189 w32_shutdown(int fd, int how)
191 SOCKET_FUNCTION(shutdown(sock, how));
195 * POSIX equivalent for socket().
199 w32_socket(int domain, int type, int protocol)
204 * We have to use WSASocket() to create non-overlapped IO sockets.
205 * Overlapped IO sockets cannot be used with read/write.
207 sock = WSASocket(domain, type, protocol, NULL, 0, 0);
208 if (sock == INVALID_SOCKET) {
209 w32_set_winsock_errno();
212 return W32_SOCKET_TO_FD(sock);
215 #ifdef HAVE_GETADDRINFO
217 inet_ntop(int af, const void *src, char *dst, socklen_t len)
220 struct sockaddr_in sin;
221 struct sockaddr_in6 sin6;
225 memset(&sin, 0, sizeof(sin));
227 memcpy(&sin.sin_addr, src, sizeof(sin.sin_addr));
228 sa = (struct sockaddr *)&sin;
230 } else if (af == AF_INET6) {
231 memset(&sin6, 0, sizeof(sin6));
232 sin6.sin6_family = af;
233 memcpy(&sin6.sin6_addr, src, sizeof(sin6.sin6_addr));
234 sa = (struct sockaddr *)&sin6;
235 salen = sizeof(sin6);
237 WSASetLastError(WSAEAFNOSUPPORT);
238 w32_set_winsock_errno();
242 if (getnameinfo(sa, salen, dst, len, NULL, 0, NI_NUMERICHOST)) {
243 WSASetLastError(WSAEAFNOSUPPORT);
244 w32_set_winsock_errno();
253 * POSIX equivalent for fcntl().
254 * Horrible hacks, just good enough support Empire's use of fcntl().
255 * F_GETFL / F_SETFL support making a socket (non-)blocking by getting
256 * flags, adding or removing O_NONBLOCK, and setting the result.
259 fcntl(int fd, int cmd, ...)
263 unsigned long nonblocking;
271 sock = W32_FD_TO_SOCKET(fd);
273 value = va_arg(ap, int);
275 nonblocking = (value & O_NONBLOCK) != 0;
277 if (ioctlsocket(sock, FIONBIO, &nonblocking) == SOCKET_ERROR) {
278 w32_set_winsock_errno();
288 * close() replacement that does the right thing for sockets
291 w32_close_maybe_socket(int fd)
295 if (fd_is_socket(fd, &sock)) {
296 if (closesocket(sock)) {
297 w32_set_winsock_errno();
301 * This always fails because the underlying handle is already
302 * gone, but it closes the fd just fine.
311 * read() replacement that does the right thing for sockets
314 w32_read_maybe_socket(int fd, void *buf, unsigned sz)
319 if (fd_is_socket(fd, &sock)) {
320 res = recv(sock, buf, sz, 0);
322 w32_set_winsock_errno();
325 return _read(fd, buf, sz);
329 * write() replacement that does the right thing for sockets
332 w32_write_maybe_socket(int fd, const void *buf, unsigned sz)
337 if (fd_is_socket(fd, &sock)) {
338 res = send(sock, buf, sz, 0);
340 w32_set_winsock_errno();
343 return _write(fd, buf, sz);
347 w32_socket_init(void)
350 WORD wVersionRequested;
353 wVersionRequested = MAKEWORD(2, 0);
354 rc = WSAStartup(wVersionRequested, &wsaData);
358 w32_close_function = w32_close_maybe_socket;
359 w32_read_function = w32_read_maybe_socket;
360 w32_write_function = w32_write_maybe_socket;