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