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