8019cc3986a11dfb65d046280d666474473a60a5
[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     StartSockets();
273     PERL_FD_ZERO(&dummy);
274     if (!rd)
275         rd = &dummy, prd = NULL;
276     else
277         prd = &nrd;
278     if (!wr)
279         wr = &dummy, pwr = NULL;
280     else
281         pwr = &nwr;
282     if (!ex)
283         ex = &dummy, pex = NULL;
284     else
285         pex = &nex;
286
287     FD_ZERO(&nrd);
288     FD_ZERO(&nwr);
289     FD_ZERO(&nex);
290     for (i = 0; i < nfds; i++) {
291         fd = TO_SOCKET(i);
292         if (PERL_FD_ISSET(i,rd))
293             FD_SET(fd, &nrd);
294         if (PERL_FD_ISSET(i,wr))
295             FD_SET(fd, &nwr);
296         if (PERL_FD_ISSET(i,ex))
297             FD_SET(fd, &nex);
298     }
299
300     SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
301
302     for (i = 0; i < nfds; i++) {
303         fd = TO_SOCKET(i);
304         if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
305             PERL_FD_CLR(i,rd);
306         if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
307             PERL_FD_CLR(i,wr);
308         if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
309             PERL_FD_CLR(i,ex);
310     }
311 #else
312     SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
313 #endif
314     return r;
315 }
316
317 int
318 win32_send(SOCKET s, const char *buf, int len, int flags)
319 {
320     int r;
321
322     SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
323     return r;
324 }
325
326 int
327 win32_sendto(SOCKET s, const char *buf, int len, int flags,
328              const struct sockaddr *to, int tolen)
329 {
330     int r;
331
332     SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
333     return r;
334 }
335
336 int
337 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
338 {
339     int r;
340
341     SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
342     return r;
343 }
344     
345 int
346 win32_shutdown(SOCKET s, int how)
347 {
348     int r;
349
350     SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
351     return r;
352 }
353
354 int
355 win32_closesocket(SOCKET s)
356 {
357     int r;
358
359     SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
360     return r;
361 }
362
363 SOCKET
364 win32_socket(int af, int type, int protocol)
365 {
366     SOCKET s;
367
368 #ifndef USE_SOCKETS_AS_HANDLES
369     SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
370 #else
371     StartSockets();
372     if((s = socket(af, type, protocol)) == INVALID_SOCKET)
373         errno = WSAGetLastError();
374     else
375         s = OPEN_SOCKET(s);
376 #endif  /* USE_SOCKETS_AS_HANDLES */
377
378     return s;
379 }
380
381 #undef fclose
382 int
383 my_fclose (FILE *pf)
384 {
385     int osf, retval;
386     if (!wsock_started)         /* No WinSock? */
387         return(fclose(pf));     /* Then not a socket. */
388     osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
389     retval = fclose(pf);        /* Must fclose() before closesocket() */
390     if (osf != -1
391         && closesocket(osf) == SOCKET_ERROR
392         && WSAGetLastError() != WSAENOTSOCK)
393     {
394         return EOF;
395     }
396     return retval;
397 }
398
399 struct hostent *
400 win32_gethostbyaddr(const char *addr, int len, int type)
401 {
402     struct hostent *r;
403
404     SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
405     return r;
406 }
407
408 struct hostent *
409 win32_gethostbyname(const char *name)
410 {
411     struct hostent *r;
412
413     SOCKET_TEST(r = gethostbyname(name), NULL);
414     return r;
415 }
416
417 int
418 win32_gethostname(char *name, int len)
419 {
420     int r;
421
422     SOCKET_TEST_ERROR(r = gethostname(name, len));
423     return r;
424 }
425
426 struct protoent *
427 win32_getprotobyname(const char *name)
428 {
429     struct protoent *r;
430
431     SOCKET_TEST(r = getprotobyname(name), NULL);
432     return r;
433 }
434
435 struct protoent *
436 win32_getprotobynumber(int num)
437 {
438     struct protoent *r;
439
440     SOCKET_TEST(r = getprotobynumber(num), NULL);
441     return r;
442 }
443
444 struct servent *
445 win32_getservbyname(const char *name, const char *proto)
446 {
447     struct servent *r;
448     dTHR;    
449
450     SOCKET_TEST(r = getservbyname(name, proto), NULL);
451     if (r) {
452         r = win32_savecopyservent(&myservent, r, proto);
453     }
454     return r;
455 }
456
457 struct servent *
458 win32_getservbyport(int port, const char *proto)
459 {
460     struct servent *r;
461     dTHR; 
462
463     SOCKET_TEST(r = getservbyport(port, proto), NULL);
464     if (r) {
465         r = win32_savecopyservent(&myservent, r, proto);
466     }
467     return r;
468 }
469
470 int
471 win32_ioctl(int i, unsigned int u, char *data)
472 {
473     u_long argp = (u_long)data;
474     int retval;
475
476     if (!wsock_started) {
477         croak("ioctl implemented only on sockets");
478         /* NOTREACHED */
479     }
480
481     retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
482     if (retval == SOCKET_ERROR) {
483         if (WSAGetLastError() == WSAENOTSOCK) {
484             croak("ioctl implemented only on sockets");
485             /* NOTREACHED */
486         }
487         errno = WSAGetLastError();
488     }
489     return retval;
490 }
491
492 char FAR *
493 win32_inet_ntoa(struct in_addr in)
494 {
495     StartSockets();
496     return inet_ntoa(in);
497 }
498
499 unsigned long
500 win32_inet_addr(const char FAR *cp)
501 {
502     StartSockets();
503     return inet_addr(cp);
504 }
505
506 /*
507  * Networking stubs
508  */
509
510 void
511 win32_endhostent() 
512 {
513     croak("endhostent not implemented!\n");
514 }
515
516 void
517 win32_endnetent()
518 {
519     croak("endnetent not implemented!\n");
520 }
521
522 void
523 win32_endprotoent()
524 {
525     croak("endprotoent not implemented!\n");
526 }
527
528 void
529 win32_endservent()
530 {
531     croak("endservent not implemented!\n");
532 }
533
534
535 struct netent *
536 win32_getnetent(void) 
537 {
538     croak("getnetent not implemented!\n");
539     return (struct netent *) NULL;
540 }
541
542 struct netent *
543 win32_getnetbyname(char *name) 
544 {
545     croak("getnetbyname not implemented!\n");
546     return (struct netent *)NULL;
547 }
548
549 struct netent *
550 win32_getnetbyaddr(long net, int type) 
551 {
552     croak("getnetbyaddr not implemented!\n");
553     return (struct netent *)NULL;
554 }
555
556 struct protoent *
557 win32_getprotoent(void) 
558 {
559     croak("getprotoent not implemented!\n");
560     return (struct protoent *) NULL;
561 }
562
563 struct servent *
564 win32_getservent(void) 
565 {
566     croak("getservent not implemented!\n");
567     return (struct servent *) NULL;
568 }
569
570 void
571 win32_sethostent(int stayopen)
572 {
573     croak("sethostent not implemented!\n");
574 }
575
576
577 void
578 win32_setnetent(int stayopen)
579 {
580     croak("setnetent not implemented!\n");
581 }
582
583
584 void
585 win32_setprotoent(int stayopen)
586 {
587     croak("setprotoent not implemented!\n");
588 }
589
590
591 void
592 win32_setservent(int stayopen)
593 {
594     croak("setservent not implemented!\n");
595 }
596
597 static struct servent*
598 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
599 {
600     d->s_name = s->s_name;
601     d->s_aliases = s->s_aliases;
602     d->s_port = s->s_port;
603 #ifndef __BORLANDC__    /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
604     if (!IsWin95() && s->s_proto && strlen(s->s_proto))
605         d->s_proto = s->s_proto;
606     else
607 #endif
608         if (proto && strlen(proto))
609         d->s_proto = (char *)proto;
610     else
611         d->s_proto = "tcp";
612    
613     return d;
614 }
615
616