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>
46 #undef getprotobynumber
64 /* thanks to Beverly Brown (beverly@datacube.com) */
65 #ifdef USE_SOCKETS_AS_HANDLES
66 # define OPEN_SOCKET(x) _open_osfhandle(x,O_RDWR|O_BINARY)
67 # define TO_SOCKET(x) _get_osfhandle(x)
69 # define OPEN_SOCKET(x) (x)
70 # define TO_SOCKET(x) (x)
71 #endif /* USE_SOCKETS_AS_HANDLES */
73 #define StartSockets() \
79 #define EndSockets() \
85 #define SOCKET_TEST(x, y) \
89 errno = WSAGetLastError(); \
92 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
94 static struct servent* win32_savecopyservent(struct servent*d,
98 __declspec(thread) struct servent myservent;
99 static int wsock_started = 0;
104 unsigned short version;
107 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
110 * initalize the winsock interface and insure that it is
111 * cleaned up at exit.
114 if(ret = WSAStartup(version, &retdata))
115 croak("Unable to locate winsock library!\n");
116 if(retdata.wVersion != version)
117 croak("Could not find version 1.1 of winsock dll\n");
119 /* atexit((void (*)(void)) EndSockets); */
121 #ifdef USE_SOCKETS_AS_HANDLES
123 * Enable the use of sockets as filehandles
125 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
126 (char *)&iSockOpt, sizeof(iSockOpt));
127 #endif /* USE_SOCKETS_AS_HANDLES */
132 #ifndef USE_SOCKETS_AS_HANDLES
134 myfdopen(int fd, char *mode)
138 int optlen = sizeof(sockbuf);
142 return(fdopen(fd, mode));
144 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
145 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
146 return(fdopen(fd, mode));
150 * If we get here, then fd is actually a socket.
152 Newz(1310, fp, 1, FILE);
166 #endif /* USE_SOCKETS_AS_HANDLES */
170 win32_htonl(u_long hostlong)
173 return htonl(hostlong);
177 win32_htons(u_short hostshort)
180 return htons(hostshort);
184 win32_ntohl(u_long netlong)
187 return ntohl(netlong);
191 win32_ntohs(u_short netshort)
194 return ntohs(netshort);
200 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
204 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
205 return OPEN_SOCKET(r);
209 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
213 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
218 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
222 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
228 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
232 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
237 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
241 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
246 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
250 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
255 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
259 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
264 win32_listen(SOCKET s, int backlog)
268 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
273 win32_recv(SOCKET s, char *buf, int len, int flags)
277 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
282 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
286 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
290 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
292 win32_select(int nfds, int* rd, int* wr, int* ex, const struct timeval* timeout)
296 int i, fd, bit, offset;
297 FD_SET nrd, nwr, nex,*prd,*pwr,*pex;
300 rd = &dummy, prd = NULL;
304 wr = &dummy, pwr = NULL;
308 ex = &dummy, pex = NULL;
315 for (i = 0; i < nfds; i++) {
317 bit = 1L<<(i % (sizeof(int)*8));
318 offset = i / (sizeof(int)*8);
319 if (rd[offset] & bit)
321 if (wr[offset] & bit)
323 if (ex[offset] & bit)
327 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
329 for (i = 0; i < nfds; i++) {
331 bit = 1L<<(i % (sizeof(int)*8));
332 offset = i / (sizeof(int)*8);
333 if (rd[offset] & bit) {
334 if (!__WSAFDIsSet(fd, &nrd))
337 if (wr[offset] & bit) {
338 if (!__WSAFDIsSet(fd, &nwr))
341 if (ex[offset] & bit) {
342 if (!__WSAFDIsSet(fd, &nex))
350 win32_send(SOCKET s, const char *buf, int len, int flags)
354 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
359 win32_sendto(SOCKET s, const char *buf, int len, int flags,
360 const struct sockaddr *to, int tolen)
364 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
369 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
373 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
378 win32_shutdown(SOCKET s, int how)
382 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
387 win32_closesocket(SOCKET s)
391 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
396 win32_socket(int af, int type, int protocol)
400 #ifndef USE_SOCKETS_AS_HANDLES
401 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
404 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
405 errno = WSAGetLastError();
408 #endif /* USE_SOCKETS_AS_HANDLES */
418 if (!wsock_started) /* No WinSock? */
419 return(fclose(pf)); /* Then not a socket. */
420 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
421 retval = fclose(pf); /* Must fclose() before closesocket() */
423 && closesocket(osf) == SOCKET_ERROR
424 && WSAGetLastError() != WSAENOTSOCK)
432 win32_gethostbyaddr(const char *addr, int len, int type)
436 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
441 win32_gethostbyname(const char *name)
445 SOCKET_TEST(r = gethostbyname(name), NULL);
450 win32_gethostname(char *name, int len)
454 SOCKET_TEST_ERROR(r = gethostname(name, len));
459 win32_getprotobyname(const char *name)
463 SOCKET_TEST(r = getprotobyname(name), NULL);
468 win32_getprotobynumber(int num)
472 SOCKET_TEST(r = getprotobynumber(num), NULL);
477 win32_getservbyname(const char *name, const char *proto)
481 SOCKET_TEST(r = getservbyname(name, proto), NULL);
483 r = win32_savecopyservent(&myservent, r, proto);
489 win32_getservbyport(int port, const char *proto)
493 SOCKET_TEST(r = getservbyport(port, proto), NULL);
495 r = win32_savecopyservent(&myservent, r, proto);
501 win32_inet_ntoa(struct in_addr in)
504 return inet_ntoa(in);
508 win32_inet_addr(const char FAR *cp)
511 return inet_addr(cp);
521 croak("endhostent not implemented!\n");
527 croak("endnetent not implemented!\n");
533 croak("endprotoent not implemented!\n");
539 croak("endservent not implemented!\n");
544 win32_getnetent(void)
546 croak("getnetent not implemented!\n");
547 return (struct netent *) NULL;
551 win32_getnetbyname(char *name)
553 croak("getnetbyname not implemented!\n");
554 return (struct netent *)NULL;
558 win32_getnetbyaddr(long net, int type)
560 croak("getnetbyaddr not implemented!\n");
561 return (struct netent *)NULL;
565 win32_getprotoent(void)
567 croak("getprotoent not implemented!\n");
568 return (struct protoent *) NULL;
572 win32_getservent(void)
574 croak("getservent not implemented!\n");
575 return (struct servent *) NULL;
579 win32_sethostent(int stayopen)
581 croak("sethostent not implemented!\n");
586 win32_setnetent(int stayopen)
588 croak("setnetent not implemented!\n");
593 win32_setprotoent(int stayopen)
595 croak("setprotoent not implemented!\n");
600 win32_setservent(int stayopen)
602 croak("setservent not implemented!\n");
605 #define WIN32IO_IS_STDIO
611 #include "win32iop.h"
616 static struct servent*
617 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
619 d->s_name = s->s_name;
620 d->s_aliases = s->s_aliases;
621 d->s_port = s->s_port;
622 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
623 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
624 d->s_proto = s->s_proto;
627 if (proto && strlen(proto))
628 d->s_proto = (char *)proto;