3 * (c) 1995 Microsoft Corporation. All rights reserved.
4 * Developed by hip communications inc., http://info.hip.com/info/
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
22 #if defined(PERL_OBJECT)
28 #include <sys/socket.h>
34 /* thanks to Beverly Brown (beverly@datacube.com) */
35 #ifdef USE_SOCKETS_AS_HANDLES
36 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
37 # define TO_SOCKET(x) _get_osfhandle(x)
39 # define OPEN_SOCKET(x) (x)
40 # define TO_SOCKET(x) (x)
41 #endif /* USE_SOCKETS_AS_HANDLES */
43 #if defined(USE_THREADS) || defined(USE_ITHREADS)
44 #define StartSockets() \
51 #define StartSockets() \
53 if (!wsock_started) { \
60 #define SOCKET_TEST(x, y) \
64 errno = WSAGetLastError(); \
67 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
69 static struct servent* win32_savecopyservent(struct servent*d,
73 static int wsock_started = 0;
86 unsigned short version;
91 * initalize the winsock interface and insure that it is
95 if(ret = WSAStartup(version, &retdata))
96 Perl_croak_nocontext("Unable to locate winsock library!\n");
97 if(retdata.wVersion != version)
98 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
100 /* atexit((void (*)(void)) EndSockets); */
107 #ifdef USE_SOCKETS_AS_HANDLES
108 #if defined(USE_THREADS) || defined(USE_ITHREADS)
110 if (!w32_init_socktype) {
112 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
114 * Enable the use of sockets as filehandles
116 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
117 (char *)&iSockOpt, sizeof(iSockOpt));
118 #if defined(USE_THREADS) || defined(USE_ITHREADS)
119 w32_init_socktype = 1;
122 #endif /* USE_SOCKETS_AS_HANDLES */
126 #ifndef USE_SOCKETS_AS_HANDLES
129 my_fdopen(int fd, char *mode)
133 int optlen = sizeof(sockbuf);
137 return(fdopen(fd, mode));
139 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
140 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
141 return(fdopen(fd, mode));
145 * If we get here, then fd is actually a socket.
147 Newz(1310, fp, 1, FILE); /* XXX leak, good thing this code isn't used */
161 #endif /* USE_SOCKETS_AS_HANDLES */
165 win32_htonl(u_long hostlong)
168 return htonl(hostlong);
172 win32_htons(u_short hostshort)
175 return htons(hostshort);
179 win32_ntohl(u_long netlong)
182 return ntohl(netlong);
186 win32_ntohs(u_short netshort)
189 return ntohs(netshort);
195 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
199 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
200 return OPEN_SOCKET(r);
204 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
208 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
213 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
217 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
223 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
227 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
232 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
236 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
241 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
245 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
250 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
254 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
259 win32_listen(SOCKET s, int backlog)
263 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
268 win32_recv(SOCKET s, char *buf, int len, int flags)
272 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
277 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
280 int frombufsize = *fromlen;
282 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
283 /* Winsock's recvfrom() only returns a valid 'from' when the socket
284 * is connectionless. Perl expects a valid 'from' for all types
285 * of sockets, so go the extra mile.
287 if (r != SOCKET_ERROR && frombufsize == *fromlen)
288 (void)win32_getpeername(s, from, fromlen);
292 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
294 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
297 #ifdef USE_SOCKETS_AS_HANDLES
299 int i, fd, bit, offset;
300 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
302 /* winsock seems incapable of dealing with all three null fd_sets,
303 * so do the (millisecond) sleep as a special case
305 if (!(rd || wr || ex)) {
307 Sleep(timeout->tv_sec * 1000 +
308 timeout->tv_usec / 1000); /* do the best we can */
314 PERL_FD_ZERO(&dummy);
316 rd = &dummy, prd = NULL;
320 wr = &dummy, pwr = NULL;
324 ex = &dummy, pex = NULL;
331 for (i = 0; i < nfds; i++) {
333 if (PERL_FD_ISSET(i,rd))
335 if (PERL_FD_ISSET(i,wr))
337 if (PERL_FD_ISSET(i,ex))
341 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
343 for (i = 0; i < nfds; i++) {
345 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
347 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
349 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
353 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
359 win32_send(SOCKET s, const char *buf, int len, int flags)
363 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
368 win32_sendto(SOCKET s, const char *buf, int len, int flags,
369 const struct sockaddr *to, int tolen)
373 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
378 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
382 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
387 win32_shutdown(SOCKET s, int how)
391 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
396 win32_closesocket(SOCKET s)
400 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
405 win32_socket(int af, int type, int protocol)
409 #ifndef USE_SOCKETS_AS_HANDLES
410 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
413 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
414 errno = WSAGetLastError();
417 #endif /* USE_SOCKETS_AS_HANDLES */
423 * close RTL fd while respecting sockets
424 * added as temporary measure until PerlIO has real
432 if (!wsock_started) /* No WinSock? */
433 return(close(fd)); /* Then not a socket. */
434 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
437 err = closesocket(osf);
439 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
440 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
442 (void)close(fd); /* handle already closed, ignore error */
445 else if (err == SOCKET_ERROR) {
446 err = WSAGetLastError();
447 if (err != WSAENOTSOCK) {
462 if (!wsock_started) /* No WinSock? */
463 return(fclose(pf)); /* Then not a socket. */
464 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
468 err = closesocket(osf);
470 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
471 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
473 (void)fclose(pf); /* handle already closed, ignore error */
476 else if (err == SOCKET_ERROR) {
477 err = WSAGetLastError();
478 if (err != WSAENOTSOCK) {
490 my_fstat(int fd, struct stat *sbufptr)
492 /* This fixes a bug in fstat() on Windows 9x. fstat() uses the
493 * GetFileType() win32 syscall, which will fail on Windows 9x.
494 * So if we recognize a socket on Windows 9x, we return the
495 * same results as on Windows NT/2000.
496 * XXX this should be extended further to set S_IFSOCK on
500 if (!wsock_started || IsWinNT())
501 return fstat(fd, sbufptr);
506 int optlen = sizeof(sockbuf);
509 retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
510 if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
511 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
512 sbufptr->st_mode = S_IFIFO;
514 sbufptr->st_mode = _S_IFIFO;
516 sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
517 sbufptr->st_nlink = 1;
518 sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
519 sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
520 sbufptr->st_size = (off_t)0;
524 return fstat(fd, sbufptr);
528 win32_gethostbyaddr(const char *addr, int len, int type)
532 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
537 win32_gethostbyname(const char *name)
541 SOCKET_TEST(r = gethostbyname(name), NULL);
546 win32_gethostname(char *name, int len)
550 SOCKET_TEST_ERROR(r = gethostname(name, len));
555 win32_getprotobyname(const char *name)
559 SOCKET_TEST(r = getprotobyname(name), NULL);
564 win32_getprotobynumber(int num)
568 SOCKET_TEST(r = getprotobynumber(num), NULL);
573 win32_getservbyname(const char *name, const char *proto)
578 SOCKET_TEST(r = getservbyname(name, proto), NULL);
580 r = win32_savecopyservent(&w32_servent, r, proto);
586 win32_getservbyport(int port, const char *proto)
591 SOCKET_TEST(r = getservbyport(port, proto), NULL);
593 r = win32_savecopyservent(&w32_servent, r, proto);
599 win32_ioctl(int i, unsigned int u, char *data)
602 u_long argp = (u_long)data;
605 if (!wsock_started) {
606 Perl_croak_nocontext("ioctl implemented only on sockets");
610 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
611 if (retval == SOCKET_ERROR) {
612 if (WSAGetLastError() == WSAENOTSOCK) {
613 Perl_croak_nocontext("ioctl implemented only on sockets");
616 errno = WSAGetLastError();
622 win32_inet_ntoa(struct in_addr in)
625 return inet_ntoa(in);
629 win32_inet_addr(const char FAR *cp)
632 return inet_addr(cp);
643 Perl_croak_nocontext("endhostent not implemented!\n");
650 Perl_croak_nocontext("endnetent not implemented!\n");
657 Perl_croak_nocontext("endprotoent not implemented!\n");
664 Perl_croak_nocontext("endservent not implemented!\n");
669 win32_getnetent(void)
672 Perl_croak_nocontext("getnetent not implemented!\n");
673 return (struct netent *) NULL;
677 win32_getnetbyname(char *name)
680 Perl_croak_nocontext("getnetbyname not implemented!\n");
681 return (struct netent *)NULL;
685 win32_getnetbyaddr(long net, int type)
688 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
689 return (struct netent *)NULL;
693 win32_getprotoent(void)
696 Perl_croak_nocontext("getprotoent not implemented!\n");
697 return (struct protoent *) NULL;
701 win32_getservent(void)
704 Perl_croak_nocontext("getservent not implemented!\n");
705 return (struct servent *) NULL;
709 win32_sethostent(int stayopen)
712 Perl_croak_nocontext("sethostent not implemented!\n");
717 win32_setnetent(int stayopen)
720 Perl_croak_nocontext("setnetent not implemented!\n");
725 win32_setprotoent(int stayopen)
728 Perl_croak_nocontext("setprotoent not implemented!\n");
733 win32_setservent(int stayopen)
736 Perl_croak_nocontext("setservent not implemented!\n");
739 static struct servent*
740 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
742 d->s_name = s->s_name;
743 d->s_aliases = s->s_aliases;
744 d->s_port = s->s_port;
745 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
746 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
747 d->s_proto = s->s_proto;
750 if (proto && strlen(proto))
751 d->s_proto = (char *)proto;