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 #define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
33 #define TO_SOCKET(x) _get_osfhandle(x)
35 #define StartSockets() \
41 #define SOCKET_TEST(x, y) \
45 errno = get_last_socket_error(); \
48 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
50 static int get_last_socket_error(void);
51 static struct servent* win32_savecopyservent(struct servent*d,
55 static int wsock_started = 0;
64 /* Translate WSAExxx values to corresponding Exxx values where possible. Not all
65 * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in
66 * VC++ 2010 and above, which have expanded <errno.h> with more values), but
67 * most missing constants are provided by win32/include/sys/errno2.h. The few
68 * that are not are returned unchanged.
70 * The list of possible WSAExxx values used here comes from the MSDN page
71 * titled "Windows Sockets Error Codes".
73 * (Note: Only the WSAExxx values are handled here; other WSAxxx values are
74 * returned unchanged. The return value normally ends up in errno/$! and at
75 * the Perl code level may be tested against the Exxx constants exported by
76 * the Errno and POSIX modules, which have never handled the other WSAxxx
77 * values themselves, apparently without any ill effect so far.)
80 convert_wsa_error_to_errno(int wsaerr)
103 case WSAEDESTADDRREQ:
111 case WSAEPROTONOSUPPORT:
112 return EPROTONOSUPPORT;
113 case WSAESOCKTNOSUPPORT:
114 return ESOCKTNOSUPPORT;
117 case WSAEPFNOSUPPORT:
119 case WSAEAFNOSUPPORT:
123 case WSAEADDRNOTAVAIL:
124 return EADDRNOTAVAIL;
131 case WSAECONNABORTED:
143 case WSAETOOMANYREFS:
147 case WSAECONNREFUSED:
151 case WSAENAMETOOLONG:
154 return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */
155 case WSAEHOSTUNREACH:
170 return WSAEDISCON; /* EDISCON is not defined */
172 return WSAENOMORE; /* ENOMORE is not defined */
174 case WSAECANCELLED: /* New in WinSock2 */
177 case WSAEINVALIDPROCTABLE:
178 return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */
179 case WSAEINVALIDPROVIDER:
180 return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */
181 case WSAEPROVIDERFAILEDINIT:
182 return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */
184 return WSAEREFUSED; /* EREFUSED is not defined */
190 #ifdef ERRNO_HAS_POSIX_SUPPLEMENT
191 /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
192 * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
193 * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
194 * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
195 * really expect to encounter them anyway in the context in which this function
197 * Other Exxx values (err < sys_nerr) are returned unchanged.
200 convert_errno_to_wsa_error(int err)
204 return WSAEADDRINUSE;
206 return WSAEADDRNOTAVAIL;
208 return WSAEAFNOSUPPORT;
212 return ERROR_INVALID_FUNCTION;
215 return WSAECANCELLED; /* New in WinSock2 */
217 return ERROR_INVALID_FUNCTION;
220 return WSAECONNABORTED;
222 return WSAECONNREFUSED;
224 return WSAECONNRESET;
226 return WSAEDESTADDRREQ;
228 return WSAEHOSTUNREACH;
230 return ERROR_INVALID_FUNCTION;
232 return WSAEINPROGRESS;
244 return WSAENETUNREACH;
248 return ERROR_INVALID_FUNCTION;
250 return ERROR_INVALID_FUNCTION;
252 return ERROR_INVALID_FUNCTION;
254 return WSAENOPROTOOPT;
256 return ERROR_INVALID_FUNCTION;
258 return ERROR_INVALID_FUNCTION;
261 case ENOTRECOVERABLE:
262 return ERROR_INVALID_FUNCTION;
266 return ERROR_INVALID_FUNCTION;
268 return WSAEOPNOTSUPP;
270 return ERROR_INVALID_FUNCTION;
272 return ERROR_INVALID_FUNCTION;
274 return ERROR_INVALID_FUNCTION;
276 return ERROR_INVALID_FUNCTION;
277 case EPROTONOSUPPORT:
278 return WSAEPROTONOSUPPORT;
280 return WSAEPROTOTYPE;
282 return ERROR_INVALID_FUNCTION;
286 return ERROR_INVALID_FUNCTION;
288 return WSAEWOULDBLOCK;
293 #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
296 get_last_socket_error(void)
298 return convert_wsa_error_to_errno(WSAGetLastError());
304 unsigned short version;
309 * initalize the winsock interface and insure that it is
310 * cleaned up at exit.
313 if(ret = WSAStartup(version, &retdata))
314 Perl_croak_nocontext("Unable to locate winsock library!\n");
315 if(retdata.wVersion != version)
316 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
318 /* atexit((void (*)(void)) EndSockets); */
322 /* in no sockets Win32 builds, these use the inline functions defined in
326 win32_htonl(u_long hostlong)
328 #ifndef WIN32_NO_SOCKETS
331 return htonl(hostlong);
335 win32_htons(u_short hostshort)
337 #ifndef WIN32_NO_SOCKETS
340 return htons(hostshort);
344 win32_ntohl(u_long netlong)
346 #ifndef WIN32_NO_SOCKETS
349 return ntohl(netlong);
353 win32_ntohs(u_short netshort)
355 #ifndef WIN32_NO_SOCKETS
358 return ntohs(netshort);
364 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
368 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
369 return OPEN_SOCKET(r);
373 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
377 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
382 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
386 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
392 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
396 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
401 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
405 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
410 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
414 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
419 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
423 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
428 win32_listen(SOCKET s, int backlog)
432 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
437 win32_recv(SOCKET s, char *buf, int len, int flags)
441 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
446 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
449 int frombufsize = *fromlen;
451 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
452 /* Winsock's recvfrom() only returns a valid 'from' when the socket
453 * is connectionless. Perl expects a valid 'from' for all types
454 * of sockets, so go the extra mile.
456 if (r != SOCKET_ERROR && frombufsize == *fromlen)
457 (void)win32_getpeername(s, from, fromlen);
461 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
463 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
466 int i, fd, save_errno = errno;
467 FD_SET nrd, nwr, nex;
468 bool just_sleep = TRUE;
475 for (i = 0; i < nfds; i++) {
476 if (rd && PERL_FD_ISSET(i,rd)) {
478 FD_SET((unsigned)fd, &nrd);
481 if (wr && PERL_FD_ISSET(i,wr)) {
483 FD_SET((unsigned)fd, &nwr);
486 if (ex && PERL_FD_ISSET(i,ex)) {
488 FD_SET((unsigned)fd, &nex);
493 /* winsock seems incapable of dealing with all three fd_sets being empty,
494 * so do the (millisecond) sleep as a special case
498 Sleep(timeout->tv_sec * 1000 +
499 timeout->tv_usec / 1000); /* do the best we can */
506 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
509 for (i = 0; i < nfds; i++) {
510 if (rd && PERL_FD_ISSET(i,rd)) {
512 if (!FD_ISSET(fd, &nrd))
515 if (wr && PERL_FD_ISSET(i,wr)) {
517 if (!FD_ISSET(fd, &nwr))
520 if (ex && PERL_FD_ISSET(i,ex)) {
522 if (!FD_ISSET(fd, &nex))
531 win32_send(SOCKET s, const char *buf, int len, int flags)
535 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
540 win32_sendto(SOCKET s, const char *buf, int len, int flags,
541 const struct sockaddr *to, int tolen)
545 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
550 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
554 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
559 win32_shutdown(SOCKET s, int how)
563 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
568 win32_closesocket(SOCKET s)
572 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
577 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
579 Copy(in, out, 1, WSAPROTOCOL_INFOA);
580 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
584 open_ifs_socket(int af, int type, int protocol)
588 unsigned long proto_buffers_len = 0;
590 SOCKET out = INVALID_SOCKET;
592 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
593 return WSASocket(af, type, protocol, NULL, 0, 0);
595 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
596 && error_code == WSAENOBUFS)
598 WSAPROTOCOL_INFOW *proto_buffers;
599 int protocols_available = 0;
601 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
604 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
605 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
608 for (i = 0; i < protocols_available; i++)
610 WSAPROTOCOL_INFOA proto_info;
612 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
613 || (type != proto_buffers[i].iSocketType)
614 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
615 protocol != proto_buffers[i].iProtocol))
618 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
621 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
623 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
628 Safefree(proto_buffers);
635 win32_socket(int af, int type, int protocol)
641 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
642 errno = get_last_socket_error();
650 * close RTL fd while respecting sockets
651 * added as temporary measure until PerlIO has real
659 if (!wsock_started) /* No WinSock? */
660 return(close(fd)); /* Then not a socket. */
661 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
664 err = closesocket(osf);
666 (void)close(fd); /* handle already closed, ignore error */
669 else if (err == SOCKET_ERROR) {
670 err = get_last_socket_error();
671 if (err != ENOTSOCK) {
686 if (!wsock_started) /* No WinSock? */
687 return(fclose(pf)); /* Then not a socket. */
688 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
692 err = closesocket(osf);
694 (void)fclose(pf); /* handle already closed, ignore error */
697 else if (err == SOCKET_ERROR) {
698 err = get_last_socket_error();
699 if (err != ENOTSOCK) {
710 win32_gethostbyaddr(const char *addr, int len, int type)
714 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
719 win32_gethostbyname(const char *name)
723 SOCKET_TEST(r = gethostbyname(name), NULL);
728 win32_gethostname(char *name, int len)
732 SOCKET_TEST_ERROR(r = gethostname(name, len));
737 win32_getprotobyname(const char *name)
741 SOCKET_TEST(r = getprotobyname(name), NULL);
746 win32_getprotobynumber(int num)
750 SOCKET_TEST(r = getprotobynumber(num), NULL);
755 win32_getservbyname(const char *name, const char *proto)
760 SOCKET_TEST(r = getservbyname(name, proto), NULL);
763 r = win32_savecopyservent(&w32_servent, r, proto);
769 win32_getservbyport(int port, const char *proto)
774 SOCKET_TEST(r = getservbyport(port, proto), NULL);
777 r = win32_savecopyservent(&w32_servent, r, proto);
783 win32_ioctl(int i, unsigned int u, char *data)
788 if (!wsock_started) {
789 Perl_croak_nocontext("ioctl implemented only on sockets");
793 /* mauke says using memcpy avoids alignment issues */
794 memcpy(&u_long_arg, data, sizeof u_long_arg);
795 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
796 memcpy(data, &u_long_arg, sizeof u_long_arg);
798 if (retval == SOCKET_ERROR) {
799 int err = get_last_socket_error();
800 if (err == ENOTSOCK) {
801 Perl_croak_nocontext("ioctl implemented only on sockets");
810 win32_inet_ntoa(struct in_addr in)
813 return inet_ntoa(in);
817 win32_inet_addr(const char FAR *cp)
820 return inet_addr(cp);
830 win32_croak_not_implemented("endhostent");
836 win32_croak_not_implemented("endnetent");
842 win32_croak_not_implemented("endprotoent");
848 win32_croak_not_implemented("endservent");
853 win32_getnetent(void)
855 win32_croak_not_implemented("getnetent");
856 return (struct netent *) NULL;
860 win32_getnetbyname(char *name)
862 win32_croak_not_implemented("getnetbyname");
863 return (struct netent *)NULL;
867 win32_getnetbyaddr(long net, int type)
869 win32_croak_not_implemented("getnetbyaddr");
870 return (struct netent *)NULL;
874 win32_getprotoent(void)
876 win32_croak_not_implemented("getprotoent");
877 return (struct protoent *) NULL;
881 win32_getservent(void)
883 win32_croak_not_implemented("getservent");
884 return (struct servent *) NULL;
888 win32_sethostent(int stayopen)
890 win32_croak_not_implemented("sethostent");
895 win32_setnetent(int stayopen)
897 win32_croak_not_implemented("setnetent");
902 win32_setprotoent(int stayopen)
904 win32_croak_not_implemented("setprotoent");
909 win32_setservent(int stayopen)
911 win32_croak_not_implemented("setservent");
914 static struct servent*
915 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
917 d->s_name = s->s_name;
918 d->s_aliases = s->s_aliases;
919 d->s_port = s->s_port;
920 if (s->s_proto && strlen(s->s_proto))
921 d->s_proto = s->s_proto;
923 if (proto && strlen(proto))
924 d->s_proto = (char *)proto;