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
20 #include <sys/socket.h>
26 /* thanks to Beverly Brown (beverly@datacube.com) */
27 #ifdef USE_SOCKETS_AS_HANDLES
28 # define OPEN_SOCKET(x) _open_osfhandle(x,O_RDWR|O_BINARY)
29 # define TO_SOCKET(x) _get_osfhandle(x)
31 # define OPEN_SOCKET(x) (x)
32 # define TO_SOCKET(x) (x)
33 #endif /* USE_SOCKETS_AS_HANDLES */
36 #define StartSockets() \
43 #define StartSockets() \
45 if (!wsock_started) { \
52 #define EndSockets() \
58 #define SOCKET_TEST(x, y) \
62 errno = WSAGetLastError(); \
65 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
67 static struct servent* win32_savecopyservent(struct servent*d,
72 #ifdef USE_DECLSPEC_THREAD
73 __declspec(thread) struct servent myservent;
74 __declspec(thread) int init_socktype;
76 #define myservent (thr->i.Wservent)
77 #define init_socktype (thr->i.Winit_socktype)
80 static struct servent myservent;
83 static int wsock_started = 0;
88 unsigned short version;
93 * initalize the winsock interface and insure that it is
97 if(ret = WSAStartup(version, &retdata))
98 croak("Unable to locate winsock library!\n");
99 if(retdata.wVersion != version)
100 croak("Could not find version 1.1 of winsock dll\n");
102 /* atexit((void (*)(void)) EndSockets); */
109 #ifdef USE_SOCKETS_AS_HANDLES
114 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
116 * Enable the use of sockets as filehandles
118 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
119 (char *)&iSockOpt, sizeof(iSockOpt));
124 #endif /* USE_SOCKETS_AS_HANDLES */
128 #ifndef USE_SOCKETS_AS_HANDLES
131 my_fdopen(int fd, char *mode)
135 int optlen = sizeof(sockbuf);
139 return(fdopen(fd, mode));
141 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
142 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
143 return(fdopen(fd, mode));
147 * If we get here, then fd is actually a socket.
149 Newz(1310, fp, 1, FILE);
163 #endif /* USE_SOCKETS_AS_HANDLES */
167 win32_htonl(u_long hostlong)
170 return htonl(hostlong);
174 win32_htons(u_short hostshort)
177 return htons(hostshort);
181 win32_ntohl(u_long netlong)
184 return ntohl(netlong);
188 win32_ntohs(u_short netshort)
191 return ntohs(netshort);
197 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
201 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
202 return OPEN_SOCKET(r);
206 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
210 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
215 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
219 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
225 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
229 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
234 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
238 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
243 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
247 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
252 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
256 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
261 win32_listen(SOCKET s, int backlog)
265 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
270 win32_recv(SOCKET s, char *buf, int len, int flags)
274 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
279 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
283 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, 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)) {
301 Sleep(timeout->tv_sec * 1000 +
302 timeout->tv_usec / 1000); /* do the best we can */
306 PERL_FD_ZERO(&dummy);
308 rd = &dummy, prd = NULL;
312 wr = &dummy, pwr = NULL;
316 ex = &dummy, pex = NULL;
323 for (i = 0; i < nfds; i++) {
325 if (PERL_FD_ISSET(i,rd))
327 if (PERL_FD_ISSET(i,wr))
329 if (PERL_FD_ISSET(i,ex))
333 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
335 for (i = 0; i < nfds; i++) {
337 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
339 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
341 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
345 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
351 win32_send(SOCKET s, const char *buf, int len, int flags)
355 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
360 win32_sendto(SOCKET s, const char *buf, int len, int flags,
361 const struct sockaddr *to, int tolen)
365 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
370 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
374 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
379 win32_shutdown(SOCKET s, int how)
383 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
388 win32_closesocket(SOCKET s)
392 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
397 win32_socket(int af, int type, int protocol)
401 #ifndef USE_SOCKETS_AS_HANDLES
402 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
405 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
406 errno = WSAGetLastError();
409 #endif /* USE_SOCKETS_AS_HANDLES */
419 if (!wsock_started) /* No WinSock? */
420 return(fclose(pf)); /* Then not a socket. */
421 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
422 retval = fclose(pf); /* Must fclose() before closesocket() */
424 && closesocket(osf) == SOCKET_ERROR
425 && WSAGetLastError() != WSAENOTSOCK)
433 win32_gethostbyaddr(const char *addr, int len, int type)
437 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
442 win32_gethostbyname(const char *name)
446 SOCKET_TEST(r = gethostbyname(name), NULL);
451 win32_gethostname(char *name, int len)
455 SOCKET_TEST_ERROR(r = gethostname(name, len));
460 win32_getprotobyname(const char *name)
464 SOCKET_TEST(r = getprotobyname(name), NULL);
469 win32_getprotobynumber(int num)
473 SOCKET_TEST(r = getprotobynumber(num), NULL);
478 win32_getservbyname(const char *name, const char *proto)
483 SOCKET_TEST(r = getservbyname(name, proto), NULL);
485 r = win32_savecopyservent(&myservent, r, proto);
491 win32_getservbyport(int port, const char *proto)
496 SOCKET_TEST(r = getservbyport(port, proto), NULL);
498 r = win32_savecopyservent(&myservent, r, proto);
504 win32_ioctl(int i, unsigned int u, char *data)
506 u_long argp = (u_long)data;
509 if (!wsock_started) {
510 croak("ioctl implemented only on sockets");
514 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
515 if (retval == SOCKET_ERROR) {
516 if (WSAGetLastError() == WSAENOTSOCK) {
517 croak("ioctl implemented only on sockets");
520 errno = WSAGetLastError();
526 win32_inet_ntoa(struct in_addr in)
529 return inet_ntoa(in);
533 win32_inet_addr(const char FAR *cp)
536 return inet_addr(cp);
546 croak("endhostent not implemented!\n");
552 croak("endnetent not implemented!\n");
558 croak("endprotoent not implemented!\n");
564 croak("endservent not implemented!\n");
569 win32_getnetent(void)
571 croak("getnetent not implemented!\n");
572 return (struct netent *) NULL;
576 win32_getnetbyname(char *name)
578 croak("getnetbyname not implemented!\n");
579 return (struct netent *)NULL;
583 win32_getnetbyaddr(long net, int type)
585 croak("getnetbyaddr not implemented!\n");
586 return (struct netent *)NULL;
590 win32_getprotoent(void)
592 croak("getprotoent not implemented!\n");
593 return (struct protoent *) NULL;
597 win32_getservent(void)
599 croak("getservent not implemented!\n");
600 return (struct servent *) NULL;
604 win32_sethostent(int stayopen)
606 croak("sethostent not implemented!\n");
611 win32_setnetent(int stayopen)
613 croak("setnetent not implemented!\n");
618 win32_setprotoent(int stayopen)
620 croak("setprotoent not implemented!\n");
625 win32_setservent(int stayopen)
627 croak("setservent not implemented!\n");
630 static struct servent*
631 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
633 d->s_name = s->s_name;
634 d->s_aliases = s->s_aliases;
635 d->s_port = s->s_port;
636 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
637 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
638 d->s_proto = s->s_proto;
641 if (proto && strlen(proto))
642 d->s_proto = (char *)proto;