xref: /openbsd/gnu/usr.bin/perl/win32/include/sys/socket.h (revision 91f110e0)
1 /* sys/socket.h */
2 
3 /* djl */
4 /* Provide UNIX compatibility */
5 
6 #ifndef  _INC_SYS_SOCKET
7 #define  _INC_SYS_SOCKET
8 
9 #define WIN32_LEAN_AND_MEAN
10 #ifdef __GNUC__
11 #  define Win32_Winsock
12 #endif
13 #include <windows.h>
14 
15 /* Too late to include winsock2.h if winsock.h has already been loaded */
16 #ifndef _WINSOCKAPI_
17 #  if defined(UNDER_CE) && UNDER_CE <= 300
18      /* winsock2 only for 4.00+ */
19 #    include <winsock.h>
20 #  else
21 #    include <winsock2.h>
22      /* We need to include ws2tcpip.h to get the IPv6 definitions.
23       * It will in turn include wspiapi.h.  Later versions of that
24       * header in the Windows SDK generate C++ template code that
25       * can't be compiled with VC6 anymore.  The _WSPIAPI_COUNTOF
26       * definition below prevents wspiapi.h from generating this
27       * incompatible code.
28       */
29 #    define _WSPIAPI_COUNTOF(_Array) (sizeof(_Array) / sizeof(_Array[0]))
30 #    include <ws2tcpip.h>
31 
32 #    ifndef SIO_GET_INTERFACE_LIST_EX
33 
34 #      ifndef MSG_WAITALL
35 #        define MSG_WAITALL     0x8
36 #      endif
37 
38        /* The ws2tcpip.h header included in VC6 doesn't define the
39         * sin6_scope_id member of sockaddr_in6.  We define our own
40         * version and redefine sockaddr_in6 to point to this one
41         * instead for compiling e.g. Socket.xs.
42         */
43 
44        struct my_sockaddr_in6 {
45            short   sin6_family;        /* AF_INET6 */
46            u_short sin6_port;          /* Transport level port number */
47            u_long  sin6_flowinfo;      /* IPv6 flow information */
48            struct in_addr6 sin6_addr;  /* IPv6 address */
49            u_long sin6_scope_id;       /* set of interfaces for a scope */
50        };
51 #      define sockaddr_in6 my_sockaddr_in6
52 
53        /* Provide implementations of IN6ADDR_SETANY() and IN6ADDR_SETLOOPBACK
54         * that also initialize the sin6_scope_id field.
55         */
56 #      undef IN6ADDR_SETANY
57 #      define IN6ADDR_SETANY(x) {\
58 (x)->sin6_family = AF_INET6; \
59 (x)->sin6_port = 0; \
60 (x)->sin6_flowinfo = 0; \
61 *((u_long *)((x)->sin6_addr.s6_addr)    ) = 0; \
62 *((u_long *)((x)->sin6_addr.s6_addr) + 1) = 0; \
63 *((u_long *)((x)->sin6_addr.s6_addr) + 2) = 0; \
64 *((u_long *)((x)->sin6_addr.s6_addr) + 3) = 0; \
65 (x)->sin6_scope_id = 0; \
66 }
67 
68 #      undef IN6ADDR_SETLOOPBACK
69 #      define IN6ADDR_SETLOOPBACK(x) {\
70 (x)->sin6_family = AF_INET6; \
71 (x)->sin6_port = 0; \
72 (x)->sin6_flowinfo = 0; \
73 *((u_long *)((x)->sin6_addr.s6_addr)    ) = 0; \
74 *((u_long *)((x)->sin6_addr.s6_addr) + 1) = 0; \
75 *((u_long *)((x)->sin6_addr.s6_addr) + 2) = 0; \
76 *((u_long *)((x)->sin6_addr.s6_addr) + 3) = 1; \
77 (x)->sin6_scope_id = 0; \
78 }
79 
80 #      ifndef IPV6_HDRINCL
81 #        define IPV6_HDRINCL            2
82 #      endif
83 #      ifndef IPV6_UNICAST_HOPS
84 #        define IPV6_UNICAST_HOPS       4
85 #      endif
86 #      ifndef IPV6_MULTICAST_IF
87 #        define IPV6_MULTICAST_IF       9
88 #      endif
89 #      ifndef IPV6_MULTICAST_HOPS
90 #        define IPV6_MULTICAST_HOPS     10
91 #      endif
92 #      ifndef IPV6_MULTICAST_LOOP
93 #        define IPV6_MULTICAST_LOOP     11
94 #      endif
95 #      ifndef IPV6_ADD_MEMBERSHIP
96 #        define IPV6_ADD_MEMBERSHIP     12
97 #      endif
98 #      ifndef IPV6_DROP_MEMBERSHIP
99 #        define IPV6_DROP_MEMBERSHIP    13
100 #      endif
101 #      ifndef IPV6_JOIN_GROUP
102 #        define IPV6_JOIN_GROUP         IPV6_ADD_MEMBERSHIP
103 #      endif
104 #      ifndef IPV6_LEAVE_GROUP
105 #        define IPV6_LEAVE_GROUP        IPV6_DROP_MEMBERSHIP
106 #      endif
107 #      ifndef IPV6_PKTINFO
108 #        define IPV6_PKTINFO            19
109 #      endif
110 #      ifndef IPV6_HOPLIMIT
111 #        define IPV6_HOPLIMIT           21
112 #      endif
113 #      ifndef IPV6_PROTECTION_LEVEL
114 #        define IPV6_PROTECTION_LEVEL   23
115 #      endif
116 
117        /* The ws2tcpip.h header included in MinGW includes ipv6_mreq already */
118 #      ifndef __GNUC__
119          typedef struct ipv6_mreq {
120              struct in_addr6 ipv6mr_multiaddr;
121              unsigned int    ipv6mr_interface;
122          } IPV6_MREQ;
123 #      endif
124 
125 #      ifndef EAI_AGAIN
126 #        define EAI_AGAIN       WSATRY_AGAIN
127 #      endif
128 #      ifndef EAI_BADFLAGS
129 #        define EAI_BADFLAGS    WSAEINVAL
130 #      endif
131 #      ifndef EAI_FAIL
132 #        define EAI_FAIL        WSANO_RECOVERY
133 #      endif
134 #      ifndef EAI_FAMILY
135 #        define EAI_FAMILY      WSAEAFNOSUPPORT
136 #      endif
137 #      ifndef EAI_MEMORY
138 #        define EAI_MEMORY      WSA_NOT_ENOUGH_MEMORY
139 #      endif
140 #      ifndef EAI_NODATA
141 #        define EAI_NODATA      WSANO_DATA
142 #      endif
143 #      ifndef EAI_NONAME
144 #        define EAI_NONAME      WSAHOST_NOT_FOUND
145 #      endif
146 #      ifndef EAI_SERVICE
147 #        define EAI_SERVICE     WSATYPE_NOT_FOUND
148 #      endif
149 #      ifndef EAI_SOCKTYPE
150 #        define EAI_SOCKTYPE    WSAESOCKTNOSUPPORT
151 #      endif
152 
153 #      ifndef NI_NOFQDN
154 #        define NI_NOFQDN       0x01
155 #      endif
156 #      ifndef NI_NUMERICHOST
157 #        define NI_NUMERICHOST  0x02
158 #      endif
159 #      ifndef NI_NAMEREQD
160 #        define NI_NAMEREQD     0x04
161 #      endif
162 #      ifndef NI_NUMERICSERV
163 #        define NI_NUMERICSERV  0x08
164 #      endif
165 #      ifndef NI_DGRAM
166 #        define NI_DGRAM        0x10
167 #      endif
168 
169 #    endif
170 
171 #  endif
172 #endif
173 
174 /* Early Platform SDKs have an incorrect definition of EAI_NODATA */
175 #if (EAI_NODATA == EAI_NONAME)
176 #  undef EAI_NODATA
177 #  define EAI_NODATA WSANO_DATA
178 #endif
179 
180 #include "win32.h"
181 
182 #ifdef __cplusplus
183 extern "C" {
184 #endif
185 
186 #undef ENOTSOCK
187 #define ENOTSOCK       WSAENOTSOCK
188 
189 #undef ECONNABORTED
190 #define ECONNABORTED WSAECONNABORTED
191 
192 #undef ECONNRESET
193 #define ECONNRESET WSAECONNRESET
194 
195 #undef EAFNOSUPPORT
196 #define EAFNOSUPPORT WSAEAFNOSUPPORT
197 
198 #ifdef USE_SOCKETS_AS_HANDLES
199 
200 #ifndef PERL_FD_SETSIZE
201 #define PERL_FD_SETSIZE		64
202 #endif
203 
204 #define PERL_BITS_PER_BYTE	8
205 #define	PERL_NFDBITS		(sizeof(Perl_fd_mask)*PERL_BITS_PER_BYTE)
206 
207 typedef int			Perl_fd_mask;
208 
209 typedef struct	Perl_fd_set {
210     Perl_fd_mask bits[(PERL_FD_SETSIZE+PERL_NFDBITS-1)/PERL_NFDBITS];
211 }				Perl_fd_set;
212 
213 #define PERL_FD_CLR(n,p) \
214     ((p)->bits[(n)/PERL_NFDBITS] &= ~((unsigned)1 << ((n)%PERL_NFDBITS)))
215 
216 #define PERL_FD_SET(n,p) \
217     ((p)->bits[(n)/PERL_NFDBITS] |=  ((unsigned)1 << ((n)%PERL_NFDBITS)))
218 
219 #define PERL_FD_ZERO(p) memset((char *)(p),0,sizeof(*(p)))
220 
221 #define PERL_FD_ISSET(n,p) \
222     ((p)->bits[(n)/PERL_NFDBITS] &   ((unsigned)1 << ((n)%PERL_NFDBITS)))
223 
224 #else	/* USE_SOCKETS_AS_HANDLES */
225 
226 #define Perl_fd_set	fd_set
227 #define PERL_FD_SET(n,p)	FD_SET(n,p)
228 #define PERL_FD_CLR(n,p)	FD_CLR(n,p)
229 #define PERL_FD_ISSET(n,p)	FD_ISSET(n,p)
230 #define PERL_FD_ZERO(p)		FD_ZERO(p)
231 
232 #endif	/* USE_SOCKETS_AS_HANDLES */
233 
234 SOCKET win32_accept (SOCKET s, struct sockaddr *addr, int *addrlen);
235 int win32_bind (SOCKET s, const struct sockaddr *addr, int namelen);
236 int win32_closesocket (SOCKET s);
237 int win32_connect (SOCKET s, const struct sockaddr *name, int namelen);
238 int win32_ioctlsocket (SOCKET s, long cmd, u_long *argp);
239 int win32_getpeername (SOCKET s, struct sockaddr *name, int * namelen);
240 int win32_getsockname (SOCKET s, struct sockaddr *name, int * namelen);
241 int win32_getsockopt (SOCKET s, int level, int optname, char * optval, int *optlen);
242 u_long win32_htonl (u_long hostlong);
243 u_short win32_htons (u_short hostshort);
244 unsigned long win32_inet_addr (const char * cp);
245 char * win32_inet_ntoa (struct in_addr in);
246 int win32_listen (SOCKET s, int backlog);
247 u_long win32_ntohl (u_long netlong);
248 u_short win32_ntohs (u_short netshort);
249 int win32_recv (SOCKET s, char * buf, int len, int flags);
250 int win32_recvfrom (SOCKET s, char * buf, int len, int flags,
251                          struct sockaddr *from, int * fromlen);
252 int win32_select (int nfds, Perl_fd_set *rfds, Perl_fd_set *wfds, Perl_fd_set *xfds,
253 		  const struct timeval *timeout);
254 int win32_send (SOCKET s, const char * buf, int len, int flags);
255 int win32_sendto (SOCKET s, const char * buf, int len, int flags,
256                        const struct sockaddr *to, int tolen);
257 int win32_setsockopt (SOCKET s, int level, int optname,
258                            const char * optval, int optlen);
259 SOCKET win32_socket (int af, int type, int protocol);
260 int win32_shutdown (SOCKET s, int how);
261 
262 /* Database function prototypes */
263 
264 struct hostent * win32_gethostbyaddr(const char * addr, int len, int type);
265 struct hostent * win32_gethostbyname(const char * name);
266 int win32_gethostname (char * name, int namelen);
267 struct servent * win32_getservbyport(int port, const char * proto);
268 struct servent * win32_getservbyname(const char * name, const char * proto);
269 struct protoent * win32_getprotobynumber(int proto);
270 struct protoent * win32_getprotobyname(const char * name);
271 struct protoent *win32_getprotoent(void);
272 struct servent *win32_getservent(void);
273 void win32_sethostent(int stayopen);
274 void win32_setnetent(int stayopen);
275 struct netent * win32_getnetent(void);
276 struct netent * win32_getnetbyname(char *name);
277 struct netent * win32_getnetbyaddr(long net, int type);
278 void win32_setprotoent(int stayopen);
279 void win32_setservent(int stayopen);
280 void win32_endhostent(void);
281 void win32_endnetent(void);
282 void win32_endprotoent(void);
283 void win32_endservent(void);
284 
285 #ifndef WIN32SCK_IS_STDSCK
286 
287 /* direct to our version */
288 
289 #define htonl		win32_htonl
290 #define htons		win32_htons
291 #define ntohl		win32_ntohl
292 #define ntohs		win32_ntohs
293 #define inet_addr	win32_inet_addr
294 #define inet_ntoa	win32_inet_ntoa
295 
296 #define socket		win32_socket
297 #define bind		win32_bind
298 #define listen		win32_listen
299 #define accept		win32_accept
300 #define connect		win32_connect
301 #define send		win32_send
302 #define sendto		win32_sendto
303 #define recv		win32_recv
304 #define recvfrom	win32_recvfrom
305 #define shutdown	win32_shutdown
306 #define closesocket	win32_closesocket
307 #define ioctlsocket	win32_ioctlsocket
308 #define setsockopt	win32_setsockopt
309 #define getsockopt	win32_getsockopt
310 #define getpeername	win32_getpeername
311 #define getsockname	win32_getsockname
312 #define gethostname	win32_gethostname
313 #define gethostbyname	win32_gethostbyname
314 #define gethostbyaddr	win32_gethostbyaddr
315 #define getprotobyname	win32_getprotobyname
316 #define getprotobynumber win32_getprotobynumber
317 #define getservbyname	win32_getservbyname
318 #define getservbyport	win32_getservbyport
319 #define select		win32_select
320 #define endhostent	win32_endhostent
321 #define endnetent	win32_endnetent
322 #define endprotoent	win32_endprotoent
323 #define endservent	win32_endservent
324 #define getnetent	win32_getnetent
325 #define getnetbyname	win32_getnetbyname
326 #define getnetbyaddr	win32_getnetbyaddr
327 #define getprotoent	win32_getprotoent
328 #define getservent	win32_getservent
329 #define sethostent	win32_sethostent
330 #define setnetent	win32_setnetent
331 #define setprotoent	win32_setprotoent
332 #define setservent	win32_setservent
333 
334 #ifdef USE_SOCKETS_AS_HANDLES
335 #undef fd_set
336 #undef FD_SET
337 #undef FD_CLR
338 #undef FD_ISSET
339 #undef FD_ZERO
340 #define fd_set		Perl_fd_set
341 #define FD_SET(n,p)	PERL_FD_SET(n,p)
342 #define FD_CLR(n,p)	PERL_FD_CLR(n,p)
343 #define FD_ISSET(n,p)	PERL_FD_ISSET(n,p)
344 #define FD_ZERO(p)	PERL_FD_ZERO(p)
345 #endif	/* USE_SOCKETS_AS_HANDLES */
346 
347 #endif	/* WIN32SCK_IS_STDSCK */
348 
349 #ifdef __cplusplus
350 }
351 #endif
352 
353 #endif	/* _INC_SYS_SOCKET */
354