Simplify DNS error messages.
[polipo.git] / mingw.c
blob45986c8b6cde5c83545660fdcb5b4af97929c76b
1 /*
2 Copyright (c) 2006 by Dan Kennedy.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "polipo.h"
25 #ifndef MINGW
27 static int dummy ATTRIBUTE((unused));
29 #else
31 #undef poll
32 #undef socket
33 #undef connect
34 #undef accept
35 #undef shutdown
36 #undef getpeername
37 #undef sleep
38 #undef inet_aton
39 #undef gettimeofday
41 /* Windows needs this header file for the implementation of inet_aton() */
42 #include <ctype.h>
44 /*
45 * Check whether "cp" is a valid ascii representation of an Internet address
46 * and convert to a binary address. Returns 1 if the address is valid, 0 if
47 * not. This replaces inet_addr, the return value from which cannot
48 * distinguish between failure and a local broadcast address.
50 * This implementation of the standard inet_aton() function was copied
51 * (with trivial modifications) from the OpenBSD project.
53 int
54 mingw_inet_aton(const char *cp, struct in_addr *addr)
56 register unsigned int val;
57 register int base, n;
58 register char c;
59 unsigned int parts[4];
60 register unsigned int *pp = parts;
62 assert(sizeof(val) == 4);
64 c = *cp;
65 while(1) {
67 * Collect number up to ``.''.
68 * Values are specified as for C:
69 * 0x=hex, 0=octal, isdigit=decimal.
71 if(!isdigit(c))
72 return (0);
73 val = 0; base = 10;
74 if(c == '0') {
75 c = *++cp;
76 if(c == 'x' || c == 'X')
77 base = 16, c = *++cp;
78 else
79 base = 8;
81 while(1) {
82 if(isascii(c) && isdigit(c)) {
83 val = (val * base) + (c - '0');
84 c = *++cp;
85 } else if(base == 16 && isascii(c) && isxdigit(c)) {
86 val = (val << 4) |
87 (c + 10 - (islower(c) ? 'a' : 'A'));
88 c = *++cp;
89 } else
90 break;
92 if(c == '.') {
94 * Internet format:
95 * a.b.c.d
96 * a.b.c (with c treated as 16 bits)
97 * a.b (with b treated as 24 bits)
99 if(pp >= parts + 3)
100 return (0);
101 *pp++ = val;
102 c = *++cp;
103 } else
104 break;
107 * Check for trailing characters.
109 if(c != '\0' && (!isascii(c) || !isspace(c)))
110 return (0);
112 * Concoct the address according to
113 * the number of parts specified.
115 n = pp - parts + 1;
116 switch(n) {
118 case 0:
119 return (0); /* initial nondigit */
121 case 1: /* a -- 32 bits */
122 break;
124 case 2: /* a.b -- 8.24 bits */
125 if((val > 0xffffff) || (parts[0] > 0xff))
126 return (0);
127 val |= parts[0] << 24;
128 break;
130 case 3: /* a.b.c -- 8.8.16 bits */
131 if((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
132 return (0);
133 val |= (parts[0] << 24) | (parts[1] << 16);
134 break;
136 case 4: /* a.b.c.d -- 8.8.8.8 bits */
137 if((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
138 return (0);
139 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
140 break;
142 if(addr)
143 addr->s_addr = htonl(val);
144 return (1);
147 unsigned int
148 mingw_sleep(unsigned int seconds)
150 Sleep(seconds * 1000);
151 return 0;
155 mingw_gettimeofday(struct timeval *tv, char *tz)
157 const long long EPOCHFILETIME = (116444736000000000LL);
158 FILETIME ft;
159 LARGE_INTEGER li;
160 long long t;
162 /* This implementation doesn't support the timezone parameter. That's Ok,
163 * as at present polipo always passed NULL as the second arg. We
164 * also need to make sure that we have at least 8 bytes of space to
165 * do the math in - otherwise there will be overflow errors.
167 assert(tz == NULL);
168 assert(sizeof(t) == 8);
170 if(tv) {
171 GetSystemTimeAsFileTime(&ft);
172 li.LowPart = ft.dwLowDateTime;
173 li.HighPart = ft.dwHighDateTime;
174 t = li.QuadPart; /* In 100-nanosecond intervals */
175 t -= EPOCHFILETIME; /* Offset to the Epoch time */
176 t /= 10; /* In microseconds */
177 tv->tv_sec = (long)(t / 1000000);
178 tv->tv_usec = (long)(t % 1000000);
180 return 0;
183 int mingw_poll(struct pollfd *fds, unsigned int nfds, int timo)
185 struct timeval timeout, *toptr;
186 fd_set ifds, ofds, efds, *ip, *op;
187 int i, rc;
189 /* Set up the file-descriptor sets in ifds, ofds and efds. */
190 FD_ZERO(&ifds);
191 FD_ZERO(&ofds);
192 FD_ZERO(&efds);
193 for (i = 0, op = ip = 0; i < nfds; ++i) {
194 fds[i].revents = 0;
195 if(fds[i].events & (POLLIN|POLLPRI)) {
196 ip = &ifds;
197 FD_SET(fds[i].fd, ip);
199 if(fds[i].events & POLLOUT) {
200 op = &ofds;
201 FD_SET(fds[i].fd, op);
203 FD_SET(fds[i].fd, &efds);
206 /* Set up the timeval structure for the timeout parameter */
207 if(timo < 0) {
208 toptr = 0;
209 } else {
210 toptr = &timeout;
211 timeout.tv_sec = timo / 1000;
212 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
215 #ifdef DEBUG_POLL
216 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
217 (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
218 #endif
219 rc = select(0, ip, op, &efds, toptr);
220 #ifdef DEBUG_POLL
221 printf("Exiting select rc=%d\n", rc);
222 #endif
224 if(rc <= 0)
225 return rc;
227 if(rc > 0) {
228 for (i = 0; i < nfds; ++i) {
229 int fd = fds[i].fd;
230 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
231 fds[i].revents |= POLLIN;
232 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
233 fds[i].revents |= POLLOUT;
234 if(FD_ISSET(fd, &efds))
235 /* Some error was detected ... should be some way to know. */
236 fds[i].revents |= POLLHUP;
237 #ifdef DEBUG_POLL
238 printf("%d %d %d revent = %x\n",
239 FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
240 fds[i].revents
242 #endif
245 return rc;
248 int mingw_close_socket(SOCKET fd) {
249 int rc;
251 rc = closesocket(fd);
252 assert(rc == 0);
253 return 0;
256 static void
257 set_errno(int winsock_err)
259 switch(winsock_err) {
260 case WSAEWOULDBLOCK:
261 errno = EAGAIN;
262 break;
263 default:
264 errno = winsock_err;
265 break;
269 int mingw_write_socket(SOCKET fd, void *buf, int n)
271 int rc = send(fd, buf, n, 0);
272 if(rc == SOCKET_ERROR) {
273 set_errno(WSAGetLastError());
275 return rc;
278 int mingw_read_socket(SOCKET fd, void *buf, int n)
280 int rc = recv(fd, buf, n, 0);
281 if(rc == SOCKET_ERROR) {
282 set_errno(WSAGetLastError());
284 return rc;
289 * Set the "non-blocking" flag on socket fd to the value specified by
290 * the second argument (i.e. if the nonblocking argument is non-zero, the
291 * socket is set to non-blocking mode). Zero is returned if the operation
292 * is successful, other -1.
295 mingw_setnonblocking(SOCKET fd, int nonblocking)
297 int rc;
299 unsigned long mode = 1;
300 rc = ioctlsocket(fd, FIONBIO, &mode);
301 if(rc != 0) {
302 set_errno(WSAGetLastError());
304 return (rc == 0 ? 0 : -1);
308 * A wrapper around the socket() function. The purpose of this wrapper
309 * is to ensure that the global errno symbol is set if an error occurs,
310 * even if we are using winsock.
312 SOCKET
313 mingw_socket(int domain, int type, int protocol)
315 SOCKET fd = socket(domain, type, protocol);
316 if(fd == INVALID_SOCKET) {
317 set_errno(WSAGetLastError());
319 return fd;
322 static void
323 set_connect_errno(int winsock_err)
325 switch(winsock_err) {
326 case WSAEINVAL:
327 case WSAEALREADY:
328 case WSAEWOULDBLOCK:
329 errno = EINPROGRESS;
330 break;
331 default:
332 errno = winsock_err;
333 break;
338 * A wrapper around the connect() function. The purpose of this wrapper
339 * is to ensure that the global errno symbol is set if an error occurs,
340 * even if we are using winsock.
343 mingw_connect(SOCKET fd, struct sockaddr *addr, socklen_t addr_len)
345 int rc = connect(fd, addr, addr_len);
346 assert(rc == 0 || rc == SOCKET_ERROR);
347 if(rc == SOCKET_ERROR) {
348 set_connect_errno(WSAGetLastError());
350 return rc;
354 * A wrapper around the accept() function. The purpose of this wrapper
355 * is to ensure that the global errno symbol is set if an error occurs,
356 * even if we are using winsock.
358 SOCKET
359 mingw_accept(SOCKET fd, struct sockaddr *addr, socklen_t *addr_len)
361 SOCKET newfd = accept(fd, addr, addr_len);
362 if(newfd == INVALID_SOCKET) {
363 set_errno(WSAGetLastError());
364 newfd = -1;
366 return newfd;
370 * A wrapper around the shutdown() function. The purpose of this wrapper
371 * is to ensure that the global errno symbol is set if an error occurs,
372 * even if we are using winsock.
375 mingw_shutdown(SOCKET fd, int mode)
377 int rc = shutdown(fd, mode);
378 assert(rc == 0 || rc == SOCKET_ERROR);
379 if(rc == SOCKET_ERROR) {
380 set_errno(WSAGetLastError());
382 return rc;
386 * A wrapper around the getpeername() function. The purpose of this wrapper
387 * is to ensure that the global errno symbol is set if an error occurs,
388 * even if we are using winsock.
391 mingw_getpeername(SOCKET fd, struct sockaddr *name, socklen_t *namelen)
393 int rc = getpeername(fd, name, namelen);
394 assert(rc == 0 || rc == SOCKET_ERROR);
395 if(rc == SOCKET_ERROR) {
396 set_errno(WSAGetLastError());
398 return rc;
401 #endif /* #ifdef MINGW */
403 #ifndef HAVE_READV_WRITEV
406 polipo_writev(int fd, const struct iovec *vector, int count)
408 int rc; /* Return Code */
409 if(count == 1) {
410 rc = WRITE(fd, vector->iov_base, vector->iov_len);
411 } else {
412 int n = 0; /* Total bytes to write */
413 char *buf = 0; /* Buffer to copy to before writing */
414 int i; /* Counter var for looping over vector[] */
415 int offset = 0; /* Offset for copying to buf */
417 /* Figure out the required buffer size */
418 for(i = 0; i < count; i++) {
419 n += vector[i].iov_len;
422 /* Allocate the buffer. If the allocation fails, bail out */
423 buf = malloc(n);
424 if(!buf) {
425 errno = ENOMEM;
426 return -1;
429 /* Copy the contents of the vector array to the buffer */
430 for(i = 0; i < count; i++) {
431 memcpy(&buf[offset], vector[i].iov_base, vector[i].iov_len);
432 offset += vector[i].iov_len;
434 assert(offset == n);
436 /* Write the entire buffer to the socket and free the allocation */
437 rc = WRITE(fd, buf, n);
438 free(buf);
440 return rc;
444 polipo_readv(int fd, const struct iovec *vector, int count)
446 int ret = 0; /* Return value */
447 int i;
448 for(i = 0; i < count; i++) {
449 int n = vector[i].iov_len;
450 int rc = READ(fd, vector[i].iov_base, n);
451 if(rc == n) {
452 ret += rc;
453 } else {
454 if(rc < 0) {
455 ret = (ret == 0 ? rc : ret);
456 } else {
457 ret += rc;
459 break;
462 return ret;
464 #endif