File indexing completed on 2024-06-02 05:05:38
0001 /* 0002 SPDX-FileCopyrightText: 2006 Dan Kennedy. 0003 SPDX-FileCopyrightText: 2006 Juliusz Chroboczek. 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 /* 0009 * Polipo was originally designed to run on Unix-like systems. This 0010 * header file (and it's accompanying implementation file mingw.c) contain 0011 * code that allows polipo to run on Microsoft Windows too. 0012 * 0013 * The target MS windows compiler is Mingw (MINimal Gnu for Windows). The 0014 * code in this file probably get's us pretty close to MSVC also, but 0015 * this has not been tested. To build polipo for Mingw, define the MINGW 0016 * symbol. For Unix or Unix-like systems, leave it undefined. 0017 */ 0018 #ifndef MINGW_H 0019 #define MINGW_H 0020 /* Unfortunately, there's no hiding it. */ 0021 // #define HAVE_WINSOCK 1 0022 0023 /* At time of writing, a fair bit of stuff doesn't work under Mingw. 0024 * Hopefully they will be fixed later (especially the disk-cache). 0025 */ 0026 0027 #include <io.h> 0028 #include <ktorrent_export.h> 0029 #include <wchar.h> 0030 /* Pull in winsock.h for (almost) berkeley sockets. */ 0031 #ifdef FD_SETSIZE 0032 #undef FD_SETSIZE 0033 #endif 0034 #define FD_SETSIZE 1000 0035 #include <winsock2.h> 0036 // #define ENOTCONN WSAENOTCONN 0037 // #define EWOULDBLOCK WSAEWOULDBLOCK 0038 // #define ENOBUFS WSAENOBUFS 0039 // #define ECONNRESET WSAECONNRESET 0040 // #define ESHUTDOWN WSAESHUTDOWN 0041 // #define EAFNOSUPPORT WSAEAFNOSUPPORT 0042 // #define EPROTONOSUPPORT WSAEPROTONOSUPPORT 0043 // #define EINPROGRESS WSAEINPROGRESS 0044 // #define EISCONN WSAEISCONN 0045 0046 #if (_WIN32_WINNT < 0x0600) // If VC++ 8.0 or older OR Windows older than Vista 0047 0048 /* winsock doesn't feature poll(), so there is a version implemented 0049 * in terms of select() in win32.cpp. The following definitions 0050 * are copied from linux man pages. A poll() macro is defined to 0051 * call the version in win32.cpp. 0052 * pollfd is defined in Windows SDK 6.0A and newer if using 0053 * MSVC2008, in what seems to be a blatant bug (MSVC2008 reports 0054 * Windows XP as 0x0600 instead of 0x501) 0055 */ 0056 #define POLLIN 0x0001 /* There is data to read */ 0057 #define POLLPRI 0x0002 /* There is urgent data to read */ 0058 #define POLLOUT 0x0004 /* Writing now will not block */ 0059 #define POLLERR 0x0008 /* Error condition */ 0060 #define POLLHUP 0x0010 /* Hung up */ 0061 #define POLLNVAL 0x0020 /* Invalid request: fd not open */ 0062 0063 struct KTORRENT_EXPORT pollfd { 0064 SOCKET fd; /* file descriptor */ 0065 short events; /* requested events */ 0066 short revents; /* returned events */ 0067 }; 0068 // #define poll(x, y, z) mingw_poll(x, y, z) 0069 #endif 0070 0071 #ifndef NAME_MAX 0072 #define NAME_MAX 255 0073 #endif 0074 /* These wrappers do nothing special except set the global errno variable if 0075 * an error occurs (winsock doesn't do this by default). They set errno 0076 * to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN), so code 0077 * outside of this file "shouldn't" have to worry about winsock specific error 0078 * handling. 0079 */ 0080 // #define socket(x, y, z) mingw_socket(x, y, z) 0081 // #define connect(x, y, z) mingw_connect(x, y, z) 0082 // #define accept(x, y, z) mingw_accept(x, y, z) 0083 // #define shutdown(x, y) mingw_shutdown(x, y) 0084 // #define getpeername(x, y, z) mingw_getpeername(x, y, z) 0085 0086 /* Wrapper macros to call misc. functions mingw is missing */ 0087 // #define sleep(x) mingw_sleep(x) 0088 // #define inet_aton(x, y) mingw_inet_aton(x, y) 0089 // #define gettimeofday(x, y) mingw_gettimeofday(x, y) 0090 // #define stat(x, y) mingw_stat(x, y) 0091 // 0092 // #define mkdir(x, y) mkdir(x) 0093 0094 /* Winsock uses int instead of the usual socklen_t */ 0095 // typedef int socklen_t; 0096 0097 /* Function prototypes for functions in mingw.c */ 0098 // unsigned int mingw_sleep(unsigned int); 0099 // int mingw_inet_aton(const char *, struct in_addr *); 0100 // int mingw_gettimeofday(struct timeval *, char *); 0101 KTORRENT_EXPORT int mingw_poll(struct pollfd *, unsigned int, int); 0102 // SOCKET mingw_socket(int, int, int); 0103 // int mingw_connect(SOCKET, struct sockaddr*, socklen_t); 0104 // SOCKET mingw_accept(SOCKET, struct sockaddr*, socklen_t *); 0105 // int mingw_shutdown(SOCKET, int); 0106 // int mingw_getpeername(SOCKET, struct sockaddr*, socklen_t *); 0107 0108 /* Three socket specific macros */ 0109 // #define READ(x, y, z) mingw_read_socket(x, y, z) 0110 // #define WRITE(x, y, z) mingw_write_socket(x, y, z) 0111 // #define CLOSE(x) mingw_close_socket(x) 0112 // 0113 // int mingw_read_socket(SOCKET, void *, int); 0114 // int mingw_write_socket(SOCKET, void *, int); 0115 // int mingw_close_socket(SOCKET); 0116 // 0117 // int mingw_setnonblocking(SOCKET, int); 0118 // int mingw_stat(const char*, struct stat*); 0119 #define strerror(e) mingw_strerror(e) 0120 0121 KTORRENT_EXPORT char *mingw_strerror(int error); 0122 0123 #if 0 0124 #ifdef POLLRDNORM 0125 #undef POLLRDNORM 0126 #undef POLLRDBAND 0127 #undef POLLIN 0128 #undef POLLPRI 0129 #undef POLLWRNORM 0130 #undef POLLOUT 0131 #undef POLLWRBAND 0132 #undef POLLERR 0133 #undef POLLHUP 0134 #undef POLLNVAL 0135 struct _pollfd { 0136 SOCKET fd; 0137 short events; 0138 short revents; 0139 }; 0140 #define pollfd _pollfd 0141 #else 0142 struct pollfd { 0143 SOCKET fd; 0144 short events; 0145 short revents; 0146 }; 0147 #endif 0148 0149 typedef unsigned int nfds_t; 0150 0151 #define POLLIN (FD_READ | FD_ACCEPT | FD_CLOSE) 0152 #define POLLPRI (FD_OOB) 0153 #define POLLOUT (FD_WRITE | FD_CONNECT | FD_CLOSE) 0154 #define POLLRDHUP (FD_CLOSE) 0155 #define POLLHUP (FD_CLOSE) 0156 #define POLLRDNORM (POLLIN) 0157 #define POLLRDBAND (POLLIN | POLLPRI) 0158 #define POLLWRNORM (POLLOUT) 0159 #define POLLWRBAND (POLLOUT | POLLPRI) 0160 // POLLERR, POLLNVAL not defined 0161 0162 KTORRENT_EXPORT int poll(struct pollfd *fds, nfds_t nfds, int timeout); 0163 0164 #define mingw_poll(a, b, c) poll(a, b, c) 0165 #endif 0166 0167 #undef ERROR 0168 #undef CopyFile 0169 0170 #endif