This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
use yyerror() instead of croak() so that compile-time failures in
[perl5.git] / win32 / win32sck.c
1 /* win32sck.c
2  *
3  * (c) 1995 Microsoft Corporation. All rights reserved. 
4  *              Developed by hip communications inc., http://info.hip.com/info/
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 #ifdef __GNUC__
15 #define Win32_Winsock
16 #endif
17 #include <windows.h>
18 #include "EXTERN.h"
19 #include "perl.h"
20
21 #if defined(PERL_OBJECT)
22 #define NO_XSLOCKS
23 extern CPerlObj* pPerl;
24 #include "XSUB.h"
25 #endif
26
27 #include "Win32iop.h"
28 #include <sys/socket.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <assert.h>
32 #include <io.h>
33
34 /* thanks to Beverly Brown      (beverly@datacube.com) */
35 #ifdef USE_SOCKETS_AS_HANDLES
36 #       define OPEN_SOCKET(x)   win32_open_osfhandle(x,O_RDWR|O_BINARY)
37 #       define TO_SOCKET(x)     _get_osfhandle(x)
38 #else
39 #       define OPEN_SOCKET(x)   (x)
40 #       define TO_SOCKET(x)     (x)
41 #endif  /* USE_SOCKETS_AS_HANDLES */
42
43 #ifdef USE_THREADS
44 #define StartSockets() \
45     STMT_START {                                        \
46         if (!wsock_started)                             \
47             start_sockets();                            \
48        set_socktype();                         \
49     } STMT_END
50 #else
51 #define StartSockets() \
52     STMT_START {                                        \
53         if (!wsock_started) {                           \
54             start_sockets();                            \
55             set_socktype();                             \
56         }                                               \
57     } STMT_END
58 #endif
59
60 #define EndSockets() \
61     STMT_START {                                        \
62         if (wsock_started)                              \
63             WSACleanup();                               \
64     } STMT_END
65
66 #define SOCKET_TEST(x, y) \
67     STMT_START {                                        \
68         StartSockets();                                 \
69         if((x) == (y))                                  \
70             errno = WSAGetLastError();                  \
71     } STMT_END
72
73 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
74
75 static struct servent* win32_savecopyservent(struct servent*d,
76                                              struct servent*s,
77                                              const char *proto);
78
79 #ifdef USE_THREADS
80 #ifdef USE_DECLSPEC_THREAD
81 __declspec(thread) struct servent myservent;
82 __declspec(thread) int init_socktype;
83 #else
84 #define myservent (thr->i.Wservent)
85 #define init_socktype (thr->i.Winit_socktype)
86 #endif
87 #else
88 static struct servent myservent;
89 #endif
90
91 static int wsock_started = 0;
92
93 void
94 start_sockets(void) 
95 {
96     unsigned short version;
97     WSADATA retdata;
98     int ret;
99
100     /*
101      * initalize the winsock interface and insure that it is
102      * cleaned up at exit.
103      */
104     version = 0x101;
105     if(ret = WSAStartup(version, &retdata))
106         croak("Unable to locate winsock library!\n");
107     if(retdata.wVersion != version)
108         croak("Could not find version 1.1 of winsock dll\n");
109
110     /* atexit((void (*)(void)) EndSockets); */
111     wsock_started = 1;
112 }
113
114 void
115 set_socktype(void)
116 {
117 #ifdef USE_SOCKETS_AS_HANDLES
118 #ifdef USE_THREADS
119     dTHR;
120     if(!init_socktype) {
121 #endif
122     int iSockOpt = SO_SYNCHRONOUS_NONALERT;
123     /*
124      * Enable the use of sockets as filehandles
125      */
126     setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
127                 (char *)&iSockOpt, sizeof(iSockOpt));
128 #ifdef USE_THREADS
129     init_socktype = 1;
130     }
131 #endif
132 #endif  /* USE_SOCKETS_AS_HANDLES */
133 }
134
135
136 #ifndef USE_SOCKETS_AS_HANDLES
137 #undef fdopen
138 FILE *
139 my_fdopen(int fd, char *mode)
140 {
141     FILE *fp;
142     char sockbuf[256];
143     int optlen = sizeof(sockbuf);
144     int retval;
145
146     if (!wsock_started)
147         return(fdopen(fd, mode));
148
149     retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
150     if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
151         return(fdopen(fd, mode));
152     }
153
154     /*
155      * If we get here, then fd is actually a socket.
156      */
157     Newz(1310, fp, 1, FILE);
158     if(fp == NULL) {
159         errno = ENOMEM;
160         return NULL;
161     }
162
163     fp->_file = fd;
164     if(*mode == 'r')
165         fp->_flag = _IOREAD;
166     else
167         fp->_flag = _IOWRT;
168    
169     return fp;
170 }
171 #endif  /* USE_SOCKETS_AS_HANDLES */
172
173
174 u_long
175 win32_htonl(u_long hostlong)
176 {
177     StartSockets();
178     return htonl(hostlong);
179 }
180
181 u_short
182 win32_htons(u_short hostshort)
183 {
184     StartSockets();
185     return htons(hostshort);
186 }
187
188 u_long
189 win32_ntohl(u_long netlong)
190 {
191     StartSockets();
192     return ntohl(netlong);
193 }
194
195 u_short
196 win32_ntohs(u_short netshort)
197 {
198     StartSockets();
199     return ntohs(netshort);
200 }
201
202
203
204 SOCKET
205 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
206 {
207     SOCKET r;
208
209     SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
210     return OPEN_SOCKET(r);
211 }
212
213 int
214 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
215 {
216     int r;
217
218     SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
219     return r;
220 }
221
222 int
223 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
224 {
225     int r;
226
227     SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
228     return r;
229 }
230
231
232 int
233 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
234 {
235     int r;
236
237     SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
238     return r;
239 }
240
241 int
242 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
243 {
244     int r;
245
246     SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
247     return r;
248 }
249
250 int
251 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
252 {
253     int r;
254
255     SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
256     return r;
257 }
258
259 int
260 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
261 {
262     int r;
263
264     SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
265     return r;
266 }
267
268 int
269 win32_listen(SOCKET s, int backlog)
270 {
271     int r;
272
273     SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
274     return r;
275 }
276
277 int
278 win32_recv(SOCKET s, char *buf, int len, int flags)
279 {
280     int r;
281
282     SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
283     return r;
284 }
285
286 int
287 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
288 {
289     int r;
290     int frombufsize = *fromlen;
291
292     SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
293     /* Winsock's recvfrom() only returns a valid 'from' when the socket
294      * is connectionless.  Perl expects a valid 'from' for all types
295      * of sockets, so go the extra mile.
296      */
297     if (r != SOCKET_ERROR && frombufsize == *fromlen)
298         (void)win32_getpeername(s, from, fromlen);
299     return r;
300 }
301
302 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
303 int
304 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
305 {
306     int r;
307 #ifdef USE_SOCKETS_AS_HANDLES
308     Perl_fd_set dummy;
309     int i, fd, bit, offset;
310     FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
311
312     /* winsock seems incapable of dealing with all three null fd_sets,
313      * so do the (millisecond) sleep as a special case
314      */
315     if (!(rd || wr || ex)) {
316         if (timeout)
317             Sleep(timeout->tv_sec  * 1000 +
318                   timeout->tv_usec / 1000);     /* do the best we can */
319         else
320             Sleep(UINT_MAX);
321         return 0;
322     }
323     StartSockets();
324     PERL_FD_ZERO(&dummy);
325     if (!rd)
326         rd = &dummy, prd = NULL;
327     else
328         prd = &nrd;
329     if (!wr)
330         wr = &dummy, pwr = NULL;
331     else
332         pwr = &nwr;
333     if (!ex)
334         ex = &dummy, pex = NULL;
335     else
336         pex = &nex;
337
338     FD_ZERO(&nrd);
339     FD_ZERO(&nwr);
340     FD_ZERO(&nex);
341     for (i = 0; i < nfds; i++) {
342         fd = TO_SOCKET(i);
343         if (PERL_FD_ISSET(i,rd))
344             FD_SET(fd, &nrd);
345         if (PERL_FD_ISSET(i,wr))
346             FD_SET(fd, &nwr);
347         if (PERL_FD_ISSET(i,ex))
348             FD_SET(fd, &nex);
349     }
350
351     SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
352
353     for (i = 0; i < nfds; i++) {
354         fd = TO_SOCKET(i);
355         if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
356             PERL_FD_CLR(i,rd);
357         if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
358             PERL_FD_CLR(i,wr);
359         if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
360             PERL_FD_CLR(i,ex);
361     }
362 #else
363     SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
364 #endif
365     return r;
366 }
367
368 int
369 win32_send(SOCKET s, const char *buf, int len, int flags)
370 {
371     int r;
372
373     SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
374     return r;
375 }
376
377 int
378 win32_sendto(SOCKET s, const char *buf, int len, int flags,
379              const struct sockaddr *to, int tolen)
380 {
381     int r;
382
383     SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
384     return r;
385 }
386
387 int
388 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
389 {
390     int r;
391
392     SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
393     return r;
394 }
395     
396 int
397 win32_shutdown(SOCKET s, int how)
398 {
399     int r;
400
401     SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
402     return r;
403 }
404
405 int
406 win32_closesocket(SOCKET s)
407 {
408     int r;
409
410     SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
411     return r;
412 }
413
414 SOCKET
415 win32_socket(int af, int type, int protocol)
416 {
417     SOCKET s;
418
419 #ifndef USE_SOCKETS_AS_HANDLES
420     SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
421 #else
422     StartSockets();
423     if((s = socket(af, type, protocol)) == INVALID_SOCKET)
424         errno = WSAGetLastError();
425     else
426         s = OPEN_SOCKET(s);
427 #endif  /* USE_SOCKETS_AS_HANDLES */
428
429     return s;
430 }
431
432 #undef fclose
433 int
434 my_fclose (FILE *pf)
435 {
436     int osf, retval;
437     if (!wsock_started)         /* No WinSock? */
438         return(fclose(pf));     /* Then not a socket. */
439     osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
440     retval = fclose(pf);        /* Must fclose() before closesocket() */
441     if (osf != -1
442         && closesocket(osf) == SOCKET_ERROR
443         && WSAGetLastError() != WSAENOTSOCK)
444     {
445         return EOF;
446     }
447     return retval;
448 }
449
450 struct hostent *
451 win32_gethostbyaddr(const char *addr, int len, int type)
452 {
453     struct hostent *r;
454
455     SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
456     return r;
457 }
458
459 struct hostent *
460 win32_gethostbyname(const char *name)
461 {
462     struct hostent *r;
463
464     SOCKET_TEST(r = gethostbyname(name), NULL);
465     return r;
466 }
467
468 int
469 win32_gethostname(char *name, int len)
470 {
471     int r;
472
473     SOCKET_TEST_ERROR(r = gethostname(name, len));
474     return r;
475 }
476
477 struct protoent *
478 win32_getprotobyname(const char *name)
479 {
480     struct protoent *r;
481
482     SOCKET_TEST(r = getprotobyname(name), NULL);
483     return r;
484 }
485
486 struct protoent *
487 win32_getprotobynumber(int num)
488 {
489     struct protoent *r;
490
491     SOCKET_TEST(r = getprotobynumber(num), NULL);
492     return r;
493 }
494
495 struct servent *
496 win32_getservbyname(const char *name, const char *proto)
497 {
498     struct servent *r;
499     dTHR;    
500
501     SOCKET_TEST(r = getservbyname(name, proto), NULL);
502     if (r) {
503         r = win32_savecopyservent(&myservent, r, proto);
504     }
505     return r;
506 }
507
508 struct servent *
509 win32_getservbyport(int port, const char *proto)
510 {
511     struct servent *r;
512     dTHR; 
513
514     SOCKET_TEST(r = getservbyport(port, proto), NULL);
515     if (r) {
516         r = win32_savecopyservent(&myservent, r, proto);
517     }
518     return r;
519 }
520
521 int
522 win32_ioctl(int i, unsigned int u, char *data)
523 {
524     u_long argp = (u_long)data;
525     int retval;
526
527     if (!wsock_started) {
528         croak("ioctl implemented only on sockets");
529         /* NOTREACHED */
530     }
531
532     retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
533     if (retval == SOCKET_ERROR) {
534         if (WSAGetLastError() == WSAENOTSOCK) {
535             croak("ioctl implemented only on sockets");
536             /* NOTREACHED */
537         }
538         errno = WSAGetLastError();
539     }
540     return retval;
541 }
542
543 char FAR *
544 win32_inet_ntoa(struct in_addr in)
545 {
546     StartSockets();
547     return inet_ntoa(in);
548 }
549
550 unsigned long
551 win32_inet_addr(const char FAR *cp)
552 {
553     StartSockets();
554     return inet_addr(cp);
555 }
556
557 /*
558  * Networking stubs
559  */
560
561 void
562 win32_endhostent() 
563 {
564     croak("endhostent not implemented!\n");
565 }
566
567 void
568 win32_endnetent()
569 {
570     croak("endnetent not implemented!\n");
571 }
572
573 void
574 win32_endprotoent()
575 {
576     croak("endprotoent not implemented!\n");
577 }
578
579 void
580 win32_endservent()
581 {
582     croak("endservent not implemented!\n");
583 }
584
585
586 struct netent *
587 win32_getnetent(void) 
588 {
589     croak("getnetent not implemented!\n");
590     return (struct netent *) NULL;
591 }
592
593 struct netent *
594 win32_getnetbyname(char *name) 
595 {
596     croak("getnetbyname not implemented!\n");
597     return (struct netent *)NULL;
598 }
599
600 struct netent *
601 win32_getnetbyaddr(long net, int type) 
602 {
603     croak("getnetbyaddr not implemented!\n");
604     return (struct netent *)NULL;
605 }
606
607 struct protoent *
608 win32_getprotoent(void) 
609 {
610     croak("getprotoent not implemented!\n");
611     return (struct protoent *) NULL;
612 }
613
614 struct servent *
615 win32_getservent(void) 
616 {
617     croak("getservent not implemented!\n");
618     return (struct servent *) NULL;
619 }
620
621 void
622 win32_sethostent(int stayopen)
623 {
624     croak("sethostent not implemented!\n");
625 }
626
627
628 void
629 win32_setnetent(int stayopen)
630 {
631     croak("setnetent not implemented!\n");
632 }
633
634
635 void
636 win32_setprotoent(int stayopen)
637 {
638     croak("setprotoent not implemented!\n");
639 }
640
641
642 void
643 win32_setservent(int stayopen)
644 {
645     croak("setservent not implemented!\n");
646 }
647
648 static struct servent*
649 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
650 {
651     d->s_name = s->s_name;
652     d->s_aliases = s->s_aliases;
653     d->s_port = s->s_port;
654 #ifndef __BORLANDC__    /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
655     if (!IsWin95() && s->s_proto && strlen(s->s_proto))
656         d->s_proto = s->s_proto;
657     else
658 #endif
659     if (proto && strlen(proto))
660         d->s_proto = (char *)proto;
661     else
662         d->s_proto = "tcp";
663    
664     return d;
665 }
666
667