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 = WSAGetLastError(); \
48 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
50 static struct servent* win32_savecopyservent(struct servent*d,
54 static int wsock_started = 0;
66 unsigned short version;
71 * initalize the winsock interface and insure that it is
75 if(ret = WSAStartup(version, &retdata))
76 Perl_croak_nocontext("Unable to locate winsock library!\n");
77 if(retdata.wVersion != version)
78 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
80 /* atexit((void (*)(void)) EndSockets); */
84 /* in no sockets Win32 builds, these use the inline functions defined in
88 win32_htonl(u_long hostlong)
90 #ifndef WIN32_NO_SOCKETS
93 return htonl(hostlong);
97 win32_htons(u_short hostshort)
99 #ifndef WIN32_NO_SOCKETS
102 return htons(hostshort);
106 win32_ntohl(u_long netlong)
108 #ifndef WIN32_NO_SOCKETS
111 return ntohl(netlong);
115 win32_ntohs(u_short netshort)
117 #ifndef WIN32_NO_SOCKETS
120 return ntohs(netshort);
126 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
130 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
131 return OPEN_SOCKET(r);
135 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
139 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
144 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
148 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
154 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
158 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
163 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
167 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
172 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
176 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
181 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
185 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
190 win32_listen(SOCKET s, int backlog)
194 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
199 win32_recv(SOCKET s, char *buf, int len, int flags)
203 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
208 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
211 int frombufsize = *fromlen;
213 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
214 /* Winsock's recvfrom() only returns a valid 'from' when the socket
215 * is connectionless. Perl expects a valid 'from' for all types
216 * of sockets, so go the extra mile.
218 if (r != SOCKET_ERROR && frombufsize == *fromlen)
219 (void)win32_getpeername(s, from, fromlen);
223 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
225 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
228 int i, fd, save_errno = errno;
229 FD_SET nrd, nwr, nex;
230 bool just_sleep = TRUE;
237 for (i = 0; i < nfds; i++) {
238 if (rd && PERL_FD_ISSET(i,rd)) {
240 FD_SET((unsigned)fd, &nrd);
243 if (wr && PERL_FD_ISSET(i,wr)) {
245 FD_SET((unsigned)fd, &nwr);
248 if (ex && PERL_FD_ISSET(i,ex)) {
250 FD_SET((unsigned)fd, &nex);
255 /* winsock seems incapable of dealing with all three fd_sets being empty,
256 * so do the (millisecond) sleep as a special case
260 Sleep(timeout->tv_sec * 1000 +
261 timeout->tv_usec / 1000); /* do the best we can */
268 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
271 for (i = 0; i < nfds; i++) {
272 if (rd && PERL_FD_ISSET(i,rd)) {
274 if (!FD_ISSET(fd, &nrd))
277 if (wr && PERL_FD_ISSET(i,wr)) {
279 if (!FD_ISSET(fd, &nwr))
282 if (ex && PERL_FD_ISSET(i,ex)) {
284 if (!FD_ISSET(fd, &nex))
293 win32_send(SOCKET s, const char *buf, int len, int flags)
297 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
302 win32_sendto(SOCKET s, const char *buf, int len, int flags,
303 const struct sockaddr *to, int tolen)
307 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
312 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
316 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
321 win32_shutdown(SOCKET s, int how)
325 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
330 win32_closesocket(SOCKET s)
334 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
339 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
341 Copy(in, out, 1, WSAPROTOCOL_INFOA);
342 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
346 open_ifs_socket(int af, int type, int protocol)
350 unsigned long proto_buffers_len = 0;
352 SOCKET out = INVALID_SOCKET;
354 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
355 return WSASocket(af, type, protocol, NULL, 0, 0);
357 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
358 && error_code == WSAENOBUFS)
360 WSAPROTOCOL_INFOW *proto_buffers;
361 int protocols_available = 0;
363 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
366 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
367 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
370 for (i = 0; i < protocols_available; i++)
372 WSAPROTOCOL_INFOA proto_info;
374 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
375 || (type != proto_buffers[i].iSocketType)
376 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
377 protocol != proto_buffers[i].iProtocol))
380 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
383 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
385 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
390 Safefree(proto_buffers);
397 win32_socket(int af, int type, int protocol)
403 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
404 errno = WSAGetLastError();
412 * close RTL fd while respecting sockets
413 * added as temporary measure until PerlIO has real
421 if (!wsock_started) /* No WinSock? */
422 return(close(fd)); /* Then not a socket. */
423 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
426 err = closesocket(osf);
428 (void)close(fd); /* handle already closed, ignore error */
431 else if (err == SOCKET_ERROR) {
432 err = WSAGetLastError();
433 if (err != WSAENOTSOCK) {
448 if (!wsock_started) /* No WinSock? */
449 return(fclose(pf)); /* Then not a socket. */
450 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
454 err = closesocket(osf);
456 (void)fclose(pf); /* handle already closed, ignore error */
459 else if (err == SOCKET_ERROR) {
460 err = WSAGetLastError();
461 if (err != WSAENOTSOCK) {
472 win32_gethostbyaddr(const char *addr, int len, int type)
476 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
481 win32_gethostbyname(const char *name)
485 SOCKET_TEST(r = gethostbyname(name), NULL);
490 win32_gethostname(char *name, int len)
494 SOCKET_TEST_ERROR(r = gethostname(name, len));
499 win32_getprotobyname(const char *name)
503 SOCKET_TEST(r = getprotobyname(name), NULL);
508 win32_getprotobynumber(int num)
512 SOCKET_TEST(r = getprotobynumber(num), NULL);
517 win32_getservbyname(const char *name, const char *proto)
522 SOCKET_TEST(r = getservbyname(name, proto), NULL);
525 r = win32_savecopyservent(&w32_servent, r, proto);
531 win32_getservbyport(int port, const char *proto)
536 SOCKET_TEST(r = getservbyport(port, proto), NULL);
539 r = win32_savecopyservent(&w32_servent, r, proto);
545 win32_ioctl(int i, unsigned int u, char *data)
550 if (!wsock_started) {
551 Perl_croak_nocontext("ioctl implemented only on sockets");
555 /* mauke says using memcpy avoids alignment issues */
556 memcpy(&u_long_arg, data, sizeof u_long_arg);
557 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
558 memcpy(data, &u_long_arg, sizeof u_long_arg);
560 if (retval == SOCKET_ERROR) {
561 if (WSAGetLastError() == WSAENOTSOCK) {
562 Perl_croak_nocontext("ioctl implemented only on sockets");
565 errno = WSAGetLastError();
571 win32_inet_ntoa(struct in_addr in)
574 return inet_ntoa(in);
578 win32_inet_addr(const char FAR *cp)
581 return inet_addr(cp);
591 win32_croak_not_implemented("endhostent");
597 win32_croak_not_implemented("endnetent");
603 win32_croak_not_implemented("endprotoent");
609 win32_croak_not_implemented("endservent");
614 win32_getnetent(void)
616 win32_croak_not_implemented("getnetent");
617 return (struct netent *) NULL;
621 win32_getnetbyname(char *name)
623 win32_croak_not_implemented("getnetbyname");
624 return (struct netent *)NULL;
628 win32_getnetbyaddr(long net, int type)
630 win32_croak_not_implemented("getnetbyaddr");
631 return (struct netent *)NULL;
635 win32_getprotoent(void)
637 win32_croak_not_implemented("getprotoent");
638 return (struct protoent *) NULL;
642 win32_getservent(void)
644 win32_croak_not_implemented("getservent");
645 return (struct servent *) NULL;
649 win32_sethostent(int stayopen)
651 win32_croak_not_implemented("sethostent");
656 win32_setnetent(int stayopen)
658 win32_croak_not_implemented("setnetent");
663 win32_setprotoent(int stayopen)
665 win32_croak_not_implemented("setprotoent");
670 win32_setservent(int stayopen)
672 win32_croak_not_implemented("setservent");
675 static struct servent*
676 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
678 d->s_name = s->s_name;
679 d->s_aliases = s->s_aliases;
680 d->s_port = s->s_port;
681 if (s->s_proto && strlen(s->s_proto))
682 d->s_proto = s->s_proto;
684 if (proto && strlen(proto))
685 d->s_proto = (char *)proto;