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
21 #if defined(PERL_OBJECT)
27 #include <sys/socket.h>
33 /* thanks to Beverly Brown (beverly@datacube.com) */
34 #ifdef USE_SOCKETS_AS_HANDLES
35 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
36 # define TO_SOCKET(x) _get_osfhandle(x)
38 # define OPEN_SOCKET(x) (x)
39 # define TO_SOCKET(x) (x)
40 #endif /* USE_SOCKETS_AS_HANDLES */
42 #if defined(USE_THREADS) || defined(USE_ITHREADS)
43 #define StartSockets() \
50 #define StartSockets() \
52 if (!wsock_started) { \
59 #define SOCKET_TEST(x, y) \
63 errno = WSAGetLastError(); \
66 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
68 static struct servent* win32_savecopyservent(struct servent*d,
72 static int wsock_started = 0;
85 unsigned short version;
90 * initalize the winsock interface and insure that it is
94 if(ret = WSAStartup(version, &retdata))
95 Perl_croak_nocontext("Unable to locate winsock library!\n");
96 if(retdata.wVersion != version)
97 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
99 /* atexit((void (*)(void)) EndSockets); */
106 #ifdef USE_SOCKETS_AS_HANDLES
107 #if defined(USE_THREADS) || defined(USE_ITHREADS)
109 if (!w32_init_socktype) {
111 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
113 * Enable the use of sockets as filehandles
115 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
116 (char *)&iSockOpt, sizeof(iSockOpt));
117 #if defined(USE_THREADS) || defined(USE_ITHREADS)
118 w32_init_socktype = 1;
121 #endif /* USE_SOCKETS_AS_HANDLES */
125 #ifndef USE_SOCKETS_AS_HANDLES
128 my_fdopen(int fd, char *mode)
132 int optlen = sizeof(sockbuf);
136 return(fdopen(fd, mode));
138 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
139 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
140 return(fdopen(fd, mode));
144 * If we get here, then fd is actually a socket.
146 Newz(1310, fp, 1, FILE); /* XXX leak, good thing this code isn't used */
160 #endif /* USE_SOCKETS_AS_HANDLES */
164 win32_htonl(u_long hostlong)
167 return htonl(hostlong);
171 win32_htons(u_short hostshort)
174 return htons(hostshort);
178 win32_ntohl(u_long netlong)
181 return ntohl(netlong);
185 win32_ntohs(u_short netshort)
188 return ntohs(netshort);
194 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
198 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
199 return OPEN_SOCKET(r);
203 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
207 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
212 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
216 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
222 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
226 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
231 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
235 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
240 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
244 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
249 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
253 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
258 win32_listen(SOCKET s, int backlog)
262 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
267 win32_recv(SOCKET s, char *buf, int len, int flags)
271 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
276 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
279 int frombufsize = *fromlen;
281 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
282 /* Winsock's recvfrom() only returns a valid 'from' when the socket
283 * is connectionless. Perl expects a valid 'from' for all types
284 * of sockets, so go the extra mile.
286 if (r != SOCKET_ERROR && frombufsize == *fromlen)
287 (void)win32_getpeername(s, from, fromlen);
291 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
293 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
296 #ifdef USE_SOCKETS_AS_HANDLES
298 int i, fd, bit, offset;
299 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
301 /* winsock seems incapable of dealing with all three null fd_sets,
302 * so do the (millisecond) sleep as a special case
304 if (!(rd || wr || ex)) {
306 Sleep(timeout->tv_sec * 1000 +
307 timeout->tv_usec / 1000); /* do the best we can */
313 PERL_FD_ZERO(&dummy);
315 rd = &dummy, prd = NULL;
319 wr = &dummy, pwr = NULL;
323 ex = &dummy, pex = NULL;
330 for (i = 0; i < nfds; i++) {
332 if (PERL_FD_ISSET(i,rd))
334 if (PERL_FD_ISSET(i,wr))
336 if (PERL_FD_ISSET(i,ex))
340 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
342 for (i = 0; i < nfds; i++) {
344 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
346 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
348 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
352 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
358 win32_send(SOCKET s, const char *buf, int len, int flags)
362 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
367 win32_sendto(SOCKET s, const char *buf, int len, int flags,
368 const struct sockaddr *to, int tolen)
372 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
377 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
381 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
386 win32_shutdown(SOCKET s, int how)
390 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
395 win32_closesocket(SOCKET s)
399 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
404 win32_socket(int af, int type, int protocol)
408 #ifndef USE_SOCKETS_AS_HANDLES
409 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
412 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
413 errno = WSAGetLastError();
416 #endif /* USE_SOCKETS_AS_HANDLES */
426 if (!wsock_started) /* No WinSock? */
427 return(fclose(pf)); /* Then not a socket. */
428 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
430 && closesocket(osf) == SOCKET_ERROR
431 && WSAGetLastError() != WSAENOTSOCK)
441 win32_gethostbyaddr(const char *addr, int len, int type)
445 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
450 win32_gethostbyname(const char *name)
454 SOCKET_TEST(r = gethostbyname(name), NULL);
459 win32_gethostname(char *name, int len)
463 SOCKET_TEST_ERROR(r = gethostname(name, len));
468 win32_getprotobyname(const char *name)
472 SOCKET_TEST(r = getprotobyname(name), NULL);
477 win32_getprotobynumber(int num)
481 SOCKET_TEST(r = getprotobynumber(num), NULL);
486 win32_getservbyname(const char *name, const char *proto)
491 SOCKET_TEST(r = getservbyname(name, proto), NULL);
493 r = win32_savecopyservent(&w32_servent, r, proto);
499 win32_getservbyport(int port, const char *proto)
504 SOCKET_TEST(r = getservbyport(port, proto), NULL);
506 r = win32_savecopyservent(&w32_servent, r, proto);
512 win32_ioctl(int i, unsigned int u, char *data)
515 u_long argp = (u_long)data;
518 if (!wsock_started) {
519 Perl_croak_nocontext("ioctl implemented only on sockets");
523 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
524 if (retval == SOCKET_ERROR) {
525 if (WSAGetLastError() == WSAENOTSOCK) {
526 Perl_croak_nocontext("ioctl implemented only on sockets");
529 errno = WSAGetLastError();
535 win32_inet_ntoa(struct in_addr in)
538 return inet_ntoa(in);
542 win32_inet_addr(const char FAR *cp)
545 return inet_addr(cp);
556 Perl_croak_nocontext("endhostent not implemented!\n");
563 Perl_croak_nocontext("endnetent not implemented!\n");
570 Perl_croak_nocontext("endprotoent not implemented!\n");
577 Perl_croak_nocontext("endservent not implemented!\n");
582 win32_getnetent(void)
585 Perl_croak_nocontext("getnetent not implemented!\n");
586 return (struct netent *) NULL;
590 win32_getnetbyname(char *name)
593 Perl_croak_nocontext("getnetbyname not implemented!\n");
594 return (struct netent *)NULL;
598 win32_getnetbyaddr(long net, int type)
601 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
602 return (struct netent *)NULL;
606 win32_getprotoent(void)
609 Perl_croak_nocontext("getprotoent not implemented!\n");
610 return (struct protoent *) NULL;
614 win32_getservent(void)
617 Perl_croak_nocontext("getservent not implemented!\n");
618 return (struct servent *) NULL;
622 win32_sethostent(int stayopen)
625 Perl_croak_nocontext("sethostent not implemented!\n");
630 win32_setnetent(int stayopen)
633 Perl_croak_nocontext("setnetent not implemented!\n");
638 win32_setprotoent(int stayopen)
641 Perl_croak_nocontext("setprotoent not implemented!\n");
646 win32_setservent(int stayopen)
649 Perl_croak_nocontext("setservent not implemented!\n");
652 static struct servent*
653 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
655 d->s_name = s->s_name;
656 d->s_aliases = s->s_aliases;
657 d->s_port = s->s_port;
658 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
659 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
660 d->s_proto = s->s_proto;
663 if (proto && strlen(proto))
664 d->s_proto = (char *)proto;