This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update CPAN-Meta-YAML to CPAN version 0.010
[perl5.git] / win32 / win32sck.c
1 /* win32sck.c
2  *
3  * (c) 1995 Microsoft Corporation. All rights reserved. 
4  *              Developed by hip communications inc.
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  */
10
11 #define WIN32IO_IS_STDIO
12 #define WIN32SCK_IS_STDSCK
13 #define WIN32_LEAN_AND_MEAN
14 #define PERLIO_NOT_STDIO 0
15 #ifdef __GNUC__
16 #define Win32_Winsock
17 #endif
18 #include <windows.h>
19 #include <ws2spi.h>
20
21 #include "EXTERN.h"
22 #include "perl.h"
23
24 #include "Win32iop.h"
25 #include <sys/socket.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <assert.h>
29 #include <io.h>
30
31 /* thanks to Beverly Brown      (beverly@datacube.com) */
32 #define OPEN_SOCKET(x)  win32_open_osfhandle(x,O_RDWR|O_BINARY)
33 #define TO_SOCKET(x)    _get_osfhandle(x)
34
35 #define StartSockets() \
36     STMT_START {                                        \
37         if (!wsock_started)                             \
38             start_sockets();                            \
39     } STMT_END
40
41 #define SOCKET_TEST(x, y) \
42     STMT_START {                                        \
43         StartSockets();                                 \
44         if((x) == (y))                                  \
45             errno = get_last_socket_error();            \
46     } STMT_END
47
48 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
49
50 static int get_last_socket_error(void);
51 static struct servent* win32_savecopyservent(struct servent*d,
52                                              struct servent*s,
53                                              const char *proto);
54
55 static int wsock_started = 0;
56
57 EXTERN_C void
58 EndSockets(void)
59 {
60     if (wsock_started)
61         WSACleanup();
62 }
63
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  *
70  * The list of possible WSAExxx values used here comes from the MSDN page
71  * titled "Windows Sockets Error Codes".
72  *
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  */
79 int
80 convert_wsa_error_to_errno(int wsaerr)
81 {
82     switch (wsaerr) {
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:
154         return WSAEHOSTDOWN;            /* EHOSTDOWN is not defined */
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:
170         return WSAEDISCON;              /* EDISCON is not defined */
171     case WSAENOMORE:
172         return WSAENOMORE;              /* ENOMORE is not defined */
173 #ifdef WSAECANCELLED
174     case WSAECANCELLED:                 /* New in WinSock2 */
175         return ECANCELED;
176 #endif
177     case WSAEINVALIDPROCTABLE:
178         return WSAEINVALIDPROCTABLE;    /* EINVALIDPROCTABLE is not defined */
179     case WSAEINVALIDPROVIDER:
180         return WSAEINVALIDPROVIDER;     /* EINVALIDPROVIDER is not defined */
181     case WSAEPROVIDERFAILEDINIT:
182         return WSAEPROVIDERFAILEDINIT;  /* EPROVIDERFAILEDINIT is not defined */
183     case WSAEREFUSED:
184         return WSAEREFUSED;             /* EREFUSED is not defined */
185     }
186
187     return wsaerr;
188 }
189
190 #ifdef ERRNO_HAS_POSIX_SUPPLEMENT
191 /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
192  * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
193  * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
194  * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
195  * really expect to encounter them anyway in the context in which this function
196  * is called.
197  * Other Exxx values (err < sys_nerr) are returned unchanged.
198  */
199 int
200 convert_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:
214 #ifdef WSAECANCELLED
215         return WSAECANCELLED;           /* New in WinSock2 */
216 #else
217         return ERROR_INVALID_FUNCTION;
218 #endif
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 }
293 #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
294
295 static int
296 get_last_socket_error(void)
297 {
298     return convert_wsa_error_to_errno(WSAGetLastError());
299 }
300
301 void
302 start_sockets(void) 
303 {
304     unsigned short version;
305     WSADATA retdata;
306     int ret;
307
308     /*
309      * initalize the winsock interface and insure that it is
310      * cleaned up at exit.
311      */
312     version = 0x2;
313     if(ret = WSAStartup(version, &retdata))
314         Perl_croak_nocontext("Unable to locate winsock library!\n");
315     if(retdata.wVersion != version)
316         Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
317
318     /* atexit((void (*)(void)) EndSockets); */
319     wsock_started = 1;
320 }
321
322 /* in no sockets Win32 builds, these use the inline functions defined in
323  * perl.h
324  */
325 u_long
326 win32_htonl(u_long hostlong)
327 {
328 #ifndef WIN32_NO_SOCKETS
329     StartSockets();
330 #endif
331     return htonl(hostlong);
332 }
333
334 u_short
335 win32_htons(u_short hostshort)
336 {
337 #ifndef WIN32_NO_SOCKETS
338     StartSockets();
339 #endif
340     return htons(hostshort);
341 }
342
343 u_long
344 win32_ntohl(u_long netlong)
345 {
346 #ifndef WIN32_NO_SOCKETS
347     StartSockets();
348 #endif
349     return ntohl(netlong);
350 }
351
352 u_short
353 win32_ntohs(u_short netshort)
354 {
355 #ifndef WIN32_NO_SOCKETS
356     StartSockets();
357 #endif
358     return ntohs(netshort);
359 }
360
361
362
363 SOCKET
364 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
365 {
366     SOCKET r;
367
368     SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
369     return OPEN_SOCKET(r);
370 }
371
372 int
373 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
374 {
375     int r;
376
377     SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
378     return r;
379 }
380
381 int
382 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
383 {
384     int r;
385
386     SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
387     return r;
388 }
389
390
391 int
392 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
393 {
394     int r;
395
396     SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
397     return r;
398 }
399
400 int
401 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
402 {
403     int r;
404
405     SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
406     return r;
407 }
408
409 int
410 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
411 {
412     int r;
413
414     SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
415     return r;
416 }
417
418 int
419 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
420 {
421     int r;
422
423     SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
424     return r;
425 }
426
427 int
428 win32_listen(SOCKET s, int backlog)
429 {
430     int r;
431
432     SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
433     return r;
434 }
435
436 int
437 win32_recv(SOCKET s, char *buf, int len, int flags)
438 {
439     int r;
440
441     SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
442     return r;
443 }
444
445 int
446 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
447 {
448     int r;
449     int frombufsize = *fromlen;
450
451     SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
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);
458     return r;
459 }
460
461 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
462 int
463 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
464 {
465     int r;
466     int i, fd, save_errno = errno;
467     FD_SET nrd, nwr, nex;
468     bool just_sleep = TRUE;
469
470     StartSockets();
471
472     FD_ZERO(&nrd);
473     FD_ZERO(&nwr);
474     FD_ZERO(&nex);
475     for (i = 0; i < nfds; i++) {
476         if (rd && PERL_FD_ISSET(i,rd)) {
477             fd = TO_SOCKET(i);
478             FD_SET((unsigned)fd, &nrd);
479             just_sleep = FALSE;
480         }
481         if (wr && PERL_FD_ISSET(i,wr)) {
482             fd = TO_SOCKET(i);
483             FD_SET((unsigned)fd, &nwr);
484             just_sleep = FALSE;
485         }
486         if (ex && PERL_FD_ISSET(i,ex)) {
487             fd = TO_SOCKET(i);
488             FD_SET((unsigned)fd, &nex);
489             just_sleep = FALSE;
490         }
491     }
492
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
505     errno = save_errno;
506     SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
507     save_errno = errno;
508
509     for (i = 0; i < nfds; i++) {
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         }
525     }
526     errno = save_errno;
527     return r;
528 }
529
530 int
531 win32_send(SOCKET s, const char *buf, int len, int flags)
532 {
533     int r;
534
535     SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
536     return r;
537 }
538
539 int
540 win32_sendto(SOCKET s, const char *buf, int len, int flags,
541              const struct sockaddr *to, int tolen)
542 {
543     int r;
544
545     SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
546     return r;
547 }
548
549 int
550 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
551 {
552     int r;
553
554     SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
555     return r;
556 }
557     
558 int
559 win32_shutdown(SOCKET s, int how)
560 {
561     int r;
562
563     SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
564     return r;
565 }
566
567 int
568 win32_closesocket(SOCKET s)
569 {
570     int r;
571
572     SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
573     return r;
574 }
575
576 void
577 convert_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
583 SOCKET
584 open_ifs_socket(int af, int type, int protocol)
585 {
586     dTHX;
587     char *s;
588     unsigned long proto_buffers_len = 0;
589     int error_code;
590     SOCKET out = INVALID_SOCKET;
591
592     if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
593         return WSASocket(af, type, protocol, NULL, 0, 0);
594
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  
601         Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
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)
614                     || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
615                         protocol != proto_buffers[i].iProtocol))
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
634 SOCKET
635 win32_socket(int af, int type, int protocol)
636 {
637     SOCKET s;
638
639     StartSockets();
640
641     if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
642         errno = get_last_socket_error();
643     else
644         s = OPEN_SOCKET(s);
645
646     return s;
647 }
648
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
656 int 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) {
666             (void)close(fd);    /* handle already closed, ignore error */
667             return 0;
668         }
669         else if (err == SOCKET_ERROR) {
670             err = get_last_socket_error();
671             if (err != ENOTSOCK) {
672                 (void)close(fd);
673                 errno = err;
674                 return EOF;
675             }
676         }
677     }
678     return close(fd);
679 }
680
681 #undef fclose
682 int
683 my_fclose (FILE *pf)
684 {
685     int osf;
686     if (!wsock_started)         /* No WinSock? */
687         return(fclose(pf));     /* Then not a socket. */
688     osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
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) {
698             err = get_last_socket_error();
699             if (err != ENOTSOCK) {
700                 (void)fclose(pf);
701                 errno = err;
702                 return EOF;
703             }
704         }
705     }
706     return fclose(pf);
707 }
708
709 struct hostent *
710 win32_gethostbyaddr(const char *addr, int len, int type)
711 {
712     struct hostent *r;
713
714     SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
715     return r;
716 }
717
718 struct hostent *
719 win32_gethostbyname(const char *name)
720 {
721     struct hostent *r;
722
723     SOCKET_TEST(r = gethostbyname(name), NULL);
724     return r;
725 }
726
727 int
728 win32_gethostname(char *name, int len)
729 {
730     int r;
731
732     SOCKET_TEST_ERROR(r = gethostname(name, len));
733     return r;
734 }
735
736 struct protoent *
737 win32_getprotobyname(const char *name)
738 {
739     struct protoent *r;
740
741     SOCKET_TEST(r = getprotobyname(name), NULL);
742     return r;
743 }
744
745 struct protoent *
746 win32_getprotobynumber(int num)
747 {
748     struct protoent *r;
749
750     SOCKET_TEST(r = getprotobynumber(num), NULL);
751     return r;
752 }
753
754 struct servent *
755 win32_getservbyname(const char *name, const char *proto)
756 {
757     dTHXa(NULL);    
758     struct servent *r;
759
760     SOCKET_TEST(r = getservbyname(name, proto), NULL);
761     if (r) {
762         aTHXa(PERL_GET_THX);
763         r = win32_savecopyservent(&w32_servent, r, proto);
764     }
765     return r;
766 }
767
768 struct servent *
769 win32_getservbyport(int port, const char *proto)
770 {
771     dTHXa(NULL); 
772     struct servent *r;
773
774     SOCKET_TEST(r = getservbyport(port, proto), NULL);
775     if (r) {
776         aTHXa(PERL_GET_THX);
777         r = win32_savecopyservent(&w32_servent, r, proto);
778     }
779     return r;
780 }
781
782 int
783 win32_ioctl(int i, unsigned int u, char *data)
784 {
785     u_long u_long_arg; 
786     int retval;
787     
788     if (!wsock_started) {
789         Perl_croak_nocontext("ioctl implemented only on sockets");
790         /* NOTREACHED */
791     }
792
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     
798     if (retval == SOCKET_ERROR) {
799         int err = get_last_socket_error();
800         if (err == ENOTSOCK) {
801             Perl_croak_nocontext("ioctl implemented only on sockets");
802             /* NOTREACHED */
803         }
804         errno = err;
805     }
806     return retval;
807 }
808
809 char FAR *
810 win32_inet_ntoa(struct in_addr in)
811 {
812     StartSockets();
813     return inet_ntoa(in);
814 }
815
816 unsigned long
817 win32_inet_addr(const char FAR *cp)
818 {
819     StartSockets();
820     return inet_addr(cp);
821 }
822
823 /*
824  * Networking stubs
825  */
826
827 void
828 win32_endhostent() 
829 {
830     win32_croak_not_implemented("endhostent");
831 }
832
833 void
834 win32_endnetent()
835 {
836     win32_croak_not_implemented("endnetent");
837 }
838
839 void
840 win32_endprotoent()
841 {
842     win32_croak_not_implemented("endprotoent");
843 }
844
845 void
846 win32_endservent()
847 {
848     win32_croak_not_implemented("endservent");
849 }
850
851
852 struct netent *
853 win32_getnetent(void) 
854 {
855     win32_croak_not_implemented("getnetent");
856     return (struct netent *) NULL;
857 }
858
859 struct netent *
860 win32_getnetbyname(char *name) 
861 {
862     win32_croak_not_implemented("getnetbyname");
863     return (struct netent *)NULL;
864 }
865
866 struct netent *
867 win32_getnetbyaddr(long net, int type) 
868 {
869     win32_croak_not_implemented("getnetbyaddr");
870     return (struct netent *)NULL;
871 }
872
873 struct protoent *
874 win32_getprotoent(void) 
875 {
876     win32_croak_not_implemented("getprotoent");
877     return (struct protoent *) NULL;
878 }
879
880 struct servent *
881 win32_getservent(void) 
882 {
883     win32_croak_not_implemented("getservent");
884     return (struct servent *) NULL;
885 }
886
887 void
888 win32_sethostent(int stayopen)
889 {
890     win32_croak_not_implemented("sethostent");
891 }
892
893
894 void
895 win32_setnetent(int stayopen)
896 {
897     win32_croak_not_implemented("setnetent");
898 }
899
900
901 void
902 win32_setprotoent(int stayopen)
903 {
904     win32_croak_not_implemented("setprotoent");
905 }
906
907
908 void
909 win32_setservent(int stayopen)
910 {
911     win32_croak_not_implemented("setservent");
912 }
913
914 static struct servent*
915 win32_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;
920     if (s->s_proto && strlen(s->s_proto))
921         d->s_proto = s->s_proto;
922     else
923     if (proto && strlen(proto))
924         d->s_proto = (char *)proto;
925     else
926         d->s_proto = "tcp";
927    
928     return d;
929 }
930
931