OpenTTD
os_abstraction.h
Go to the documentation of this file.
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  */
7 
14 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
15 #define NETWORK_CORE_OS_ABSTRACTION_H
16 
17 /* Include standard stuff per OS */
18 
19 /* Windows stuff */
20 #if defined(_WIN32)
21 #include <errno.h>
22 #include <winsock2.h>
23 #include <ws2tcpip.h>
24 #include <windows.h>
25 
26 #define GET_LAST_ERROR() WSAGetLastError()
27 #undef EWOULDBLOCK
28 #define EWOULDBLOCK WSAEWOULDBLOCK
29 /* Windows has some different names for some types */
30 typedef unsigned long in_addr_t;
31 
32 /* Handle cross-compilation with --build=*-*-cygwin --host=*-*-mingw32 */
33 #if defined(__MINGW32__) && !defined(AI_ADDRCONFIG)
34 # define AI_ADDRCONFIG 0x00000400
35 #endif
36 
37 #if !(defined(__MINGW32__) || defined(__CYGWIN__))
38  /* Windows has some different names for some types */
39  typedef SSIZE_T ssize_t;
40  typedef int socklen_t;
41 # define IPPROTO_IPV6 41
42 #endif /* !(__MINGW32__ && __CYGWIN__) */
43 #endif /* _WIN32 */
44 
45 /* UNIX stuff */
46 #if defined(UNIX) && !defined(__OS2__)
47 # if defined(OPENBSD) || defined(__NetBSD__)
48 # define AI_ADDRCONFIG 0
49 # endif
50 # define SOCKET int
51 # define INVALID_SOCKET -1
52 # define ioctlsocket ioctl
53 # define closesocket close
54 # define GET_LAST_ERROR() (errno)
55 /* Need this for FIONREAD on solaris */
56 # define BSD_COMP
57 
58 /* Includes needed for UNIX-like systems */
59 # include <unistd.h>
60 # include <sys/ioctl.h>
61 # include <sys/socket.h>
62 # include <netinet/in.h>
63 # include <netinet/tcp.h>
64 # include <arpa/inet.h>
65 # include <net/if.h>
66 /* According to glibc/NEWS, <ifaddrs.h> appeared in glibc-2.3. */
67 # if !defined(__sgi__) && !defined(SUNOS) && !defined(__INNOTEK_LIBC__) \
68  && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__) && !defined(HPUX)
69 /* If for any reason ifaddrs.h does not exist on your system, comment out
70  * the following two lines and an alternative way will be used to fetch
71  * the list of IPs from the system. */
72 # include <ifaddrs.h>
73 # define HAVE_GETIFADDRS
74 # endif
75 # if !defined(INADDR_NONE)
76 # define INADDR_NONE 0xffffffff
77 # endif
78 
79 # if defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
80  typedef uint32_t in_addr_t;
81 # endif
82 
83 # include <errno.h>
84 # include <sys/time.h>
85 # include <netdb.h>
86 #endif /* UNIX */
87 
88 /* OS/2 stuff */
89 #if defined(__OS2__)
90 # define SOCKET int
91 # define INVALID_SOCKET -1
92 # define ioctlsocket ioctl
93 # define closesocket close
94 # define GET_LAST_ERROR() (sock_errno())
95 
96 /* Includes needed for OS/2 systems */
97 # include <types.h>
98 # include <unistd.h>
99 # include <sys/ioctl.h>
100 # include <sys/socket.h>
101 # include <netinet/in.h>
102 # include <netinet/tcp.h>
103 # include <arpa/inet.h>
104 # include <net/if.h>
105 # include <errno.h>
106 # include <sys/time.h>
107 # include <netdb.h>
108 # include <nerrno.h>
109 # define INADDR_NONE 0xffffffff
110 # include "../../3rdparty/os2/getaddrinfo.h"
111 # include "../../3rdparty/os2/getnameinfo.h"
112 
113 #define IPV6_V6ONLY 27
114 
115 /*
116  * IPv6 address
117  */
118 struct in6_addr {
119  union {
120  uint8_t __u6_addr8[16];
121  uint16_t __u6_addr16[8];
122  uint32_t __u6_addr32[4];
123  } __u6_addr; /* 128-bit IP6 address */
124 };
125 
126 #define s6_addr __u6_addr.__u6_addr8
127 
128 struct sockaddr_in6 {
129  uint8_t sin6_len; /* length of this struct */
130  sa_family_t sin6_family; /* AF_INET6 */
131  in_port_t sin6_port; /* Transport layer port # */
132  uint32_t sin6_flowinfo; /* IP6 flow information */
133  struct in6_addr sin6_addr; /* IP6 address */
134  uint32_t sin6_scope_id; /* scope zone index */
135 };
136 
137 typedef int socklen_t;
138 #if !defined(__INNOTEK_LIBC__)
139 typedef unsigned long in_addr_t;
140 #endif /* __INNOTEK_LIBC__ */
141 
142 #endif /* OS/2 */
143 
149 static inline bool SetNonBlocking(SOCKET d)
150 {
151 #ifdef _WIN32
152  u_long nonblocking = 1;
153 #else
154  int nonblocking = 1;
155 #endif
156  return ioctlsocket(d, FIONBIO, &nonblocking) == 0;
157 }
158 
164 static inline bool SetNoDelay(SOCKET d)
165 {
166  /* XXX should this be done at all? */
167  int b = 1;
168  /* The (const char*) cast is needed for windows */
169  return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)) == 0;
170 }
171 
172 /* Make sure these structures have the size we expect them to be */
173 assert_compile(sizeof(in_addr) == 4);
174 assert_compile(sizeof(in6_addr) == 16);
175 
176 #endif /* NETWORK_CORE_OS_ABSTRACTION_H */
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
assert_compile(sizeof(in_addr)==4)
IPv4 addresses should be 4 bytes.
static bool SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.