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
24 * Polipo was originally designed to run on Unix-like systems. This
25 * header file (and it's accompanying implementation file mingw.c) contain
26 * code that allows polipo to run on Microsoft Windows too.
28 * The target MS windows compiler is Mingw (MINimal Gnu for Windows). The
29 * code in this file probably get's us pretty close to MSVC also, but
30 * this has not been tested. To build polipo for Mingw, define the MINGW
31 * symbol. For Unix or Unix-like systems, leave it undefined.
36 /* Unfortunately, there's no hiding it. */
37 #define HAVE_WINSOCK 1
39 /* At time of writing, a fair bit of stuff doesn't work under Mingw.
40 * Hopefully they will be fixed later (especially the disk-cache).
46 #define S_IROTH S_IREAD
48 /* Pull in winsock.h for (almost) berkeley sockets. */
50 #define ENOTCONN WSAENOTCONN
51 #define EWOULDBLOCK WSAEWOULDBLOCK
52 #define ENOBUFS WSAENOBUFS
53 #define ECONNRESET WSAECONNRESET
54 #define ESHUTDOWN WSAESHUTDOWN
55 #define EAFNOSUPPORT WSAEAFNOSUPPORT
56 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
57 #define EINPROGRESS WSAEINPROGRESS
58 #define EISCONN WSAEISCONN
60 /* winsock doesn't feature poll(), so there is a version implemented
61 * in terms of select() in mingw.c. The following definitions
62 * are copied from linux man pages. A poll() macro is defined to
63 * call the version in mingw.c.
65 #define POLLIN 0x0001 /* There is data to read */
66 #define POLLPRI 0x0002 /* There is urgent data to read */
67 #define POLLOUT 0x0004 /* Writing now will not block */
68 #define POLLERR 0x0008 /* Error condition */
69 #define POLLHUP 0x0010 /* Hung up */
70 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
72 SOCKET fd
; /* file descriptor */
73 short events
; /* requested events */
74 short revents
; /* returned events */
76 #define poll(x, y, z) mingw_poll(x, y, z)
78 /* These wrappers do nothing special except set the global errno variable if
79 * an error occurs (winsock doesn't do this by default). They set errno
80 * to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN), so code
81 * outside of this file "shouldn't" have to worry about winsock specific error
84 #define socket(x, y, z) mingw_socket(x, y, z)
85 #define connect(x, y, z) mingw_connect(x, y, z)
86 #define accept(x, y, z) mingw_accept(x, y, z)
87 #define shutdown(x, y) mingw_shutdown(x, y)
88 #define getpeername(x, y, z) mingw_getpeername(x, y, z)
90 /* Wrapper macros to call misc. functions mingw is missing */
91 #define sleep(x) mingw_sleep(x)
92 #define inet_aton(x, y) mingw_inet_aton(x, y)
93 #define gettimeofday(x, y) mingw_gettimeofday(x, y)
95 #define mkdir(x, y) mkdir(x)
97 /* Winsock uses int instead of the usual socklen_t */
98 typedef int socklen_t
;
100 /* Function prototypes for functions in mingw.c */
101 unsigned int mingw_sleep(unsigned int);
102 int mingw_inet_aton(const char *, struct in_addr
*);
103 int mingw_gettimeofday(struct timeval
*, char *);
104 int mingw_poll(struct pollfd
*, unsigned int, int);
105 SOCKET
mingw_socket(int, int, int);
106 int mingw_connect(SOCKET
, struct sockaddr
*, socklen_t
);
107 SOCKET
mingw_accept(SOCKET
, struct sockaddr
*, socklen_t
*);
108 int mingw_shutdown(SOCKET
, int);
109 int mingw_getpeername(SOCKET
, struct sockaddr
*, socklen_t
*);
111 /* Three socket specific macros */
112 #define READ(x, y, z) mingw_read_socket(x, y, z)
113 #define WRITE(x, y, z) mingw_write_socket(x, y, z)
114 #define CLOSE(x) mingw_close_socket(x)
116 int mingw_read_socket(SOCKET
, void *, int);
117 int mingw_write_socket(SOCKET
, void *, int);
118 int mingw_close_socket(SOCKET
);
120 int mingw_setnonblocking(SOCKET
, int);
124 #ifndef HAVE_READV_WRITEV
126 * The HAVE_READV_WRITEV symbol should be defined if the system features
127 * the vector IO functions readv() and writev() and those functions may
128 * be legally used with sockets.
131 void *iov_base
; /* Starting address */
132 size_t iov_len
; /* Number of bytes */
134 #define WRITEV(x, y, z) polipo_writev(x, y, z)
135 #define READV(x, y, z) polipo_readv(x, y, z)
136 int polipo_readv(int fd
, const struct iovec
*vector
, int count
);
137 int polipo_writev(int fd
, const struct iovec
*vector
, int count
);