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