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