libktorrent  2.1.1
win32.h
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 /*
25  * Polipo was originally designed to run on Unix-like systems. This
26  * header file (and it's accompanying implementation file mingw.c) contain
27  * code that allows polipo to run on Microsoft Windows too.
28  *
29  * The target MS windows compiler is Mingw (MINimal Gnu for Windows). The
30  * code in this file probably get's us pretty close to MSVC also, but
31  * this has not been tested. To build polipo for Mingw, define the MINGW
32  * symbol. For Unix or Unix-like systems, leave it undefined.
33  */
34 #ifndef MINGW_H
35 #define MINGW_H
36 /* Unfortunately, there's no hiding it. */
37 // #define HAVE_WINSOCK 1
38 
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).
41  */
42 
43 #include <io.h>
44 #include <wchar.h>
45 #include <ktorrent_export.h>
46 /* Pull in winsock.h for (almost) berkeley sockets. */
47 #ifdef FD_SETSIZE
48 #undef FD_SETSIZE
49 #endif
50 #define FD_SETSIZE 1000
51 #include <winsock2.h>
52 // #define ENOTCONN WSAENOTCONN
53 // #define EWOULDBLOCK WSAEWOULDBLOCK
54 // #define ENOBUFS WSAENOBUFS
55 // #define ECONNRESET WSAECONNRESET
56 // #define ESHUTDOWN WSAESHUTDOWN
57 // #define EAFNOSUPPORT WSAEAFNOSUPPORT
58 // #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
59 // #define EINPROGRESS WSAEINPROGRESS
60 // #define EISCONN WSAEISCONN
61 
62 
63 
64 #if (_WIN32_WINNT < 0x0600) // If VC++ 8.0 or older OR Windows older than Vista
65 
66 /* winsock doesn't feature poll(), so there is a version implemented
67  * in terms of select() in win32.cpp. The following definitions
68  * are copied from linux man pages. A poll() macro is defined to
69  * call the version in win32.cpp.
70  * pollfd is defined in Windows SDK 6.0A and newer if using
71  * MSVC2008, in what seems to be a blatant bug (MSVC2008 reports
72  * Windows XP as 0x0600 instead of 0x501)
73  */
74 #define POLLIN 0x0001 /* There is data to read */
75 #define POLLPRI 0x0002 /* There is urgent data to read */
76 #define POLLOUT 0x0004 /* Writing now will not block */
77 #define POLLERR 0x0008 /* Error condition */
78 #define POLLHUP 0x0010 /* Hung up */
79 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
80 
81 struct KTORRENT_EXPORT pollfd {
82  SOCKET fd; /* file descriptor */
83  short events; /* requested events */
84  short revents; /* returned events */
85 };
86 // #define poll(x, y, z) mingw_poll(x, y, z)
87 #endif
88 
89 #ifndef NAME_MAX
90 #define NAME_MAX 255
91 #endif
92 /* These wrappers do nothing special except set the global errno variable if
93 * an error occurs (winsock doesn't do this by default). They set errno
94 * to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN), so code
95 * outside of this file "shouldn't" have to worry about winsock specific error
96 * handling.
97 */
98 // #define socket(x, y, z) mingw_socket(x, y, z)
99 // #define connect(x, y, z) mingw_connect(x, y, z)
100 // #define accept(x, y, z) mingw_accept(x, y, z)
101 // #define shutdown(x, y) mingw_shutdown(x, y)
102 // #define getpeername(x, y, z) mingw_getpeername(x, y, z)
103 
104 /* Wrapper macros to call misc. functions mingw is missing */
105 // #define sleep(x) mingw_sleep(x)
106 // #define inet_aton(x, y) mingw_inet_aton(x, y)
107 // #define gettimeofday(x, y) mingw_gettimeofday(x, y)
108 // #define stat(x, y) mingw_stat(x, y)
109 //
110 // #define mkdir(x, y) mkdir(x)
111 
112 /* Winsock uses int instead of the usual socklen_t */
113 // typedef int socklen_t;
114 
115 /* Function prototypes for functions in mingw.c */
116 // unsigned int mingw_sleep(unsigned int);
117 // int mingw_inet_aton(const char *, struct in_addr *);
118 // int mingw_gettimeofday(struct timeval *, char *);
119 KTORRENT_EXPORT int mingw_poll(struct pollfd *, unsigned int, int);
120 // SOCKET mingw_socket(int, int, int);
121 // int mingw_connect(SOCKET, struct sockaddr*, socklen_t);
122 // SOCKET mingw_accept(SOCKET, struct sockaddr*, socklen_t *);
123 // int mingw_shutdown(SOCKET, int);
124 // int mingw_getpeername(SOCKET, struct sockaddr*, socklen_t *);
125 
126 /* Three socket specific macros */
127 // #define READ(x, y, z) mingw_read_socket(x, y, z)
128 // #define WRITE(x, y, z) mingw_write_socket(x, y, z)
129 // #define CLOSE(x) mingw_close_socket(x)
130 //
131 // int mingw_read_socket(SOCKET, void *, int);
132 // int mingw_write_socket(SOCKET, void *, int);
133 // int mingw_close_socket(SOCKET);
134 //
135 // int mingw_setnonblocking(SOCKET, int);
136 // int mingw_stat(const char*, struct stat*);
137 #define strerror(e) mingw_strerror(e)
138 
139 KTORRENT_EXPORT char *mingw_strerror(int error);
140 
141 #if 0
142 #ifdef POLLRDNORM
143 #undef POLLRDNORM
144 #undef POLLRDBAND
145 #undef POLLIN
146 #undef POLLPRI
147 #undef POLLWRNORM
148 #undef POLLOUT
149 #undef POLLWRBAND
150 #undef POLLERR
151 #undef POLLHUP
152 #undef POLLNVAL
153 struct _pollfd {
154  SOCKET fd;
155  short events;
156  short revents;
157 };
158 #define pollfd _pollfd
159 #else
160 struct pollfd {
161  SOCKET fd;
162  short events;
163  short revents;
164 };
165 #endif
166 
167 typedef unsigned int nfds_t;
168 
169 #define POLLIN (FD_READ | FD_ACCEPT | FD_CLOSE)
170 #define POLLPRI (FD_OOB)
171 #define POLLOUT (FD_WRITE | FD_CONNECT | FD_CLOSE)
172 #define POLLRDHUP (FD_CLOSE)
173 #define POLLHUP (FD_CLOSE)
174 #define POLLRDNORM (POLLIN)
175 #define POLLRDBAND (POLLIN | POLLPRI)
176 #define POLLWRNORM (POLLOUT)
177 #define POLLWRBAND (POLLOUT | POLLPRI)
178 // POLLERR, POLLNVAL not defined
179 
180 KTORRENT_EXPORT int poll(struct pollfd *fds, nfds_t nfds, int timeout);
181 
182 #define mingw_poll(a, b, c) poll(a, b, c)
183 #endif
184 
185 #undef ERROR
186 #undef CopyFile
187 
188 #endif
pollfd
Definition: win32.h:81