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) \
46 int wsaerr = WSAGetLastError(); \
47 errno = convert_wsa_error_to_errno(wsaerr); \
48 SetLastError(wsaerr); \
52 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
54 static struct servent* win32_savecopyservent(struct servent*d,
58 static int wsock_started = 0;
60 #ifdef WIN32_DYN_IOINFO_SIZE
61 EXTERN_C Size_t w32_ioinfo_size;
71 /* Translate WSAExxx values to corresponding Exxx values where possible. Not all
72 * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in
73 * VC++ 2010 and above, which have expanded <errno.h> with more values), but
74 * most missing constants are provided by win32/include/sys/errno2.h. The few
75 * that are not are returned unchanged.
77 * The list of possible WSAExxx values used here comes from the MSDN page
78 * titled "Windows Sockets Error Codes".
80 * (Note: Only the WSAExxx values are handled here; other WSAxxx values are
81 * returned unchanged. The return value normally ends up in errno/$! and at
82 * the Perl code level may be tested against the Exxx constants exported by
83 * the Errno and POSIX modules, which have never handled the other WSAxxx
84 * values themselves, apparently without any ill effect so far.)
87 convert_wsa_error_to_errno(int wsaerr)
110 case WSAEDESTADDRREQ:
118 case WSAEPROTONOSUPPORT:
119 return EPROTONOSUPPORT;
120 case WSAESOCKTNOSUPPORT:
121 return ESOCKTNOSUPPORT;
124 case WSAEPFNOSUPPORT:
126 case WSAEAFNOSUPPORT:
130 case WSAEADDRNOTAVAIL:
131 return EADDRNOTAVAIL;
138 case WSAECONNABORTED:
150 case WSAETOOMANYREFS:
154 case WSAECONNREFUSED:
158 case WSAENAMETOOLONG:
161 return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */
162 case WSAEHOSTUNREACH:
177 return WSAEDISCON; /* EDISCON is not defined */
179 return WSAENOMORE; /* ENOMORE is not defined */
181 case WSAECANCELLED: /* New in WinSock2 */
184 case WSAEINVALIDPROCTABLE:
185 return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */
186 case WSAEINVALIDPROVIDER:
187 return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */
188 case WSAEPROVIDERFAILEDINIT:
189 return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */
191 return WSAEREFUSED; /* EREFUSED is not defined */
197 #ifdef ERRNO_HAS_POSIX_SUPPLEMENT
198 /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
199 * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
200 * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
201 * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
202 * really expect to encounter them anyway in the context in which this function
204 * Some versions of MinGW/gcc-4.8 and above also define most, but not all, of
205 * these extra Exxx values. The missing ones are all cases for which there is no
206 * corresponding WSAExxx constant anyway, so we simply omit the cases for them
208 * Other Exxx values (err < sys_nerr) are returned unchanged.
211 convert_errno_to_wsa_error(int err)
215 return WSAEADDRINUSE;
217 return WSAEADDRNOTAVAIL;
219 return WSAEAFNOSUPPORT;
223 case EBADMSG: /* Not defined in gcc-4.8.0 */
224 return ERROR_INVALID_FUNCTION;
228 return WSAECANCELLED; /* New in WinSock2 */
230 return ERROR_INVALID_FUNCTION;
233 return WSAECONNABORTED;
235 return WSAECONNREFUSED;
237 return WSAECONNRESET;
239 return WSAEDESTADDRREQ;
241 return WSAEHOSTUNREACH;
243 case EIDRM: /* Not defined in gcc-4.8.0 */
244 return ERROR_INVALID_FUNCTION;
247 return WSAEINPROGRESS;
259 return WSAENETUNREACH;
263 case ENODATA: /* Not defined in gcc-4.8.0 */
264 return ERROR_INVALID_FUNCTION;
267 case ENOLINK: /* Not defined in gcc-4.8.0 */
268 return ERROR_INVALID_FUNCTION;
271 case ENOMSG: /* Not defined in gcc-4.8.0 */
272 return ERROR_INVALID_FUNCTION;
275 return WSAENOPROTOOPT;
277 case ENOSR: /* Not defined in gcc-4.8.0 */
278 return ERROR_INVALID_FUNCTION;
281 case ENOSTR: /* Not defined in gcc-4.8.0 */
282 return ERROR_INVALID_FUNCTION;
286 #ifdef ENOTRECOVERABLE
287 case ENOTRECOVERABLE: /* Not defined in gcc-4.8.0 */
288 return ERROR_INVALID_FUNCTION;
293 return ERROR_INVALID_FUNCTION;
295 return WSAEOPNOTSUPP;
297 case EOTHER: /* Not defined in gcc-4.8.0 */
298 return ERROR_INVALID_FUNCTION;
301 return ERROR_INVALID_FUNCTION;
303 return ERROR_INVALID_FUNCTION;
305 return ERROR_INVALID_FUNCTION;
306 case EPROTONOSUPPORT:
307 return WSAEPROTONOSUPPORT;
309 return WSAEPROTOTYPE;
311 case ETIME: /* Not defined in gcc-4.8.0 */
312 return ERROR_INVALID_FUNCTION;
317 case ETXTBSY: /* Not defined in gcc-4.8.0 */
318 return ERROR_INVALID_FUNCTION;
321 return WSAEWOULDBLOCK;
326 #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
331 unsigned short version;
336 * initalize the winsock interface and insure that it is
337 * cleaned up at exit.
340 if(ret = WSAStartup(version, &retdata))
341 Perl_croak_nocontext("Unable to locate winsock library!\n");
342 if(retdata.wVersion != version)
343 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
345 /* atexit((void (*)(void)) EndSockets); */
349 /* in no sockets Win32 builds, these use the inline functions defined in
353 win32_htonl(u_long hostlong)
355 #ifndef WIN32_NO_SOCKETS
358 return htonl(hostlong);
362 win32_htons(u_short hostshort)
364 #ifndef WIN32_NO_SOCKETS
367 return htons(hostshort);
371 win32_ntohl(u_long netlong)
373 #ifndef WIN32_NO_SOCKETS
376 return ntohl(netlong);
380 win32_ntohs(u_short netshort)
382 #ifndef WIN32_NO_SOCKETS
385 return ntohs(netshort);
391 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
395 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
396 return OPEN_SOCKET(r);
400 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
404 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
409 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
413 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
419 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
423 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
428 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
432 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
437 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
441 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
446 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
450 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
455 win32_listen(SOCKET s, int backlog)
459 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
464 win32_recv(SOCKET s, char *buf, int len, int flags)
468 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
473 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
476 int frombufsize = *fromlen;
478 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
479 /* Winsock's recvfrom() only returns a valid 'from' when the socket
480 * is connectionless. Perl expects a valid 'from' for all types
481 * of sockets, so go the extra mile.
483 if (r != SOCKET_ERROR && frombufsize == *fromlen)
484 (void)win32_getpeername(s, from, fromlen);
488 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
490 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
493 int i, fd, save_errno = errno;
494 FD_SET nrd, nwr, nex;
495 bool just_sleep = TRUE;
502 for (i = 0; i < nfds; i++) {
503 if (rd && PERL_FD_ISSET(i,rd)) {
505 FD_SET((unsigned)fd, &nrd);
508 if (wr && PERL_FD_ISSET(i,wr)) {
510 FD_SET((unsigned)fd, &nwr);
513 if (ex && PERL_FD_ISSET(i,ex)) {
515 FD_SET((unsigned)fd, &nex);
520 /* winsock seems incapable of dealing with all three fd_sets being empty,
521 * so do the (millisecond) sleep as a special case
525 Sleep(timeout->tv_sec * 1000 +
526 timeout->tv_usec / 1000); /* do the best we can */
533 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout));
536 for (i = 0; i < nfds; i++) {
537 if (rd && PERL_FD_ISSET(i,rd)) {
539 if (!FD_ISSET(fd, &nrd))
542 if (wr && PERL_FD_ISSET(i,wr)) {
544 if (!FD_ISSET(fd, &nwr))
547 if (ex && PERL_FD_ISSET(i,ex)) {
549 if (!FD_ISSET(fd, &nex))
558 win32_send(SOCKET s, const char *buf, int len, int flags)
562 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
567 win32_sendto(SOCKET s, const char *buf, int len, int flags,
568 const struct sockaddr *to, int tolen)
572 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
577 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
581 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
586 win32_shutdown(SOCKET s, int how)
590 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
595 win32_closesocket(SOCKET s)
599 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
604 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
606 Copy(in, out, 1, WSAPROTOCOL_INFOA);
607 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
611 open_ifs_socket(int af, int type, int protocol)
615 unsigned long proto_buffers_len = 0;
617 SOCKET out = INVALID_SOCKET;
619 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
620 return WSASocket(af, type, protocol, NULL, 0, 0);
622 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
623 && error_code == WSAENOBUFS)
625 WSAPROTOCOL_INFOW *proto_buffers;
626 int protocols_available = 0;
628 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
631 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
632 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
635 for (i = 0; i < protocols_available; i++)
637 WSAPROTOCOL_INFOA proto_info;
639 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
640 || (type != proto_buffers[i].iSocketType)
641 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
642 protocol != proto_buffers[i].iProtocol))
645 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
648 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
650 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
655 Safefree(proto_buffers);
662 win32_socket(int af, int type, int protocol)
668 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
670 int wsaerr = WSAGetLastError();
671 errno = convert_wsa_error_to_errno(wsaerr);
672 SetLastError(wsaerr);
681 * close RTL fd while respecting sockets
682 * added as temporary measure until PerlIO has real
690 if (!wsock_started) /* No WinSock? */
691 return(close(fd)); /* Then not a socket. */
692 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
695 err = closesocket(osf);
698 assert(_osfhnd(fd) == osf); /* catch a bad ioinfo struct def */
699 /* don't close freed handle */
700 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
703 (void)close(fd); /* handle already closed, ignore error */
707 else if (err == SOCKET_ERROR) {
708 int wsaerr = WSAGetLastError();
709 err = convert_wsa_error_to_errno(wsaerr);
710 if (err != ENOTSOCK) {
713 SetLastError(wsaerr);
726 if (!wsock_started) /* No WinSock? */
727 return(fclose(pf)); /* Then not a socket. */
728 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
732 err = closesocket(osf);
735 assert(_osfhnd(win32_fileno(pf)) == osf); /* catch a bad ioinfo struct def */
736 /* don't close freed handle */
737 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
740 (void)fclose(pf); /* handle already closed, ignore error */
744 else if (err == SOCKET_ERROR) {
745 int wsaerr = WSAGetLastError();
746 err = convert_wsa_error_to_errno(wsaerr);
747 if (err != ENOTSOCK) {
750 SetLastError(wsaerr);
759 win32_gethostbyaddr(const char *addr, int len, int type)
763 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
768 win32_gethostbyname(const char *name)
772 SOCKET_TEST(r = gethostbyname(name), NULL);
777 win32_gethostname(char *name, int len)
781 SOCKET_TEST_ERROR(r = gethostname(name, len));
786 win32_getprotobyname(const char *name)
790 SOCKET_TEST(r = getprotobyname(name), NULL);
795 win32_getprotobynumber(int num)
799 SOCKET_TEST(r = getprotobynumber(num), NULL);
804 win32_getservbyname(const char *name, const char *proto)
809 SOCKET_TEST(r = getservbyname(name, proto), NULL);
812 r = win32_savecopyservent(&w32_servent, r, proto);
818 win32_getservbyport(int port, const char *proto)
823 SOCKET_TEST(r = getservbyport(port, proto), NULL);
826 r = win32_savecopyservent(&w32_servent, r, proto);
832 win32_ioctl(int i, unsigned int u, char *data)
837 if (!wsock_started) {
838 Perl_croak_nocontext("ioctl implemented only on sockets");
842 /* mauke says using memcpy avoids alignment issues */
843 memcpy(&u_long_arg, data, sizeof u_long_arg);
844 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
845 memcpy(data, &u_long_arg, sizeof u_long_arg);
847 if (retval == SOCKET_ERROR) {
848 int wsaerr = WSAGetLastError();
849 int err = convert_wsa_error_to_errno(wsaerr);
850 if (err == ENOTSOCK) {
851 Perl_croak_nocontext("ioctl implemented only on sockets");
855 SetLastError(wsaerr);
861 win32_inet_ntoa(struct in_addr in)
864 return inet_ntoa(in);
868 win32_inet_addr(const char FAR *cp)
871 return inet_addr(cp);
881 win32_croak_not_implemented("endhostent");
887 win32_croak_not_implemented("endnetent");
893 win32_croak_not_implemented("endprotoent");
899 win32_croak_not_implemented("endservent");
904 win32_getnetent(void)
906 win32_croak_not_implemented("getnetent");
907 return (struct netent *) NULL;
911 win32_getnetbyname(char *name)
913 win32_croak_not_implemented("getnetbyname");
914 return (struct netent *)NULL;
918 win32_getnetbyaddr(long net, int type)
920 win32_croak_not_implemented("getnetbyaddr");
921 return (struct netent *)NULL;
925 win32_getprotoent(void)
927 win32_croak_not_implemented("getprotoent");
928 return (struct protoent *) NULL;
932 win32_getservent(void)
934 win32_croak_not_implemented("getservent");
935 return (struct servent *) NULL;
939 win32_sethostent(int stayopen)
941 win32_croak_not_implemented("sethostent");
946 win32_setnetent(int stayopen)
948 win32_croak_not_implemented("setnetent");
953 win32_setprotoent(int stayopen)
955 win32_croak_not_implemented("setprotoent");
960 win32_setservent(int stayopen)
962 win32_croak_not_implemented("setservent");
965 static struct servent*
966 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
968 d->s_name = s->s_name;
969 d->s_aliases = s->s_aliases;
970 d->s_port = s->s_port;
971 if (s->s_proto && strlen(s->s_proto))
972 d->s_proto = s->s_proto;
974 if (proto && strlen(proto))
975 d->s_proto = (char *)proto;