Commit | Line | Data |
---|---|---|
7f799388 M |
1 | /* |
2 | Copyright (c) 2006 by Dan Kennedy. | |
3 | Copyright (c) 2006 by Juliusz Chroboczek. | |
4 | ||
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: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in | |
13 | all copies or substantial portions of the Software. | |
14 | ||
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 | |
21 | THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #ifndef WIN32 | |
25 | ||
26 | static int dummy ATTRIBUTE((unused)); | |
27 | ||
28 | #else | |
29 | #include "win32_compat.h" | |
30 | #include <errno.h> | |
31 | #include <stdio.h> | |
32 | #undef poll | |
33 | #undef socket | |
34 | #undef connect | |
35 | #undef accept | |
36 | #undef shutdown | |
37 | #undef getpeername | |
38 | #undef sleep | |
39 | #undef inet_aton | |
40 | #undef gettimeofday | |
41 | #undef stat | |
42 | #define bzero(a,b) memset((a),(0),(b)) | |
43 | #define assert(a) | |
44 | ||
45 | /* Windows needs this header file for the implementation of inet_aton() */ | |
46 | #include <ctype.h> | |
47 | ||
48 | int win32_inet_pton(int af, const char * src, void * dst) | |
49 | { | |
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); | |
54 | return temp; | |
55 | } | |
56 | ||
57 | /* | |
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. | |
62 | * | |
63 | * This implementation of the standard inet_aton() function was copied | |
64 | * (with trivial modifications) from the OpenBSD project. | |
65 | */ | |
66 | int | |
67 | win32_inet_aton(const char *cp, struct in_addr *addr) | |
68 | { | |
69 | register unsigned int val; | |
70 | register int base, n; | |
71 | register char c; | |
72 | unsigned int parts[4]; | |
73 | register unsigned int *pp = parts; | |
74 | ||
75 | assert(sizeof(val) == 4); | |
76 | ||
77 | c = *cp; | |
78 | while(1) { | |
79 | /* | |
80 | * Collect number up to ``.''. | |
81 | * Values are specified as for C: | |
82 | * 0x=hex, 0=octal, isdigit=decimal. | |
83 | */ | |
84 | if(!isdigit(c)) | |
85 | return (0); | |
86 | val = 0; base = 10; | |
87 | if(c == '0') { | |
88 | c = *++cp; | |
89 | if(c == 'x' || c == 'X') | |
90 | base = 16, c = *++cp; | |
91 | else | |
92 | base = 8; | |
93 | } | |
94 | while(1) { | |
95 | if(isascii(c) && isdigit(c)) { | |
96 | val = (val * base) + (c - '0'); | |
97 | c = *++cp; | |
98 | } else if(base == 16 && isascii(c) && isxdigit(c)) { | |
99 | val = (val << 4) | | |
100 | (c + 10 - (islower(c) ? 'a' : 'A')); | |
101 | c = *++cp; | |
102 | } else | |
103 | break; | |
104 | } | |
105 | if(c == '.') { | |
106 | /* | |
107 | * Internet format: | |
108 | * a.b.c.d | |
109 | * a.b.c (with c treated as 16 bits) | |
110 | * a.b (with b treated as 24 bits) | |
111 | */ | |
112 | if(pp >= parts + 3) | |
113 | return (0); | |
114 | *pp++ = val; | |
115 | c = *++cp; | |
116 | } else | |
117 | break; | |
118 | } | |
119 | /* | |
120 | * Check for trailing characters. | |
121 | */ | |
122 | if(c != '\0' && (!isascii(c) || !isspace(c))) | |
123 | return (0); | |
124 | /* | |
125 | * Concoct the address according to | |
126 | * the number of parts specified. | |
127 | */ | |
128 | n = pp - parts + 1; | |
129 | switch(n) { | |
130 | ||
131 | case 0: | |
132 | return (0); /* initial nondigit */ | |
133 | ||
134 | case 1: /* a -- 32 bits */ | |
135 | break; | |
136 | ||
137 | case 2: /* a.b -- 8.24 bits */ | |
138 | if((val > 0xffffff) || (parts[0] > 0xff)) | |
139 | return (0); | |
140 | val |= parts[0] << 24; | |
141 | break; | |
142 | ||
143 | case 3: /* a.b.c -- 8.8.16 bits */ | |
144 | if((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) | |
145 | return (0); | |
146 | val |= (parts[0] << 24) | (parts[1] << 16); | |
147 | break; | |
148 | ||
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)) | |
152 | return (0); | |
153 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | |
154 | break; | |
155 | } | |
156 | if(addr) | |
157 | addr->s_addr = htonl(val); | |
158 | return (1); | |
159 | } | |
160 | ||
161 | unsigned int | |
162 | win32_sleep(unsigned int seconds) | |
163 | { | |
164 | Sleep(seconds * 1000); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | int win32_poll(struct pollfd *fds, int nfsd, int timeout) | |
169 | { | |
170 | fd_set rfds, wfds, efds; | |
171 | int ret; | |
172 | ||
173 | FD_ZERO(&rfds); | |
174 | FD_ZERO(&wfds); | |
175 | FD_ZERO(&efds); | |
176 | if (fds->events & POLLIN) | |
177 | { | |
178 | FD_SET(fds->fd, &rfds); | |
179 | } | |
180 | if (fds->events & POLLOUT) | |
181 | { | |
182 | FD_SET(fds->fd, &wfds); | |
183 | } | |
184 | FD_SET(fds->fd, &efds); | |
185 | ret = select(fds->fd + 1, &rfds, &wfds, &efds, NULL); | |
186 | fds->revents = 0; | |
187 | ||
188 | if (FD_ISSET(fds->fd, &rfds)) | |
189 | { | |
190 | fds->revents |= POLLIN; | |
191 | } | |
192 | ||
193 | if (FD_ISSET(fds->fd, &wfds)) | |
194 | { | |
195 | fds->revents |= POLLOUT; | |
196 | } | |
197 | ||
198 | if (FD_ISSET(fds->fd, &efds)) | |
199 | { | |
200 | fds->revents |= POLLHUP; | |
201 | } | |
202 | return ret; | |
203 | } | |
204 | ||
205 | /*int win32_poll(struct pollfd *fds, unsigned int nfds, int timo) | |
206 | { | |
207 | struct timeval timeout, *toptr; | |
208 | fd_set ifds, ofds, efds, *ip, *op; | |
209 | int i, rc; | |
210 | ||
211 | // Set up the file-descriptor sets in ifds, ofds and efds. | |
212 | FD_ZERO(&ifds); | |
213 | FD_ZERO(&ofds); | |
214 | FD_ZERO(&efds); | |
215 | for (i = 0, op = ip = 0; i < nfds; ++i) { | |
216 | fds[i].revents = 0; | |
217 | if(fds[i].events & (POLLIN|POLLPRI)) { | |
218 | ip = &ifds; | |
219 | FD_SET(fds[i].fd, ip); | |
220 | } | |
221 | if(fds[i].events & POLLOUT) { | |
222 | op = &ofds; | |
223 | FD_SET(fds[i].fd, op); | |
224 | } | |
225 | FD_SET(fds[i].fd, &efds); | |
226 | } | |
227 | ||
228 | // Set up the timeval structure for the timeout parameter | |
229 | if(timo < 0) { | |
230 | toptr = 0; | |
231 | } else { | |
232 | toptr = &timeout; | |
233 | timeout.tv_sec = timo / 1000; | |
234 | timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000; | |
235 | } | |
236 | ||
237 | #ifdef DEBUG_POLL | |
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); | |
240 | #endif | |
241 | rc = select(0, ip, op, &efds, toptr); | |
242 | #ifdef DEBUG_POLL | |
243 | printf("Exiting select rc=%d\n", rc); | |
244 | #endif | |
245 | ||
246 | if(rc <= 0) | |
247 | return rc; | |
248 | ||
249 | if(rc > 0) { | |
250 | for (i = 0; i < nfds; ++i) { | |
251 | int fd = fds[i].fd; | |
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; | |
259 | #ifdef DEBUG_POLL | |
260 | printf("%d %d %d revent = %x\n", | |
261 | FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), | |
262 | fds[i].revents | |
263 | ); | |
264 | #endif | |
265 | } | |
266 | } | |
267 | return rc; | |
268 | } | |
269 | */ | |
270 | #endif |