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