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