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>
22 #ifdef USE_SOCKETS_AS_HANDLES
23 /* thanks to Beverly Brown (beverly@datacube.com) */
25 #define OPEN_SOCKET(x) _open_osfhandle(x,_O_RDWR|_O_BINARY)
26 #define TO_SOCKET(x) _get_osfhandle(x)
30 # define OPEN_SOCKET(x) (x)
31 # define TO_SOCKET(x) (x)
33 #endif /* USE_SOCKETS_AS_HANDLES */
35 static struct servent* win32_savecopyservent(struct servent*d,
38 #define SOCKETAPI PASCAL
40 typedef SOCKET (SOCKETAPI *LPSOCKACCEPT)(SOCKET, struct sockaddr *, int *);
41 typedef int (SOCKETAPI *LPSOCKBIND)(SOCKET, const struct sockaddr *, int);
42 typedef int (SOCKETAPI *LPSOCKCLOSESOCKET)(SOCKET);
43 typedef int (SOCKETAPI *LPSOCKCONNECT)(SOCKET, const struct sockaddr *, int);
44 typedef int (SOCKETAPI *LPSOCKIOCTLSOCKET)(SOCKET, long, u_long *);
45 typedef int (SOCKETAPI *LPSOCKGETPEERNAME)(SOCKET, struct sockaddr *, int *);
46 typedef int (SOCKETAPI *LPSOCKGETSOCKNAME)(SOCKET, struct sockaddr *, int *);
47 typedef int (SOCKETAPI *LPSOCKGETSOCKOPT)(SOCKET, int, int, char *, int *);
48 typedef u_long (SOCKETAPI *LPSOCKHTONL)(u_long);
49 typedef u_short (SOCKETAPI *LPSOCKHTONS)(u_short);
50 typedef int (SOCKETAPI *LPSOCKLISTEN)(SOCKET, int);
51 typedef u_long (SOCKETAPI *LPSOCKNTOHL)(u_long);
52 typedef u_short (SOCKETAPI *LPSOCKNTOHS)(u_short);
53 typedef int (SOCKETAPI *LPSOCKRECV)(SOCKET, char *, int, int);
54 typedef int (SOCKETAPI *LPSOCKRECVFROM)(SOCKET, char *, int, int, struct sockaddr *, int *);
55 typedef int (SOCKETAPI *LPSOCKSELECT)(int, fd_set *, fd_set *, fd_set *, const struct timeval *);
56 typedef int (SOCKETAPI *LPSOCKSEND)(SOCKET, const char *, int, int);
57 typedef int (SOCKETAPI *LPSOCKSENDTO)(SOCKET, const char *, int, int, const struct sockaddr *, int);
58 typedef int (SOCKETAPI *LPSOCKSETSOCKOPT)(SOCKET, int, int, const char *, int);
59 typedef int (SOCKETAPI *LPSOCKSHUTDOWN)(SOCKET, int);
60 typedef SOCKET (SOCKETAPI *LPSOCKSOCKET)(int, int, int);
61 typedef char FAR *(SOCKETAPI *LPSOCKINETNTOA)(struct in_addr in);
62 typedef unsigned long (SOCKETAPI *LPSOCKINETADDR)(const char FAR * cp);
65 /* Database function prototypes */
66 typedef struct hostent *(SOCKETAPI *LPSOCKGETHOSTBYADDR)(const char *, int, int);
67 typedef struct hostent *(SOCKETAPI *LPSOCKGETHOSTBYNAME)(const char *);
68 typedef int (SOCKETAPI *LPSOCKGETHOSTNAME)(char *, int);
69 typedef struct servent *(SOCKETAPI *LPSOCKGETSERVBYPORT)(int, const char *);
70 typedef struct servent *(SOCKETAPI *LPSOCKGETSERVBYNAME)(const char *, const char *);
71 typedef struct protoent *(SOCKETAPI *LPSOCKGETPROTOBYNUMBER)(int);
72 typedef struct protoent *(SOCKETAPI *LPSOCKGETPROTOBYNAME)(const char *);
74 /* Microsoft Windows Extension function prototypes */
75 typedef int (SOCKETAPI *LPSOCKWSASTARTUP)(unsigned short, LPWSADATA);
76 typedef int (SOCKETAPI *LPSOCKWSACLEANUP)(void);
77 typedef int (SOCKETAPI *LPSOCKWSAGETLASTERROR)(void);
78 typedef int (SOCKETAPI *LPWSAFDIsSet)(SOCKET, fd_set *);
80 static HINSTANCE hWinSockDll = 0;
81 /* extern CRITICAL_SECTION csSock; */
83 static LPSOCKACCEPT paccept = 0;
84 static LPSOCKBIND pbind = 0;
85 static LPSOCKCLOSESOCKET pclosesocket = 0;
86 static LPSOCKCONNECT pconnect = 0;
87 static LPSOCKIOCTLSOCKET pioctlsocket = 0;
88 static LPSOCKGETPEERNAME pgetpeername = 0;
89 static LPSOCKGETSOCKNAME pgetsockname = 0;
90 static LPSOCKGETSOCKOPT pgetsockopt = 0;
91 static LPSOCKHTONL phtonl = 0;
92 static LPSOCKHTONS phtons = 0;
93 static LPSOCKLISTEN plisten = 0;
94 static LPSOCKNTOHL pntohl = 0;
95 static LPSOCKNTOHS pntohs = 0;
96 static LPSOCKRECV precv = 0;
97 static LPSOCKRECVFROM precvfrom = 0;
98 static LPSOCKSELECT pselect = 0;
99 static LPSOCKSEND psend = 0;
100 static LPSOCKSENDTO psendto = 0;
101 static LPSOCKSETSOCKOPT psetsockopt = 0;
102 static LPSOCKSHUTDOWN pshutdown = 0;
103 static LPSOCKSOCKET psocket = 0;
104 static LPSOCKGETHOSTBYADDR pgethostbyaddr = 0;
105 static LPSOCKGETHOSTBYNAME pgethostbyname = 0;
106 static LPSOCKGETHOSTNAME pgethostname = 0;
107 static LPSOCKGETSERVBYPORT pgetservbyport = 0;
108 static LPSOCKGETSERVBYNAME pgetservbyname = 0;
109 static LPSOCKGETPROTOBYNUMBER pgetprotobynumber = 0;
110 static LPSOCKGETPROTOBYNAME pgetprotobyname = 0;
111 static LPSOCKWSASTARTUP pWSAStartup = 0;
112 static LPSOCKWSACLEANUP pWSACleanup = 0;
113 static LPSOCKWSAGETLASTERROR pWSAGetLastError = 0;
114 static LPWSAFDIsSet pWSAFDIsSet = 0;
115 static LPSOCKINETNTOA pinet_ntoa = 0;
116 static LPSOCKINETADDR pinet_addr = 0;
118 __declspec(thread) struct servent myservent;
122 GetAddress(HINSTANCE hInstance, char *lpFunctionName)
124 FARPROC proc = GetProcAddress(hInstance, lpFunctionName);
126 CROAK("Unable to get address of %s in WSock32.dll", lpFunctionName);
133 /* EnterCriticalSection(&csSock); */
134 if(hWinSockDll == NULL) {
135 HINSTANCE hLib = LoadLibrary("WSock32.DLL");
137 CROAK("Could not load WSock32.dll\n");
139 paccept = (LPSOCKACCEPT)GetAddress(hLib, "accept");
140 pbind = (LPSOCKBIND)GetAddress(hLib, "bind");
141 pclosesocket = (LPSOCKCLOSESOCKET)GetAddress(hLib, "closesocket");
142 pconnect = (LPSOCKCONNECT)GetAddress(hLib, "connect");
143 pioctlsocket = (LPSOCKIOCTLSOCKET)GetAddress(hLib, "ioctlsocket");
144 pgetpeername = (LPSOCKGETPEERNAME)GetAddress(hLib, "getpeername");
145 pgetsockname = (LPSOCKGETSOCKNAME)GetAddress(hLib, "getsockname");
146 pgetsockopt = (LPSOCKGETSOCKOPT)GetAddress(hLib, "getsockopt");
147 phtonl = (LPSOCKHTONL)GetAddress(hLib, "htonl");
148 phtons = (LPSOCKHTONS)GetAddress(hLib, "htons");
149 plisten = (LPSOCKLISTEN)GetAddress(hLib, "listen");
150 pntohl = (LPSOCKNTOHL)GetAddress(hLib, "ntohl");
151 pntohs = (LPSOCKNTOHS)GetAddress(hLib, "ntohs");
152 precv = (LPSOCKRECV)GetAddress(hLib, "recv");
153 precvfrom = (LPSOCKRECVFROM)GetAddress(hLib, "recvfrom");
154 pselect = (LPSOCKSELECT)GetAddress(hLib, "select");
155 psend = (LPSOCKSEND)GetAddress(hLib, "send");
156 psendto = (LPSOCKSENDTO)GetAddress(hLib, "sendto");
157 psetsockopt = (LPSOCKSETSOCKOPT)GetAddress(hLib, "setsockopt");
158 pshutdown = (LPSOCKSHUTDOWN)GetAddress(hLib, "shutdown");
159 psocket = (LPSOCKSOCKET)GetAddress(hLib, "socket");
160 pgethostbyaddr = (LPSOCKGETHOSTBYADDR)GetAddress(hLib, "gethostbyaddr");
161 pgethostbyname = (LPSOCKGETHOSTBYNAME)GetAddress(hLib, "gethostbyname");
162 pgethostname = (LPSOCKGETHOSTNAME)GetAddress(hLib, "gethostname");
163 pgetservbyport = (LPSOCKGETSERVBYPORT)GetAddress(hLib, "getservbyport");
164 pgetservbyname = (LPSOCKGETSERVBYNAME)GetAddress(hLib, "getservbyname");
165 pgetprotobynumber = (LPSOCKGETPROTOBYNUMBER)GetAddress(hLib, "getprotobynumber");
166 pgetprotobyname = (LPSOCKGETPROTOBYNAME)GetAddress(hLib, "getprotobyname");
167 pWSAStartup = (LPSOCKWSASTARTUP)GetAddress(hLib, "WSAStartup");
168 pWSACleanup = (LPSOCKWSACLEANUP)GetAddress(hLib, "WSACleanup");
169 pWSAGetLastError = (LPSOCKWSAGETLASTERROR)GetAddress(hLib, "WSAGetLastError");
170 pWSAFDIsSet = (LPWSAFDIsSet)GetAddress(hLib, "__WSAFDIsSet");
171 pinet_addr = (LPSOCKINETADDR)GetAddress(hLib,"inet_addr");
172 pinet_ntoa = (LPSOCKINETNTOA)GetAddress(hLib,"inet_ntoa");
176 /* LeaveCriticalSection(&csSock); */
182 if(hWinSockDll != NULL) {
184 FreeLibrary(hWinSockDll);
192 unsigned short version;
195 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
199 * initalize the winsock interface and insure that it is
200 * cleaned up at exit.
203 if(ret = pWSAStartup(version, &retdata))
204 CROAK("Unable to locate winsock library!\n");
205 if(retdata.wVersion != version)
206 CROAK("Could not find version 1.1 of winsock dll\n");
208 /* atexit((void (*)(void)) EndSockets); */
210 #ifdef USE_SOCKETS_AS_HANDLES
212 * Enable the use of sockets as filehandles
214 psetsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
215 (char *)&iSockOpt, sizeof(iSockOpt));
216 #endif /* USE_SOCKETS_AS_HANDLES */
220 #ifndef USE_SOCKETS_AS_HANDLES
222 myfdopen(int fd, char *mode)
226 int optlen = sizeof(sockbuf);
229 if (hWinSockDll == 0)
232 retval = pgetsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
233 if(retval == SOCKET_ERROR && pWSAGetLastError() == WSAENOTSOCK) {
234 return(_fdopen(fd, mode));
238 * If we get here, then fd is actually a socket.
240 Newz(1310, fp, 1, FILE);
254 #endif /* USE_SOCKETS_AS_HANDLES */
258 win32_htonl(u_long hostlong)
263 return phtonl(hostlong);
267 win32_htons(u_short hostshort)
272 return phtons(hostshort);
276 win32_ntohl(u_long netlong)
281 return pntohl(netlong);
285 win32_ntohs(u_short netshort)
290 return pntohs(netshort);
294 #define SOCKET_TEST(x, y) if(hWinSockDll == 0) StartSockets();\
295 if((x) == (y)) errno = pWSAGetLastError()
297 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
300 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
304 SOCKET_TEST((r = paccept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
305 return OPEN_SOCKET(r);
309 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
313 SOCKET_TEST_ERROR(r = pbind(TO_SOCKET(s), addr, addrlen));
318 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
322 SOCKET_TEST_ERROR(r = pconnect(TO_SOCKET(s), addr, addrlen));
328 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
332 SOCKET_TEST_ERROR(r = pgetpeername(TO_SOCKET(s), addr, addrlen));
337 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
341 SOCKET_TEST_ERROR(r = pgetsockname(TO_SOCKET(s), addr, addrlen));
346 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
350 SOCKET_TEST_ERROR(r = pgetsockopt(TO_SOCKET(s), level, optname, optval, optlen));
355 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
359 SOCKET_TEST_ERROR(r = pioctlsocket(TO_SOCKET(s), cmd, argp));
364 win32_listen(SOCKET s, int backlog)
368 SOCKET_TEST_ERROR(r = plisten(TO_SOCKET(s), backlog));
373 win32_recv(SOCKET s, char *buf, int len, int flags)
377 SOCKET_TEST_ERROR(r = precv(TO_SOCKET(s), buf, len, flags));
382 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
386 SOCKET_TEST_ERROR(r = precvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
390 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
392 win32_select(int nfds, int* rd, int* wr, int* ex, const struct timeval* timeout)
396 int i, fd, bit, offset;
397 FD_SET nrd, nwr, nex,*prd,*pwr,*pex;
400 rd = &dummy, prd = NULL;
404 wr = &dummy, pwr = NULL;
408 ex = &dummy, pex = NULL;
415 for (i = 0; i < nfds; i++) {
417 bit = 1L<<(i % (sizeof(int)*8));
418 offset = i / (sizeof(int)*8);
419 if (rd[offset] & bit)
421 if (wr[offset] & bit)
423 if (ex[offset] & bit)
427 SOCKET_TEST_ERROR(r = pselect(nfds, prd, pwr, pex, timeout));
429 for (i = 0; i < nfds; i++) {
431 bit = 1L<<(i % (sizeof(int)*8));
432 offset = i / (sizeof(int)*8);
433 if (rd[offset] & bit) {
434 if (!pWSAFDIsSet(fd, &nrd))
437 if (wr[offset] & bit) {
438 if (!pWSAFDIsSet(fd, &nwr))
441 if (ex[offset] & bit) {
442 if (!pWSAFDIsSet(fd, &nex))
450 win32_send(SOCKET s, const char *buf, int len, int flags)
454 SOCKET_TEST_ERROR(r = psend(TO_SOCKET(s), buf, len, flags));
459 win32_sendto(SOCKET s, const char *buf, int len, int flags,
460 const struct sockaddr *to, int tolen)
464 SOCKET_TEST_ERROR(r = psendto(TO_SOCKET(s), buf, len, flags, to, tolen));
469 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
473 SOCKET_TEST_ERROR(r = psetsockopt(TO_SOCKET(s), level, optname, optval, optlen));
478 win32_shutdown(SOCKET s, int how)
482 SOCKET_TEST_ERROR(r = pshutdown(TO_SOCKET(s), how));
487 win32_socket(int af, int type, int protocol)
491 #ifndef USE_SOCKETS_AS_HANDLES
492 SOCKET_TEST(s = psocket(af, type, protocol), INVALID_SOCKET);
497 if((s = psocket(af, type, protocol)) == INVALID_SOCKET)
498 errno = pWSAGetLastError();
501 #endif /* USE_SOCKETS_AS_HANDLES */
507 win32_gethostbyaddr(const char *addr, int len, int type)
511 SOCKET_TEST(r = pgethostbyaddr(addr, len, type), NULL);
516 win32_gethostbyname(const char *name)
520 SOCKET_TEST(r = pgethostbyname(name), NULL);
525 win32_gethostname(char *name, int len)
529 SOCKET_TEST_ERROR(r = pgethostname(name, len));
534 win32_getprotobyname(const char *name)
538 SOCKET_TEST(r = pgetprotobyname(name), NULL);
543 win32_getprotobynumber(int num)
547 SOCKET_TEST(r = pgetprotobynumber(num), NULL);
552 win32_getservbyname(const char *name, const char *proto)
556 SOCKET_TEST(r = pgetservbyname(name, proto), NULL);
558 r = win32_savecopyservent(&myservent, r, proto);
564 win32_getservbyport(int port, const char *proto)
568 SOCKET_TEST(r = pgetservbyport(port, proto), NULL);
570 r = win32_savecopyservent(&myservent, r, proto);
576 win32_inet_ntoa(struct in_addr in)
581 return pinet_ntoa(in);
585 win32_inet_addr(const char FAR *cp)
590 return pinet_addr(cp);
603 CROAK("endhostent not implemented!\n");
609 CROAK("endnetent not implemented!\n");
615 CROAK("endprotoent not implemented!\n");
621 CROAK("endservent not implemented!\n");
626 win32_getnetent(void)
628 CROAK("getnetent not implemented!\n");
629 return (struct netent *) NULL;
633 win32_getnetbyname(char *name)
635 CROAK("getnetbyname not implemented!\n");
636 return (struct netent *)NULL;
640 win32_getnetbyaddr(long net, int type)
642 CROAK("getnetbyaddr not implemented!\n");
643 return (struct netent *)NULL;
647 win32_getprotoent(void)
649 CROAK("getprotoent not implemented!\n");
650 return (struct protoent *) NULL;
654 win32_getservent(void)
656 CROAK("getservent not implemented!\n");
657 return (struct servent *) NULL;
661 win32_sethostent(int stayopen)
663 CROAK("sethostent not implemented!\n");
668 win32_setnetent(int stayopen)
670 CROAK("setnetent not implemented!\n");
675 win32_setprotoent(int stayopen)
677 CROAK("setprotoent not implemented!\n");
682 win32_setservent(int stayopen)
684 CROAK("setservent not implemented!\n");
687 #define WIN32IO_IS_STDIO
689 #include "win32iop.h"
691 static struct servent*
692 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
694 d->s_name = s->s_name;
695 d->s_aliases = s->s_aliases;
696 d->s_port = s->s_port;
697 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
698 d->s_proto = s->s_proto;
699 else if (proto && strlen(proto))
700 d->s_proto = (char *)proto;