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