| 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 | int ret = -1; |
| 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); |
| 56 | |
| 57 | if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)dst,&temp) == 0 ) |
| 58 | { |
| 59 | ret = 1; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | if( WSAGetLastError() == WSAEINVAL ) |
| 64 | { |
| 65 | ret = -1; |
| 66 | } |
| 67 | } |
| 68 | //free(srcNonConst); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | /* |
| 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. |
| 77 | * |
| 78 | * This implementation of the standard inet_aton() function was copied |
| 79 | * (with trivial modifications) from the OpenBSD project. |
| 80 | */ |
| 81 | int |
| 82 | win32_inet_aton(const char *cp, struct in_addr *addr) |
| 83 | { |
| 84 | register unsigned int val; |
| 85 | register int base, n; |
| 86 | register char c; |
| 87 | unsigned int parts[4]; |
| 88 | register unsigned int *pp = parts; |
| 89 | |
| 90 | assert(sizeof(val) == 4); |
| 91 | |
| 92 | c = *cp; |
| 93 | while(1) { |
| 94 | /* |
| 95 | * Collect number up to ``.''. |
| 96 | * Values are specified as for C: |
| 97 | * 0x=hex, 0=octal, isdigit=decimal. |
| 98 | */ |
| 99 | if(!isdigit(c)) |
| 100 | return (0); |
| 101 | val = 0; base = 10; |
| 102 | if(c == '0') { |
| 103 | c = *++cp; |
| 104 | if(c == 'x' || c == 'X') |
| 105 | base = 16, c = *++cp; |
| 106 | else |
| 107 | base = 8; |
| 108 | } |
| 109 | while(1) { |
| 110 | if(isascii(c) && isdigit(c)) { |
| 111 | val = (val * base) + (c - '0'); |
| 112 | c = *++cp; |
| 113 | } else if(base == 16 && isascii(c) && isxdigit(c)) { |
| 114 | val = (val << 4) | |
| 115 | (c + 10 - (islower(c) ? 'a' : 'A')); |
| 116 | c = *++cp; |
| 117 | } else |
| 118 | break; |
| 119 | } |
| 120 | if(c == '.') { |
| 121 | /* |
| 122 | * Internet format: |
| 123 | * a.b.c.d |
| 124 | * a.b.c (with c treated as 16 bits) |
| 125 | * a.b (with b treated as 24 bits) |
| 126 | */ |
| 127 | if(pp >= parts + 3) |
| 128 | return (0); |
| 129 | *pp++ = val; |
| 130 | c = *++cp; |
| 131 | } else |
| 132 | break; |
| 133 | } |
| 134 | /* |
| 135 | * Check for trailing characters. |
| 136 | */ |
| 137 | if(c != '\0' && (!isascii(c) || !isspace(c))) |
| 138 | return (0); |
| 139 | /* |
| 140 | * Concoct the address according to |
| 141 | * the number of parts specified. |
| 142 | */ |
| 143 | n = pp - parts + 1; |
| 144 | switch(n) { |
| 145 | |
| 146 | case 0: |
| 147 | return (0); /* initial nondigit */ |
| 148 | |
| 149 | case 1: /* a -- 32 bits */ |
| 150 | break; |
| 151 | |
| 152 | case 2: /* a.b -- 8.24 bits */ |
| 153 | if((val > 0xffffff) || (parts[0] > 0xff)) |
| 154 | return (0); |
| 155 | val |= parts[0] << 24; |
| 156 | break; |
| 157 | |
| 158 | case 3: /* a.b.c -- 8.8.16 bits */ |
| 159 | if((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) |
| 160 | return (0); |
| 161 | val |= (parts[0] << 24) | (parts[1] << 16); |
| 162 | break; |
| 163 | |
| 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)) |
| 167 | return (0); |
| 168 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); |
| 169 | break; |
| 170 | } |
| 171 | if(addr) |
| 172 | addr->s_addr = htonl(val); |
| 173 | return (1); |
| 174 | } |
| 175 | |
| 176 | unsigned int |
| 177 | win32_sleep(unsigned int seconds) |
| 178 | { |
| 179 | Sleep(seconds * 1000); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int win32_poll(struct pollfd *fds, int nfsd, int timeout) |
| 184 | { |
| 185 | fd_set rfds, wfds, efds; |
| 186 | int ret; |
| 187 | |
| 188 | FD_ZERO(&rfds); |
| 189 | FD_ZERO(&wfds); |
| 190 | FD_ZERO(&efds); |
| 191 | if (fds->events & POLLIN) |
| 192 | { |
| 193 | FD_SET(fds->fd, &rfds); |
| 194 | } |
| 195 | if (fds->events & POLLOUT) |
| 196 | { |
| 197 | FD_SET(fds->fd, &wfds); |
| 198 | } |
| 199 | FD_SET(fds->fd, &efds); |
| 200 | ret = select(fds->fd + 1, &rfds, &wfds, &efds, NULL); |
| 201 | fds->revents = 0; |
| 202 | |
| 203 | if (FD_ISSET(fds->fd, &rfds)) |
| 204 | { |
| 205 | fds->revents |= POLLIN; |
| 206 | } |
| 207 | |
| 208 | if (FD_ISSET(fds->fd, &wfds)) |
| 209 | { |
| 210 | fds->revents |= POLLOUT; |
| 211 | } |
| 212 | |
| 213 | if (FD_ISSET(fds->fd, &efds)) |
| 214 | { |
| 215 | fds->revents |= POLLHUP; |
| 216 | } |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /*int win32_poll(struct pollfd *fds, unsigned int nfds, int timo) |
| 221 | { |
| 222 | struct timeval timeout, *toptr; |
| 223 | fd_set ifds, ofds, efds, *ip, *op; |
| 224 | int i, rc; |
| 225 | |
| 226 | // Set up the file-descriptor sets in ifds, ofds and efds. |
| 227 | FD_ZERO(&ifds); |
| 228 | FD_ZERO(&ofds); |
| 229 | FD_ZERO(&efds); |
| 230 | for (i = 0, op = ip = 0; i < nfds; ++i) { |
| 231 | fds[i].revents = 0; |
| 232 | if(fds[i].events & (POLLIN|POLLPRI)) { |
| 233 | ip = &ifds; |
| 234 | FD_SET(fds[i].fd, ip); |
| 235 | } |
| 236 | if(fds[i].events & POLLOUT) { |
| 237 | op = &ofds; |
| 238 | FD_SET(fds[i].fd, op); |
| 239 | } |
| 240 | FD_SET(fds[i].fd, &efds); |
| 241 | } |
| 242 | |
| 243 | // Set up the timeval structure for the timeout parameter |
| 244 | if(timo < 0) { |
| 245 | toptr = 0; |
| 246 | } else { |
| 247 | toptr = &timeout; |
| 248 | timeout.tv_sec = timo / 1000; |
| 249 | timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000; |
| 250 | } |
| 251 | |
| 252 | #ifdef DEBUG_POLL |
| 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); |
| 255 | #endif |
| 256 | rc = select(0, ip, op, &efds, toptr); |
| 257 | #ifdef DEBUG_POLL |
| 258 | printf("Exiting select rc=%d\n", rc); |
| 259 | #endif |
| 260 | |
| 261 | if(rc <= 0) |
| 262 | return rc; |
| 263 | |
| 264 | if(rc > 0) { |
| 265 | for (i = 0; i < nfds; ++i) { |
| 266 | int fd = fds[i].fd; |
| 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; |
| 274 | #ifdef DEBUG_POLL |
| 275 | printf("%d %d %d revent = %x\n", |
| 276 | FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), |
| 277 | fds[i].revents |
| 278 | ); |
| 279 | #endif |
| 280 | } |
| 281 | } |
| 282 | return rc; |
| 283 | } |
| 284 | */ |
| 285 | #endif |