mingw.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright (c) 2006 by Dan Kennedy.
  3. Copyright (c) 2006 by Juliusz Chroboczek.
  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:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. * Polipo was originally designed to run on Unix-like systems. This
  22. * header file (and it's accompanying implementation file mingw.c) contain
  23. * code that allows polipo to run on Microsoft Windows too.
  24. *
  25. * The target MS windows compiler is Mingw (MINimal Gnu for Windows). The
  26. * code in this file probably get's us pretty close to MSVC also, but
  27. * this has not been tested. To build polipo for Mingw, define the MINGW
  28. * symbol. For Unix or Unix-like systems, leave it undefined.
  29. */
  30. #ifdef WIN32 /*MINGW*/
  31. /* Unfortunately, there's no hiding it. */
  32. #define HAVE_WINSOCK 1
  33. #ifndef WINVER
  34. #define WINVER 0x0501
  35. #endif
  36. #ifndef _WIN32_WINNT
  37. #define _WIN32_WINNT 0x0501
  38. #endif
  39. #ifndef _WIN32_WINDOWS
  40. #define _WIN32_WINDOWS 0x0410
  41. #endif
  42. #ifndef _WIN32_IE
  43. #define _WIN32_IE 0x0700
  44. #endif
  45. /* At time of writing, a fair bit of stuff doesn't work under Mingw.
  46. * Hopefully they will be fixed later (especially the disk-cache).
  47. */
  48. #define NO_IPv6 1
  49. #define S_IROTH S_IREAD
  50. /* Pull in winsock2.h for (almost) berkeley sockets. */
  51. #include <winsock2.h>
  52. // here we smash the errno defines so we can smash the socket functions later to smash errno. yay!
  53. #ifdef ENOTCONN
  54. #undef ENOTCONN
  55. #endif
  56. #ifdef EWOULDBLOCK
  57. #undef EWOULDBLOCK
  58. #endif
  59. #ifdef ENOBUFS
  60. #undef ENOBUFS
  61. #endif
  62. #ifdef ECONNRESET
  63. #undef ECONNRESET
  64. #endif
  65. #ifdef EAFNOSUPPORT
  66. #undef EAFNOSUPPORT
  67. #endif
  68. #ifdef EPROTONOSUPPORT
  69. #undef EPROTONOSUPPORT
  70. #endif
  71. #ifdef EINPROGRESS
  72. #undef EINPROGRESS
  73. #endif
  74. #ifdef EISCONN
  75. #undef EISCONN
  76. #endif
  77. #define ENOTCONN WSAENOTCONN
  78. #define EWOULDBLOCK WSAEWOULDBLOCK
  79. #define ENOBUFS WSAENOBUFS
  80. #define ECONNRESET WSAECONNRESET
  81. #define ESHUTDOWN WSAESHUTDOWN
  82. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  83. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  84. #define EINPROGRESS WSAEINPROGRESS
  85. #define EISCONN WSAEISCONN
  86. #include <direct.h>
  87. #include <io.h>
  88. #include <process.h>
  89. /* winsock doesn't feature poll(), so there is a version implemented
  90. * in terms of select() in mingw.c. The following definitions
  91. * are copied from linux man pages. A poll() macro is defined to
  92. * call the version in mingw.c.
  93. */
  94. #define POLLIN 0x0001 /* There is data to read */
  95. #define POLLPRI 0x0002 /* There is urgent data to read */
  96. #define POLLOUT 0x0004 /* Writing now will not block */
  97. #define POLLERR 0x0008 /* Error condition */
  98. #define POLLHUP 0x0010 /* Hung up */
  99. #define POLLNVAL 0x0020 /* Invalid request: fd not open */
  100. struct pollfd {
  101. SOCKET fd; /* file descriptor */
  102. short events; /* requested events */
  103. short revents; /* returned events */
  104. };
  105. #define poll(x, y, z) win32_poll(x, y, z)
  106. /* These wrappers do nothing special except set the global errno variable if
  107. * an error occurs (winsock doesn't do this by default). They set errno
  108. * to unix-like values (i.e. WSAEWOULDBLOCK is mapped to EAGAIN), so code
  109. * outside of this file "shouldn't" have to worry about winsock specific error
  110. * handling.
  111. */
  112. #define socket(x, y, z) win32_socket(x, y, z)
  113. #define connect(x, y, z) win32_connect(x, y, z)
  114. #define accept(x, y, z) win32_accept(x, y, z)
  115. #define shutdown(x, y) win32_shutdown(x, y)
  116. #define getpeername(x, y, z) win32_getpeername(x, y, z)
  117. /* Wrapper macros to call misc. functions mingw is missing */
  118. #define sleep(x) win32_sleep(x)
  119. #define inet_aton(x, y) win32_inet_aton(x, y)
  120. #define gettimeofday(x, y) win32_gettimeofday(x, y)
  121. #define stat(x, y) win32_stat(x, y)
  122. #define snprintf win32_snprintf
  123. #define mkdir(x, y) mkdir(x)
  124. #define getcwd _getcwd
  125. #define getpid _getpid
  126. #ifndef S_ISDIR
  127. #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  128. #endif
  129. #ifndef S_ISREG
  130. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  131. #endif
  132. /* Winsock uses int instead of the usual socklen_t */
  133. typedef int socklen_t;
  134. /* Function prototypes for functions in mingw.c */
  135. unsigned int win32_sleep(unsigned int);
  136. int win32_inet_aton(const char *, struct in_addr *);
  137. int win32_gettimeofday(struct timeval *, char *);
  138. int win32_poll(struct pollfd *, unsigned int, int);
  139. SOCKET win32_socket(int, int, int);
  140. int win32_connect(SOCKET, struct sockaddr*, socklen_t);
  141. SOCKET win32_accept(SOCKET, struct sockaddr*, socklen_t *);
  142. int win32_shutdown(SOCKET, int);
  143. int win32_getpeername(SOCKET, struct sockaddr*, socklen_t *);
  144. int win32_snprintf(char* dest, size_t count, const char* format, ...);
  145. /* Three socket specific macros */
  146. #define READ(x, y, z) win32_read_socket(x, y, z)
  147. #define WRITE(x, y, z) win32_write_socket(x, y, z)
  148. #define CLOSE(x) win32_close_socket(x)
  149. int win32_read_socket(SOCKET, void *, int);
  150. int win32_write_socket(SOCKET, void *, int);
  151. int win32_close_socket(SOCKET);
  152. int win32_setnonblocking(SOCKET, int);
  153. int win32_stat(const char*, struct stat*);
  154. #endif
  155. #ifndef HAVE_READV_WRITEV
  156. /*
  157. * The HAVE_READV_WRITEV symbol should be defined if the system features
  158. * the vector IO functions readv() and writev() and those functions may
  159. * be legally used with sockets.
  160. */
  161. struct iovec {
  162. void *iov_base; /* Starting address */
  163. size_t iov_len; /* Number of bytes */
  164. };
  165. #define WRITEV(x, y, z) polipo_writev(x, y, z)
  166. #define READV(x, y, z) polipo_readv(x, y, z)
  167. int polipo_readv(int fd, const struct iovec *vector, int count);
  168. int polipo_writev(int fd, const struct iovec *vector, int count);
  169. #endif