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
28 static int dummy
ATTRIBUTE((unused
));
43 /* Windows needs this header file for the implementation of inet_aton() */
47 * Check whether "cp" is a valid ascii representation of an Internet address
48 * and convert to a binary address. Returns 1 if the address is valid, 0 if
49 * not. This replaces inet_addr, the return value from which cannot
50 * distinguish between failure and a local broadcast address.
52 * This implementation of the standard inet_aton() function was copied
53 * (with trivial modifications) from the OpenBSD project.
56 mingw_inet_aton(const char *cp
, struct in_addr
*addr
)
58 register unsigned int val
;
61 unsigned int parts
[4];
62 register unsigned int *pp
= parts
;
64 assert(sizeof(val
) == 4);
69 * Collect number up to ``.''.
70 * Values are specified as for C:
71 * 0x=hex, 0=octal, isdigit=decimal.
78 if(c
== 'x' || c
== 'X')
84 if(isascii(c
) && isdigit(c
)) {
85 val
= (val
* base
) + (c
- '0');
87 } else if(base
== 16 && isascii(c
) && isxdigit(c
)) {
89 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
98 * a.b.c (with c treated as 16 bits)
99 * a.b (with b treated as 24 bits)
109 * Check for trailing characters.
111 if(c
!= '\0' && (!isascii(c
) || !isspace(c
)))
114 * Concoct the address according to
115 * the number of parts specified.
121 return (0); /* initial nondigit */
123 case 1: /* a -- 32 bits */
126 case 2: /* a.b -- 8.24 bits */
127 if((val
> 0xffffff) || (parts
[0] > 0xff))
129 val
|= parts
[0] << 24;
132 case 3: /* a.b.c -- 8.8.16 bits */
133 if((val
> 0xffff) || (parts
[0] > 0xff) || (parts
[1] > 0xff))
135 val
|= (parts
[0] << 24) | (parts
[1] << 16);
138 case 4: /* a.b.c.d -- 8.8.8.8 bits */
139 if((val
> 0xff) || (parts
[0] > 0xff) ||
140 (parts
[1] > 0xff) || (parts
[2] > 0xff))
142 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
146 addr
->s_addr
= htonl(val
);
151 mingw_sleep(unsigned int seconds
)
153 Sleep(seconds
* 1000);
158 mingw_gettimeofday(struct timeval
*tv
, char *tz
)
160 const long long EPOCHFILETIME
= (116444736000000000LL);
165 /* This implementation doesn't support the timezone parameter. That's Ok,
166 * as at present polipo always passed NULL as the second arg. We
167 * also need to make sure that we have at least 8 bytes of space to
168 * do the math in - otherwise there will be overflow errors.
171 assert(sizeof(t
) == 8);
174 GetSystemTimeAsFileTime(&ft
);
175 li
.LowPart
= ft
.dwLowDateTime
;
176 li
.HighPart
= ft
.dwHighDateTime
;
177 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
178 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
179 t
/= 10; /* In microseconds */
180 tv
->tv_sec
= (long)(t
/ 1000000);
181 tv
->tv_usec
= (long)(t
% 1000000);
186 int mingw_poll(struct pollfd
*fds
, unsigned int nfds
, int timo
)
188 struct timeval timeout
, *toptr
;
189 fd_set ifds
, ofds
, efds
, *ip
, *op
;
192 /* Set up the file-descriptor sets in ifds, ofds and efds. */
196 for (i
= 0, op
= ip
= 0; i
< nfds
; ++i
) {
198 if(fds
[i
].events
& (POLLIN
|POLLPRI
)) {
200 FD_SET(fds
[i
].fd
, ip
);
202 if(fds
[i
].events
& POLLOUT
) {
204 FD_SET(fds
[i
].fd
, op
);
206 FD_SET(fds
[i
].fd
, &efds
);
209 /* Set up the timeval structure for the timeout parameter */
214 timeout
.tv_sec
= timo
/ 1000;
215 timeout
.tv_usec
= (timo
- timeout
.tv_sec
* 1000) * 1000;
219 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
220 (long)timeout
.tv_sec
, (long)timeout
.tv_usec
, (long)ip
, (long)op
);
222 rc
= select(0, ip
, op
, &efds
, toptr
);
224 printf("Exiting select rc=%d\n", rc
);
231 for (i
= 0; i
< nfds
; ++i
) {
233 if(fds
[i
].events
& (POLLIN
|POLLPRI
) && FD_ISSET(fd
, &ifds
))
234 fds
[i
].revents
|= POLLIN
;
235 if(fds
[i
].events
& POLLOUT
&& FD_ISSET(fd
, &ofds
))
236 fds
[i
].revents
|= POLLOUT
;
237 if(FD_ISSET(fd
, &efds
))
238 /* Some error was detected ... should be some way to know. */
239 fds
[i
].revents
|= POLLHUP
;
241 printf("%d %d %d revent = %x\n",
242 FD_ISSET(fd
, &ifds
), FD_ISSET(fd
, &ofds
), FD_ISSET(fd
, &efds
),
251 int mingw_close_socket(SOCKET fd
) {
254 rc
= closesocket(fd
);
259 set_errno(int winsock_err
)
261 switch(winsock_err
) {
271 int mingw_write_socket(SOCKET fd
, void *buf
, int n
)
273 int rc
= send(fd
, buf
, n
, 0);
274 if(rc
== SOCKET_ERROR
) {
275 set_errno(WSAGetLastError());
280 int mingw_read_socket(SOCKET fd
, void *buf
, int n
)
282 int rc
= recv(fd
, buf
, n
, 0);
283 if(rc
== SOCKET_ERROR
) {
284 set_errno(WSAGetLastError());
291 * Set the "non-blocking" flag on socket fd to the value specified by
292 * the second argument (i.e. if the nonblocking argument is non-zero, the
293 * socket is set to non-blocking mode). Zero is returned if the operation
294 * is successful, other -1.
297 mingw_setnonblocking(SOCKET fd
, int nonblocking
)
301 unsigned long mode
= 1;
302 rc
= ioctlsocket(fd
, FIONBIO
, &mode
);
304 set_errno(WSAGetLastError());
306 return (rc
== 0 ? 0 : -1);
310 * A wrapper around the socket() function. The purpose of this wrapper
311 * is to ensure that the global errno symbol is set if an error occurs,
312 * even if we are using winsock.
315 mingw_socket(int domain
, int type
, int protocol
)
317 SOCKET fd
= socket(domain
, type
, protocol
);
318 if(fd
== INVALID_SOCKET
) {
319 set_errno(WSAGetLastError());
325 set_connect_errno(int winsock_err
)
327 switch(winsock_err
) {
340 * A wrapper around the connect() function. The purpose of this wrapper
341 * is to ensure that the global errno symbol is set if an error occurs,
342 * even if we are using winsock.
345 mingw_connect(SOCKET fd
, struct sockaddr
*addr
, socklen_t addr_len
)
347 int rc
= connect(fd
, addr
, addr_len
);
348 assert(rc
== 0 || rc
== SOCKET_ERROR
);
349 if(rc
== SOCKET_ERROR
) {
350 set_connect_errno(WSAGetLastError());
356 * A wrapper around the accept() function. The purpose of this wrapper
357 * is to ensure that the global errno symbol is set if an error occurs,
358 * even if we are using winsock.
361 mingw_accept(SOCKET fd
, struct sockaddr
*addr
, socklen_t
*addr_len
)
363 SOCKET newfd
= accept(fd
, addr
, addr_len
);
364 if(newfd
== INVALID_SOCKET
) {
365 set_errno(WSAGetLastError());
372 * A wrapper around the shutdown() function. The purpose of this wrapper
373 * is to ensure that the global errno symbol is set if an error occurs,
374 * even if we are using winsock.
377 mingw_shutdown(SOCKET fd
, int mode
)
379 int rc
= shutdown(fd
, mode
);
380 assert(rc
== 0 || rc
== SOCKET_ERROR
);
381 if(rc
== SOCKET_ERROR
) {
382 set_errno(WSAGetLastError());
388 * A wrapper around the getpeername() function. The purpose of this wrapper
389 * is to ensure that the global errno symbol is set if an error occurs,
390 * even if we are using winsock.
393 mingw_getpeername(SOCKET fd
, struct sockaddr
*name
, socklen_t
*namelen
)
395 int rc
= getpeername(fd
, name
, namelen
);
396 assert(rc
== 0 || rc
== SOCKET_ERROR
);
397 if(rc
== SOCKET_ERROR
) {
398 set_errno(WSAGetLastError());
403 /* Stat doesn't work on directories if the name ends in a slash. */
406 mingw_stat(const char *filename
, struct stat
*ss
)
408 int len
, rc
, saved_errno
;
411 len
= strlen(filename
);
412 if(len
<= 1 || filename
[len
- 1] != '/')
413 return stat(filename
, ss
);
415 noslash
= malloc(len
);
419 memcpy(noslash
, filename
, len
- 1);
420 noslash
[len
- 1] = '\0';
422 rc
= stat(noslash
, ss
);
428 #endif /* #ifdef MINGW */
430 #ifndef HAVE_READV_WRITEV
433 polipo_writev(int fd
, const struct iovec
*vector
, int count
)
435 int rc
; /* Return Code */
437 rc
= WRITE(fd
, vector
->iov_base
, vector
->iov_len
);
439 int n
= 0; /* Total bytes to write */
440 char *buf
= 0; /* Buffer to copy to before writing */
441 int i
; /* Counter var for looping over vector[] */
442 int offset
= 0; /* Offset for copying to buf */
444 /* Figure out the required buffer size */
445 for(i
= 0; i
< count
; i
++) {
446 n
+= vector
[i
].iov_len
;
449 /* Allocate the buffer. If the allocation fails, bail out */
456 /* Copy the contents of the vector array to the buffer */
457 for(i
= 0; i
< count
; i
++) {
458 memcpy(&buf
[offset
], vector
[i
].iov_base
, vector
[i
].iov_len
);
459 offset
+= vector
[i
].iov_len
;
463 /* Write the entire buffer to the socket and free the allocation */
464 rc
= WRITE(fd
, buf
, n
);
471 polipo_readv(int fd
, const struct iovec
*vector
, int count
)
473 int ret
= 0; /* Return value */
475 for(i
= 0; i
< count
; i
++) {
476 int n
= vector
[i
].iov_len
;
477 int rc
= READ(fd
, vector
[i
].iov_base
, n
);
482 ret
= (ret
== 0 ? rc
: ret
);