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_5005THREADS) || 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); */
104 #ifdef USE_SOCKETS_AS_HANDLES
105 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
107 if (!w32_init_socktype) {
108 w32_init_socktype = 1;
111 #endif /* USE_SOCKETS_AS_HANDLES */
115 #ifndef USE_SOCKETS_AS_HANDLES
118 my_fdopen(int fd, char *mode)
122 int optlen = sizeof(sockbuf);
126 return(fdopen(fd, mode));
128 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
129 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
130 return(fdopen(fd, mode));
134 * If we get here, then fd is actually a socket.
136 Newxz(fp, 1, FILE); /* XXX leak, good thing this code isn't used */
150 #endif /* USE_SOCKETS_AS_HANDLES */
154 win32_htonl(u_long hostlong)
157 return htonl(hostlong);
161 win32_htons(u_short hostshort)
164 return htons(hostshort);
168 win32_ntohl(u_long netlong)
171 return ntohl(netlong);
175 win32_ntohs(u_short netshort)
178 return ntohs(netshort);
184 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
188 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
189 return OPEN_SOCKET(r);
193 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
197 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
202 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
206 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
212 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
216 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
221 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
225 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
230 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
234 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
239 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
243 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
248 win32_listen(SOCKET s, int backlog)
252 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
257 win32_recv(SOCKET s, char *buf, int len, int flags)
261 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
266 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
269 int frombufsize = *fromlen;
271 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
272 /* Winsock's recvfrom() only returns a valid 'from' when the socket
273 * is connectionless. Perl expects a valid 'from' for all types
274 * of sockets, so go the extra mile.
276 if (r != SOCKET_ERROR && frombufsize == *fromlen)
277 (void)win32_getpeername(s, from, fromlen);
281 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
283 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
286 #ifdef USE_SOCKETS_AS_HANDLES
288 int i, fd, save_errno = errno;
289 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
291 /* winsock seems incapable of dealing with all three null fd_sets,
292 * so do the (millisecond) sleep as a special case
294 if (!(rd || wr || ex)) {
296 Sleep(timeout->tv_sec * 1000 +
297 timeout->tv_usec / 1000); /* do the best we can */
303 PERL_FD_ZERO(&dummy);
305 rd = &dummy, prd = NULL;
309 wr = &dummy, pwr = NULL;
313 ex = &dummy, pex = NULL;
320 for (i = 0; i < nfds; i++) {
322 if (PERL_FD_ISSET(i,rd))
323 FD_SET((unsigned)fd, &nrd);
324 if (PERL_FD_ISSET(i,wr))
325 FD_SET((unsigned)fd, &nwr);
326 if (PERL_FD_ISSET(i,ex))
327 FD_SET((unsigned)fd, &nex);
331 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
334 for (i = 0; i < nfds; i++) {
336 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
338 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
340 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
345 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
351 win32_send(SOCKET s, const char *buf, int len, int flags)
355 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
360 win32_sendto(SOCKET s, const char *buf, int len, int flags,
361 const struct sockaddr *to, int tolen)
365 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
370 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
374 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
379 win32_shutdown(SOCKET s, int how)
383 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
388 win32_closesocket(SOCKET s)
392 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
396 #ifdef USE_SOCKETS_AS_HANDLES
397 #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol)
400 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
402 Copy(in, out, 1, WSAPROTOCOL_INFOA);
403 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
407 open_ifs_socket(int af, int type, int protocol)
411 unsigned long proto_buffers_len = 0;
413 SOCKET out = INVALID_SOCKET;
415 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
416 return WSASocket(af, type, protocol, NULL, 0, 0);
418 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
419 && error_code == WSAENOBUFS)
421 WSAPROTOCOL_INFOW *proto_buffers;
422 int protocols_available = 0;
424 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
427 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
428 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
431 for (i = 0; i < protocols_available; i++)
433 WSAPROTOCOL_INFOA proto_info;
435 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
436 || (type != proto_buffers[i].iSocketType)
437 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
438 protocol != proto_buffers[i].iProtocol))
441 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
444 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
446 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
451 Safefree(proto_buffers);
458 #define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol)
462 win32_socket(int af, int type, int protocol)
466 #ifndef USE_SOCKETS_AS_HANDLES
467 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
471 if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET)
472 errno = WSAGetLastError();
475 #endif /* USE_SOCKETS_AS_HANDLES */
481 * close RTL fd while respecting sockets
482 * added as temporary measure until PerlIO has real
490 if (!wsock_started) /* No WinSock? */
491 return(close(fd)); /* Then not a socket. */
492 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
495 err = closesocket(osf);
497 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
498 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
500 (void)close(fd); /* handle already closed, ignore error */
503 else if (err == SOCKET_ERROR) {
504 err = WSAGetLastError();
505 if (err != WSAENOTSOCK) {
520 if (!wsock_started) /* No WinSock? */
521 return(fclose(pf)); /* Then not a socket. */
522 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
526 err = closesocket(osf);
528 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
529 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
531 (void)fclose(pf); /* handle already closed, ignore error */
534 else if (err == SOCKET_ERROR) {
535 err = WSAGetLastError();
536 if (err != WSAENOTSOCK) {
548 my_fstat(int fd, Stat_t *sbufptr)
550 /* This fixes a bug in fstat() on Windows 9x. fstat() uses the
551 * GetFileType() win32 syscall, which will fail on Windows 9x.
552 * So if we recognize a socket on Windows 9x, we return the
553 * same results as on Windows NT/2000.
554 * XXX this should be extended further to set S_IFSOCK on
558 if (!wsock_started || IsWinNT()) {
559 #if defined(WIN64) || defined(USE_LARGE_FILES)
560 #if defined(__BORLANDC__) /* buk */
561 return win32_fstat(fd, sbufptr );
563 return _fstati64(fd, sbufptr);
566 return fstat(fd, sbufptr);
573 int optlen = sizeof(sockbuf);
576 retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
577 if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
578 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
579 sbufptr->st_mode = S_IFIFO;
581 sbufptr->st_mode = _S_IFIFO;
583 sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
584 sbufptr->st_nlink = 1;
585 sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
586 sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
587 sbufptr->st_size = (Off_t)0;
591 #if defined(WIN64) || defined(USE_LARGE_FILES)
592 #if defined(__BORLANDC__) /* buk */
593 return win32_fstat(fd, sbufptr );
595 return _fstati64(fd, sbufptr);
598 return fstat(fd, sbufptr);
603 win32_gethostbyaddr(const char *addr, int len, int type)
607 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
612 win32_gethostbyname(const char *name)
616 SOCKET_TEST(r = gethostbyname(name), NULL);
621 win32_gethostname(char *name, int len)
625 SOCKET_TEST_ERROR(r = gethostname(name, len));
630 win32_getprotobyname(const char *name)
634 SOCKET_TEST(r = getprotobyname(name), NULL);
639 win32_getprotobynumber(int num)
643 SOCKET_TEST(r = getprotobynumber(num), NULL);
648 win32_getservbyname(const char *name, const char *proto)
653 SOCKET_TEST(r = getservbyname(name, proto), NULL);
655 r = win32_savecopyservent(&w32_servent, r, proto);
661 win32_getservbyport(int port, const char *proto)
666 SOCKET_TEST(r = getservbyport(port, proto), NULL);
668 r = win32_savecopyservent(&w32_servent, r, proto);
674 win32_ioctl(int i, unsigned int u, char *data)
677 u_long argp = (u_long)data;
680 if (!wsock_started) {
681 Perl_croak_nocontext("ioctl implemented only on sockets");
685 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
686 if (retval == SOCKET_ERROR) {
687 if (WSAGetLastError() == WSAENOTSOCK) {
688 Perl_croak_nocontext("ioctl implemented only on sockets");
691 errno = WSAGetLastError();
697 win32_inet_ntoa(struct in_addr in)
700 return inet_ntoa(in);
704 win32_inet_addr(const char FAR *cp)
707 return inet_addr(cp);
718 Perl_croak_nocontext("endhostent not implemented!\n");
725 Perl_croak_nocontext("endnetent not implemented!\n");
732 Perl_croak_nocontext("endprotoent not implemented!\n");
739 Perl_croak_nocontext("endservent not implemented!\n");
744 win32_getnetent(void)
747 Perl_croak_nocontext("getnetent not implemented!\n");
748 return (struct netent *) NULL;
752 win32_getnetbyname(char *name)
755 Perl_croak_nocontext("getnetbyname not implemented!\n");
756 return (struct netent *)NULL;
760 win32_getnetbyaddr(long net, int type)
763 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
764 return (struct netent *)NULL;
768 win32_getprotoent(void)
771 Perl_croak_nocontext("getprotoent not implemented!\n");
772 return (struct protoent *) NULL;
776 win32_getservent(void)
779 Perl_croak_nocontext("getservent not implemented!\n");
780 return (struct servent *) NULL;
784 win32_sethostent(int stayopen)
787 Perl_croak_nocontext("sethostent not implemented!\n");
792 win32_setnetent(int stayopen)
795 Perl_croak_nocontext("setnetent not implemented!\n");
800 win32_setprotoent(int stayopen)
803 Perl_croak_nocontext("setprotoent not implemented!\n");
808 win32_setservent(int stayopen)
811 Perl_croak_nocontext("setservent not implemented!\n");
814 static struct servent*
815 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
817 d->s_name = s->s_name;
818 d->s_aliases = s->s_aliases;
819 d->s_port = s->s_port;
820 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
821 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
822 d->s_proto = s->s_proto;
825 if (proto && strlen(proto))
826 d->s_proto = (char *)proto;