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.
12 #define WIN32_LEAN_AND_MEAN
15 #include <sys/socket.h>
45 #undef getprotobynumber
63 /* thanks to Beverly Brown (beverly@datacube.com) */
64 #ifdef USE_SOCKETS_AS_HANDLES
65 # define OPEN_SOCKET(x) _open_osfhandle(x,O_RDWR|O_BINARY)
66 # define TO_SOCKET(x) _get_osfhandle(x)
68 # define OPEN_SOCKET(x) (x)
69 # define TO_SOCKET(x) (x)
70 #endif /* USE_SOCKETS_AS_HANDLES */
72 #define StartSockets() \
78 #define EndSockets() \
84 #define SOCKET_TEST(x, y) \
88 errno = WSAGetLastError(); \
91 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
93 static struct servent* win32_savecopyservent(struct servent*d,
97 __declspec(thread) struct servent myservent;
98 static int wsock_started = 0;
103 unsigned short version;
106 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
109 * initalize the winsock interface and insure that it is
110 * cleaned up at exit.
113 if(ret = WSAStartup(version, &retdata))
114 croak("Unable to locate winsock library!\n");
115 if(retdata.wVersion != version)
116 croak("Could not find version 1.1 of winsock dll\n");
118 /* atexit((void (*)(void)) EndSockets); */
120 #ifdef USE_SOCKETS_AS_HANDLES
122 * Enable the use of sockets as filehandles
124 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
125 (char *)&iSockOpt, sizeof(iSockOpt));
126 #endif /* USE_SOCKETS_AS_HANDLES */
131 #ifndef USE_SOCKETS_AS_HANDLES
133 myfdopen(int fd, char *mode)
137 int optlen = sizeof(sockbuf);
141 return(fdopen(fd, mode));
143 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
144 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
145 return(fdopen(fd, mode));
149 * If we get here, then fd is actually a socket.
151 Newz(1310, fp, 1, FILE);
165 #endif /* USE_SOCKETS_AS_HANDLES */
169 win32_htonl(u_long hostlong)
172 return htonl(hostlong);
176 win32_htons(u_short hostshort)
179 return htons(hostshort);
183 win32_ntohl(u_long netlong)
186 return ntohl(netlong);
190 win32_ntohs(u_short netshort)
193 return ntohs(netshort);
199 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
203 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
204 return OPEN_SOCKET(r);
208 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
212 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
217 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
221 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
227 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
231 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
236 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
240 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
245 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
249 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
254 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
258 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
263 win32_listen(SOCKET s, int backlog)
267 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
272 win32_recv(SOCKET s, char *buf, int len, int flags)
276 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
281 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
285 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
289 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
291 win32_select(int nfds, int* rd, int* wr, int* ex, const struct timeval* timeout)
295 int i, fd, bit, offset;
296 FD_SET nrd, nwr, nex,*prd,*pwr,*pex;
299 rd = &dummy, prd = NULL;
303 wr = &dummy, pwr = NULL;
307 ex = &dummy, pex = NULL;
314 for (i = 0; i < nfds; i++) {
316 bit = 1L<<(i % (sizeof(int)*8));
317 offset = i / (sizeof(int)*8);
318 if (rd[offset] & bit)
320 if (wr[offset] & bit)
322 if (ex[offset] & bit)
326 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
328 for (i = 0; i < nfds; i++) {
330 bit = 1L<<(i % (sizeof(int)*8));
331 offset = i / (sizeof(int)*8);
332 if (rd[offset] & bit) {
333 if (!__WSAFDIsSet(fd, &nrd))
336 if (wr[offset] & bit) {
337 if (!__WSAFDIsSet(fd, &nwr))
340 if (ex[offset] & bit) {
341 if (!__WSAFDIsSet(fd, &nex))
349 win32_send(SOCKET s, const char *buf, int len, int flags)
353 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
358 win32_sendto(SOCKET s, const char *buf, int len, int flags,
359 const struct sockaddr *to, int tolen)
363 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
368 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
372 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
377 win32_shutdown(SOCKET s, int how)
381 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
386 win32_socket(int af, int type, int protocol)
391 #ifndef USE_SOCKETS_AS_HANDLES
392 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
394 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
395 errno = WSAGetLastError();
398 #endif /* USE_SOCKETS_AS_HANDLES */
408 if (!wsock_started) /* No WinSock? */
409 return(fclose(pf)); /* Then not a socket. */
410 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
411 retval = fclose(pf); /* Must fclose() before closesocket() */
413 && closesocket(osf) == SOCKET_ERROR
414 && WSAGetLastError() != WSAENOTSOCK)
422 win32_gethostbyaddr(const char *addr, int len, int type)
426 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
431 win32_gethostbyname(const char *name)
435 SOCKET_TEST(r = gethostbyname(name), NULL);
440 win32_gethostname(char *name, int len)
444 SOCKET_TEST_ERROR(r = gethostname(name, len));
449 win32_getprotobyname(const char *name)
453 SOCKET_TEST(r = getprotobyname(name), NULL);
458 win32_getprotobynumber(int num)
462 SOCKET_TEST(r = getprotobynumber(num), NULL);
467 win32_getservbyname(const char *name, const char *proto)
471 SOCKET_TEST(r = getservbyname(name, proto), NULL);
473 r = win32_savecopyservent(&myservent, r, proto);
479 win32_getservbyport(int port, const char *proto)
483 SOCKET_TEST(r = getservbyport(port, proto), NULL);
485 r = win32_savecopyservent(&myservent, r, proto);
491 win32_inet_ntoa(struct in_addr in)
494 return inet_ntoa(in);
498 win32_inet_addr(const char FAR *cp)
501 return inet_addr(cp);
511 croak("endhostent not implemented!\n");
517 croak("endnetent not implemented!\n");
523 croak("endprotoent not implemented!\n");
529 croak("endservent not implemented!\n");
534 win32_getnetent(void)
536 croak("getnetent not implemented!\n");
537 return (struct netent *) NULL;
541 win32_getnetbyname(char *name)
543 croak("getnetbyname not implemented!\n");
544 return (struct netent *)NULL;
548 win32_getnetbyaddr(long net, int type)
550 croak("getnetbyaddr not implemented!\n");
551 return (struct netent *)NULL;
555 win32_getprotoent(void)
557 croak("getprotoent not implemented!\n");
558 return (struct protoent *) NULL;
562 win32_getservent(void)
564 croak("getservent not implemented!\n");
565 return (struct servent *) NULL;
569 win32_sethostent(int stayopen)
571 croak("sethostent not implemented!\n");
576 win32_setnetent(int stayopen)
578 croak("setnetent not implemented!\n");
583 win32_setprotoent(int stayopen)
585 croak("setprotoent not implemented!\n");
590 win32_setservent(int stayopen)
592 croak("setservent not implemented!\n");
595 #define WIN32IO_IS_STDIO
601 #include "win32iop.h"
606 static struct servent*
607 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
609 d->s_name = s->s_name;
610 d->s_aliases = s->s_aliases;
611 d->s_port = s->s_port;
612 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
613 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
614 d->s_proto = s->s_proto;
617 if (proto && strlen(proto))
618 d->s_proto = (char *)proto;