1 /* Copyright (C) 1995, 2001-2021 Free Software Foundation, Inc.
2 
3 This file is part of GNU Emacs.
4 
5 GNU Emacs is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or (at
8 your option) any later version.
9 
10 GNU Emacs is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 
19 /* Workable version of <sys/socket.h> based on winsock.h */
20 
21 #ifndef _SOCKET_H_
22 #define _SOCKET_H_
23 
24 /* defeat the multiple include protection */
25 #ifdef _WINSOCKAPI_
26 #undef _WINSOCKAPI_
27 #endif
28 #ifdef _WINSOCK_H
29 #undef _WINSOCK_H
30 #endif
31 
32 /* avoid confusion with our version of select */
33 #ifdef select
34 #undef select
35 #define MUST_REDEF_SELECT
36 #endif
37 
38 /* avoid clashing with our version of FD_SET if already defined */
39 #ifdef FD_SET
40 #undef FD_SET
41 #undef FD_CLR
42 #undef FD_ISSET
43 #undef FD_ZERO
44 #endif
45 
46 /* Avoid duplicate definition of timeval.  MinGW uses _TIMEVAL_DEFINED
47    in sys/time.h to avoid that.  */
48 #if defined (HAVE_TIMEVAL) && defined (_MSC_VER)
49 #define timeval ws_timeval
50 #endif
51 
52 #if defined __MINGW32_VERSION && __MINGW32_VERSION >= 5000002L
53 /* Need winerror.h before winsock2.h with mingw.org's MinGW 5.x,
54    otherwise some error codes are not defined.  */
55 # include <winerror.h>
56 #endif
57 #include <winsock2.h>
58 #include <ws2tcpip.h>
59 /* process.c uses uint16_t (from C99) for IPv6, but
60    apparently it is not defined in some versions of mingw and msvc.  */
61 #include <stdint.h>
62 #ifndef UINT16_C
63 typedef unsigned short uint16_t;
64 #endif
65 
66 /* redefine select to reference our version */
67 #ifdef MUST_REDEF_SELECT
68 #define select sys_select
69 #undef MUST_REDEF_SELECT
70 #endif
71 
72 /* Revert to our version of FD_SET, but not when included from test
73    programs run by configure.  */
74 #ifdef EMACS_CONFIG_H
75 #undef FD_SET
76 #undef FD_CLR
77 #undef FD_ISSET
78 #undef FD_ZERO
79 
80 /* allow us to provide our own version of fd_set */
81 #define fd_set ws_fd_set
82 #include "w32.h"
83 #endif	/* EMACS_CONFIG_H */
84 
85 #if defined (HAVE_TIMEVAL) && defined (_MSC_VER)
86 #undef timeval
87 #endif
88 
89 /* shadow functions where we provide our own wrapper */
90 #define socket         sys_socket
91 #define bind           sys_bind
92 #define connect        sys_connect
93 #define htons          sys_htons
94 #define ntohs          sys_ntohs
95 #define htonl          sys_htonl
96 #define ntohl          sys_ntohl
97 #define inet_addr      sys_inet_addr
98 #define gethostname    sys_gethostname
99 #define gethostbyname  sys_gethostbyname
100 #define getpeername    sys_getpeername
101 #define getservbyname  sys_getservbyname
102 #define shutdown       sys_shutdown
103 #define setsockopt     sys_setsockopt
104 #define listen         sys_listen
105 #define getsockname    sys_getsockname
106 #define accept         sys_accept
107 #define recvfrom       sys_recvfrom
108 #define sendto         sys_sendto
109 #define getaddrinfo    sys_getaddrinfo
110 #define freeaddrinfo   sys_freeaddrinfo
111 
112 int sys_socket(int af, int type, int protocol);
113 int sys_bind (int s, const struct sockaddr *addr, int namelen);
114 int sys_connect (int s, const struct sockaddr *addr, int namelen);
115 u_short sys_htons (u_short hostshort);
116 u_short sys_ntohs (u_short netshort);
117 u_long sys_htonl (u_long hostlong);
118 u_long sys_ntohl (u_long netlong);
119 unsigned long sys_inet_addr (const char * cp);
120 int sys_gethostname (char * name, int namelen);
121 struct hostent * sys_gethostbyname (const char * name);
122 struct servent * sys_getservbyname (const char * name, const char * proto);
123 int sys_getpeername (int s, struct sockaddr *addr, int * namelen);
124 int sys_shutdown (int socket, int how);
125 int sys_setsockopt (int s, int level, int oname, const void * oval, int olen);
126 int sys_listen (int s, int backlog);
127 int sys_getsockname (int s, struct sockaddr * name, int * namelen);
128 int sys_accept (int s, struct sockaddr *addr, int *addrlen);
129 int sys_recvfrom (int s, char *buf, int len, int flags,
130 		  struct sockaddr *from, int * fromlen);
131 int sys_sendto (int s, const char * buf, int len, int flags,
132 		const struct sockaddr *to, int tolen);
133 int sys_getaddrinfo (const char * node, const char * service,
134 		     const struct addrinfo * hints, struct addrinfo ** res);
135 void sys_freeaddrinfo (struct addrinfo * ai);
136 
137 /* In addition to wrappers for the winsock functions, we also provide
138    an fcntl function, for setting sockets to non-blocking mode.  */
139 int fcntl (int s, int cmd, int options);
140 #define F_SETFL   4
141 #define F_SETFD   2
142 #define O_NONBLOCK  04000
143 #define O_CLOEXEC O_NOINHERIT
144 #define F_DUPFD_CLOEXEC 0x40000000
145 #define FD_CLOEXEC 1
146 
147 /* we are providing a real h_errno variable */
148 #undef h_errno
149 extern int h_errno;
150 
151 /* map winsock error codes to standard names */
152 #if defined(EWOULDBLOCK)
153 #undef EWOULDBLOCK
154 #endif
155 #define EWOULDBLOCK             WSAEWOULDBLOCK
156 #if defined(EINPROGRESS)
157 #undef EINPROGRESS
158 #endif
159 #define EINPROGRESS             WSAEINPROGRESS
160 #if defined(EALREADY)
161 #undef EALREADY
162 #endif
163 #define EALREADY                WSAEALREADY
164 #if defined(ENOTSOCK)
165 #undef ENOTSOCK
166 #endif
167 #define ENOTSOCK                WSAENOTSOCK
168 #if defined(EDESTADDRREQ)
169 #undef EDESTADDRREQ
170 #endif
171 #define EDESTADDRREQ            WSAEDESTADDRREQ
172 #if defined(EMSGSIZE)
173 #undef EMSGSIZE
174 #endif
175 #define EMSGSIZE                WSAEMSGSIZE
176 #if defined(EPROTOTYPE)
177 #undef EPROTOTYPE
178 #endif
179 #define EPROTOTYPE              WSAEPROTOTYPE
180 #if defined(ENOPROTOOPT)
181 #undef ENOPROTOOPT
182 #endif
183 #define ENOPROTOOPT             WSAENOPROTOOPT
184 #if defined(EPROTONOSUPPORT)
185 #undef EPROTONOSUPPORT
186 #endif
187 #define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
188 #if defined(ESOCKTNOSUPPORT)
189 #undef ESOCKTNOSUPPORT
190 #endif
191 #define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
192 #if defined(EOPNOTSUPP)
193 #undef EOPNOTSUPP
194 #endif
195 #define EOPNOTSUPP              WSAEOPNOTSUPP
196 #if defined(EPFNOSUPPORT)
197 #undef EPFNOSUPPORT
198 #endif
199 #define EPFNOSUPPORT            WSAEPFNOSUPPORT
200 #if defined(EAFNOSUPPORT)
201 #undef EAFNOSUPPORT
202 #endif
203 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
204 #if defined(EADDRINUSE)
205 #undef EADDRINUSE
206 #endif
207 #define EADDRINUSE              WSAEADDRINUSE
208 #if defined(EADDRNOTAVAIL)
209 #undef EADDRNOTAVAIL
210 #endif
211 #define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
212 #if defined(ENETDOWN)
213 #undef ENETDOWN
214 #endif
215 #define ENETDOWN                WSAENETDOWN
216 #if defined(ENETUNREACH)
217 #undef ENETUNREACH
218 #endif
219 #define ENETUNREACH             WSAENETUNREACH
220 #if defined(ENETRESET)
221 #undef ENETRESET
222 #endif
223 #define ENETRESET               WSAENETRESET
224 #if defined(ECONNABORTED)
225 #undef ECONNABORTED
226 #endif
227 #define ECONNABORTED            WSAECONNABORTED
228 #if defined(ECONNRESET)
229 #undef ECONNRESET
230 #endif
231 #define ECONNRESET              WSAECONNRESET
232 #if defined(ENOBUFS)
233 #undef ENOBUFS
234 #endif
235 #define ENOBUFS                 WSAENOBUFS
236 #if defined(EISCONN)
237 #undef EISCONN
238 #endif
239 #define EISCONN                 WSAEISCONN
240 #if defined(ENOTCONN)
241 #undef ENOTCONN
242 #endif
243 #define ENOTCONN                WSAENOTCONN
244 #if defined(ESHUTDOWN)
245 #undef ESHUTDOWN
246 #endif
247 #define ESHUTDOWN               WSAESHUTDOWN
248 #if defined(ETOOMANYREFS)
249 #undef ETOOMANYREFS
250 #endif
251 #define ETOOMANYREFS            WSAETOOMANYREFS
252 #if defined(ETIMEDOUT)
253 #undef ETIMEDOUT
254 #endif
255 #define ETIMEDOUT               WSAETIMEDOUT
256 #if defined(ECONNREFUSED)
257 #undef ECONNREFUSED
258 #endif
259 #define ECONNREFUSED            WSAECONNREFUSED
260 #if defined(ELOOP)
261 #undef ELOOP
262 #endif
263 #define ELOOP                   WSAELOOP
264 /* #define ENAMETOOLONG            WSAENAMETOOLONG */
265 #if defined(EHOSTDOWN)
266 #undef EHOSTDOWN
267 #endif
268 #define EHOSTDOWN               WSAEHOSTDOWN
269 #if defined(EHOSTUNREACH)
270 #undef EHOSTUNREACH
271 #endif
272 #define EHOSTUNREACH            WSAEHOSTUNREACH
273 /* #define ENOTEMPTY               WSAENOTEMPTY */
274 #if defined(EPROCLIM)
275 #undef EPROCLIM
276 #endif
277 #define EPROCLIM                WSAEPROCLIM
278 #if defined(EUSERS)
279 #undef EUSERS
280 #endif
281 #define EUSERS                  WSAEUSERS
282 #if defined(EDQUOT)
283 #undef EDQUOT
284 #endif
285 #define EDQUOT                  WSAEDQUOT
286 #if defined(ESTALE)
287 #undef ESTALE
288 #endif
289 #define ESTALE                  WSAESTALE
290 #if defined(EREMOTE)
291 #undef EREMOTE
292 #endif
293 #define EREMOTE                 WSAEREMOTE
294 
295 #endif /* _SOCKET_H_ */
296 
297 /* end of socket.h */
298