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)
23 extern CPerlObj* pPerl;
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 */
44 #define StartSockets() \
51 #define StartSockets() \
53 if (!wsock_started) { \
60 #define EndSockets() \
66 #define SOCKET_TEST(x, y) \
70 errno = WSAGetLastError(); \
73 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
75 static struct servent* win32_savecopyservent(struct servent*d,
80 #ifdef USE_DECLSPEC_THREAD
81 __declspec(thread) struct servent myservent;
82 __declspec(thread) int init_socktype;
84 #define myservent (thr->i.Wservent)
85 #define init_socktype (thr->i.Winit_socktype)
88 static struct servent myservent;
91 static int wsock_started = 0;
96 unsigned short version;
101 * initalize the winsock interface and insure that it is
102 * cleaned up at exit.
105 if(ret = WSAStartup(version, &retdata))
106 Perl_croak_nocontext("Unable to locate winsock library!\n");
107 if(retdata.wVersion != version)
108 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
110 /* atexit((void (*)(void)) EndSockets); */
117 #ifdef USE_SOCKETS_AS_HANDLES
122 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
124 * Enable the use of sockets as filehandles
126 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
127 (char *)&iSockOpt, sizeof(iSockOpt));
132 #endif /* USE_SOCKETS_AS_HANDLES */
136 #ifndef USE_SOCKETS_AS_HANDLES
139 my_fdopen(int fd, char *mode)
143 int optlen = sizeof(sockbuf);
147 return(fdopen(fd, mode));
149 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
150 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
151 return(fdopen(fd, mode));
155 * If we get here, then fd is actually a socket.
157 Newz(1310, fp, 1, FILE);
171 #endif /* USE_SOCKETS_AS_HANDLES */
175 win32_htonl(u_long hostlong)
178 return htonl(hostlong);
182 win32_htons(u_short hostshort)
185 return htons(hostshort);
189 win32_ntohl(u_long netlong)
192 return ntohl(netlong);
196 win32_ntohs(u_short netshort)
199 return ntohs(netshort);
205 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
209 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
210 return OPEN_SOCKET(r);
214 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
218 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
223 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
227 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
233 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
237 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
242 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
246 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
251 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
255 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
260 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
264 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
269 win32_listen(SOCKET s, int backlog)
273 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
278 win32_recv(SOCKET s, char *buf, int len, int flags)
282 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
287 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
290 int frombufsize = *fromlen;
292 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
293 /* Winsock's recvfrom() only returns a valid 'from' when the socket
294 * is connectionless. Perl expects a valid 'from' for all types
295 * of sockets, so go the extra mile.
297 if (r != SOCKET_ERROR && frombufsize == *fromlen)
298 (void)win32_getpeername(s, from, fromlen);
302 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
304 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
307 #ifdef USE_SOCKETS_AS_HANDLES
309 int i, fd, bit, offset;
310 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
312 /* winsock seems incapable of dealing with all three null fd_sets,
313 * so do the (millisecond) sleep as a special case
315 if (!(rd || wr || ex)) {
317 Sleep(timeout->tv_sec * 1000 +
318 timeout->tv_usec / 1000); /* do the best we can */
324 PERL_FD_ZERO(&dummy);
326 rd = &dummy, prd = NULL;
330 wr = &dummy, pwr = NULL;
334 ex = &dummy, pex = NULL;
341 for (i = 0; i < nfds; i++) {
343 if (PERL_FD_ISSET(i,rd))
345 if (PERL_FD_ISSET(i,wr))
347 if (PERL_FD_ISSET(i,ex))
351 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
353 for (i = 0; i < nfds; i++) {
355 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
357 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
359 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
363 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
369 win32_send(SOCKET s, const char *buf, int len, int flags)
373 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
378 win32_sendto(SOCKET s, const char *buf, int len, int flags,
379 const struct sockaddr *to, int tolen)
383 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
388 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
392 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
397 win32_shutdown(SOCKET s, int how)
401 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
406 win32_closesocket(SOCKET s)
410 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
415 win32_socket(int af, int type, int protocol)
419 #ifndef USE_SOCKETS_AS_HANDLES
420 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
423 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
424 errno = WSAGetLastError();
427 #endif /* USE_SOCKETS_AS_HANDLES */
437 if (!wsock_started) /* No WinSock? */
438 return(fclose(pf)); /* Then not a socket. */
439 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
440 retval = fclose(pf); /* Must fclose() before closesocket() */
442 && closesocket(osf) == SOCKET_ERROR
443 && WSAGetLastError() != WSAENOTSOCK)
451 win32_gethostbyaddr(const char *addr, int len, int type)
455 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
460 win32_gethostbyname(const char *name)
464 SOCKET_TEST(r = gethostbyname(name), NULL);
469 win32_gethostname(char *name, int len)
473 SOCKET_TEST_ERROR(r = gethostname(name, len));
478 win32_getprotobyname(const char *name)
482 SOCKET_TEST(r = getprotobyname(name), NULL);
487 win32_getprotobynumber(int num)
491 SOCKET_TEST(r = getprotobynumber(num), NULL);
496 win32_getservbyname(const char *name, const char *proto)
501 SOCKET_TEST(r = getservbyname(name, proto), NULL);
503 r = win32_savecopyservent(&myservent, r, proto);
509 win32_getservbyport(int port, const char *proto)
514 SOCKET_TEST(r = getservbyport(port, proto), NULL);
516 r = win32_savecopyservent(&myservent, r, proto);
522 win32_ioctl(int i, unsigned int u, char *data)
524 u_long argp = (u_long)data;
527 if (!wsock_started) {
528 Perl_croak_nocontext("ioctl implemented only on sockets");
532 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
533 if (retval == SOCKET_ERROR) {
534 if (WSAGetLastError() == WSAENOTSOCK) {
535 Perl_croak_nocontext("ioctl implemented only on sockets");
538 errno = WSAGetLastError();
544 win32_inet_ntoa(struct in_addr in)
547 return inet_ntoa(in);
551 win32_inet_addr(const char FAR *cp)
554 return inet_addr(cp);
564 Perl_croak_nocontext("endhostent not implemented!\n");
570 Perl_croak_nocontext("endnetent not implemented!\n");
576 Perl_croak_nocontext("endprotoent not implemented!\n");
582 Perl_croak_nocontext("endservent not implemented!\n");
587 win32_getnetent(void)
589 Perl_croak_nocontext("getnetent not implemented!\n");
590 return (struct netent *) NULL;
594 win32_getnetbyname(char *name)
596 Perl_croak_nocontext("getnetbyname not implemented!\n");
597 return (struct netent *)NULL;
601 win32_getnetbyaddr(long net, int type)
603 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
604 return (struct netent *)NULL;
608 win32_getprotoent(void)
610 Perl_croak_nocontext("getprotoent not implemented!\n");
611 return (struct protoent *) NULL;
615 win32_getservent(void)
617 Perl_croak_nocontext("getservent not implemented!\n");
618 return (struct servent *) NULL;
622 win32_sethostent(int stayopen)
624 Perl_croak_nocontext("sethostent not implemented!\n");
629 win32_setnetent(int stayopen)
631 Perl_croak_nocontext("setnetent not implemented!\n");
636 win32_setprotoent(int stayopen)
638 Perl_croak_nocontext("setprotoent not implemented!\n");
643 win32_setservent(int stayopen)
645 Perl_croak_nocontext("setservent not implemented!\n");
648 static struct servent*
649 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
651 d->s_name = s->s_name;
652 d->s_aliases = s->s_aliases;
653 d->s_port = s->s_port;
654 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
655 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
656 d->s_proto = s->s_proto;
659 if (proto && strlen(proto))
660 d->s_proto = (char *)proto;