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