3 * (c) 1995 Microsoft Corporation. All rights reserved.
4 * Developed by hip communications inc.
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 #define StartSockets() \
46 #define SOCKET_TEST(x, y) \
50 errno = WSAGetLastError(); \
53 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
55 static struct servent* win32_savecopyservent(struct servent*d,
59 static int wsock_started = 0;
72 unsigned short version;
77 * initalize the winsock interface and insure that it is
81 if(ret = WSAStartup(version, &retdata))
82 Perl_croak_nocontext("Unable to locate winsock library!\n");
83 if(retdata.wVersion != version)
84 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
86 /* atexit((void (*)(void)) EndSockets); */
90 #ifndef USE_SOCKETS_AS_HANDLES
93 my_fdopen(int fd, char *mode)
97 int optlen = sizeof(sockbuf);
101 return(fdopen(fd, mode));
103 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
104 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
105 return(fdopen(fd, mode));
109 * If we get here, then fd is actually a socket.
111 Newxz(fp, 1, FILE); /* XXX leak, good thing this code isn't used */
125 #endif /* USE_SOCKETS_AS_HANDLES */
129 win32_htonl(u_long hostlong)
132 return htonl(hostlong);
136 win32_htons(u_short hostshort)
139 return htons(hostshort);
143 win32_ntohl(u_long netlong)
146 return ntohl(netlong);
150 win32_ntohs(u_short netshort)
153 return ntohs(netshort);
159 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
163 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
164 return OPEN_SOCKET(r);
168 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
172 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
177 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
181 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
187 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
191 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
196 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
200 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
205 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
209 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
214 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
218 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
223 win32_listen(SOCKET s, int backlog)
227 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
232 win32_recv(SOCKET s, char *buf, int len, int flags)
236 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
241 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
244 int frombufsize = *fromlen;
246 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
247 /* Winsock's recvfrom() only returns a valid 'from' when the socket
248 * is connectionless. Perl expects a valid 'from' for all types
249 * of sockets, so go the extra mile.
251 if (r != SOCKET_ERROR && frombufsize == *fromlen)
252 (void)win32_getpeername(s, from, fromlen);
256 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
258 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
261 #ifdef USE_SOCKETS_AS_HANDLES
262 int i, fd, save_errno = errno;
263 FD_SET nrd, nwr, nex;
264 bool just_sleep = TRUE;
271 for (i = 0; i < nfds; i++) {
272 if (rd && PERL_FD_ISSET(i,rd)) {
274 FD_SET((unsigned)fd, &nrd);
277 if (wr && PERL_FD_ISSET(i,wr)) {
279 FD_SET((unsigned)fd, &nwr);
282 if (ex && PERL_FD_ISSET(i,ex)) {
284 FD_SET((unsigned)fd, &nex);
289 /* winsock seems incapable of dealing with all three fd_sets being empty,
290 * so do the (millisecond) sleep as a special case
294 Sleep(timeout->tv_sec * 1000 +
295 timeout->tv_usec / 1000); /* do the best we can */
302 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
305 for (i = 0; i < nfds; i++) {
306 if (rd && PERL_FD_ISSET(i,rd)) {
308 if (!FD_ISSET(fd, &nrd))
311 if (wr && PERL_FD_ISSET(i,wr)) {
313 if (!FD_ISSET(fd, &nwr))
316 if (ex && PERL_FD_ISSET(i,ex)) {
318 if (!FD_ISSET(fd, &nex))
324 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
330 win32_send(SOCKET s, const char *buf, int len, int flags)
334 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
339 win32_sendto(SOCKET s, const char *buf, int len, int flags,
340 const struct sockaddr *to, int tolen)
344 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
349 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
353 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
358 win32_shutdown(SOCKET s, int how)
362 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
367 win32_closesocket(SOCKET s)
371 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
375 #ifdef USE_SOCKETS_AS_HANDLES
376 #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol)
379 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
381 Copy(in, out, 1, WSAPROTOCOL_INFOA);
382 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
386 open_ifs_socket(int af, int type, int protocol)
390 unsigned long proto_buffers_len = 0;
392 SOCKET out = INVALID_SOCKET;
394 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
395 return WSASocket(af, type, protocol, NULL, 0, 0);
397 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
398 && error_code == WSAENOBUFS)
400 WSAPROTOCOL_INFOW *proto_buffers;
401 int protocols_available = 0;
403 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
406 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
407 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
410 for (i = 0; i < protocols_available; i++)
412 WSAPROTOCOL_INFOA proto_info;
414 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
415 || (type != proto_buffers[i].iSocketType)
416 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
417 protocol != proto_buffers[i].iProtocol))
420 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
423 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
425 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
430 Safefree(proto_buffers);
437 #define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol)
441 win32_socket(int af, int type, int protocol)
445 #ifndef USE_SOCKETS_AS_HANDLES
446 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
450 if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET)
451 errno = WSAGetLastError();
454 #endif /* USE_SOCKETS_AS_HANDLES */
460 * close RTL fd while respecting sockets
461 * added as temporary measure until PerlIO has real
469 if (!wsock_started) /* No WinSock? */
470 return(close(fd)); /* Then not a socket. */
471 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
474 err = closesocket(osf);
476 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
477 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
479 (void)close(fd); /* handle already closed, ignore error */
482 else if (err == SOCKET_ERROR) {
483 err = WSAGetLastError();
484 if (err != WSAENOTSOCK) {
499 if (!wsock_started) /* No WinSock? */
500 return(fclose(pf)); /* Then not a socket. */
501 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
505 err = closesocket(osf);
507 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
508 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
510 (void)fclose(pf); /* handle already closed, ignore error */
513 else if (err == SOCKET_ERROR) {
514 err = WSAGetLastError();
515 if (err != WSAENOTSOCK) {
527 my_fstat(int fd, Stat_t *sbufptr)
529 /* This fixes a bug in fstat() on Windows 9x. fstat() uses the
530 * GetFileType() win32 syscall, which will fail on Windows 9x.
531 * So if we recognize a socket on Windows 9x, we return the
532 * same results as on Windows NT/2000.
533 * XXX this should be extended further to set S_IFSOCK on
537 if (!wsock_started || IsWinNT()) {
538 #if defined(WIN64) || defined(USE_LARGE_FILES)
539 #if defined(__BORLANDC__) /* buk */
540 return win32_fstat(fd, sbufptr );
542 return _fstati64(fd, sbufptr);
545 return fstat(fd, sbufptr);
552 int optlen = sizeof(sockbuf);
555 retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
556 if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
557 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
558 sbufptr->st_mode = S_IFIFO;
560 sbufptr->st_mode = _S_IFIFO;
562 sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
563 sbufptr->st_nlink = 1;
564 sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
565 sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
566 sbufptr->st_size = (Off_t)0;
570 #if defined(WIN64) || defined(USE_LARGE_FILES)
571 #if defined(__BORLANDC__) /* buk */
572 return win32_fstat(fd, sbufptr );
574 return _fstati64(fd, sbufptr);
577 return fstat(fd, sbufptr);
582 win32_gethostbyaddr(const char *addr, int len, int type)
586 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
591 win32_gethostbyname(const char *name)
595 SOCKET_TEST(r = gethostbyname(name), NULL);
600 win32_gethostname(char *name, int len)
604 SOCKET_TEST_ERROR(r = gethostname(name, len));
609 win32_getprotobyname(const char *name)
613 SOCKET_TEST(r = getprotobyname(name), NULL);
618 win32_getprotobynumber(int num)
622 SOCKET_TEST(r = getprotobynumber(num), NULL);
627 win32_getservbyname(const char *name, const char *proto)
632 SOCKET_TEST(r = getservbyname(name, proto), NULL);
634 r = win32_savecopyservent(&w32_servent, r, proto);
640 win32_getservbyport(int port, const char *proto)
645 SOCKET_TEST(r = getservbyport(port, proto), NULL);
647 r = win32_savecopyservent(&w32_servent, r, proto);
653 win32_ioctl(int i, unsigned int u, char *data)
659 if (!wsock_started) {
660 Perl_croak_nocontext("ioctl implemented only on sockets");
664 /* mauke says using memcpy avoids alignment issues */
665 memcpy(&u_long_arg, data, sizeof u_long_arg);
666 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
667 memcpy(data, &u_long_arg, sizeof u_long_arg);
669 if (retval == SOCKET_ERROR) {
670 if (WSAGetLastError() == WSAENOTSOCK) {
671 Perl_croak_nocontext("ioctl implemented only on sockets");
674 errno = WSAGetLastError();
680 win32_inet_ntoa(struct in_addr in)
683 return inet_ntoa(in);
687 win32_inet_addr(const char FAR *cp)
690 return inet_addr(cp);
701 Perl_croak_nocontext("endhostent not implemented!\n");
708 Perl_croak_nocontext("endnetent not implemented!\n");
715 Perl_croak_nocontext("endprotoent not implemented!\n");
722 Perl_croak_nocontext("endservent not implemented!\n");
727 win32_getnetent(void)
730 Perl_croak_nocontext("getnetent not implemented!\n");
731 return (struct netent *) NULL;
735 win32_getnetbyname(char *name)
738 Perl_croak_nocontext("getnetbyname not implemented!\n");
739 return (struct netent *)NULL;
743 win32_getnetbyaddr(long net, int type)
746 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
747 return (struct netent *)NULL;
751 win32_getprotoent(void)
754 Perl_croak_nocontext("getprotoent not implemented!\n");
755 return (struct protoent *) NULL;
759 win32_getservent(void)
762 Perl_croak_nocontext("getservent not implemented!\n");
763 return (struct servent *) NULL;
767 win32_sethostent(int stayopen)
770 Perl_croak_nocontext("sethostent not implemented!\n");
775 win32_setnetent(int stayopen)
778 Perl_croak_nocontext("setnetent not implemented!\n");
783 win32_setprotoent(int stayopen)
786 Perl_croak_nocontext("setprotoent not implemented!\n");
791 win32_setservent(int stayopen)
794 Perl_croak_nocontext("setservent not implemented!\n");
797 static struct servent*
798 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
800 d->s_name = s->s_name;
801 d->s_aliases = s->s_aliases;
802 d->s_port = s->s_port;
803 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
804 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
805 d->s_proto = s->s_proto;
808 if (proto && strlen(proto))
809 d->s_proto = (char *)proto;