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
17 #undef __attribute__ /* seems broken in 2.8.0 */
18 #define __attribute__(p)
25 #if defined(PERL_OBJECT)
27 extern CPerlObj* pPerl;
32 #include <sys/socket.h>
38 /* thanks to Beverly Brown (beverly@datacube.com) */
39 #ifdef USE_SOCKETS_AS_HANDLES
40 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
41 # define TO_SOCKET(x) _get_osfhandle(x)
43 # define OPEN_SOCKET(x) (x)
44 # define TO_SOCKET(x) (x)
45 #endif /* USE_SOCKETS_AS_HANDLES */
48 #define StartSockets() \
55 #define StartSockets() \
57 if (!wsock_started) { \
64 #define EndSockets() \
70 #define SOCKET_TEST(x, y) \
74 errno = WSAGetLastError(); \
77 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
79 static struct servent* win32_savecopyservent(struct servent*d,
84 #ifdef USE_DECLSPEC_THREAD
85 __declspec(thread) struct servent myservent;
86 __declspec(thread) int init_socktype;
88 #define myservent (thr->i.Wservent)
89 #define init_socktype (thr->i.Winit_socktype)
92 static struct servent myservent;
95 static int wsock_started = 0;
100 unsigned short version;
105 * initalize the winsock interface and insure that it is
106 * cleaned up at exit.
109 if(ret = WSAStartup(version, &retdata))
110 croak("Unable to locate winsock library!\n");
111 if(retdata.wVersion != version)
112 croak("Could not find version 1.1 of winsock dll\n");
114 /* atexit((void (*)(void)) EndSockets); */
121 #ifdef USE_SOCKETS_AS_HANDLES
126 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
128 * Enable the use of sockets as filehandles
130 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
131 (char *)&iSockOpt, sizeof(iSockOpt));
136 #endif /* USE_SOCKETS_AS_HANDLES */
140 #ifndef USE_SOCKETS_AS_HANDLES
143 my_fdopen(int fd, char *mode)
147 int optlen = sizeof(sockbuf);
151 return(fdopen(fd, mode));
153 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
154 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
155 return(fdopen(fd, mode));
159 * If we get here, then fd is actually a socket.
161 Newz(1310, fp, 1, FILE);
175 #endif /* USE_SOCKETS_AS_HANDLES */
179 win32_htonl(u_long hostlong)
182 return htonl(hostlong);
186 win32_htons(u_short hostshort)
189 return htons(hostshort);
193 win32_ntohl(u_long netlong)
196 return ntohl(netlong);
200 win32_ntohs(u_short netshort)
203 return ntohs(netshort);
209 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
213 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
214 return OPEN_SOCKET(r);
218 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
222 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
227 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
231 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
237 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
241 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
246 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
250 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
255 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
259 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
264 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
268 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
273 win32_listen(SOCKET s, int backlog)
277 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
282 win32_recv(SOCKET s, char *buf, int len, int flags)
286 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
291 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
295 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
299 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
301 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
304 #ifdef USE_SOCKETS_AS_HANDLES
306 int i, fd, bit, offset;
307 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
309 /* winsock seems incapable of dealing with all three null fd_sets,
310 * so do the (millisecond) sleep as a special case
312 if (!(rd || wr || ex)) {
313 Sleep(timeout->tv_sec * 1000 +
314 timeout->tv_usec / 1000); /* do the best we can */
318 PERL_FD_ZERO(&dummy);
320 rd = &dummy, prd = NULL;
324 wr = &dummy, pwr = NULL;
328 ex = &dummy, pex = NULL;
335 for (i = 0; i < nfds; i++) {
337 if (PERL_FD_ISSET(i,rd))
339 if (PERL_FD_ISSET(i,wr))
341 if (PERL_FD_ISSET(i,ex))
345 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
347 for (i = 0; i < nfds; i++) {
349 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
351 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
353 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
357 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
363 win32_send(SOCKET s, const char *buf, int len, int flags)
367 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
372 win32_sendto(SOCKET s, const char *buf, int len, int flags,
373 const struct sockaddr *to, int tolen)
377 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
382 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
386 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
391 win32_shutdown(SOCKET s, int how)
395 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
400 win32_closesocket(SOCKET s)
404 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
409 win32_socket(int af, int type, int protocol)
413 #ifndef USE_SOCKETS_AS_HANDLES
414 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
417 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
418 errno = WSAGetLastError();
421 #endif /* USE_SOCKETS_AS_HANDLES */
431 if (!wsock_started) /* No WinSock? */
432 return(fclose(pf)); /* Then not a socket. */
433 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
434 retval = fclose(pf); /* Must fclose() before closesocket() */
436 && closesocket(osf) == SOCKET_ERROR
437 && WSAGetLastError() != WSAENOTSOCK)
445 win32_gethostbyaddr(const char *addr, int len, int type)
449 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
454 win32_gethostbyname(const char *name)
458 SOCKET_TEST(r = gethostbyname(name), NULL);
463 win32_gethostname(char *name, int len)
467 SOCKET_TEST_ERROR(r = gethostname(name, len));
472 win32_getprotobyname(const char *name)
476 SOCKET_TEST(r = getprotobyname(name), NULL);
481 win32_getprotobynumber(int num)
485 SOCKET_TEST(r = getprotobynumber(num), NULL);
490 win32_getservbyname(const char *name, const char *proto)
495 SOCKET_TEST(r = getservbyname(name, proto), NULL);
497 r = win32_savecopyservent(&myservent, r, proto);
503 win32_getservbyport(int port, const char *proto)
508 SOCKET_TEST(r = getservbyport(port, proto), NULL);
510 r = win32_savecopyservent(&myservent, r, proto);
516 win32_ioctl(int i, unsigned int u, char *data)
518 u_long argp = (u_long)data;
521 if (!wsock_started) {
522 croak("ioctl implemented only on sockets");
526 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
527 if (retval == SOCKET_ERROR) {
528 if (WSAGetLastError() == WSAENOTSOCK) {
529 croak("ioctl implemented only on sockets");
532 errno = WSAGetLastError();
538 win32_inet_ntoa(struct in_addr in)
541 return inet_ntoa(in);
545 win32_inet_addr(const char FAR *cp)
548 return inet_addr(cp);
558 croak("endhostent not implemented!\n");
564 croak("endnetent not implemented!\n");
570 croak("endprotoent not implemented!\n");
576 croak("endservent not implemented!\n");
581 win32_getnetent(void)
583 croak("getnetent not implemented!\n");
584 return (struct netent *) NULL;
588 win32_getnetbyname(char *name)
590 croak("getnetbyname not implemented!\n");
591 return (struct netent *)NULL;
595 win32_getnetbyaddr(long net, int type)
597 croak("getnetbyaddr not implemented!\n");
598 return (struct netent *)NULL;
602 win32_getprotoent(void)
604 croak("getprotoent not implemented!\n");
605 return (struct protoent *) NULL;
609 win32_getservent(void)
611 croak("getservent not implemented!\n");
612 return (struct servent *) NULL;
616 win32_sethostent(int stayopen)
618 croak("sethostent not implemented!\n");
623 win32_setnetent(int stayopen)
625 croak("setnetent not implemented!\n");
630 win32_setprotoent(int stayopen)
632 croak("setprotoent not implemented!\n");
637 win32_setservent(int stayopen)
639 croak("setservent not implemented!\n");
642 static struct servent*
643 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
645 d->s_name = s->s_name;
646 d->s_aliases = s->s_aliases;
647 d->s_port = s->s_port;
648 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
649 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
650 d->s_proto = s->s_proto;
653 if (proto && strlen(proto))
654 d->s_proto = (char *)proto;