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