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