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.
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.
11 #define WIN32IO_IS_STDIO
12 #define WIN32SCK_IS_STDSCK
13 #define WIN32_LEAN_AND_MEAN
14 #define PERLIO_NOT_STDIO 0
25 #include <sys/socket.h>
31 /* thanks to Beverly Brown (beverly@datacube.com) */
32 #ifdef USE_SOCKETS_AS_HANDLES
33 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
34 # define TO_SOCKET(x) _get_osfhandle(x)
36 # define OPEN_SOCKET(x) (x)
37 # define TO_SOCKET(x) (x)
38 #endif /* USE_SOCKETS_AS_HANDLES */
40 #if defined(USE_ITHREADS)
41 #define StartSockets() \
48 #define StartSockets() \
50 if (!wsock_started) { \
57 #define SOCKET_TEST(x, y) \
61 errno = WSAGetLastError(); \
64 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
66 static struct servent* win32_savecopyservent(struct servent*d,
70 static int wsock_started = 0;
83 unsigned short version;
88 * initalize the winsock interface and insure that it is
92 if(ret = WSAStartup(version, &retdata))
93 Perl_croak_nocontext("Unable to locate winsock library!\n");
94 if(retdata.wVersion != version)
95 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
97 /* atexit((void (*)(void)) EndSockets); */
107 #ifndef USE_SOCKETS_AS_HANDLES
110 my_fdopen(int fd, char *mode)
114 int optlen = sizeof(sockbuf);
118 return(fdopen(fd, mode));
120 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
121 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
122 return(fdopen(fd, mode));
126 * If we get here, then fd is actually a socket.
128 Newz(1310, fp, 1, FILE); /* XXX leak, good thing this code isn't used */
142 #endif /* USE_SOCKETS_AS_HANDLES */
146 win32_htonl(u_long hostlong)
149 return htonl(hostlong);
153 win32_htons(u_short hostshort)
156 return htons(hostshort);
160 win32_ntohl(u_long netlong)
163 return ntohl(netlong);
167 win32_ntohs(u_short netshort)
170 return ntohs(netshort);
176 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
180 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
181 return OPEN_SOCKET(r);
185 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
189 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
194 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
198 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
204 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
208 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
213 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
217 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
222 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
226 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
231 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
235 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
240 win32_listen(SOCKET s, int backlog)
244 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
249 win32_recv(SOCKET s, char *buf, int len, int flags)
253 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
258 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
261 int frombufsize = *fromlen;
263 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
264 /* Winsock's recvfrom() only returns a valid 'from' when the socket
265 * is connectionless. Perl expects a valid 'from' for all types
266 * of sockets, so go the extra mile.
268 if (r != SOCKET_ERROR && frombufsize == *fromlen)
269 (void)win32_getpeername(s, from, fromlen);
273 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
275 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
278 #ifdef USE_SOCKETS_AS_HANDLES
280 int i, fd, save_errno = errno;
281 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
283 /* winsock seems incapable of dealing with all three null fd_sets,
284 * so do the (millisecond) sleep as a special case
286 if (!(rd || wr || ex)) {
288 Sleep(timeout->tv_sec * 1000 +
289 timeout->tv_usec / 1000); /* do the best we can */
295 PERL_FD_ZERO(&dummy);
297 rd = &dummy, prd = NULL;
301 wr = &dummy, pwr = NULL;
305 ex = &dummy, pex = NULL;
312 for (i = 0; i < nfds; i++) {
314 if (PERL_FD_ISSET(i,rd))
315 FD_SET((unsigned)fd, &nrd);
316 if (PERL_FD_ISSET(i,wr))
317 FD_SET((unsigned)fd, &nwr);
318 if (PERL_FD_ISSET(i,ex))
319 FD_SET((unsigned)fd, &nex);
323 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
326 for (i = 0; i < nfds; i++) {
328 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
330 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
332 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
337 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
343 win32_send(SOCKET s, const char *buf, int len, int flags)
347 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
352 win32_sendto(SOCKET s, const char *buf, int len, int flags,
353 const struct sockaddr *to, int tolen)
357 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
362 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
366 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
371 win32_shutdown(SOCKET s, int how)
375 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
380 win32_closesocket(SOCKET s)
384 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
388 #ifdef USE_SOCKETS_AS_HANDLES
389 #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol)
392 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
394 Copy(in, out, 1, WSAPROTOCOL_INFOA);
395 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
399 open_ifs_socket(int af, int type, int protocol)
403 unsigned long proto_buffers_len = 0;
405 SOCKET out = INVALID_SOCKET;
407 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
408 return WSASocket(af, type, protocol, NULL, 0, 0);
410 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
411 && error_code == WSAENOBUFS)
413 WSAPROTOCOL_INFOW *proto_buffers;
414 int protocols_available = 0;
416 New(1, proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
419 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
420 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
423 for (i = 0; i < protocols_available; i++)
425 WSAPROTOCOL_INFOA proto_info;
427 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
428 || (type != proto_buffers[i].iSocketType)
429 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
430 protocol != proto_buffers[i].iProtocol))
433 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
436 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
438 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
443 Safefree(proto_buffers);
450 #define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol)
454 win32_socket(int af, int type, int protocol)
458 #ifndef USE_SOCKETS_AS_HANDLES
459 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
463 if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET)
464 errno = WSAGetLastError();
467 #endif /* USE_SOCKETS_AS_HANDLES */
473 * close RTL fd while respecting sockets
474 * added as temporary measure until PerlIO has real
482 if (!wsock_started) /* No WinSock? */
483 return(close(fd)); /* Then not a socket. */
484 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
487 err = closesocket(osf);
489 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
490 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
492 (void)close(fd); /* handle already closed, ignore error */
495 else if (err == SOCKET_ERROR) {
496 err = WSAGetLastError();
497 if (err != WSAENOTSOCK) {
512 if (!wsock_started) /* No WinSock? */
513 return(fclose(pf)); /* Then not a socket. */
514 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
518 err = closesocket(osf);
520 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
521 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
523 (void)fclose(pf); /* handle already closed, ignore error */
526 else if (err == SOCKET_ERROR) {
527 err = WSAGetLastError();
528 if (err != WSAENOTSOCK) {
540 my_fstat(int fd, Stat_t *sbufptr)
542 /* This fixes a bug in fstat() on Windows 9x. fstat() uses the
543 * GetFileType() win32 syscall, which will fail on Windows 9x.
544 * So if we recognize a socket on Windows 9x, we return the
545 * same results as on Windows NT/2000.
546 * XXX this should be extended further to set S_IFSOCK on
550 if (!wsock_started || IsWinNT()) {
551 #if defined(WIN64) || defined(USE_LARGE_FILES)
552 #if defined(__BORLANDC__) /* buk */
553 return win32_fstat(fd, sbufptr );
555 return _fstati64(fd, sbufptr);
558 return fstat(fd, sbufptr);
565 int optlen = sizeof(sockbuf);
568 retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
569 if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
570 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
571 sbufptr->st_mode = S_IFIFO;
573 sbufptr->st_mode = _S_IFIFO;
575 sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
576 sbufptr->st_nlink = 1;
577 sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
578 sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
579 sbufptr->st_size = (Off_t)0;
583 #if defined(WIN64) || defined(USE_LARGE_FILES)
584 #if defined(__BORLANDC__) /* buk */
585 return win32_fstat(fd, sbufptr );
587 return _fstati64(fd, sbufptr);
590 return fstat(fd, sbufptr);
595 win32_gethostbyaddr(const char *addr, int len, int type)
599 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
604 win32_gethostbyname(const char *name)
608 SOCKET_TEST(r = gethostbyname(name), NULL);
613 win32_gethostname(char *name, int len)
617 SOCKET_TEST_ERROR(r = gethostname(name, len));
622 win32_getprotobyname(const char *name)
626 SOCKET_TEST(r = getprotobyname(name), NULL);
631 win32_getprotobynumber(int num)
635 SOCKET_TEST(r = getprotobynumber(num), NULL);
640 win32_getservbyname(const char *name, const char *proto)
645 SOCKET_TEST(r = getservbyname(name, proto), NULL);
647 r = win32_savecopyservent(&w32_servent, r, proto);
653 win32_getservbyport(int port, const char *proto)
658 SOCKET_TEST(r = getservbyport(port, proto), NULL);
660 r = win32_savecopyservent(&w32_servent, r, proto);
666 win32_ioctl(int i, unsigned int u, char *data)
669 u_long argp = (u_long)data;
672 if (!wsock_started) {
673 Perl_croak_nocontext("ioctl implemented only on sockets");
677 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
678 if (retval == SOCKET_ERROR) {
679 if (WSAGetLastError() == WSAENOTSOCK) {
680 Perl_croak_nocontext("ioctl implemented only on sockets");
683 errno = WSAGetLastError();
689 win32_inet_ntoa(struct in_addr in)
692 return inet_ntoa(in);
696 win32_inet_addr(const char FAR *cp)
699 return inet_addr(cp);
710 Perl_croak_nocontext("endhostent not implemented!\n");
717 Perl_croak_nocontext("endnetent not implemented!\n");
724 Perl_croak_nocontext("endprotoent not implemented!\n");
731 Perl_croak_nocontext("endservent not implemented!\n");
736 win32_getnetent(void)
739 Perl_croak_nocontext("getnetent not implemented!\n");
740 return (struct netent *) NULL;
744 win32_getnetbyname(char *name)
747 Perl_croak_nocontext("getnetbyname not implemented!\n");
748 return (struct netent *)NULL;
752 win32_getnetbyaddr(long net, int type)
755 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
756 return (struct netent *)NULL;
760 win32_getprotoent(void)
763 Perl_croak_nocontext("getprotoent not implemented!\n");
764 return (struct protoent *) NULL;
768 win32_getservent(void)
771 Perl_croak_nocontext("getservent not implemented!\n");
772 return (struct servent *) NULL;
776 win32_sethostent(int stayopen)
779 Perl_croak_nocontext("sethostent not implemented!\n");
784 win32_setnetent(int stayopen)
787 Perl_croak_nocontext("setnetent not implemented!\n");
792 win32_setprotoent(int stayopen)
795 Perl_croak_nocontext("setprotoent not implemented!\n");
800 win32_setservent(int stayopen)
803 Perl_croak_nocontext("setservent not implemented!\n");
806 static struct servent*
807 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
809 d->s_name = s->s_name;
810 d->s_aliases = s->s_aliases;
811 d->s_port = s->s_port;
812 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
813 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
814 d->s_proto = s->s_proto;
817 if (proto && strlen(proto))
818 d->s_proto = (char *)proto;