This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add the DynaLoader upgrade to perldelta
[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 = WSAGetLastError();                  \
46     } STMT_END
47
48 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
49
50 static struct servent* win32_savecopyservent(struct servent*d,
51                                              struct servent*s,
52                                              const char *proto);
53
54 static int wsock_started = 0;
55
56 EXTERN_C void
57 EndSockets(void)
58 {
59     if (wsock_started)
60         WSACleanup();
61 }
62
63 void
64 start_sockets(void) 
65 {
66     dTHX;
67     unsigned short version;
68     WSADATA retdata;
69     int ret;
70
71     /*
72      * initalize the winsock interface and insure that it is
73      * cleaned up at exit.
74      */
75     version = 0x2;
76     if(ret = WSAStartup(version, &retdata))
77         Perl_croak_nocontext("Unable to locate winsock library!\n");
78     if(retdata.wVersion != version)
79         Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
80
81     /* atexit((void (*)(void)) EndSockets); */
82     wsock_started = 1;
83 }
84
85 u_long
86 win32_htonl(u_long hostlong)
87 {
88     StartSockets();
89     return htonl(hostlong);
90 }
91
92 u_short
93 win32_htons(u_short hostshort)
94 {
95     StartSockets();
96     return htons(hostshort);
97 }
98
99 u_long
100 win32_ntohl(u_long netlong)
101 {
102     StartSockets();
103     return ntohl(netlong);
104 }
105
106 u_short
107 win32_ntohs(u_short netshort)
108 {
109     StartSockets();
110     return ntohs(netshort);
111 }
112
113
114
115 SOCKET
116 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
117 {
118     SOCKET r;
119
120     SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
121     return OPEN_SOCKET(r);
122 }
123
124 int
125 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
126 {
127     int r;
128
129     SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
130     return r;
131 }
132
133 int
134 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
135 {
136     int r;
137
138     SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
139     return r;
140 }
141
142
143 int
144 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
145 {
146     int r;
147
148     SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
149     return r;
150 }
151
152 int
153 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
154 {
155     int r;
156
157     SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
158     return r;
159 }
160
161 int
162 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
163 {
164     int r;
165
166     SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
167     return r;
168 }
169
170 int
171 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
172 {
173     int r;
174
175     SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
176     return r;
177 }
178
179 int
180 win32_listen(SOCKET s, int backlog)
181 {
182     int r;
183
184     SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
185     return r;
186 }
187
188 int
189 win32_recv(SOCKET s, char *buf, int len, int flags)
190 {
191     int r;
192
193     SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
194     return r;
195 }
196
197 int
198 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
199 {
200     int r;
201     int frombufsize = *fromlen;
202
203     SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
204     /* Winsock's recvfrom() only returns a valid 'from' when the socket
205      * is connectionless.  Perl expects a valid 'from' for all types
206      * of sockets, so go the extra mile.
207      */
208     if (r != SOCKET_ERROR && frombufsize == *fromlen)
209         (void)win32_getpeername(s, from, fromlen);
210     return r;
211 }
212
213 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
214 int
215 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
216 {
217     int r;
218     int i, fd, save_errno = errno;
219     FD_SET nrd, nwr, nex;
220     bool just_sleep = TRUE;
221
222     StartSockets();
223
224     FD_ZERO(&nrd);
225     FD_ZERO(&nwr);
226     FD_ZERO(&nex);
227     for (i = 0; i < nfds; i++) {
228         if (rd && PERL_FD_ISSET(i,rd)) {
229             fd = TO_SOCKET(i);
230             FD_SET((unsigned)fd, &nrd);
231             just_sleep = FALSE;
232         }
233         if (wr && PERL_FD_ISSET(i,wr)) {
234             fd = TO_SOCKET(i);
235             FD_SET((unsigned)fd, &nwr);
236             just_sleep = FALSE;
237         }
238         if (ex && PERL_FD_ISSET(i,ex)) {
239             fd = TO_SOCKET(i);
240             FD_SET((unsigned)fd, &nex);
241             just_sleep = FALSE;
242         }
243     }
244
245     /* winsock seems incapable of dealing with all three fd_sets being empty,
246      * so do the (millisecond) sleep as a special case
247      */
248     if (just_sleep) {
249         if (timeout)
250             Sleep(timeout->tv_sec  * 1000 +
251                   timeout->tv_usec / 1000);     /* do the best we can */
252         else
253             Sleep(UINT_MAX);
254         return 0;
255     }
256
257     errno = save_errno;
258     SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
259     save_errno = errno;
260
261     for (i = 0; i < nfds; i++) {
262         if (rd && PERL_FD_ISSET(i,rd)) {
263             fd = TO_SOCKET(i);
264             if (!FD_ISSET(fd, &nrd))
265                 PERL_FD_CLR(i,rd);
266         }
267         if (wr && PERL_FD_ISSET(i,wr)) {
268             fd = TO_SOCKET(i);
269             if (!FD_ISSET(fd, &nwr))
270                 PERL_FD_CLR(i,wr);
271         }
272         if (ex && PERL_FD_ISSET(i,ex)) {
273             fd = TO_SOCKET(i);
274             if (!FD_ISSET(fd, &nex))
275                 PERL_FD_CLR(i,ex);
276         }
277     }
278     errno = save_errno;
279     return r;
280 }
281
282 int
283 win32_send(SOCKET s, const char *buf, int len, int flags)
284 {
285     int r;
286
287     SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
288     return r;
289 }
290
291 int
292 win32_sendto(SOCKET s, const char *buf, int len, int flags,
293              const struct sockaddr *to, int tolen)
294 {
295     int r;
296
297     SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
298     return r;
299 }
300
301 int
302 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
303 {
304     int r;
305
306     SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
307     return r;
308 }
309     
310 int
311 win32_shutdown(SOCKET s, int how)
312 {
313     int r;
314
315     SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
316     return r;
317 }
318
319 int
320 win32_closesocket(SOCKET s)
321 {
322     int r;
323
324     SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
325     return r;
326 }
327
328 void
329 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
330 {
331     Copy(in, out, 1, WSAPROTOCOL_INFOA);
332     wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
333 }
334
335 SOCKET
336 open_ifs_socket(int af, int type, int protocol)
337 {
338     dTHX;
339     char *s;
340     unsigned long proto_buffers_len = 0;
341     int error_code;
342     SOCKET out = INVALID_SOCKET;
343
344     if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
345         return WSASocket(af, type, protocol, NULL, 0, 0);
346
347     if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
348         && error_code == WSAENOBUFS)
349     {
350         WSAPROTOCOL_INFOW *proto_buffers;
351         int protocols_available = 0;       
352  
353         Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
354             WSAPROTOCOL_INFOW);
355
356         if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers, 
357             &proto_buffers_len, &error_code)) != SOCKET_ERROR)
358         {
359             int i;
360             for (i = 0; i < protocols_available; i++)
361             {
362                 WSAPROTOCOL_INFOA proto_info;
363
364                 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
365                     || (type != proto_buffers[i].iSocketType)
366                     || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
367                         protocol != proto_buffers[i].iProtocol))
368                     continue;
369
370                 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
371                     continue;
372
373                 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
374
375                 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
376                 break;
377             }
378         }
379
380         Safefree(proto_buffers);
381     }
382
383     return out;
384 }
385
386 SOCKET
387 win32_socket(int af, int type, int protocol)
388 {
389     SOCKET s;
390
391     StartSockets();
392
393     if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
394         errno = WSAGetLastError();
395     else
396         s = OPEN_SOCKET(s);
397
398     return s;
399 }
400
401 /*
402  * close RTL fd while respecting sockets
403  * added as temporary measure until PerlIO has real
404  * Win32 native layer
405  *   -- BKS, 11-11-2000
406 */
407
408 int my_close(int fd)
409 {
410     int osf;
411     if (!wsock_started)         /* No WinSock? */
412         return(close(fd));      /* Then not a socket. */
413     osf = TO_SOCKET(fd);/* Get it now before it's gone! */
414     if (osf != -1) {
415         int err;
416         err = closesocket(osf);
417         if (err == 0) {
418             (void)close(fd);    /* handle already closed, ignore error */
419             return 0;
420         }
421         else if (err == SOCKET_ERROR) {
422             err = WSAGetLastError();
423             if (err != WSAENOTSOCK) {
424                 (void)close(fd);
425                 errno = err;
426                 return EOF;
427             }
428         }
429     }
430     return close(fd);
431 }
432
433 #undef fclose
434 int
435 my_fclose (FILE *pf)
436 {
437     int osf;
438     if (!wsock_started)         /* No WinSock? */
439         return(fclose(pf));     /* Then not a socket. */
440     osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
441     if (osf != -1) {
442         int err;
443         win32_fflush(pf);
444         err = closesocket(osf);
445         if (err == 0) {
446             (void)fclose(pf);   /* handle already closed, ignore error */
447             return 0;
448         }
449         else if (err == SOCKET_ERROR) {
450             err = WSAGetLastError();
451             if (err != WSAENOTSOCK) {
452                 (void)fclose(pf);
453                 errno = err;
454                 return EOF;
455             }
456         }
457     }
458     return fclose(pf);
459 }
460
461 struct hostent *
462 win32_gethostbyaddr(const char *addr, int len, int type)
463 {
464     struct hostent *r;
465
466     SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
467     return r;
468 }
469
470 struct hostent *
471 win32_gethostbyname(const char *name)
472 {
473     struct hostent *r;
474
475     SOCKET_TEST(r = gethostbyname(name), NULL);
476     return r;
477 }
478
479 int
480 win32_gethostname(char *name, int len)
481 {
482     int r;
483
484     SOCKET_TEST_ERROR(r = gethostname(name, len));
485     return r;
486 }
487
488 struct protoent *
489 win32_getprotobyname(const char *name)
490 {
491     struct protoent *r;
492
493     SOCKET_TEST(r = getprotobyname(name), NULL);
494     return r;
495 }
496
497 struct protoent *
498 win32_getprotobynumber(int num)
499 {
500     struct protoent *r;
501
502     SOCKET_TEST(r = getprotobynumber(num), NULL);
503     return r;
504 }
505
506 struct servent *
507 win32_getservbyname(const char *name, const char *proto)
508 {
509     dTHX;    
510     struct servent *r;
511
512     SOCKET_TEST(r = getservbyname(name, proto), NULL);
513     if (r) {
514         r = win32_savecopyservent(&w32_servent, r, proto);
515     }
516     return r;
517 }
518
519 struct servent *
520 win32_getservbyport(int port, const char *proto)
521 {
522     dTHX; 
523     struct servent *r;
524
525     SOCKET_TEST(r = getservbyport(port, proto), NULL);
526     if (r) {
527         r = win32_savecopyservent(&w32_servent, r, proto);
528     }
529     return r;
530 }
531
532 int
533 win32_ioctl(int i, unsigned int u, char *data)
534 {
535     dTHX;
536     u_long u_long_arg; 
537     int retval;
538     
539     if (!wsock_started) {
540         Perl_croak_nocontext("ioctl implemented only on sockets");
541         /* NOTREACHED */
542     }
543
544     /* mauke says using memcpy avoids alignment issues */
545     memcpy(&u_long_arg, data, sizeof u_long_arg); 
546     retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
547     memcpy(data, &u_long_arg, sizeof u_long_arg);
548     
549     if (retval == SOCKET_ERROR) {
550         if (WSAGetLastError() == WSAENOTSOCK) {
551             Perl_croak_nocontext("ioctl implemented only on sockets");
552             /* NOTREACHED */
553         }
554         errno = WSAGetLastError();
555     }
556     return retval;
557 }
558
559 char FAR *
560 win32_inet_ntoa(struct in_addr in)
561 {
562     StartSockets();
563     return inet_ntoa(in);
564 }
565
566 unsigned long
567 win32_inet_addr(const char FAR *cp)
568 {
569     StartSockets();
570     return inet_addr(cp);
571 }
572
573 /*
574  * Networking stubs
575  */
576
577 void
578 win32_endhostent() 
579 {
580     dTHX;
581     Perl_croak_nocontext("endhostent not implemented!\n");
582 }
583
584 void
585 win32_endnetent()
586 {
587     dTHX;
588     Perl_croak_nocontext("endnetent not implemented!\n");
589 }
590
591 void
592 win32_endprotoent()
593 {
594     dTHX;
595     Perl_croak_nocontext("endprotoent not implemented!\n");
596 }
597
598 void
599 win32_endservent()
600 {
601     dTHX;
602     Perl_croak_nocontext("endservent not implemented!\n");
603 }
604
605
606 struct netent *
607 win32_getnetent(void) 
608 {
609     dTHX;
610     Perl_croak_nocontext("getnetent not implemented!\n");
611     return (struct netent *) NULL;
612 }
613
614 struct netent *
615 win32_getnetbyname(char *name) 
616 {
617     dTHX;
618     Perl_croak_nocontext("getnetbyname not implemented!\n");
619     return (struct netent *)NULL;
620 }
621
622 struct netent *
623 win32_getnetbyaddr(long net, int type) 
624 {
625     dTHX;
626     Perl_croak_nocontext("getnetbyaddr not implemented!\n");
627     return (struct netent *)NULL;
628 }
629
630 struct protoent *
631 win32_getprotoent(void) 
632 {
633     dTHX;
634     Perl_croak_nocontext("getprotoent not implemented!\n");
635     return (struct protoent *) NULL;
636 }
637
638 struct servent *
639 win32_getservent(void) 
640 {
641     dTHX;
642     Perl_croak_nocontext("getservent not implemented!\n");
643     return (struct servent *) NULL;
644 }
645
646 void
647 win32_sethostent(int stayopen)
648 {
649     dTHX;
650     Perl_croak_nocontext("sethostent not implemented!\n");
651 }
652
653
654 void
655 win32_setnetent(int stayopen)
656 {
657     dTHX;
658     Perl_croak_nocontext("setnetent not implemented!\n");
659 }
660
661
662 void
663 win32_setprotoent(int stayopen)
664 {
665     dTHX;
666     Perl_croak_nocontext("setprotoent not implemented!\n");
667 }
668
669
670 void
671 win32_setservent(int stayopen)
672 {
673     dTHX;
674     Perl_croak_nocontext("setservent not implemented!\n");
675 }
676
677 static struct servent*
678 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
679 {
680     d->s_name = s->s_name;
681     d->s_aliases = s->s_aliases;
682     d->s_port = s->s_port;
683     if (s->s_proto && strlen(s->s_proto))
684         d->s_proto = s->s_proto;
685     else
686     if (proto && strlen(proto))
687         d->s_proto = (char *)proto;
688     else
689         d->s_proto = "tcp";
690    
691     return d;
692 }
693
694