Codechange: Update minimum CMake version to 3.16 for all parts. (#13141)
[openttd-github.git] / src / network / core / os_abstraction.h
blobcc35c01436398552df924b224c4f158f5d9f0600
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file os_abstraction.h Network stuff has many things that needs to be
10 * included and/or implemented by default.
11 * All those things are in this file.
14 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
15 #define NETWORK_CORE_OS_ABSTRACTION_H
17 /**
18 * Abstraction of a network error where all implementation details of the
19 * error codes are encapsulated in this class and the abstraction layer.
21 class NetworkError {
22 private:
23 int error; ///< The underlying error number from errno or WSAGetLastError.
24 mutable std::string message; ///< The string representation of the error (set on first call to #AsString).
25 public:
26 NetworkError(int error);
28 bool HasError() const;
29 bool WouldBlock() const;
30 bool IsConnectionReset() const;
31 bool IsConnectInProgress() const;
32 const std::string &AsString() const;
34 static NetworkError GetLast();
37 /* Include standard stuff per OS */
39 /* Windows stuff */
40 #if defined(_WIN32)
41 #include <errno.h>
42 #include <winsock2.h>
43 #include <ws2tcpip.h>
44 #include <windows.h>
46 /* Windows has some different names for some types */
47 typedef unsigned long in_addr_t;
49 /* Handle cross-compilation with --build=*-*-cygwin --host=*-*-mingw32 */
50 #if defined(__MINGW32__) && !defined(AI_ADDRCONFIG)
51 # define AI_ADDRCONFIG 0x00000400
52 #endif
54 #if !(defined(__MINGW32__) || defined(__CYGWIN__))
55 /* Windows has some different names for some types */
56 typedef SSIZE_T ssize_t;
57 typedef int socklen_t;
58 # define IPPROTO_IPV6 41
59 #endif /* !(__MINGW32__ && __CYGWIN__) */
60 #endif /* _WIN32 */
62 /* UNIX stuff */
63 #if defined(UNIX)
64 # if defined(OPENBSD) || defined(__NetBSD__)
65 # define AI_ADDRCONFIG 0
66 # endif
67 # define SOCKET int
68 # define INVALID_SOCKET -1
69 # define closesocket close
70 /* Need this for FIONREAD on solaris */
71 # define BSD_COMP
73 /* Includes needed for UNIX-like systems */
74 # include <unistd.h>
75 # include <sys/ioctl.h>
76 # include <sys/socket.h>
77 # include <netinet/in.h>
78 # include <netinet/tcp.h>
79 # include <arpa/inet.h>
80 # include <net/if.h>
81 # include <ifaddrs.h>
82 # if !defined(INADDR_NONE)
83 # define INADDR_NONE 0xffffffff
84 # endif
86 # if defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
87 typedef uint32_t in_addr_t;
88 # endif
90 # include <errno.h>
91 # include <sys/time.h>
92 # include <netdb.h>
94 # if defined(__EMSCRIPTEN__)
95 /* Emscripten doesn't support AI_ADDRCONFIG and errors out on it. */
96 # undef AI_ADDRCONFIG
97 # define AI_ADDRCONFIG 0
98 /* Emscripten says it supports FD_SETSIZE fds, but it really only supports 64.
99 * https://github.com/emscripten-core/emscripten/issues/1711 */
100 # undef FD_SETSIZE
101 # define FD_SETSIZE 64
102 # endif
104 /* Haiku says it supports FD_SETSIZE fds, but it really only supports 512. */
105 # if defined(__HAIKU__)
106 # undef FD_SETSIZE
107 # define FD_SETSIZE 512
108 # endif
110 #endif /* UNIX */
112 #ifdef __EMSCRIPTEN__
114 * Emscripten doesn't set 'addrlen' for accept(), getsockname(), getpeername()
115 * and recvfrom(), which confuses other functions and causes them to crash.
116 * This function needs to be called after these four functions to make sure
117 * 'addrlen' is patched up.
119 * https://github.com/emscripten-core/emscripten/issues/12996
121 * @param address The address returned by those four functions.
122 * @return The correct value for addrlen.
124 inline socklen_t FixAddrLenForEmscripten(struct sockaddr_storage &address)
126 switch (address.ss_family) {
127 case AF_INET6: return sizeof(struct sockaddr_in6);
128 case AF_INET: return sizeof(struct sockaddr_in);
129 default: NOT_REACHED();
132 #endif
135 bool SetNonBlocking(SOCKET d);
136 bool SetNoDelay(SOCKET d);
137 bool SetReusePort(SOCKET d);
138 NetworkError GetSocketError(SOCKET d);
140 /* Make sure these structures have the size we expect them to be */
141 static_assert(sizeof(in_addr) == 4); ///< IPv4 addresses should be 4 bytes.
142 static_assert(sizeof(in6_addr) == 16); ///< IPv6 addresses should be 16 bytes.
144 #endif /* NETWORK_CORE_OS_ABSTRACTION_H */