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 WIN32_LEAN_AND_MEAN
16 #include <sys/socket.h>
48 #undef getprotobynumber
66 /* thanks to Beverly Brown (beverly@datacube.com) */
67 #ifdef USE_SOCKETS_AS_HANDLES
68 # define OPEN_SOCKET(x) _open_osfhandle(x,O_RDWR|O_BINARY)
69 # define TO_SOCKET(x) _get_osfhandle(x)
71 # define OPEN_SOCKET(x) (x)
72 # define TO_SOCKET(x) (x)
73 #endif /* USE_SOCKETS_AS_HANDLES */
75 #define StartSockets() \
81 #define EndSockets() \
87 #define SOCKET_TEST(x, y) \
91 errno = WSAGetLastError(); \
94 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
96 static struct servent* win32_savecopyservent(struct servent*d,
100 __declspec(thread) struct servent myservent;
101 static int wsock_started = 0;
106 unsigned short version;
109 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
112 * initalize the winsock interface and insure that it is
113 * cleaned up at exit.
116 if(ret = WSAStartup(version, &retdata))
117 croak("Unable to locate winsock library!\n");
118 if(retdata.wVersion != version)
119 croak("Could not find version 1.1 of winsock dll\n");
121 /* atexit((void (*)(void)) EndSockets); */
123 #ifdef USE_SOCKETS_AS_HANDLES
125 * Enable the use of sockets as filehandles
127 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
128 (char *)&iSockOpt, sizeof(iSockOpt));
129 #endif /* USE_SOCKETS_AS_HANDLES */
134 #ifndef USE_SOCKETS_AS_HANDLES
136 myfdopen(int fd, char *mode)
140 int optlen = sizeof(sockbuf);
144 return(fdopen(fd, mode));
146 retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
147 if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
148 return(fdopen(fd, mode));
152 * If we get here, then fd is actually a socket.
154 Newz(1310, fp, 1, FILE);
168 #endif /* USE_SOCKETS_AS_HANDLES */
172 win32_htonl(u_long hostlong)
175 return htonl(hostlong);
179 win32_htons(u_short hostshort)
182 return htons(hostshort);
186 win32_ntohl(u_long netlong)
189 return ntohl(netlong);
193 win32_ntohs(u_short netshort)
196 return ntohs(netshort);
202 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
206 SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
207 return OPEN_SOCKET(r);
211 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
215 SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
220 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
224 SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
230 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
234 SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
239 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
243 SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
248 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
252 SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
257 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
261 SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
266 win32_listen(SOCKET s, int backlog)
270 SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
275 win32_recv(SOCKET s, char *buf, int len, int flags)
279 SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
284 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
288 SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
292 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
294 win32_select(int nfds, int* rd, int* wr, int* ex, const struct timeval* timeout)
298 int i, fd, bit, offset;
299 FD_SET nrd, nwr, nex,*prd,*pwr,*pex;
302 rd = &dummy, prd = NULL;
306 wr = &dummy, pwr = NULL;
310 ex = &dummy, pex = NULL;
317 for (i = 0; i < nfds; i++) {
319 bit = 1L<<(i % (sizeof(int)*8));
320 offset = i / (sizeof(int)*8);
321 if (rd[offset] & bit)
323 if (wr[offset] & bit)
325 if (ex[offset] & bit)
329 SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
331 for (i = 0; i < nfds; i++) {
333 bit = 1L<<(i % (sizeof(int)*8));
334 offset = i / (sizeof(int)*8);
335 if (rd[offset] & bit) {
336 if (!__WSAFDIsSet(fd, &nrd))
339 if (wr[offset] & bit) {
340 if (!__WSAFDIsSet(fd, &nwr))
343 if (ex[offset] & bit) {
344 if (!__WSAFDIsSet(fd, &nex))
352 win32_send(SOCKET s, const char *buf, int len, int flags)
356 SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
361 win32_sendto(SOCKET s, const char *buf, int len, int flags,
362 const struct sockaddr *to, int tolen)
366 SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
371 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
375 SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
380 win32_shutdown(SOCKET s, int how)
384 SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
389 win32_closesocket(SOCKET s)
393 SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
398 win32_socket(int af, int type, int protocol)
402 #ifndef USE_SOCKETS_AS_HANDLES
403 SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
406 if((s = socket(af, type, protocol)) == INVALID_SOCKET)
407 errno = WSAGetLastError();
410 #endif /* USE_SOCKETS_AS_HANDLES */
420 if (!wsock_started) /* No WinSock? */
421 return(fclose(pf)); /* Then not a socket. */
422 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
423 retval = fclose(pf); /* Must fclose() before closesocket() */
425 && closesocket(osf) == SOCKET_ERROR
426 && WSAGetLastError() != WSAENOTSOCK)
434 win32_gethostbyaddr(const char *addr, int len, int type)
438 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
443 win32_gethostbyname(const char *name)
447 SOCKET_TEST(r = gethostbyname(name), NULL);
452 win32_gethostname(char *name, int len)
456 SOCKET_TEST_ERROR(r = gethostname(name, len));
461 win32_getprotobyname(const char *name)
465 SOCKET_TEST(r = getprotobyname(name), NULL);
470 win32_getprotobynumber(int num)
474 SOCKET_TEST(r = getprotobynumber(num), NULL);
479 win32_getservbyname(const char *name, const char *proto)
483 SOCKET_TEST(r = getservbyname(name, proto), NULL);
485 r = win32_savecopyservent(&myservent, r, proto);
491 win32_getservbyport(int port, const char *proto)
495 SOCKET_TEST(r = getservbyport(port, proto), NULL);
497 r = win32_savecopyservent(&myservent, r, proto);
503 win32_inet_ntoa(struct in_addr in)
506 return inet_ntoa(in);
510 win32_inet_addr(const char FAR *cp)
513 return inet_addr(cp);
523 croak("endhostent not implemented!\n");
529 croak("endnetent not implemented!\n");
535 croak("endprotoent not implemented!\n");
541 croak("endservent not implemented!\n");
546 win32_getnetent(void)
548 croak("getnetent not implemented!\n");
549 return (struct netent *) NULL;
553 win32_getnetbyname(char *name)
555 croak("getnetbyname not implemented!\n");
556 return (struct netent *)NULL;
560 win32_getnetbyaddr(long net, int type)
562 croak("getnetbyaddr not implemented!\n");
563 return (struct netent *)NULL;
567 win32_getprotoent(void)
569 croak("getprotoent not implemented!\n");
570 return (struct protoent *) NULL;
574 win32_getservent(void)
576 croak("getservent not implemented!\n");
577 return (struct servent *) NULL;
581 win32_sethostent(int stayopen)
583 croak("sethostent not implemented!\n");
588 win32_setnetent(int stayopen)
590 croak("setnetent not implemented!\n");
595 win32_setprotoent(int stayopen)
597 croak("setprotoent not implemented!\n");
602 win32_setservent(int stayopen)
604 croak("setservent not implemented!\n");
607 static struct servent*
608 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
610 d->s_name = s->s_name;
611 d->s_aliases = s->s_aliases;
612 d->s_port = s->s_port;
613 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
614 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
615 d->s_proto = s->s_proto;
618 if (proto && strlen(proto))
619 d->s_proto = (char *)proto;