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 #include <sys/socket.h>
23 /* thanks to Beverly Brown (beverly@datacube.com) */
24 #ifdef USE_SOCKETS_AS_HANDLES
25 # define OPEN_SOCKET(x) _open_osfhandle(x,O_RDWR|O_BINARY)
26 # define TO_SOCKET(x) _get_osfhandle(x)
28 # define OPEN_SOCKET(x) (x)
29 # define TO_SOCKET(x) (x)
30 #endif /* USE_SOCKETS_AS_HANDLES */
32 #define StartSockets() \
38 #define EndSockets() \
44 #define SOCKET_TEST(x, y) \
48 errno = WSAGetLastError(); \
51 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
53 static struct servent* win32_savecopyservent(struct servent*d,
57 __declspec(thread) struct servent myservent;
58 static int wsock_started = 0;
63 unsigned short version;
66 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
69 * initalize the winsock interface and insure that it is
73 if(ret = WSAStartup(version, &retdata))
74 croak("Unable to locate winsock library!\n");
75 if(retdata.wVersion != version)
76 croak("Could not find version 1.1 of winsock dll\n");
78 /* atexit((void (*)(void)) EndSockets); */
80 #ifdef USE_SOCKETS_AS_HANDLES
82 * Enable the use of sockets as filehandles
84 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
85 (char *)&iSockOpt, sizeof(iSockOpt));
86 #endif /* USE_SOCKETS_AS_HANDLES */
91 #ifndef USE_SOCKETS_AS_HANDLES
94 my_fdopen(int fd, char *mode)
98 int optlen = sizeof(sockbuf);
102 return(fdopen(fd, mode));
104 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
105 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
106 return(fdopen(fd, mode));
110 * If we get here, then fd is actually a socket.
112 Newz(1310, fp, 1, FILE);
126 #endif /* USE_SOCKETS_AS_HANDLES */
130 win32_htonl(u_long hostlong)
133 return htonl(hostlong);
137 win32_htons(u_short hostshort)
140 return htons(hostshort);
144 win32_ntohl(u_long netlong)
147 return ntohl(netlong);
151 win32_ntohs(u_short netshort)
154 return ntohs(netshort);
160 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
164 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
165 return OPEN_SOCKET(r);
169 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
173 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
178 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
182 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
188 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
192 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
197 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
201 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
206 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
210 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
215 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
219 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
224 win32_listen(SOCKET s, int backlog)
228 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
233 win32_recv(SOCKET s, char *buf, int len, int flags)
237 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
242 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
246 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
250 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
252 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
255 #ifdef USE_SOCKETS_AS_HANDLES
257 int i, fd, bit, offset;
258 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
260 PERL_FD_ZERO(&dummy);
262 rd = &dummy, prd = NULL;
266 wr = &dummy, pwr = NULL;
270 ex = &dummy, pex = NULL;
277 for (i = 0; i < nfds; i++) {
279 if (PERL_FD_ISSET(i,rd))
281 if (PERL_FD_ISSET(i,wr))
283 if (PERL_FD_ISSET(i,ex))
287 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
289 for (i = 0; i < nfds; i++) {
291 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
293 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
295 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
299 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
305 win32_send(SOCKET s, const char *buf, int len, int flags)
309 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
314 win32_sendto(SOCKET s, const char *buf, int len, int flags,
315 const struct sockaddr *to, int tolen)
319 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
324 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
328 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
333 win32_shutdown(SOCKET s, int how)
337 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
342 win32_closesocket(SOCKET s)
346 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
351 win32_socket(int af, int type, int protocol)
355 #ifndef USE_SOCKETS_AS_HANDLES
356 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
359 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
360 errno = WSAGetLastError();
363 #endif /* USE_SOCKETS_AS_HANDLES */
373 if (!wsock_started) /* No WinSock? */
374 return(fclose(pf)); /* Then not a socket. */
375 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
376 retval = fclose(pf); /* Must fclose() before closesocket() */
378 && closesocket(osf) == SOCKET_ERROR
379 && WSAGetLastError() != WSAENOTSOCK)
387 win32_gethostbyaddr(const char *addr, int len, int type)
391 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
396 win32_gethostbyname(const char *name)
400 SOCKET_TEST(r = gethostbyname(name), NULL);
405 win32_gethostname(char *name, int len)
409 SOCKET_TEST_ERROR(r = gethostname(name, len));
414 win32_getprotobyname(const char *name)
418 SOCKET_TEST(r = getprotobyname(name), NULL);
423 win32_getprotobynumber(int num)
427 SOCKET_TEST(r = getprotobynumber(num), NULL);
432 win32_getservbyname(const char *name, const char *proto)
436 SOCKET_TEST(r = getservbyname(name, proto), NULL);
438 r = win32_savecopyservent(&myservent, r, proto);
444 win32_getservbyport(int port, const char *proto)
448 SOCKET_TEST(r = getservbyport(port, proto), NULL);
450 r = win32_savecopyservent(&myservent, r, proto);
456 win32_inet_ntoa(struct in_addr in)
459 return inet_ntoa(in);
463 win32_inet_addr(const char FAR *cp)
466 return inet_addr(cp);
476 croak("endhostent not implemented!\n");
482 croak("endnetent not implemented!\n");
488 croak("endprotoent not implemented!\n");
494 croak("endservent not implemented!\n");
499 win32_getnetent(void)
501 croak("getnetent not implemented!\n");
502 return (struct netent *) NULL;
506 win32_getnetbyname(char *name)
508 croak("getnetbyname not implemented!\n");
509 return (struct netent *)NULL;
513 win32_getnetbyaddr(long net, int type)
515 croak("getnetbyaddr not implemented!\n");
516 return (struct netent *)NULL;
520 win32_getprotoent(void)
522 croak("getprotoent not implemented!\n");
523 return (struct protoent *) NULL;
527 win32_getservent(void)
529 croak("getservent not implemented!\n");
530 return (struct servent *) NULL;
534 win32_sethostent(int stayopen)
536 croak("sethostent not implemented!\n");
541 win32_setnetent(int stayopen)
543 croak("setnetent not implemented!\n");
548 win32_setprotoent(int stayopen)
550 croak("setprotoent not implemented!\n");
555 win32_setservent(int stayopen)
557 croak("setservent not implemented!\n");
560 static struct servent*
561 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
563 d->s_name = s->s_name;
564 d->s_aliases = s->s_aliases;
565 d->s_port = s->s_port;
566 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
567 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
568 d->s_proto = s->s_proto;
571 if (proto && strlen(proto))
572 d->s_proto = (char *)proto;