This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlre: Add another mnemonic for /d
[perl5.git] / win32 / win32sck.c
CommitLineData
326b05e3 1/* win32sck.c
68dc0745
PP
2 *
3 * (c) 1995 Microsoft Corporation. All rights reserved.
0d130a44 4 * Developed by hip communications inc.
68dc0745
PP
5 * Portions (c) 1993 Intergraph Corporation. All rights reserved.
6 *
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
9 */
0a753a76 10
390b85e7 11#define WIN32IO_IS_STDIO
55d25626 12#define WIN32SCK_IS_STDSCK
0a753a76 13#define WIN32_LEAN_AND_MEAN
8fada4f6 14#define PERLIO_NOT_STDIO 0
a835ef8a
NIS
15#ifdef __GNUC__
16#define Win32_Winsock
17#endif
81a306d4 18#include <wchar.h>
390b85e7 19#include <windows.h>
036c1c1e
SH
20#include <ws2spi.h>
21
0a753a76
PP
22#include "EXTERN.h"
23#include "perl.h"
c69f6586 24
58a50f62 25#include "Win32iop.h"
0a753a76
PP
26#include <sys/socket.h>
27#include <fcntl.h>
28#include <sys/stat.h>
29#include <assert.h>
390b85e7 30#include <io.h>
0a753a76 31
68dc0745 32/* thanks to Beverly Brown (beverly@datacube.com) */
72e6b643
SH
33#define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
34#define TO_SOCKET(x) _get_osfhandle(x)
0a753a76 35
326b05e3
GS
36#define StartSockets() \
37 STMT_START { \
38 if (!wsock_started) \
39 start_sockets(); \
40 } STMT_END
41
326b05e3
GS
42#define SOCKET_TEST(x, y) \
43 STMT_START { \
44 StartSockets(); \
45 if((x) == (y)) \
22e30cb2
SH
46 { \
47 int wsaerr = WSAGetLastError(); \
48 errno = convert_wsa_error_to_errno(wsaerr); \
49 SetLastError(wsaerr); \
50 } \
326b05e3
GS
51 } STMT_END
52
53#define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
54
137443ea
PP
55static struct servent* win32_savecopyservent(struct servent*d,
56 struct servent*s,
57 const char *proto);
0a753a76 58
326b05e3 59static int wsock_started = 0;
68dc0745 60
b47a847f
DD
61#ifdef WIN32_DYN_IOINFO_SIZE
62EXTERN_C Size_t w32_ioinfo_size;
63#endif
64
b73db59c
GS
65EXTERN_C void
66EndSockets(void)
67{
68 if (wsock_started)
69 WSACleanup();
70}
71
d31f3d60
SH
72/* Translate WSAExxx values to corresponding Exxx values where possible. Not all
73 * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in
74 * VC++ 2010 and above, which have expanded <errno.h> with more values), but
75 * most missing constants are provided by win32/include/sys/errno2.h. The few
76 * that are not are returned unchanged.
77 *
c9beaf97
SH
78 * The list of possible WSAExxx values used here comes from the MSDN page
79 * titled "Windows Sockets Error Codes".
d31f3d60 80 *
c9beaf97
SH
81 * (Note: Only the WSAExxx values are handled here; other WSAxxx values are
82 * returned unchanged. The return value normally ends up in errno/$! and at
83 * the Perl code level may be tested against the Exxx constants exported by
84 * the Errno and POSIX modules, which have never handled the other WSAxxx
85 * values themselves, apparently without any ill effect so far.)
86 */
87int
88convert_wsa_error_to_errno(int wsaerr)
89{
90 switch (wsaerr) {
b0ba2190
SH
91 case WSAEINTR:
92 return EINTR;
93 case WSAEBADF:
94 return EBADF;
95 case WSAEACCES:
96 return EACCES;
97 case WSAEFAULT:
98 return EFAULT;
99 case WSAEINVAL:
100 return EINVAL;
101 case WSAEMFILE:
102 return EMFILE;
103 case WSAEWOULDBLOCK:
104 return EWOULDBLOCK;
105 case WSAEINPROGRESS:
106 return EINPROGRESS;
107 case WSAEALREADY:
108 return EALREADY;
109 case WSAENOTSOCK:
110 return ENOTSOCK;
111 case WSAEDESTADDRREQ:
112 return EDESTADDRREQ;
113 case WSAEMSGSIZE:
114 return EMSGSIZE;
115 case WSAEPROTOTYPE:
116 return EPROTOTYPE;
117 case WSAENOPROTOOPT:
118 return ENOPROTOOPT;
119 case WSAEPROTONOSUPPORT:
120 return EPROTONOSUPPORT;
121 case WSAESOCKTNOSUPPORT:
122 return ESOCKTNOSUPPORT;
123 case WSAEOPNOTSUPP:
124 return EOPNOTSUPP;
125 case WSAEPFNOSUPPORT:
126 return EPFNOSUPPORT;
127 case WSAEAFNOSUPPORT:
128 return EAFNOSUPPORT;
129 case WSAEADDRINUSE:
130 return EADDRINUSE;
131 case WSAEADDRNOTAVAIL:
132 return EADDRNOTAVAIL;
133 case WSAENETDOWN:
134 return ENETDOWN;
135 case WSAENETUNREACH:
136 return ENETUNREACH;
137 case WSAENETRESET:
138 return ENETRESET;
139 case WSAECONNABORTED:
140 return ECONNABORTED;
141 case WSAECONNRESET:
142 return ECONNRESET;
143 case WSAENOBUFS:
144 return ENOBUFS;
145 case WSAEISCONN:
146 return EISCONN;
147 case WSAENOTCONN:
148 return ENOTCONN;
149 case WSAESHUTDOWN:
150 return ESHUTDOWN;
151 case WSAETOOMANYREFS:
152 return ETOOMANYREFS;
153 case WSAETIMEDOUT:
154 return ETIMEDOUT;
155 case WSAECONNREFUSED:
156 return ECONNREFUSED;
157 case WSAELOOP:
158 return ELOOP;
159 case WSAENAMETOOLONG:
160 return ENAMETOOLONG;
161 case WSAEHOSTDOWN:
d31f3d60 162 return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */
b0ba2190
SH
163 case WSAEHOSTUNREACH:
164 return EHOSTUNREACH;
165 case WSAENOTEMPTY:
166 return ENOTEMPTY;
167 case WSAEPROCLIM:
168 return EPROCLIM;
169 case WSAEUSERS:
170 return EUSERS;
171 case WSAEDQUOT:
172 return EDQUOT;
173 case WSAESTALE:
174 return ESTALE;
175 case WSAEREMOTE:
176 return EREMOTE;
177 case WSAEDISCON:
d31f3d60 178 return WSAEDISCON; /* EDISCON is not defined */
b0ba2190 179 case WSAENOMORE:
d31f3d60 180 return WSAENOMORE; /* ENOMORE is not defined */
4eaf3102
SH
181#ifdef WSAECANCELLED
182 case WSAECANCELLED: /* New in WinSock2 */
cd6a3131 183 return ECANCELED;
4eaf3102 184#endif
b0ba2190 185 case WSAEINVALIDPROCTABLE:
d31f3d60 186 return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */
b0ba2190 187 case WSAEINVALIDPROVIDER:
d31f3d60 188 return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */
b0ba2190 189 case WSAEPROVIDERFAILEDINIT:
d31f3d60 190 return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */
b0ba2190 191 case WSAEREFUSED:
d31f3d60 192 return WSAEREFUSED; /* EREFUSED is not defined */
b0ba2190
SH
193 }
194
c9beaf97 195 return wsaerr;
b0ba2190
SH
196}
197
e85fa3eb 198#ifdef ERRNO_HAS_POSIX_SUPPLEMENT
cd6a3131
SH
199/* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
200 * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
4eaf3102 201 * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
cd6a3131 202 * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
e85fa3eb
SH
203 * really expect to encounter them anyway in the context in which this function
204 * is called.
4f79e9b1
SH
205 * Some versions of MinGW/gcc-4.8 and above also define most, but not all, of
206 * these extra Exxx values. The missing ones are all cases for which there is no
207 * corresponding WSAExxx constant anyway, so we simply omit the cases for them
208 * here.
cd6a3131
SH
209 * Other Exxx values (err < sys_nerr) are returned unchanged.
210 */
211int
212convert_errno_to_wsa_error(int err)
213{
214 switch (err) {
215 case EADDRINUSE:
216 return WSAEADDRINUSE;
217 case EADDRNOTAVAIL:
218 return WSAEADDRNOTAVAIL;
219 case EAFNOSUPPORT:
220 return WSAEAFNOSUPPORT;
221 case EALREADY:
222 return WSAEALREADY;
4f79e9b1
SH
223#ifdef EBADMSG
224 case EBADMSG: /* Not defined in gcc-4.8.0 */
cd6a3131 225 return ERROR_INVALID_FUNCTION;
4f79e9b1 226#endif
cd6a3131 227 case ECANCELED:
4eaf3102
SH
228#ifdef WSAECANCELLED
229 return WSAECANCELLED; /* New in WinSock2 */
230#else
231 return ERROR_INVALID_FUNCTION;
232#endif
cd6a3131
SH
233 case ECONNABORTED:
234 return WSAECONNABORTED;
235 case ECONNREFUSED:
236 return WSAECONNREFUSED;
237 case ECONNRESET:
238 return WSAECONNRESET;
239 case EDESTADDRREQ:
240 return WSAEDESTADDRREQ;
241 case EHOSTUNREACH:
242 return WSAEHOSTUNREACH;
4f79e9b1
SH
243#ifdef EIDRM
244 case EIDRM: /* Not defined in gcc-4.8.0 */
cd6a3131 245 return ERROR_INVALID_FUNCTION;
4f79e9b1 246#endif
cd6a3131
SH
247 case EINPROGRESS:
248 return WSAEINPROGRESS;
249 case EISCONN:
250 return WSAEISCONN;
251 case ELOOP:
252 return WSAELOOP;
253 case EMSGSIZE:
254 return WSAEMSGSIZE;
255 case ENETDOWN:
256 return WSAENETDOWN;
257 case ENETRESET:
258 return WSAENETRESET;
259 case ENETUNREACH:
260 return WSAENETUNREACH;
261 case ENOBUFS:
262 return WSAENOBUFS;
4f79e9b1
SH
263#ifdef ENODATA
264 case ENODATA: /* Not defined in gcc-4.8.0 */
cd6a3131 265 return ERROR_INVALID_FUNCTION;
4f79e9b1
SH
266#endif
267#ifdef ENOLINK
268 case ENOLINK: /* Not defined in gcc-4.8.0 */
cd6a3131 269 return ERROR_INVALID_FUNCTION;
4f79e9b1
SH
270#endif
271#ifdef ENOMSG
272 case ENOMSG: /* Not defined in gcc-4.8.0 */
cd6a3131 273 return ERROR_INVALID_FUNCTION;
4f79e9b1 274#endif
cd6a3131
SH
275 case ENOPROTOOPT:
276 return WSAENOPROTOOPT;
4f79e9b1
SH
277#ifdef ENOSR
278 case ENOSR: /* Not defined in gcc-4.8.0 */
cd6a3131 279 return ERROR_INVALID_FUNCTION;
4f79e9b1
SH
280#endif
281#ifdef ENOSTR
282 case ENOSTR: /* Not defined in gcc-4.8.0 */
cd6a3131 283 return ERROR_INVALID_FUNCTION;
4f79e9b1 284#endif
cd6a3131
SH
285 case ENOTCONN:
286 return WSAENOTCONN;
4f79e9b1
SH
287#ifdef ENOTRECOVERABLE
288 case ENOTRECOVERABLE: /* Not defined in gcc-4.8.0 */
cd6a3131 289 return ERROR_INVALID_FUNCTION;
4f79e9b1 290#endif
cd6a3131
SH
291 case ENOTSOCK:
292 return WSAENOTSOCK;
293 case ENOTSUP:
294 return ERROR_INVALID_FUNCTION;
295 case EOPNOTSUPP:
296 return WSAEOPNOTSUPP;
4f79e9b1
SH
297#ifdef EOTHER
298 case EOTHER: /* Not defined in gcc-4.8.0 */
cd6a3131 299 return ERROR_INVALID_FUNCTION;
4f79e9b1 300#endif
cd6a3131
SH
301 case EOVERFLOW:
302 return ERROR_INVALID_FUNCTION;
303 case EOWNERDEAD:
304 return ERROR_INVALID_FUNCTION;
305 case EPROTO:
306 return ERROR_INVALID_FUNCTION;
307 case EPROTONOSUPPORT:
308 return WSAEPROTONOSUPPORT;
309 case EPROTOTYPE:
310 return WSAEPROTOTYPE;
4f79e9b1
SH
311#ifdef ETIME
312 case ETIME: /* Not defined in gcc-4.8.0 */
cd6a3131 313 return ERROR_INVALID_FUNCTION;
4f79e9b1 314#endif
cd6a3131
SH
315 case ETIMEDOUT:
316 return WSAETIMEDOUT;
4f79e9b1
SH
317#ifdef ETXTBSY
318 case ETXTBSY: /* Not defined in gcc-4.8.0 */
cd6a3131 319 return ERROR_INVALID_FUNCTION;
4f79e9b1 320#endif
cd6a3131
SH
321 case EWOULDBLOCK:
322 return WSAEWOULDBLOCK;
323 }
324
325 return err;
326}
e85fa3eb 327#endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
cd6a3131 328
68dc0745 329void
326b05e3 330start_sockets(void)
68dc0745
PP
331{
332 unsigned short version;
333 WSADATA retdata;
334 int ret;
68dc0745 335
68dc0745
PP
336 /*
337 * initalize the winsock interface and insure that it is
338 * cleaned up at exit.
339 */
036c1c1e 340 version = 0x2;
326b05e3 341 if(ret = WSAStartup(version, &retdata))
4f63d024 342 Perl_croak_nocontext("Unable to locate winsock library!\n");
68dc0745 343 if(retdata.wVersion != version)
036c1c1e 344 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
68dc0745
PP
345
346 /* atexit((void (*)(void)) EndSockets); */
401ef382
SN
347 wsock_started = 1;
348}
0a753a76 349
36bb5e1d
NC
350/* in no sockets Win32 builds, these use the inline functions defined in
351 * perl.h
19253ae6 352 */
68dc0745
PP
353u_long
354win32_htonl(u_long hostlong)
0a753a76 355{
36bb5e1d 356#ifndef WIN32_NO_SOCKETS
326b05e3 357 StartSockets();
19253ae6 358#endif
326b05e3 359 return htonl(hostlong);
0a753a76
PP
360}
361
68dc0745
PP
362u_short
363win32_htons(u_short hostshort)
0a753a76 364{
36bb5e1d 365#ifndef WIN32_NO_SOCKETS
326b05e3 366 StartSockets();
19253ae6 367#endif
326b05e3 368 return htons(hostshort);
0a753a76
PP
369}
370
68dc0745
PP
371u_long
372win32_ntohl(u_long netlong)
0a753a76 373{
36bb5e1d 374#ifndef WIN32_NO_SOCKETS
326b05e3 375 StartSockets();
19253ae6 376#endif
326b05e3 377 return ntohl(netlong);
0a753a76
PP
378}
379
68dc0745
PP
380u_short
381win32_ntohs(u_short netshort)
0a753a76 382{
36bb5e1d 383#ifndef WIN32_NO_SOCKETS
326b05e3 384 StartSockets();
19253ae6 385#endif
326b05e3 386 return ntohs(netshort);
0a753a76
PP
387}
388
389
0a753a76 390
68dc0745
PP
391SOCKET
392win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 393{
68dc0745 394 SOCKET r;
0a753a76 395
326b05e3 396 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
68dc0745 397 return OPEN_SOCKET(r);
0a753a76
PP
398}
399
68dc0745
PP
400int
401win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
0a753a76 402{
68dc0745 403 int r;
0a753a76 404
326b05e3 405 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
68dc0745 406 return r;
0a753a76
PP
407}
408
68dc0745
PP
409int
410win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
0a753a76 411{
68dc0745 412 int r;
0a753a76 413
326b05e3 414 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
68dc0745 415 return r;
0a753a76
PP
416}
417
418
68dc0745
PP
419int
420win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 421{
68dc0745 422 int r;
0a753a76 423
326b05e3 424 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
68dc0745 425 return r;
0a753a76
PP
426}
427
68dc0745
PP
428int
429win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 430{
68dc0745 431 int r;
0a753a76 432
326b05e3 433 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
68dc0745 434 return r;
0a753a76
PP
435}
436
68dc0745
PP
437int
438win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
0a753a76 439{
68dc0745 440 int r;
0a753a76 441
326b05e3 442 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
68dc0745 443 return r;
0a753a76
PP
444}
445
2d7a9237 446int
68dc0745 447win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
0a753a76 448{
68dc0745 449 int r;
0a753a76 450
326b05e3 451 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
68dc0745 452 return r;
0a753a76
PP
453}
454
68dc0745
PP
455int
456win32_listen(SOCKET s, int backlog)
0a753a76 457{
68dc0745 458 int r;
0a753a76 459
326b05e3 460 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
68dc0745 461 return r;
0a753a76
PP
462}
463
68dc0745
PP
464int
465win32_recv(SOCKET s, char *buf, int len, int flags)
0a753a76 466{
68dc0745 467 int r;
0a753a76 468
326b05e3 469 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
68dc0745 470 return r;
0a753a76
PP
471}
472
68dc0745
PP
473int
474win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
0a753a76 475{
68dc0745 476 int r;
e4449fe1 477 int frombufsize = *fromlen;
0a753a76 478
326b05e3 479 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
e4449fe1
GS
480 /* Winsock's recvfrom() only returns a valid 'from' when the socket
481 * is connectionless. Perl expects a valid 'from' for all types
482 * of sockets, so go the extra mile.
483 */
484 if (r != SOCKET_ERROR && frombufsize == *fromlen)
485 (void)win32_getpeername(s, from, fromlen);
68dc0745 486 return r;
0a753a76
PP
487}
488
68dc0745
PP
489/* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
490int
55d25626 491win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
0a753a76 492{
55d25626 493 int r;
3ead22cb 494 int i, fd, save_errno = errno;
8dc00b2f 495 FD_SET nrd, nwr, nex;
e4d771f5 496 bool just_sleep = TRUE;
0a753a76 497
f2bb5751 498 StartSockets();
0a753a76 499
68dc0745
PP
500 FD_ZERO(&nrd);
501 FD_ZERO(&nwr);
502 FD_ZERO(&nex);
503 for (i = 0; i < nfds; i++) {
f7bbabd3
JD
504 if (rd && PERL_FD_ISSET(i,rd)) {
505 fd = TO_SOCKET(i);
47660177 506 FD_SET((unsigned)fd, &nrd);
e4d771f5 507 just_sleep = FALSE;
f7bbabd3
JD
508 }
509 if (wr && PERL_FD_ISSET(i,wr)) {
510 fd = TO_SOCKET(i);
47660177 511 FD_SET((unsigned)fd, &nwr);
e4d771f5 512 just_sleep = FALSE;
f7bbabd3
JD
513 }
514 if (ex && PERL_FD_ISSET(i,ex)) {
515 fd = TO_SOCKET(i);
47660177 516 FD_SET((unsigned)fd, &nex);
e4d771f5 517 just_sleep = FALSE;
f7bbabd3 518 }
68dc0745
PP
519 }
520
e4d771f5
JD
521 /* winsock seems incapable of dealing with all three fd_sets being empty,
522 * so do the (millisecond) sleep as a special case
523 */
524 if (just_sleep) {
525 if (timeout)
526 Sleep(timeout->tv_sec * 1000 +
527 timeout->tv_usec / 1000); /* do the best we can */
528 else
529 Sleep(UINT_MAX);
530 return 0;
531 }
532
1c878075 533 errno = save_errno;
6fc8e913 534 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout));
1c878075 535 save_errno = errno;
0a753a76 536
68dc0745 537 for (i = 0; i < nfds; i++) {
f7bbabd3
JD
538 if (rd && PERL_FD_ISSET(i,rd)) {
539 fd = TO_SOCKET(i);
540 if (!FD_ISSET(fd, &nrd))
541 PERL_FD_CLR(i,rd);
542 }
543 if (wr && PERL_FD_ISSET(i,wr)) {
544 fd = TO_SOCKET(i);
545 if (!FD_ISSET(fd, &nwr))
546 PERL_FD_CLR(i,wr);
547 }
548 if (ex && PERL_FD_ISSET(i,ex)) {
549 fd = TO_SOCKET(i);
550 if (!FD_ISSET(fd, &nex))
551 PERL_FD_CLR(i,ex);
552 }
68dc0745 553 }
1c878075 554 errno = save_errno;
68dc0745 555 return r;
0a753a76
PP
556}
557
68dc0745
PP
558int
559win32_send(SOCKET s, const char *buf, int len, int flags)
0a753a76 560{
68dc0745 561 int r;
0a753a76 562
326b05e3 563 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
68dc0745 564 return r;
0a753a76
PP
565}
566
68dc0745
PP
567int
568win32_sendto(SOCKET s, const char *buf, int len, int flags,
569 const struct sockaddr *to, int tolen)
0a753a76 570{
68dc0745 571 int r;
0a753a76 572
326b05e3 573 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
68dc0745 574 return r;
0a753a76
PP
575}
576
68dc0745
PP
577int
578win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
0a753a76 579{
68dc0745 580 int r;
0a753a76 581
326b05e3 582 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
68dc0745 583 return r;
0a753a76
PP
584}
585
68dc0745
PP
586int
587win32_shutdown(SOCKET s, int how)
0a753a76 588{
68dc0745 589 int r;
0a753a76 590
326b05e3 591 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
68dc0745 592 return r;
0a753a76
PP
593}
594
3a25acb4
GS
595int
596win32_closesocket(SOCKET s)
597{
598 int r;
599
600 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
601 return r;
602}
603
036c1c1e
SH
604void
605convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
606{
607 Copy(in, out, 1, WSAPROTOCOL_INFOA);
608 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
609}
610
611SOCKET
612open_ifs_socket(int af, int type, int protocol)
613{
1c972609
SH
614 dTHX;
615 char *s;
036c1c1e 616 unsigned long proto_buffers_len = 0;
125ddee8 617 int error_code, found = 0;
036c1c1e
SH
618 SOCKET out = INVALID_SOCKET;
619
1c972609
SH
620 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
621 return WSASocket(af, type, protocol, NULL, 0, 0);
622
036c1c1e
SH
623 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
624 && error_code == WSAENOBUFS)
625 {
626 WSAPROTOCOL_INFOW *proto_buffers;
627 int protocols_available = 0;
628
a02a5408 629 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
036c1c1e
SH
630 WSAPROTOCOL_INFOW);
631
632 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
633 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
634 {
635 int i;
636 for (i = 0; i < protocols_available; i++)
637 {
638 WSAPROTOCOL_INFOA proto_info;
639
640 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
641 || (type != proto_buffers[i].iSocketType)
f9169742
SH
642 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
643 protocol != proto_buffers[i].iProtocol))
036c1c1e
SH
644 continue;
645
646 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
647 continue;
648
125ddee8 649 found = 1;
036c1c1e
SH
650 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
651
652 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
653 break;
654 }
125ddee8
TK
655
656 if (!found)
657 WSASetLastError(WSAEPROTONOSUPPORT);
036c1c1e
SH
658 }
659
660 Safefree(proto_buffers);
661 }
662
663 return out;
664}
665
68dc0745
PP
666SOCKET
667win32_socket(int af, int type, int protocol)
0a753a76 668{
68dc0745 669 SOCKET s;
0a753a76 670
3a25acb4 671 StartSockets();
036c1c1e 672
72e6b643 673 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
22e30cb2
SH
674 {
675 int wsaerr = WSAGetLastError();
676 errno = convert_wsa_error_to_errno(wsaerr);
677 SetLastError(wsaerr);
678 }
68dc0745
PP
679 else
680 s = OPEN_SOCKET(s);
0a753a76 681
68dc0745 682 return s;
0a753a76
PP
683}
684
00b02797
JH
685/*
686 * close RTL fd while respecting sockets
687 * added as temporary measure until PerlIO has real
688 * Win32 native layer
689 * -- BKS, 11-11-2000
690*/
691
692int my_close(int fd)
693{
694 int osf;
695 if (!wsock_started) /* No WinSock? */
696 return(close(fd)); /* Then not a socket. */
697 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
698 if (osf != -1) {
699 int err;
700 err = closesocket(osf);
701 if (err == 0) {
1f664ef5 702#ifdef _set_osfhnd
b47a847f
DD
703 assert(_osfhnd(fd) == osf); /* catch a bad ioinfo struct def */
704 /* don't close freed handle */
705 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
706 return close(fd);
1f664ef5
SH
707#else
708 (void)close(fd); /* handle already closed, ignore error */
709 return 0;
710#endif
00b02797
JH
711 }
712 else if (err == SOCKET_ERROR) {
22e30cb2
SH
713 int wsaerr = WSAGetLastError();
714 err = convert_wsa_error_to_errno(wsaerr);
b0ba2190 715 if (err != ENOTSOCK) {
00b02797
JH
716 (void)close(fd);
717 errno = err;
22e30cb2 718 SetLastError(wsaerr);
00b02797
JH
719 return EOF;
720 }
721 }
722 }
723 return close(fd);
724}
725
fb73857a
PP
726#undef fclose
727int
728my_fclose (FILE *pf)
729{
4e945249 730 int osf;
326b05e3
GS
731 if (!wsock_started) /* No WinSock? */
732 return(fclose(pf)); /* Then not a socket. */
00b02797 733 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
d3d49e29
GS
734 if (osf != -1) {
735 int err;
736 win32_fflush(pf);
737 err = closesocket(osf);
738 if (err == 0) {
1f664ef5 739#ifdef _set_osfhnd
b47a847f
DD
740 assert(_osfhnd(win32_fileno(pf)) == osf); /* catch a bad ioinfo struct def */
741 /* don't close freed handle */
742 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
743 return fclose(pf);
1f664ef5
SH
744#else
745 (void)fclose(pf); /* handle already closed, ignore error */
746 return 0;
747#endif
d3d49e29
GS
748 }
749 else if (err == SOCKET_ERROR) {
22e30cb2
SH
750 int wsaerr = WSAGetLastError();
751 err = convert_wsa_error_to_errno(wsaerr);
b0ba2190 752 if (err != ENOTSOCK) {
d3d49e29
GS
753 (void)fclose(pf);
754 errno = err;
22e30cb2 755 SetLastError(wsaerr);
d3d49e29
GS
756 return EOF;
757 }
758 }
326b05e3 759 }
d3d49e29 760 return fclose(pf);
fb73857a
PP
761}
762
68dc0745
PP
763struct hostent *
764win32_gethostbyaddr(const char *addr, int len, int type)
0a753a76 765{
68dc0745 766 struct hostent *r;
0a753a76 767
326b05e3 768 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
68dc0745 769 return r;
0a753a76
PP
770}
771
68dc0745
PP
772struct hostent *
773win32_gethostbyname(const char *name)
0a753a76 774{
68dc0745 775 struct hostent *r;
0a753a76 776
326b05e3 777 SOCKET_TEST(r = gethostbyname(name), NULL);
68dc0745 778 return r;
0a753a76
PP
779}
780
68dc0745
PP
781int
782win32_gethostname(char *name, int len)
0a753a76 783{
68dc0745 784 int r;
0a753a76 785
326b05e3 786 SOCKET_TEST_ERROR(r = gethostname(name, len));
68dc0745 787 return r;
0a753a76
PP
788}
789
68dc0745
PP
790struct protoent *
791win32_getprotobyname(const char *name)
0a753a76 792{
68dc0745 793 struct protoent *r;
0a753a76 794
326b05e3 795 SOCKET_TEST(r = getprotobyname(name), NULL);
68dc0745 796 return r;
0a753a76
PP
797}
798
68dc0745
PP
799struct protoent *
800win32_getprotobynumber(int num)
0a753a76 801{
68dc0745 802 struct protoent *r;
0a753a76 803
326b05e3 804 SOCKET_TEST(r = getprotobynumber(num), NULL);
68dc0745 805 return r;
0a753a76
PP
806}
807
68dc0745
PP
808struct servent *
809win32_getservbyname(const char *name, const char *proto)
0a753a76 810{
04a2c3d9 811 dTHXa(NULL);
68dc0745 812 struct servent *r;
c53bd28a 813
326b05e3 814 SOCKET_TEST(r = getservbyname(name, proto), NULL);
68dc0745 815 if (r) {
04a2c3d9 816 aTHXa(PERL_GET_THX);
3352bfcb 817 r = win32_savecopyservent(&w32_servent, r, proto);
68dc0745
PP
818 }
819 return r;
0a753a76
PP
820}
821
68dc0745
PP
822struct servent *
823win32_getservbyport(int port, const char *proto)
0a753a76 824{
04a2c3d9 825 dTHXa(NULL);
68dc0745 826 struct servent *r;
0a753a76 827
326b05e3 828 SOCKET_TEST(r = getservbyport(port, proto), NULL);
68dc0745 829 if (r) {
04a2c3d9 830 aTHXa(PERL_GET_THX);
3352bfcb 831 r = win32_savecopyservent(&w32_servent, r, proto);
68dc0745
PP
832 }
833 return r;
0a753a76
PP
834}
835
f998180f
GS
836int
837win32_ioctl(int i, unsigned int u, char *data)
838{
69a143a6 839 u_long u_long_arg;
f998180f 840 int retval;
69a143a6 841
f998180f 842 if (!wsock_started) {
4f63d024 843 Perl_croak_nocontext("ioctl implemented only on sockets");
f998180f
GS
844 /* NOTREACHED */
845 }
846
69a143a6
YO
847 /* mauke says using memcpy avoids alignment issues */
848 memcpy(&u_long_arg, data, sizeof u_long_arg);
849 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
850 memcpy(data, &u_long_arg, sizeof u_long_arg);
851
f998180f 852 if (retval == SOCKET_ERROR) {
22e30cb2
SH
853 int wsaerr = WSAGetLastError();
854 int err = convert_wsa_error_to_errno(wsaerr);
b0ba2190 855 if (err == ENOTSOCK) {
4f63d024 856 Perl_croak_nocontext("ioctl implemented only on sockets");
f998180f
GS
857 /* NOTREACHED */
858 }
b0ba2190 859 errno = err;
22e30cb2 860 SetLastError(wsaerr);
f998180f
GS
861 }
862 return retval;
863}
864
68dc0745
PP
865char FAR *
866win32_inet_ntoa(struct in_addr in)
0a753a76 867{
326b05e3
GS
868 StartSockets();
869 return inet_ntoa(in);
0a753a76
PP
870}
871
68dc0745
PP
872unsigned long
873win32_inet_addr(const char FAR *cp)
0a753a76 874{
326b05e3
GS
875 StartSockets();
876 return inet_addr(cp);
0a753a76 877}
68dc0745
PP
878
879/*
880 * Networking stubs
881 */
0a753a76 882
68dc0745
PP
883void
884win32_endhostent()
0a753a76 885{
073dd035 886 win32_croak_not_implemented("endhostent");
0a753a76
PP
887}
888
68dc0745
PP
889void
890win32_endnetent()
0a753a76 891{
073dd035 892 win32_croak_not_implemented("endnetent");
0a753a76
PP
893}
894
68dc0745
PP
895void
896win32_endprotoent()
0a753a76 897{
073dd035 898 win32_croak_not_implemented("endprotoent");
0a753a76
PP
899}
900
68dc0745
PP
901void
902win32_endservent()
0a753a76 903{
073dd035 904 win32_croak_not_implemented("endservent");
0a753a76
PP
905}
906
907
68dc0745
PP
908struct netent *
909win32_getnetent(void)
0a753a76 910{
073dd035 911 win32_croak_not_implemented("getnetent");
68dc0745 912 return (struct netent *) NULL;
0a753a76
PP
913}
914
68dc0745
PP
915struct netent *
916win32_getnetbyname(char *name)
0a753a76 917{
073dd035 918 win32_croak_not_implemented("getnetbyname");
68dc0745 919 return (struct netent *)NULL;
0a753a76
PP
920}
921
68dc0745
PP
922struct netent *
923win32_getnetbyaddr(long net, int type)
0a753a76 924{
073dd035 925 win32_croak_not_implemented("getnetbyaddr");
68dc0745 926 return (struct netent *)NULL;
0a753a76
PP
927}
928
68dc0745
PP
929struct protoent *
930win32_getprotoent(void)
0a753a76 931{
073dd035 932 win32_croak_not_implemented("getprotoent");
68dc0745 933 return (struct protoent *) NULL;
0a753a76
PP
934}
935
68dc0745
PP
936struct servent *
937win32_getservent(void)
0a753a76 938{
073dd035 939 win32_croak_not_implemented("getservent");
68dc0745 940 return (struct servent *) NULL;
0a753a76
PP
941}
942
68dc0745
PP
943void
944win32_sethostent(int stayopen)
0a753a76 945{
073dd035 946 win32_croak_not_implemented("sethostent");
0a753a76
PP
947}
948
949
68dc0745
PP
950void
951win32_setnetent(int stayopen)
0a753a76 952{
073dd035 953 win32_croak_not_implemented("setnetent");
0a753a76
PP
954}
955
956
68dc0745
PP
957void
958win32_setprotoent(int stayopen)
0a753a76 959{
073dd035 960 win32_croak_not_implemented("setprotoent");
0a753a76
PP
961}
962
963
68dc0745
PP
964void
965win32_setservent(int stayopen)
0a753a76 966{
073dd035 967 win32_croak_not_implemented("setservent");
0a753a76
PP
968}
969
137443ea
PP
970static struct servent*
971win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
972{
973 d->s_name = s->s_name;
974 d->s_aliases = s->s_aliases;
975 d->s_port = s->s_port;
8cbe99e5 976 if (s->s_proto && strlen(s->s_proto))
137443ea 977 d->s_proto = s->s_proto;
0af56dfe 978 else
c69f6586 979 if (proto && strlen(proto))
137443ea
PP
980 d->s_proto = (char *)proto;
981 else
982 d->s_proto = "tcp";
983
984 return d;
985}
986
0a753a76 987