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
);
52 int len
= strlen(src
) + 1;
53 wchar_t *srcNonConst
= (wchar_t *)malloc(len
);
54 memset(srcNonConst
, 0, len
);
55 MultiByteToWideChar(CP_ACP
, NULL
, src
, -1, srcNonConst
, len
);
57 if( WSAStringToAddress(srcNonConst
,af
,NULL
,(LPSOCKADDR
)dst
,&temp
) == 0 )
63 if( WSAGetLastError() == WSAEINVAL
)
73 * Check whether "cp" is a valid ascii representation of an Internet address
74 * and convert to a binary address. Returns 1 if the address is valid, 0 if
75 * not. This replaces inet_addr, the return value from which cannot
76 * distinguish between failure and a local broadcast address.
78 * This implementation of the standard inet_aton() function was copied
79 * (with trivial modifications) from the OpenBSD project.
82 win32_inet_aton(const char *cp
, struct in_addr
*addr
)
84 register unsigned int val
;
87 unsigned int parts
[4];
88 register unsigned int *pp
= parts
;
90 assert(sizeof(val
) == 4);
95 * Collect number up to ``.''.
96 * Values are specified as for C:
97 * 0x=hex, 0=octal, isdigit=decimal.
104 if(c
== 'x' || c
== 'X')
105 base
= 16, c
= *++cp
;
110 if(isascii(c
) && isdigit(c
)) {
111 val
= (val
* base
) + (c
- '0');
113 } else if(base
== 16 && isascii(c
) && isxdigit(c
)) {
115 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
124 * a.b.c (with c treated as 16 bits)
125 * a.b (with b treated as 24 bits)
135 * Check for trailing characters.
137 if(c
!= '\0' && (!isascii(c
) || !isspace(c
)))
140 * Concoct the address according to
141 * the number of parts specified.
147 return (0); /* initial nondigit */
149 case 1: /* a -- 32 bits */
152 case 2: /* a.b -- 8.24 bits */
153 if((val
> 0xffffff) || (parts
[0] > 0xff))
155 val
|= parts
[0] << 24;
158 case 3: /* a.b.c -- 8.8.16 bits */
159 if((val
> 0xffff) || (parts
[0] > 0xff) || (parts
[1] > 0xff))
161 val
|= (parts
[0] << 24) | (parts
[1] << 16);
164 case 4: /* a.b.c.d -- 8.8.8.8 bits */
165 if((val
> 0xff) || (parts
[0] > 0xff) ||
166 (parts
[1] > 0xff) || (parts
[2] > 0xff))
168 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
172 addr
->s_addr
= htonl(val
);
177 win32_sleep(unsigned int seconds
)
179 Sleep(seconds
* 1000);
183 int win32_poll(struct pollfd
*fds
, int nfsd
, int timeout
)
185 fd_set rfds
, wfds
, efds
;
191 if (fds
->events
& POLLIN
)
193 FD_SET(fds
->fd
, &rfds
);
195 if (fds
->events
& POLLOUT
)
197 FD_SET(fds
->fd
, &wfds
);
199 FD_SET(fds
->fd
, &efds
);
200 ret
= select(fds
->fd
+ 1, &rfds
, &wfds
, &efds
, NULL
);
203 if (FD_ISSET(fds
->fd
, &rfds
))
205 fds
->revents
|= POLLIN
;
208 if (FD_ISSET(fds
->fd
, &wfds
))
210 fds
->revents
|= POLLOUT
;
213 if (FD_ISSET(fds
->fd
, &efds
))
215 fds
->revents
|= POLLHUP
;
220 /*int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
222 struct timeval timeout, *toptr;
223 fd_set ifds, ofds, efds, *ip, *op;
226 // Set up the file-descriptor sets in ifds, ofds and efds.
230 for (i = 0, op = ip = 0; i < nfds; ++i) {
232 if(fds[i].events & (POLLIN|POLLPRI)) {
234 FD_SET(fds[i].fd, ip);
236 if(fds[i].events & POLLOUT) {
238 FD_SET(fds[i].fd, op);
240 FD_SET(fds[i].fd, &efds);
243 // Set up the timeval structure for the timeout parameter
248 timeout.tv_sec = timo / 1000;
249 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
253 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
254 (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
256 rc = select(0, ip, op, &efds, toptr);
258 printf("Exiting select rc=%d\n", rc);
265 for (i = 0; i < nfds; ++i) {
267 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
268 fds[i].revents |= POLLIN;
269 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
270 fds[i].revents |= POLLOUT;
271 if(FD_ISSET(fd, &efds))
272 // Some error was detected ... should be some way to know.
273 fds[i].revents |= POLLHUP;
275 printf("%d %d %d revent = %x\n",
276 FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),