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
26 #include <sys/socket.h>
33 #define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
34 #define TO_SOCKET(x) _get_osfhandle(x)
36 #define SOCKET_TEST(x, y) \
40 int wsaerr = WSAGetLastError(); \
41 errno = convert_wsa_error_to_errno(wsaerr); \
42 SetLastError(wsaerr); \
46 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
48 static struct servent* win32_savecopyservent(struct servent*d,
52 #ifdef WIN32_DYN_IOINFO_SIZE
53 EXTERN_C Size_t w32_ioinfo_size;
62 /* Translate WSAExxx values to corresponding Exxx values where possible. Not all
63 * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in
64 * VC++ 2010 and above, which have expanded <errno.h> with more values), but
65 * most missing constants are provided by win32/include/sys/errno2.h. The few
66 * that are not are returned unchanged.
68 * The list of possible WSAExxx values used here comes from the MSDN page
69 * titled "Windows Sockets Error Codes".
71 * (Note: Only the WSAExxx values are handled here; other WSAxxx values are
72 * returned unchanged. The return value normally ends up in errno/$! and at
73 * the Perl code level may be tested against the Exxx constants exported by
74 * the Errno and POSIX modules, which have never handled the other WSAxxx
75 * values themselves, apparently without any ill effect so far.)
78 convert_wsa_error_to_errno(int wsaerr)
101 case WSAEDESTADDRREQ:
109 case WSAEPROTONOSUPPORT:
110 return EPROTONOSUPPORT;
111 case WSAESOCKTNOSUPPORT:
112 return ESOCKTNOSUPPORT;
115 case WSAEPFNOSUPPORT:
117 case WSAEAFNOSUPPORT:
121 case WSAEADDRNOTAVAIL:
122 return EADDRNOTAVAIL;
129 case WSAECONNABORTED:
141 case WSAETOOMANYREFS:
145 case WSAECONNREFUSED:
149 case WSAENAMETOOLONG:
152 return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */
153 case WSAEHOSTUNREACH:
168 return WSAEDISCON; /* EDISCON is not defined */
170 return WSAENOMORE; /* ENOMORE is not defined */
172 case WSAECANCELLED: /* New in WinSock2 */
175 case WSAEINVALIDPROCTABLE:
176 return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */
177 case WSAEINVALIDPROVIDER:
178 return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */
179 case WSAEPROVIDERFAILEDINIT:
180 return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */
182 return WSAEREFUSED; /* EREFUSED is not defined */
188 #ifdef ERRNO_HAS_POSIX_SUPPLEMENT
189 /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
190 * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
191 * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
192 * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
193 * really expect to encounter them anyway in the context in which this function
195 * Some versions of MinGW/gcc-4.8 and above also define most, but not all, of
196 * these extra Exxx values. The missing ones are all cases for which there is no
197 * corresponding WSAExxx constant anyway, so we simply omit the cases for them
199 * Other Exxx values (err < sys_nerr) are returned unchanged.
202 convert_errno_to_wsa_error(int err)
206 return WSAEADDRINUSE;
208 return WSAEADDRNOTAVAIL;
210 return WSAEAFNOSUPPORT;
214 case EBADMSG: /* Not defined in gcc-4.8.0 */
215 return ERROR_INVALID_FUNCTION;
219 return WSAECANCELLED; /* New in WinSock2 */
221 return ERROR_INVALID_FUNCTION;
224 return WSAECONNABORTED;
226 return WSAECONNREFUSED;
228 return WSAECONNRESET;
230 return WSAEDESTADDRREQ;
232 return WSAEHOSTUNREACH;
234 case EIDRM: /* Not defined in gcc-4.8.0 */
235 return ERROR_INVALID_FUNCTION;
238 return WSAEINPROGRESS;
250 return WSAENETUNREACH;
254 case ENODATA: /* Not defined in gcc-4.8.0 */
255 return ERROR_INVALID_FUNCTION;
258 case ENOLINK: /* Not defined in gcc-4.8.0 */
259 return ERROR_INVALID_FUNCTION;
262 case ENOMSG: /* Not defined in gcc-4.8.0 */
263 return ERROR_INVALID_FUNCTION;
266 return WSAENOPROTOOPT;
268 case ENOSR: /* Not defined in gcc-4.8.0 */
269 return ERROR_INVALID_FUNCTION;
272 case ENOSTR: /* Not defined in gcc-4.8.0 */
273 return ERROR_INVALID_FUNCTION;
277 #ifdef ENOTRECOVERABLE
278 case ENOTRECOVERABLE: /* Not defined in gcc-4.8.0 */
279 return ERROR_INVALID_FUNCTION;
284 return ERROR_INVALID_FUNCTION;
286 return WSAEOPNOTSUPP;
288 case EOTHER: /* Not defined in gcc-4.8.0 */
289 return ERROR_INVALID_FUNCTION;
292 return ERROR_INVALID_FUNCTION;
294 return ERROR_INVALID_FUNCTION;
296 return ERROR_INVALID_FUNCTION;
297 case EPROTONOSUPPORT:
298 return WSAEPROTONOSUPPORT;
300 return WSAEPROTOTYPE;
302 case ETIME: /* Not defined in gcc-4.8.0 */
303 return ERROR_INVALID_FUNCTION;
308 case ETXTBSY: /* Not defined in gcc-4.8.0 */
309 return ERROR_INVALID_FUNCTION;
312 return WSAEWOULDBLOCK;
317 #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
320 win32_htonl(u_long hostlong)
322 return htonl(hostlong);
326 win32_htons(u_short hostshort)
328 return htons(hostshort);
332 win32_ntohl(u_long netlong)
334 return ntohl(netlong);
338 win32_ntohs(u_short netshort)
340 return ntohs(netshort);
346 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
350 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
351 return OPEN_SOCKET(r);
355 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
359 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
364 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
368 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
374 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
378 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
383 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
387 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
392 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
396 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
401 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
405 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
410 win32_listen(SOCKET s, int backlog)
414 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
419 win32_recv(SOCKET s, char *buf, int len, int flags)
423 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
428 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
431 int frombufsize = *fromlen;
433 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
434 /* Winsock's recvfrom() only returns a valid 'from' when the socket
435 * is connectionless. Perl expects a valid 'from' for all types
436 * of sockets, so go the extra mile.
438 if (r != SOCKET_ERROR && frombufsize == *fromlen)
439 (void)win32_getpeername(s, from, fromlen);
445 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
448 int i, fd, save_errno = errno;
449 FD_SET nrd, nwr, nex;
450 bool just_sleep = TRUE;
455 for (i = 0; i < nfds; i++) {
456 if (rd && PERL_FD_ISSET(i,rd)) {
458 FD_SET((unsigned)fd, &nrd);
461 if (wr && PERL_FD_ISSET(i,wr)) {
463 FD_SET((unsigned)fd, &nwr);
466 if (ex && PERL_FD_ISSET(i,ex)) {
468 FD_SET((unsigned)fd, &nex);
473 /* winsock seems incapable of dealing with all three fd_sets being empty,
474 * so do the (millisecond) sleep as a special case
478 Sleep(timeout->tv_sec * 1000 +
479 timeout->tv_usec / 1000); /* do the best we can */
486 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout));
489 for (i = 0; i < nfds; i++) {
490 if (rd && PERL_FD_ISSET(i,rd)) {
492 if (!FD_ISSET(fd, &nrd))
495 if (wr && PERL_FD_ISSET(i,wr)) {
497 if (!FD_ISSET(fd, &nwr))
500 if (ex && PERL_FD_ISSET(i,ex)) {
502 if (!FD_ISSET(fd, &nex))
511 win32_send(SOCKET s, const char *buf, int len, int flags)
515 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
520 win32_sendto(SOCKET s, const char *buf, int len, int flags,
521 const struct sockaddr *to, int tolen)
525 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
530 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
534 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
539 win32_shutdown(SOCKET s, int how)
543 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
548 win32_closesocket(SOCKET s)
552 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
557 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
559 Copy(in, out, 1, WSAPROTOCOL_INFOA);
560 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
564 open_ifs_socket(int af, int type, int protocol)
568 unsigned long proto_buffers_len = 0;
569 int error_code, found = 0;
570 SOCKET out = INVALID_SOCKET;
572 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
573 return WSASocket(af, type, protocol, NULL, 0, 0);
575 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
576 && error_code == WSAENOBUFS)
578 WSAPROTOCOL_INFOW *proto_buffers;
579 int protocols_available = 0;
581 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
584 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
585 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
588 for (i = 0; i < protocols_available; i++)
590 WSAPROTOCOL_INFOA proto_info;
592 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
593 || (type != proto_buffers[i].iSocketType)
594 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
595 protocol != proto_buffers[i].iProtocol))
598 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
602 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
604 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
609 WSASetLastError(WSAEPROTONOSUPPORT);
612 Safefree(proto_buffers);
619 win32_socket(int af, int type, int protocol)
623 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
625 int wsaerr = WSAGetLastError();
626 errno = convert_wsa_error_to_errno(wsaerr);
627 SetLastError(wsaerr);
636 win32_gethostbyaddr(const char *addr, int len, int type)
640 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
645 win32_gethostbyname(const char *name)
649 SOCKET_TEST(r = gethostbyname(name), NULL);
654 win32_gethostname(char *name, int len)
658 SOCKET_TEST_ERROR(r = gethostname(name, len));
663 win32_getprotobyname(const char *name)
667 SOCKET_TEST(r = getprotobyname(name), NULL);
672 win32_getprotobynumber(int num)
676 SOCKET_TEST(r = getprotobynumber(num), NULL);
681 win32_getservbyname(const char *name, const char *proto)
686 SOCKET_TEST(r = getservbyname(name, proto), NULL);
689 r = win32_savecopyservent(&w32_servent, r, proto);
695 win32_getservbyport(int port, const char *proto)
700 SOCKET_TEST(r = getservbyport(port, proto), NULL);
703 r = win32_savecopyservent(&w32_servent, r, proto);
709 win32_ioctl(int i, unsigned int u, char *data)
714 /* mauke says using memcpy avoids alignment issues */
715 memcpy(&u_long_arg, data, sizeof u_long_arg);
716 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
717 memcpy(data, &u_long_arg, sizeof u_long_arg);
719 if (retval == SOCKET_ERROR) {
720 int wsaerr = WSAGetLastError();
721 int err = convert_wsa_error_to_errno(wsaerr);
722 if (err == ENOTSOCK) {
723 Perl_croak_nocontext("ioctl implemented only on sockets");
727 SetLastError(wsaerr);
733 win32_inet_ntoa(struct in_addr in)
735 return inet_ntoa(in);
739 win32_inet_addr(const char FAR *cp)
741 return inet_addr(cp);
751 win32_croak_not_implemented("endhostent");
757 win32_croak_not_implemented("endnetent");
763 win32_croak_not_implemented("endprotoent");
769 win32_croak_not_implemented("endservent");
774 win32_getnetent(void)
776 win32_croak_not_implemented("getnetent");
777 return (struct netent *) NULL;
781 win32_getnetbyname(char *name)
783 win32_croak_not_implemented("getnetbyname");
784 return (struct netent *)NULL;
788 win32_getnetbyaddr(long net, int type)
790 win32_croak_not_implemented("getnetbyaddr");
791 return (struct netent *)NULL;
795 win32_getprotoent(void)
797 win32_croak_not_implemented("getprotoent");
798 return (struct protoent *) NULL;
802 win32_getservent(void)
804 win32_croak_not_implemented("getservent");
805 return (struct servent *) NULL;
809 win32_sethostent(int stayopen)
811 win32_croak_not_implemented("sethostent");
816 win32_setnetent(int stayopen)
818 win32_croak_not_implemented("setnetent");
823 win32_setprotoent(int stayopen)
825 win32_croak_not_implemented("setprotoent");
830 win32_setservent(int stayopen)
832 win32_croak_not_implemented("setservent");
835 static char tcp_proto[] = "tcp";
837 static struct servent*
838 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
840 d->s_name = s->s_name;
841 d->s_aliases = s->s_aliases;
842 d->s_port = s->s_port;
843 if (s->s_proto && strlen(s->s_proto))
844 d->s_proto = s->s_proto;
846 if (proto && strlen(proto))
847 d->s_proto = (char *)proto;
849 d->s_proto = tcp_proto;