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 */
35 #define StartSockets() \
41 #define EndSockets() \
47 #define SOCKET_TEST(x, y) \
51 errno = WSAGetLastError(); \
54 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
56 static struct servent* win32_savecopyservent(struct servent*d,
61 #ifdef USE_DECLSPEC_THREAD
62 __declspec(thread) struct servent myservent;
64 #define myservent (thr->i.Wservent)
67 static struct servent myservent;
70 static int wsock_started = 0;
75 unsigned short version;
78 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
81 * initalize the winsock interface and insure that it is
85 if(ret = WSAStartup(version, &retdata))
86 croak("Unable to locate winsock library!\n");
87 if(retdata.wVersion != version)
88 croak("Could not find version 1.1 of winsock dll\n");
90 /* atexit((void (*)(void)) EndSockets); */
92 #ifdef USE_SOCKETS_AS_HANDLES
94 * Enable the use of sockets as filehandles
96 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
97 (char *)&iSockOpt, sizeof(iSockOpt));
98 #endif /* USE_SOCKETS_AS_HANDLES */
103 #ifndef USE_SOCKETS_AS_HANDLES
106 my_fdopen(int fd, char *mode)
110 int optlen = sizeof(sockbuf);
114 return(fdopen(fd, mode));
116 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
117 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
118 return(fdopen(fd, mode));
122 * If we get here, then fd is actually a socket.
124 Newz(1310, fp, 1, FILE);
138 #endif /* USE_SOCKETS_AS_HANDLES */
142 win32_htonl(u_long hostlong)
145 return htonl(hostlong);
149 win32_htons(u_short hostshort)
152 return htons(hostshort);
156 win32_ntohl(u_long netlong)
159 return ntohl(netlong);
163 win32_ntohs(u_short netshort)
166 return ntohs(netshort);
172 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
176 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
177 return OPEN_SOCKET(r);
181 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
185 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
190 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
194 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
200 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
204 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
209 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
213 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
218 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
222 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
227 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
231 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
236 win32_listen(SOCKET s, int backlog)
240 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
245 win32_recv(SOCKET s, char *buf, int len, int flags)
249 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
254 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
258 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
262 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
264 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
267 #ifdef USE_SOCKETS_AS_HANDLES
269 int i, fd, bit, offset;
270 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
272 PERL_FD_ZERO(&dummy);
274 rd = &dummy, prd = NULL;
278 wr = &dummy, pwr = NULL;
282 ex = &dummy, pex = NULL;
289 for (i = 0; i < nfds; i++) {
291 if (PERL_FD_ISSET(i,rd))
293 if (PERL_FD_ISSET(i,wr))
295 if (PERL_FD_ISSET(i,ex))
299 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
301 for (i = 0; i < nfds; i++) {
303 if (PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd))
305 if (PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr))
307 if (PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex))
311 SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
317 win32_send(SOCKET s, const char *buf, int len, int flags)
321 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
326 win32_sendto(SOCKET s, const char *buf, int len, int flags,
327 const struct sockaddr *to, int tolen)
331 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
336 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
340 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
345 win32_shutdown(SOCKET s, int how)
349 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
354 win32_closesocket(SOCKET s)
358 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
363 win32_socket(int af, int type, int protocol)
367 #ifndef USE_SOCKETS_AS_HANDLES
368 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
371 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
372 errno = WSAGetLastError();
375 #endif /* USE_SOCKETS_AS_HANDLES */
385 if (!wsock_started) /* No WinSock? */
386 return(fclose(pf)); /* Then not a socket. */
387 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
388 retval = fclose(pf); /* Must fclose() before closesocket() */
390 && closesocket(osf) == SOCKET_ERROR
391 && WSAGetLastError() != WSAENOTSOCK)
399 win32_gethostbyaddr(const char *addr, int len, int type)
403 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
408 win32_gethostbyname(const char *name)
412 SOCKET_TEST(r = gethostbyname(name), NULL);
417 win32_gethostname(char *name, int len)
421 SOCKET_TEST_ERROR(r = gethostname(name, len));
426 win32_getprotobyname(const char *name)
430 SOCKET_TEST(r = getprotobyname(name), NULL);
435 win32_getprotobynumber(int num)
439 SOCKET_TEST(r = getprotobynumber(num), NULL);
444 win32_getservbyname(const char *name, const char *proto)
449 SOCKET_TEST(r = getservbyname(name, proto), NULL);
451 r = win32_savecopyservent(&myservent, r, proto);
457 win32_getservbyport(int port, const char *proto)
462 SOCKET_TEST(r = getservbyport(port, proto), NULL);
464 r = win32_savecopyservent(&myservent, r, proto);
470 win32_ioctl(int i, unsigned int u, char *data)
472 u_long argp = (u_long)data;
475 if (!wsock_started) {
476 croak("ioctl implemented only on sockets");
480 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
481 if (retval == SOCKET_ERROR) {
482 if (WSAGetLastError() == WSAENOTSOCK) {
483 croak("ioctl implemented only on sockets");
486 errno = WSAGetLastError();
492 win32_inet_ntoa(struct in_addr in)
495 return inet_ntoa(in);
499 win32_inet_addr(const char FAR *cp)
502 return inet_addr(cp);
512 croak("endhostent not implemented!\n");
518 croak("endnetent not implemented!\n");
524 croak("endprotoent not implemented!\n");
530 croak("endservent not implemented!\n");
535 win32_getnetent(void)
537 croak("getnetent not implemented!\n");
538 return (struct netent *) NULL;
542 win32_getnetbyname(char *name)
544 croak("getnetbyname not implemented!\n");
545 return (struct netent *)NULL;
549 win32_getnetbyaddr(long net, int type)
551 croak("getnetbyaddr not implemented!\n");
552 return (struct netent *)NULL;
556 win32_getprotoent(void)
558 croak("getprotoent not implemented!\n");
559 return (struct protoent *) NULL;
563 win32_getservent(void)
565 croak("getservent not implemented!\n");
566 return (struct servent *) NULL;
570 win32_sethostent(int stayopen)
572 croak("sethostent not implemented!\n");
577 win32_setnetent(int stayopen)
579 croak("setnetent not implemented!\n");
584 win32_setprotoent(int stayopen)
586 croak("setprotoent not implemented!\n");
591 win32_setservent(int stayopen)
593 croak("setservent not implemented!\n");
596 static struct servent*
597 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
599 d->s_name = s->s_name;
600 d->s_aliases = s->s_aliases;
601 d->s_port = s->s_port;
602 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
603 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
604 d->s_proto = s->s_proto;
607 if (proto && strlen(proto))
608 d->s_proto = (char *)proto;