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
23 #include <sys/socket.h>
29 /* thanks to Beverly Brown (beverly@datacube.com) */
30 #ifdef USE_SOCKETS_AS_HANDLES
31 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
32 # define TO_SOCKET(x) _get_osfhandle(x)
34 # define OPEN_SOCKET(x) (x)
35 # define TO_SOCKET(x) (x)
36 #endif /* USE_SOCKETS_AS_HANDLES */
38 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
39 #define StartSockets() \
46 #define StartSockets() \
48 if (!wsock_started) { \
55 #define SOCKET_TEST(x, y) \
59 errno = WSAGetLastError(); \
62 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
64 static struct servent* win32_savecopyservent(struct servent*d,
68 static int wsock_started = 0;
81 unsigned short version;
86 * initalize the winsock interface and insure that it is
90 if(ret = WSAStartup(version, &retdata))
91 Perl_croak_nocontext("Unable to locate winsock library!\n");
92 if(retdata.wVersion != version)
93 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
95 /* atexit((void (*)(void)) EndSockets); */
102 #ifdef USE_SOCKETS_AS_HANDLES
103 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
105 if (!w32_init_socktype) {
107 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
109 * Enable the use of sockets as filehandles
111 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
112 (char *)&iSockOpt, sizeof(iSockOpt));
113 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
114 w32_init_socktype = 1;
117 #endif /* USE_SOCKETS_AS_HANDLES */
121 #ifndef USE_SOCKETS_AS_HANDLES
124 my_fdopen(int fd, char *mode)
128 int optlen = sizeof(sockbuf);
132 return(fdopen(fd, mode));
134 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
135 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
136 return(fdopen(fd, mode));
140 * If we get here, then fd is actually a socket.
142 Newz(1310, fp, 1, FILE); /* XXX leak, good thing this code isn't used */
156 #endif /* USE_SOCKETS_AS_HANDLES */
160 win32_htonl(u_long hostlong)
163 return htonl(hostlong);
167 win32_htons(u_short hostshort)
170 return htons(hostshort);
174 win32_ntohl(u_long netlong)
177 return ntohl(netlong);
181 win32_ntohs(u_short netshort)
184 return ntohs(netshort);
190 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
194 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
195 return OPEN_SOCKET(r);
199 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
203 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
208 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
212 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
218 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
222 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
227 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
231 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
236 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
240 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
245 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
249 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
254 win32_listen(SOCKET s, int backlog)
258 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
263 win32_recv(SOCKET s, char *buf, int len, int flags)
267 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
272 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
275 int frombufsize = *fromlen;
277 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
278 /* Winsock's recvfrom() only returns a valid 'from' when the socket
279 * is connectionless. Perl expects a valid 'from' for all types
280 * of sockets, so go the extra mile.
282 if (r != SOCKET_ERROR && frombufsize == *fromlen)
283 (void)win32_getpeername(s, from, fromlen);
287 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
289 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
292 #ifdef USE_SOCKETS_AS_HANDLES
294 int i, fd, bit, offset;
295 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
297 /* winsock seems incapable of dealing with all three null fd_sets,
298 * so do the (millisecond) sleep as a special case
300 if (!(rd || wr || ex)) {
302 Sleep(timeout->tv_sec * 1000 +
303 timeout->tv_usec / 1000); /* do the best we can */
309 PERL_FD_ZERO(&dummy);
311 rd = &dummy, prd = NULL;
315 wr = &dummy, pwr = NULL;
319 ex = &dummy, pex = NULL;
326 for (i = 0; i < nfds; i++) {
328 if (PERL_FD_ISSET(i,rd))
330 if (PERL_FD_ISSET(i,wr))
332 if (PERL_FD_ISSET(i,ex))
336 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
338 for (i = 0; i < nfds; i++) {
340 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
342 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
344 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
348 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
354 win32_send(SOCKET s, const char *buf, int len, int flags)
358 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
363 win32_sendto(SOCKET s, const char *buf, int len, int flags,
364 const struct sockaddr *to, int tolen)
368 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
373 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
377 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
382 win32_shutdown(SOCKET s, int how)
386 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
391 win32_closesocket(SOCKET s)
395 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
400 win32_socket(int af, int type, int protocol)
404 #ifndef USE_SOCKETS_AS_HANDLES
405 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
408 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
409 errno = WSAGetLastError();
412 #endif /* USE_SOCKETS_AS_HANDLES */
418 * close RTL fd while respecting sockets
419 * added as temporary measure until PerlIO has real
427 if (!wsock_started) /* No WinSock? */
428 return(close(fd)); /* Then not a socket. */
429 osf = TO_SOCKET(fd);/* Get it now before it's gone! */
432 err = closesocket(osf);
434 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
435 _set_osfhnd(fd, INVALID_HANDLE_VALUE);
437 (void)close(fd); /* handle already closed, ignore error */
440 else if (err == SOCKET_ERROR) {
441 err = WSAGetLastError();
442 if (err != WSAENOTSOCK) {
457 if (!wsock_started) /* No WinSock? */
458 return(fclose(pf)); /* Then not a socket. */
459 osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
463 err = closesocket(osf);
465 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
466 _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
468 (void)fclose(pf); /* handle already closed, ignore error */
471 else if (err == SOCKET_ERROR) {
472 err = WSAGetLastError();
473 if (err != WSAENOTSOCK) {
485 my_fstat(int fd, struct stat *sbufptr)
487 /* This fixes a bug in fstat() on Windows 9x. fstat() uses the
488 * GetFileType() win32 syscall, which will fail on Windows 9x.
489 * So if we recognize a socket on Windows 9x, we return the
490 * same results as on Windows NT/2000.
491 * XXX this should be extended further to set S_IFSOCK on
495 if (!wsock_started || IsWinNT())
496 return fstat(fd, sbufptr);
501 int optlen = sizeof(sockbuf);
504 retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
505 if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
506 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
507 sbufptr->st_mode = S_IFIFO;
509 sbufptr->st_mode = _S_IFIFO;
511 sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
512 sbufptr->st_nlink = 1;
513 sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
514 sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
515 sbufptr->st_size = (off_t)0;
519 return fstat(fd, sbufptr);
523 win32_gethostbyaddr(const char *addr, int len, int type)
527 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
532 win32_gethostbyname(const char *name)
536 SOCKET_TEST(r = gethostbyname(name), NULL);
541 win32_gethostname(char *name, int len)
545 SOCKET_TEST_ERROR(r = gethostname(name, len));
550 win32_getprotobyname(const char *name)
554 SOCKET_TEST(r = getprotobyname(name), NULL);
559 win32_getprotobynumber(int num)
563 SOCKET_TEST(r = getprotobynumber(num), NULL);
568 win32_getservbyname(const char *name, const char *proto)
573 SOCKET_TEST(r = getservbyname(name, proto), NULL);
575 r = win32_savecopyservent(&w32_servent, r, proto);
581 win32_getservbyport(int port, const char *proto)
586 SOCKET_TEST(r = getservbyport(port, proto), NULL);
588 r = win32_savecopyservent(&w32_servent, r, proto);
594 win32_ioctl(int i, unsigned int u, char *data)
597 u_long argp = (u_long)data;
600 if (!wsock_started) {
601 Perl_croak_nocontext("ioctl implemented only on sockets");
605 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
606 if (retval == SOCKET_ERROR) {
607 if (WSAGetLastError() == WSAENOTSOCK) {
608 Perl_croak_nocontext("ioctl implemented only on sockets");
611 errno = WSAGetLastError();
617 win32_inet_ntoa(struct in_addr in)
620 return inet_ntoa(in);
624 win32_inet_addr(const char FAR *cp)
627 return inet_addr(cp);
638 Perl_croak_nocontext("endhostent not implemented!\n");
645 Perl_croak_nocontext("endnetent not implemented!\n");
652 Perl_croak_nocontext("endprotoent not implemented!\n");
659 Perl_croak_nocontext("endservent not implemented!\n");
664 win32_getnetent(void)
667 Perl_croak_nocontext("getnetent not implemented!\n");
668 return (struct netent *) NULL;
672 win32_getnetbyname(char *name)
675 Perl_croak_nocontext("getnetbyname not implemented!\n");
676 return (struct netent *)NULL;
680 win32_getnetbyaddr(long net, int type)
683 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
684 return (struct netent *)NULL;
688 win32_getprotoent(void)
691 Perl_croak_nocontext("getprotoent not implemented!\n");
692 return (struct protoent *) NULL;
696 win32_getservent(void)
699 Perl_croak_nocontext("getservent not implemented!\n");
700 return (struct servent *) NULL;
704 win32_sethostent(int stayopen)
707 Perl_croak_nocontext("sethostent not implemented!\n");
712 win32_setnetent(int stayopen)
715 Perl_croak_nocontext("setnetent not implemented!\n");
720 win32_setprotoent(int stayopen)
723 Perl_croak_nocontext("setprotoent not implemented!\n");
728 win32_setservent(int stayopen)
731 Perl_croak_nocontext("setservent not implemented!\n");
734 static struct servent*
735 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
737 d->s_name = s->s_name;
738 d->s_aliases = s->s_aliases;
739 d->s_port = s->s_port;
740 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
741 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
742 d->s_proto = s->s_proto;
745 if (proto && strlen(proto))
746 d->s_proto = (char *)proto;