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
27 static int dummy
ATTRIBUTE((unused
));
41 /* Windows needs this header file for the implementation of inet_aton() */
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.
54 mingw_inet_aton(const char *cp
, struct in_addr
*addr
)
56 register unsigned int val
;
59 unsigned int parts
[4];
60 register unsigned int *pp
= parts
;
62 assert(sizeof(val
) == 4);
67 * Collect number up to ``.''.
68 * Values are specified as for C:
69 * 0x=hex, 0=octal, isdigit=decimal.
76 if(c
== 'x' || c
== 'X')
82 if(isascii(c
) && isdigit(c
)) {
83 val
= (val
* base
) + (c
- '0');
85 } else if(base
== 16 && isascii(c
) && isxdigit(c
)) {
87 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
96 * a.b.c (with c treated as 16 bits)
97 * a.b (with b treated as 24 bits)
107 * Check for trailing characters.
109 if(c
!= '\0' && (!isascii(c
) || !isspace(c
)))
112 * Concoct the address according to
113 * the number of parts specified.
119 return (0); /* initial nondigit */
121 case 1: /* a -- 32 bits */
124 case 2: /* a.b -- 8.24 bits */
125 if((val
> 0xffffff) || (parts
[0] > 0xff))
127 val
|= parts
[0] << 24;
130 case 3: /* a.b.c -- 8.8.16 bits */
131 if((val
> 0xffff) || (parts
[0] > 0xff) || (parts
[1] > 0xff))
133 val
|= (parts
[0] << 24) | (parts
[1] << 16);
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))
139 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
143 addr
->s_addr
= htonl(val
);
148 mingw_sleep(unsigned int seconds
)
150 Sleep(seconds
* 1000);
155 mingw_gettimeofday(struct timeval
*tv
, char *tz
)
157 const long long EPOCHFILETIME
= (116444736000000000LL);
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.
168 assert(sizeof(t
) == 8);
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);
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
;
189 /* Set up the file-descriptor sets in ifds, ofds and efds. */
193 for (i
= 0, op
= ip
= 0; i
< nfds
; ++i
) {
195 if(fds
[i
].events
& (POLLIN
|POLLPRI
)) {
197 FD_SET(fds
[i
].fd
, ip
);
199 if(fds
[i
].events
& POLLOUT
) {
201 FD_SET(fds
[i
].fd
, op
);
203 FD_SET(fds
[i
].fd
, &efds
);
206 /* Set up the timeval structure for the timeout parameter */
211 timeout
.tv_sec
= timo
/ 1000;
212 timeout
.tv_usec
= (timo
- timeout
.tv_sec
* 1000) * 1000;
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
);
219 rc
= select(0, ip
, op
, &efds
, toptr
);
221 printf("Exiting select rc=%d\n", rc
);
228 for (i
= 0; i
< nfds
; ++i
) {
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
;
238 printf("%d %d %d revent = %x\n",
239 FD_ISSET(fd
, &ifds
), FD_ISSET(fd
, &ofds
), FD_ISSET(fd
, &efds
),
248 int mingw_close_socket(SOCKET fd
) {
251 rc
= closesocket(fd
);
257 set_errno(int winsock_err
)
259 switch(winsock_err
) {
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());
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());
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
)
299 unsigned long mode
= 1;
300 rc
= ioctlsocket(fd
, FIONBIO
, &mode
);
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.
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());
323 set_connect_errno(int winsock_err
)
325 switch(winsock_err
) {
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());
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.
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());
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());
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());
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 */
410 rc
= WRITE(fd
, vector
->iov_base
, vector
->iov_len
);
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 */
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
;
436 /* Write the entire buffer to the socket and free the allocation */
437 rc
= WRITE(fd
, buf
, n
);
444 polipo_readv(int fd
, const struct iovec
*vector
, int count
)
446 int ret
= 0; /* Return value */
448 for(i
= 0; i
< count
; i
++) {
449 int n
= vector
[i
].iov_len
;
450 int rc
= READ(fd
, vector
[i
].iov_base
, n
);
455 ret
= (ret
== 0 ? rc
: ret
);