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;
57 #ifdef WIN32_DYN_IOINFO_SIZE
58 EXTERN_C Size_t w32_ioinfo_size;
68 /* Translate WSAExxx values to corresponding Exxx values where possible. Not all
69 * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in
70 * VC++ 2010 and above, which have expanded <errno.h> with more values), but
71 * most missing constants are provided by win32/include/sys/errno2.h. The few
72 * that are not are returned unchanged.
74 * The list of possible WSAExxx values used here comes from the MSDN page
75 * titled "Windows Sockets Error Codes".
77 * (Note: Only the WSAExxx values are handled here; other WSAxxx values are
78 * returned unchanged. The return value normally ends up in errno/$! and at
79 * the Perl code level may be tested against the Exxx constants exported by
80 * the Errno and POSIX modules, which have never handled the other WSAxxx
81 * values themselves, apparently without any ill effect so far.)
84 convert_wsa_error_to_errno(int wsaerr)
107 case WSAEDESTADDRREQ:
115 case WSAEPROTONOSUPPORT:
116 return EPROTONOSUPPORT;
117 case WSAESOCKTNOSUPPORT:
118 return ESOCKTNOSUPPORT;
121 case WSAEPFNOSUPPORT:
123 case WSAEAFNOSUPPORT:
127 case WSAEADDRNOTAVAIL:
128 return EADDRNOTAVAIL;
135 case WSAECONNABORTED:
147 case WSAETOOMANYREFS:
151 case WSAECONNREFUSED:
155 case WSAENAMETOOLONG:
158 return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */
159 case WSAEHOSTUNREACH:
174 return WSAEDISCON; /* EDISCON is not defined */
176 return WSAENOMORE; /* ENOMORE is not defined */
178 case WSAECANCELLED: /* New in WinSock2 */
181 case WSAEINVALIDPROCTABLE:
182 return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */
183 case WSAEINVALIDPROVIDER:
184 return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */
185 case WSAEPROVIDERFAILEDINIT:
186 return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */
188 return WSAEREFUSED; /* EREFUSED is not defined */
194 #ifdef ERRNO_HAS_POSIX_SUPPLEMENT
195 /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and
196 * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not
197 * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>;
198 * we just use ERROR_INVALID_FUNCTION for those that are missing but do not
199 * really expect to encounter them anyway in the context in which this function
201 * Some versions of MinGW/gcc-4.8 and above also define most, but not all, of
202 * these extra Exxx values. The missing ones are all cases for which there is no
203 * corresponding WSAExxx constant anyway, so we simply omit the cases for them
205 * Other Exxx values (err < sys_nerr) are returned unchanged.
208 convert_errno_to_wsa_error(int err)
212 return WSAEADDRINUSE;
214 return WSAEADDRNOTAVAIL;
216 return WSAEAFNOSUPPORT;
220 case EBADMSG: /* Not defined in gcc-4.8.0 */
221 return ERROR_INVALID_FUNCTION;
225 return WSAECANCELLED; /* New in WinSock2 */
227 return ERROR_INVALID_FUNCTION;
230 return WSAECONNABORTED;
232 return WSAECONNREFUSED;
234 return WSAECONNRESET;
236 return WSAEDESTADDRREQ;
238 return WSAEHOSTUNREACH;
240 case EIDRM: /* Not defined in gcc-4.8.0 */
241 return ERROR_INVALID_FUNCTION;
244 return WSAEINPROGRESS;
256 return WSAENETUNREACH;
260 case ENODATA: /* Not defined in gcc-4.8.0 */
261 return ERROR_INVALID_FUNCTION;
264 case ENOLINK: /* Not defined in gcc-4.8.0 */
265 return ERROR_INVALID_FUNCTION;
268 case ENOMSG: /* Not defined in gcc-4.8.0 */
269 return ERROR_INVALID_FUNCTION;
272 return WSAENOPROTOOPT;
274 case ENOSR: /* Not defined in gcc-4.8.0 */
275 return ERROR_INVALID_FUNCTION;
278 case ENOSTR: /* Not defined in gcc-4.8.0 */
279 return ERROR_INVALID_FUNCTION;
283 #ifdef ENOTRECOVERABLE
284 case ENOTRECOVERABLE: /* Not defined in gcc-4.8.0 */
285 return ERROR_INVALID_FUNCTION;
290 return ERROR_INVALID_FUNCTION;
292 return WSAEOPNOTSUPP;
294 case EOTHER: /* Not defined in gcc-4.8.0 */
295 return ERROR_INVALID_FUNCTION;
298 return ERROR_INVALID_FUNCTION;
300 return ERROR_INVALID_FUNCTION;
302 return ERROR_INVALID_FUNCTION;
303 case EPROTONOSUPPORT:
304 return WSAEPROTONOSUPPORT;
306 return WSAEPROTOTYPE;
308 case ETIME: /* Not defined in gcc-4.8.0 */
309 return ERROR_INVALID_FUNCTION;
314 case ETXTBSY: /* Not defined in gcc-4.8.0 */
315 return ERROR_INVALID_FUNCTION;
318 return WSAEWOULDBLOCK;
323 #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */
326 get_last_socket_error(void)
328 return convert_wsa_error_to_errno(WSAGetLastError());
334 unsigned short version;
339 * initalize the winsock interface and insure that it is
340 * cleaned up at exit.
343 if(ret = WSAStartup(version, &retdata))
344 Perl_croak_nocontext("Unable to locate winsock library!\n");
345 if(retdata.wVersion != version)
346 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
348 /* atexit((void (*)(void)) EndSockets); */
352 /* in no sockets Win32 builds, these use the inline functions defined in
356 win32_htonl(u_long hostlong)
358 #ifndef WIN32_NO_SOCKETS
361 return htonl(hostlong);
365 win32_htons(u_short hostshort)
367 #ifndef WIN32_NO_SOCKETS
370 return htons(hostshort);
374 win32_ntohl(u_long netlong)
376 #ifndef WIN32_NO_SOCKETS
379 return ntohl(netlong);
383 win32_ntohs(u_short netshort)
385 #ifndef WIN32_NO_SOCKETS
388 return ntohs(netshort);
394 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
398 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
399 return OPEN_SOCKET(r);
403 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
407 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
412 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
416 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
422 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
426 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
431 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
435 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
440 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
444 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
449 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
453 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
458 win32_listen(SOCKET s, int backlog)
462 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
467 win32_recv(SOCKET s, char *buf, int len, int flags)
471 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
476 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
479 int frombufsize = *fromlen;
481 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
482 /* Winsock's recvfrom() only returns a valid 'from' when the socket
483 * is connectionless. Perl expects a valid 'from' for all types
484 * of sockets, so go the extra mile.
486 if (r != SOCKET_ERROR && frombufsize == *fromlen)
487 (void)win32_getpeername(s, from, fromlen);
491 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
493 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
496 int i, fd, save_errno = errno;
497 FD_SET nrd, nwr, nex;
498 bool just_sleep = TRUE;
505 for (i = 0; i < nfds; i++) {
506 if (rd && PERL_FD_ISSET(i,rd)) {
508 FD_SET((unsigned)fd, &nrd);
511 if (wr && PERL_FD_ISSET(i,wr)) {
513 FD_SET((unsigned)fd, &nwr);
516 if (ex && PERL_FD_ISSET(i,ex)) {
518 FD_SET((unsigned)fd, &nex);
523 /* winsock seems incapable of dealing with all three fd_sets being empty,
524 * so do the (millisecond) sleep as a special case
528 Sleep(timeout->tv_sec * 1000 +
529 timeout->tv_usec / 1000); /* do the best we can */
536 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout));
539 for (i = 0; i < nfds; i++) {
540 if (rd && PERL_FD_ISSET(i,rd)) {
542 if (!FD_ISSET(fd, &nrd))
545 if (wr && PERL_FD_ISSET(i,wr)) {
547 if (!FD_ISSET(fd, &nwr))
550 if (ex && PERL_FD_ISSET(i,ex)) {
552 if (!FD_ISSET(fd, &nex))
561 win32_send(SOCKET s, const char *buf, int len, int flags)
565 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
570 win32_sendto(SOCKET s, const char *buf, int len, int flags,
571 const struct sockaddr *to, int tolen)
575 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
580 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
584 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
589 win32_shutdown(SOCKET s, int how)
593 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
598 win32_closesocket(SOCKET s)
602 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
607 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
609 Copy(in, out, 1, WSAPROTOCOL_INFOA);
610 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
614 open_ifs_socket(int af, int type, int protocol)
618 unsigned long proto_buffers_len = 0;
620 SOCKET out = INVALID_SOCKET;
622 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
623 return WSASocket(af, type, protocol, NULL, 0, 0);
625 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
626 && error_code == WSAENOBUFS)
628 WSAPROTOCOL_INFOW *proto_buffers;
629 int protocols_available = 0;
631 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
634 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
635 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
638 for (i = 0; i < protocols_available; i++)
640 WSAPROTOCOL_INFOA proto_info;
642 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
643 || (type != proto_buffers[i].iSocketType)
644 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
645 protocol != proto_buffers[i].iProtocol))
648 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
651 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
653 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
658 Safefree(proto_buffers);
665 win32_socket(int af, int type, int protocol)
671 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
672 errno = get_last_socket_error();
680 * close RTL fd while respecting sockets
681 * added as temporary measure until PerlIO has real
689 if (!wsock_started) /* No WinSock? */
690 return(close(fd)); /* Then not a socket. */
691 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
694 err = closesocket(osf);
696 assert(_osfhnd(fd) == osf); /* catch a bad ioinfo struct def */
697 /* don't close freed handle */
698 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
701 else if (err == SOCKET_ERROR) {
702 err = get_last_socket_error();
703 if (err != ENOTSOCK) {
718 if (!wsock_started) /* No WinSock? */
719 return(fclose(pf)); /* Then not a socket. */
720 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
724 err = closesocket(osf);
726 assert(_osfhnd(win32_fileno(pf)) == osf); /* catch a bad ioinfo struct def */
727 /* don't close freed handle */
728 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
731 else if (err == SOCKET_ERROR) {
732 err = get_last_socket_error();
733 if (err != ENOTSOCK) {
744 win32_gethostbyaddr(const char *addr, int len, int type)
748 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
753 win32_gethostbyname(const char *name)
757 SOCKET_TEST(r = gethostbyname(name), NULL);
762 win32_gethostname(char *name, int len)
766 SOCKET_TEST_ERROR(r = gethostname(name, len));
771 win32_getprotobyname(const char *name)
775 SOCKET_TEST(r = getprotobyname(name), NULL);
780 win32_getprotobynumber(int num)
784 SOCKET_TEST(r = getprotobynumber(num), NULL);
789 win32_getservbyname(const char *name, const char *proto)
794 SOCKET_TEST(r = getservbyname(name, proto), NULL);
797 r = win32_savecopyservent(&w32_servent, r, proto);
803 win32_getservbyport(int port, const char *proto)
808 SOCKET_TEST(r = getservbyport(port, proto), NULL);
811 r = win32_savecopyservent(&w32_servent, r, proto);
817 win32_ioctl(int i, unsigned int u, char *data)
822 if (!wsock_started) {
823 Perl_croak_nocontext("ioctl implemented only on sockets");
827 /* mauke says using memcpy avoids alignment issues */
828 memcpy(&u_long_arg, data, sizeof u_long_arg);
829 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
830 memcpy(data, &u_long_arg, sizeof u_long_arg);
832 if (retval == SOCKET_ERROR) {
833 int err = get_last_socket_error();
834 if (err == ENOTSOCK) {
835 Perl_croak_nocontext("ioctl implemented only on sockets");
844 win32_inet_ntoa(struct in_addr in)
847 return inet_ntoa(in);
851 win32_inet_addr(const char FAR *cp)
854 return inet_addr(cp);
864 win32_croak_not_implemented("endhostent");
870 win32_croak_not_implemented("endnetent");
876 win32_croak_not_implemented("endprotoent");
882 win32_croak_not_implemented("endservent");
887 win32_getnetent(void)
889 win32_croak_not_implemented("getnetent");
890 return (struct netent *) NULL;
894 win32_getnetbyname(char *name)
896 win32_croak_not_implemented("getnetbyname");
897 return (struct netent *)NULL;
901 win32_getnetbyaddr(long net, int type)
903 win32_croak_not_implemented("getnetbyaddr");
904 return (struct netent *)NULL;
908 win32_getprotoent(void)
910 win32_croak_not_implemented("getprotoent");
911 return (struct protoent *) NULL;
915 win32_getservent(void)
917 win32_croak_not_implemented("getservent");
918 return (struct servent *) NULL;
922 win32_sethostent(int stayopen)
924 win32_croak_not_implemented("sethostent");
929 win32_setnetent(int stayopen)
931 win32_croak_not_implemented("setnetent");
936 win32_setprotoent(int stayopen)
938 win32_croak_not_implemented("setprotoent");
943 win32_setservent(int stayopen)
945 win32_croak_not_implemented("setservent");
948 static struct servent*
949 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
951 d->s_name = s->s_name;
952 d->s_aliases = s->s_aliases;
953 d->s_port = s->s_port;
954 if (s->s_proto && strlen(s->s_proto))
955 d->s_proto = s->s_proto;
957 if (proto && strlen(proto))
958 d->s_proto = (char *)proto;