2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * w32sockets.c: POSIX socket emulation layer for Windows
29 * Known contributors to this file:
30 * Ron Koenderink, 2007-2010
31 * Markus Armbruster, 2007-2013
35 * POSIX sockets are file descriptors. Windows sockets are something
36 * else, with a separate set of functions to operate on them. To
37 * present a more POSIX-like interface to our application code, we
38 * provide a compatibility layer that wraps file descriptors around
48 * Need to include winsock2.h before ws2tcpip.h.
49 * Use sys/socket.h to ensure the #undef NS_ALL
50 * is not missed after including winsock2.h.
52 #include "sys/socket.h"
55 #include "arpa/inet.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)
72 int size = sizeof(val);
74 sock = W32_FD_TO_SOCKET(fd);
77 return getsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, &size)
82 w32_set_winsock_errno(void)
84 int err = WSAGetLastError();
87 /* Map some WSAE* errors to the runtime library's error codes. */
89 case WSA_INVALID_HANDLE:
92 case WSA_NOT_ENOUGH_MEMORY:
95 case WSA_INVALID_PARAMETER:
101 case WSAENAMETOOLONG:
102 errno = ENAMETOOLONG;
108 errno = (err > 10000 && err < 10025) ? err - 10000 : err;
113 #define SOCKET_FUNCTION(expr) do { \
114 SOCKET sock = W32_FD_TO_SOCKET(fd); \
116 if (res == SOCKET_ERROR) { \
117 w32_set_winsock_errno(); \
124 * POSIX equivalent for accept().
128 w32_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
132 sock = accept(W32_FD_TO_SOCKET(fd), addr, addrlen);
133 if (sock == INVALID_SOCKET) {
134 w32_set_winsock_errno();
138 return W32_SOCKET_TO_FD(sock);
142 * POSIX equivalent for bind().
146 w32_bind(int fd, const struct sockaddr *name, socklen_t namelen)
148 SOCKET_FUNCTION(bind(sock, name, namelen));
152 * POSIX compatible connect() replacement
156 w32_connect(int sockfd, const struct sockaddr *addr, int addrlen)
158 SOCKET sock = W32_FD_TO_SOCKET(sockfd);
161 result = connect(sock, addr, addrlen);
162 if (result == SOCKET_ERROR) {
163 /* FIXME map WSAEWOULDBLOCK to EINPROGRESS */
164 w32_set_winsock_errno();
171 * POSIX equivalent for listen().
175 w32_listen(int fd, int backlog)
177 SOCKET_FUNCTION(listen(sock, backlog));
181 * POSIX equivalent for setsockopt().
185 w32_setsockopt(int fd, int level, int optname,
186 const void *optval, socklen_t optlen)
189 * SO_REUSEADDR requests to permit another bind even when the
190 * port is still in state TIME_WAIT. Windows' SO_REUSEADDR is
191 * broken: it makes bind() succeed no matter what, even if
192 * there's another server running on the same port. Luckily,
193 * bind() seems to be broken as well: it seems to succeed while
194 * the port is in state TIME_WAIT by default; thus we get the
195 * behavior we want by not setting SO_REUSEADDR.
197 if (level == SOL_SOCKET && optname == SO_REUSEADDR)
200 SOCKET_FUNCTION(setsockopt(sock, level, optname, optval, optlen));
205 * POSIX equivalent for shutdown().
209 w32_shutdown(int fd, int how)
211 SOCKET_FUNCTION(shutdown(sock, how));
215 * POSIX equivalent for socket().
219 w32_socket(int domain, int type, int protocol)
224 * We have to use WSASocket() to create non-overlapped I/O sockets.
225 * Overlapped I/O sockets cannot be used with read/write.
227 sock = WSASocket(domain, type, protocol, NULL, 0, 0);
228 if (sock == INVALID_SOCKET) {
229 w32_set_winsock_errno();
232 return W32_SOCKET_TO_FD(sock);
235 #ifdef HAVE_GETADDRINFO
237 inet_ntop(int af, const void *src, char *dst, socklen_t len)
240 struct sockaddr_in sin;
241 struct sockaddr_in6 sin6;
245 memset(&sin, 0, sizeof(sin));
247 memcpy(&sin.sin_addr, src, sizeof(sin.sin_addr));
248 sa = (struct sockaddr *)&sin;
250 } else if (af == AF_INET6) {
251 memset(&sin6, 0, sizeof(sin6));
252 sin6.sin6_family = af;
253 memcpy(&sin6.sin6_addr, src, sizeof(sin6.sin6_addr));
254 sa = (struct sockaddr *)&sin6;
255 salen = sizeof(sin6);
257 WSASetLastError(WSAEAFNOSUPPORT);
258 w32_set_winsock_errno();
262 if (getnameinfo(sa, salen, dst, len, NULL, 0, NI_NUMERICHOST)) {
263 WSASetLastError(WSAEAFNOSUPPORT);
264 w32_set_winsock_errno();
273 * POSIX equivalent for fcntl().
274 * Horrible hacks, just good enough support Empire's use of fcntl().
275 * F_GETFL / F_SETFL support making a socket (non-)blocking by getting
276 * flags, adding or removing O_NONBLOCK, and setting the result.
279 fcntl(int fd, int cmd, ...)
283 unsigned long nonblocking;
290 sock = W32_FD_TO_SOCKET(fd);
292 value = va_arg(ap, int);
294 nonblocking = (value & O_NONBLOCK) != 0;
296 if (ioctlsocket(sock, FIONBIO, &nonblocking) == SOCKET_ERROR) {
297 w32_set_winsock_errno();
307 * close() replacement that does the right thing for sockets
310 w32_close_maybe_socket(int fd)
314 if (fd_is_socket(fd, &sock)) {
315 if (closesocket(sock)) {
316 w32_set_winsock_errno();
320 * This always fails because the underlying handle is already
321 * gone, but it closes the fd just fine.
330 * read() replacement that does the right thing for sockets
333 w32_read_maybe_socket(int fd, void *buf, unsigned sz)
338 if (fd_is_socket(fd, &sock)) {
339 res = recv(sock, buf, sz, 0);
341 w32_set_winsock_errno();
344 return _read(fd, buf, sz);
348 * write() replacement that does the right thing for sockets
351 w32_write_maybe_socket(int fd, const void *buf, unsigned sz)
356 if (fd_is_socket(fd, &sock)) {
357 res = send(sock, buf, sz, 0);
359 w32_set_winsock_errno();
362 return _write(fd, buf, sz);
366 w32_socket_init(void)
369 WORD wVersionRequested;
372 wVersionRequested = MAKEWORD(2, 0);
373 rc = WSAStartup(wVersionRequested, &wsaData);
377 w32_close_function = w32_close_maybe_socket;
378 w32_read_function = w32_read_maybe_socket;
379 w32_write_function = w32_write_maybe_socket;