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;
67 unsigned short version;
72 * initalize the winsock interface and insure that it is
76 if(ret = WSAStartup(version, &retdata))
77 Perl_croak_nocontext("Unable to locate winsock library!\n");
78 if(retdata.wVersion != version)
79 Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
81 /* atexit((void (*)(void)) EndSockets); */
86 win32_htonl(u_long hostlong)
89 return htonl(hostlong);
93 win32_htons(u_short hostshort)
96 return htons(hostshort);
100 win32_ntohl(u_long netlong)
103 return ntohl(netlong);
107 win32_ntohs(u_short netshort)
110 return ntohs(netshort);
116 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
120 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
121 return OPEN_SOCKET(r);
125 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
129 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
134 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
138 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
144 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
148 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
153 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
157 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
162 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
166 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
171 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
175 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
180 win32_listen(SOCKET s, int backlog)
184 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
189 win32_recv(SOCKET s, char *buf, int len, int flags)
193 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
198 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
201 int frombufsize = *fromlen;
203 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
204 /* Winsock's recvfrom() only returns a valid 'from' when the socket
205 * is connectionless. Perl expects a valid 'from' for all types
206 * of sockets, so go the extra mile.
208 if (r != SOCKET_ERROR && frombufsize == *fromlen)
209 (void)win32_getpeername(s, from, fromlen);
213 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
215 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
218 int i, fd, save_errno = errno;
219 FD_SET nrd, nwr, nex;
220 bool just_sleep = TRUE;
227 for (i = 0; i < nfds; i++) {
228 if (rd && PERL_FD_ISSET(i,rd)) {
230 FD_SET((unsigned)fd, &nrd);
233 if (wr && PERL_FD_ISSET(i,wr)) {
235 FD_SET((unsigned)fd, &nwr);
238 if (ex && PERL_FD_ISSET(i,ex)) {
240 FD_SET((unsigned)fd, &nex);
245 /* winsock seems incapable of dealing with all three fd_sets being empty,
246 * so do the (millisecond) sleep as a special case
250 Sleep(timeout->tv_sec * 1000 +
251 timeout->tv_usec / 1000); /* do the best we can */
258 SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
261 for (i = 0; i < nfds; i++) {
262 if (rd && PERL_FD_ISSET(i,rd)) {
264 if (!FD_ISSET(fd, &nrd))
267 if (wr && PERL_FD_ISSET(i,wr)) {
269 if (!FD_ISSET(fd, &nwr))
272 if (ex && PERL_FD_ISSET(i,ex)) {
274 if (!FD_ISSET(fd, &nex))
283 win32_send(SOCKET s, const char *buf, int len, int flags)
287 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
292 win32_sendto(SOCKET s, const char *buf, int len, int flags,
293 const struct sockaddr *to, int tolen)
297 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
302 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
306 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
311 win32_shutdown(SOCKET s, int how)
315 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
320 win32_closesocket(SOCKET s)
324 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
329 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
331 Copy(in, out, 1, WSAPROTOCOL_INFOA);
332 wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol));
336 open_ifs_socket(int af, int type, int protocol)
340 unsigned long proto_buffers_len = 0;
342 SOCKET out = INVALID_SOCKET;
344 if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
345 return WSASocket(af, type, protocol, NULL, 0, 0);
347 if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
348 && error_code == WSAENOBUFS)
350 WSAPROTOCOL_INFOW *proto_buffers;
351 int protocols_available = 0;
353 Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
356 if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers,
357 &proto_buffers_len, &error_code)) != SOCKET_ERROR)
360 for (i = 0; i < protocols_available; i++)
362 WSAPROTOCOL_INFOA proto_info;
364 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
365 || (type != proto_buffers[i].iSocketType)
366 || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
367 protocol != proto_buffers[i].iProtocol))
370 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
373 convert_proto_info_w2a(&(proto_buffers[i]), &proto_info);
375 out = WSASocket(af, type, protocol, &proto_info, 0, 0);
380 Safefree(proto_buffers);
387 win32_socket(int af, int type, int protocol)
393 if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
394 errno = WSAGetLastError();
402 * close RTL fd while respecting sockets
403 * added as temporary measure until PerlIO has real
411 if (!wsock_started) /* No WinSock? */
412 return(close(fd)); /* Then not a socket. */
413 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
416 err = closesocket(osf);
418 (void)close(fd); /* handle already closed, ignore error */
421 else if (err == SOCKET_ERROR) {
422 err = WSAGetLastError();
423 if (err != WSAENOTSOCK) {
438 if (!wsock_started) /* No WinSock? */
439 return(fclose(pf)); /* Then not a socket. */
440 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
444 err = closesocket(osf);
446 (void)fclose(pf); /* handle already closed, ignore error */
449 else if (err == SOCKET_ERROR) {
450 err = WSAGetLastError();
451 if (err != WSAENOTSOCK) {
462 win32_gethostbyaddr(const char *addr, int len, int type)
466 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
471 win32_gethostbyname(const char *name)
475 SOCKET_TEST(r = gethostbyname(name), NULL);
480 win32_gethostname(char *name, int len)
484 SOCKET_TEST_ERROR(r = gethostname(name, len));
489 win32_getprotobyname(const char *name)
493 SOCKET_TEST(r = getprotobyname(name), NULL);
498 win32_getprotobynumber(int num)
502 SOCKET_TEST(r = getprotobynumber(num), NULL);
507 win32_getservbyname(const char *name, const char *proto)
512 SOCKET_TEST(r = getservbyname(name, proto), NULL);
514 r = win32_savecopyservent(&w32_servent, r, proto);
520 win32_getservbyport(int port, const char *proto)
525 SOCKET_TEST(r = getservbyport(port, proto), NULL);
527 r = win32_savecopyservent(&w32_servent, r, proto);
533 win32_ioctl(int i, unsigned int u, char *data)
539 if (!wsock_started) {
540 Perl_croak_nocontext("ioctl implemented only on sockets");
544 /* mauke says using memcpy avoids alignment issues */
545 memcpy(&u_long_arg, data, sizeof u_long_arg);
546 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
547 memcpy(data, &u_long_arg, sizeof u_long_arg);
549 if (retval == SOCKET_ERROR) {
550 if (WSAGetLastError() == WSAENOTSOCK) {
551 Perl_croak_nocontext("ioctl implemented only on sockets");
554 errno = WSAGetLastError();
560 win32_inet_ntoa(struct in_addr in)
563 return inet_ntoa(in);
567 win32_inet_addr(const char FAR *cp)
570 return inet_addr(cp);
581 Perl_croak_nocontext("endhostent not implemented!\n");
588 Perl_croak_nocontext("endnetent not implemented!\n");
595 Perl_croak_nocontext("endprotoent not implemented!\n");
602 Perl_croak_nocontext("endservent not implemented!\n");
607 win32_getnetent(void)
610 Perl_croak_nocontext("getnetent not implemented!\n");
611 return (struct netent *) NULL;
615 win32_getnetbyname(char *name)
618 Perl_croak_nocontext("getnetbyname not implemented!\n");
619 return (struct netent *)NULL;
623 win32_getnetbyaddr(long net, int type)
626 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
627 return (struct netent *)NULL;
631 win32_getprotoent(void)
634 Perl_croak_nocontext("getprotoent not implemented!\n");
635 return (struct protoent *) NULL;
639 win32_getservent(void)
642 Perl_croak_nocontext("getservent not implemented!\n");
643 return (struct servent *) NULL;
647 win32_sethostent(int stayopen)
650 Perl_croak_nocontext("sethostent not implemented!\n");
655 win32_setnetent(int stayopen)
658 Perl_croak_nocontext("setnetent not implemented!\n");
663 win32_setprotoent(int stayopen)
666 Perl_croak_nocontext("setprotoent not implemented!\n");
671 win32_setservent(int stayopen)
674 Perl_croak_nocontext("setservent not implemented!\n");
677 static struct servent*
678 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
680 d->s_name = s->s_name;
681 d->s_aliases = s->s_aliases;
682 d->s_port = s->s_port;
683 if (s->s_proto && strlen(s->s_proto))
684 d->s_proto = s->s_proto;
686 if (proto && strlen(proto))
687 d->s_proto = (char *)proto;