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