2 Copyright (c) 2006 by Dan Kennedy.
3 Copyright (c) 2006 by Juliusz Chroboczek.
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 static int dummy
ATTRIBUTE((unused
));
29 #include "win32_compat.h"
42 #define bzero(a,b) memset((a),(0),(b))
45 /* Windows needs this header file for the implementation of inet_aton() */
48 int win32_inet_pton(int af
, const char * src
, void * dst
)
50 int temp
= sizeof(struct sockaddr_in
);
51 char *srcNonConst
= (char *)malloc(strlen(src
)+1);
52 strncpy(srcNonConst
,src
,strlen(src
));
53 WSAStringToAddress(srcNonConst
,af
,NULL
,(LPSOCKADDR
)dst
,&temp
);
58 * Check whether "cp" is a valid ascii representation of an Internet address
59 * and convert to a binary address. Returns 1 if the address is valid, 0 if
60 * not. This replaces inet_addr, the return value from which cannot
61 * distinguish between failure and a local broadcast address.
63 * This implementation of the standard inet_aton() function was copied
64 * (with trivial modifications) from the OpenBSD project.
67 win32_inet_aton(const char *cp
, struct in_addr
*addr
)
69 register unsigned int val
;
72 unsigned int parts
[4];
73 register unsigned int *pp
= parts
;
75 assert(sizeof(val
) == 4);
80 * Collect number up to ``.''.
81 * Values are specified as for C:
82 * 0x=hex, 0=octal, isdigit=decimal.
89 if(c
== 'x' || c
== 'X')
95 if(isascii(c
) && isdigit(c
)) {
96 val
= (val
* base
) + (c
- '0');
98 } else if(base
== 16 && isascii(c
) && isxdigit(c
)) {
100 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
109 * a.b.c (with c treated as 16 bits)
110 * a.b (with b treated as 24 bits)
120 * Check for trailing characters.
122 if(c
!= '\0' && (!isascii(c
) || !isspace(c
)))
125 * Concoct the address according to
126 * the number of parts specified.
132 return (0); /* initial nondigit */
134 case 1: /* a -- 32 bits */
137 case 2: /* a.b -- 8.24 bits */
138 if((val
> 0xffffff) || (parts
[0] > 0xff))
140 val
|= parts
[0] << 24;
143 case 3: /* a.b.c -- 8.8.16 bits */
144 if((val
> 0xffff) || (parts
[0] > 0xff) || (parts
[1] > 0xff))
146 val
|= (parts
[0] << 24) | (parts
[1] << 16);
149 case 4: /* a.b.c.d -- 8.8.8.8 bits */
150 if((val
> 0xff) || (parts
[0] > 0xff) ||
151 (parts
[1] > 0xff) || (parts
[2] > 0xff))
153 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
157 addr
->s_addr
= htonl(val
);
162 win32_sleep(unsigned int seconds
)
164 Sleep(seconds
* 1000);
168 int win32_poll(struct pollfd
*fds
, int nfsd
, int timeout
)
170 fd_set rfds
, wfds
, efds
;
176 if (fds
->events
& POLLIN
)
178 FD_SET(fds
->fd
, &rfds
);
180 if (fds
->events
& POLLOUT
)
182 FD_SET(fds
->fd
, &wfds
);
184 FD_SET(fds
->fd
, &efds
);
185 ret
= select(fds
->fd
+ 1, &rfds
, &wfds
, &efds
, NULL
);
188 if (FD_ISSET(fds
->fd
, &rfds
))
190 fds
->revents
|= POLLIN
;
193 if (FD_ISSET(fds
->fd
, &wfds
))
195 fds
->revents
|= POLLOUT
;
198 if (FD_ISSET(fds
->fd
, &efds
))
200 fds
->revents
|= POLLHUP
;
205 /*int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
207 struct timeval timeout, *toptr;
208 fd_set ifds, ofds, efds, *ip, *op;
211 // Set up the file-descriptor sets in ifds, ofds and efds.
215 for (i = 0, op = ip = 0; i < nfds; ++i) {
217 if(fds[i].events & (POLLIN|POLLPRI)) {
219 FD_SET(fds[i].fd, ip);
221 if(fds[i].events & POLLOUT) {
223 FD_SET(fds[i].fd, op);
225 FD_SET(fds[i].fd, &efds);
228 // Set up the timeval structure for the timeout parameter
233 timeout.tv_sec = timo / 1000;
234 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
238 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
239 (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
241 rc = select(0, ip, op, &efds, toptr);
243 printf("Exiting select rc=%d\n", rc);
250 for (i = 0; i < nfds; ++i) {
252 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
253 fds[i].revents |= POLLIN;
254 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
255 fds[i].revents |= POLLOUT;
256 if(FD_ISSET(fd, &efds))
257 // Some error was detected ... should be some way to know.
258 fds[i].revents |= POLLHUP;
260 printf("%d %d %d revent = %x\n",
261 FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),