mingw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. #include "polipo.h"
  21. #ifndef WIN32 /*MINGW*/
  22. static int dummy ATTRIBUTE((unused));
  23. #else
  24. #undef poll
  25. #undef socket
  26. #undef connect
  27. #undef accept
  28. #undef shutdown
  29. #undef getpeername
  30. #undef sleep
  31. #undef inet_aton
  32. #undef gettimeofday
  33. #undef stat
  34. /* Windows needs this header file for the implementation of inet_aton() */
  35. #include <ctype.h>
  36. /* _snprintf and friends have broken NULL termination semantics */
  37. int win32_snprintf(char* dest, size_t count, const char* format, ...)
  38. {
  39. int r;
  40. va_list args;
  41. va_start(args, format);
  42. r = _vsnprintf(dest, count, format, args);
  43. va_end(args);
  44. if (count > 0) {
  45. dest[count-1] = '\0';
  46. }
  47. return r;
  48. }
  49. /*
  50. * Check whether "cp" is a valid ascii representation of an Internet address
  51. * and convert to a binary address. Returns 1 if the address is valid, 0 if
  52. * not. This replaces inet_addr, the return value from which cannot
  53. * distinguish between failure and a local broadcast address.
  54. *
  55. * This implementation of the standard inet_aton() function was copied
  56. * (with trivial modifications) from the OpenBSD project.
  57. */
  58. int
  59. win32_inet_aton(const char *cp, struct in_addr *addr)
  60. {
  61. register unsigned int val;
  62. register int base, n;
  63. register char c;
  64. unsigned int parts[4];
  65. register unsigned int *pp = parts;
  66. assert(sizeof(val) == 4);
  67. c = *cp;
  68. while(1) {
  69. /*
  70. * Collect number up to ``.''.
  71. * Values are specified as for C:
  72. * 0x=hex, 0=octal, isdigit=decimal.
  73. */
  74. if(!isdigit(c))
  75. return (0);
  76. val = 0; base = 10;
  77. if(c == '0') {
  78. c = *++cp;
  79. if(c == 'x' || c == 'X')
  80. base = 16, c = *++cp;
  81. else
  82. base = 8;
  83. }
  84. while(1) {
  85. if(isascii(c) && isdigit(c)) {
  86. val = (val * base) + (c - '0');
  87. c = *++cp;
  88. } else if(base == 16 && isascii(c) && isxdigit(c)) {
  89. val = (val << 4) |
  90. (c + 10 - (islower(c) ? 'a' : 'A'));
  91. c = *++cp;
  92. } else
  93. break;
  94. }
  95. if(c == '.') {
  96. /*
  97. * Internet format:
  98. * a.b.c.d
  99. * a.b.c (with c treated as 16 bits)
  100. * a.b (with b treated as 24 bits)
  101. */
  102. if(pp >= parts + 3)
  103. return (0);
  104. *pp++ = val;
  105. c = *++cp;
  106. } else
  107. break;
  108. }
  109. /*
  110. * Check for trailing characters.
  111. */
  112. if(c != '\0' && (!isascii(c) || !isspace(c)))
  113. return (0);
  114. /*
  115. * Concoct the address according to
  116. * the number of parts specified.
  117. */
  118. n = pp - parts + 1;
  119. switch(n) {
  120. case 0:
  121. return (0); /* initial nondigit */
  122. case 1: /* a -- 32 bits */
  123. break;
  124. case 2: /* a.b -- 8.24 bits */
  125. if((val > 0xffffff) || (parts[0] > 0xff))
  126. return (0);
  127. val |= parts[0] << 24;
  128. break;
  129. case 3: /* a.b.c -- 8.8.16 bits */
  130. if((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
  131. return (0);
  132. val |= (parts[0] << 24) | (parts[1] << 16);
  133. break;
  134. case 4: /* a.b.c.d -- 8.8.8.8 bits */
  135. if((val > 0xff) || (parts[0] > 0xff) ||
  136. (parts[1] > 0xff) || (parts[2] > 0xff))
  137. return (0);
  138. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  139. break;
  140. }
  141. if(addr)
  142. addr->s_addr = htonl(val);
  143. return (1);
  144. }
  145. unsigned int
  146. win32_sleep(unsigned int seconds)
  147. {
  148. Sleep(seconds * 1000);
  149. return 0;
  150. }
  151. int
  152. win32_gettimeofday(struct timeval *tv, char *tz)
  153. {
  154. const long long EPOCHFILETIME = (116444736000000000LL);
  155. FILETIME ft;
  156. LARGE_INTEGER li;
  157. long long t;
  158. /* This implementation doesn't support the timezone parameter. That's Ok,
  159. * as at present polipo always passed NULL as the second arg. We
  160. * also need to make sure that we have at least 8 bytes of space to
  161. * do the math in - otherwise there will be overflow errors.
  162. */
  163. assert(tz == NULL);
  164. assert(sizeof(t) == 8);
  165. if(tv) {
  166. GetSystemTimeAsFileTime(&ft);
  167. li.LowPart = ft.dwLowDateTime;
  168. li.HighPart = ft.dwHighDateTime;
  169. t = li.QuadPart; /* In 100-nanosecond intervals */
  170. t -= EPOCHFILETIME; /* Offset to the Epoch time */
  171. t /= 10; /* In microseconds */
  172. tv->tv_sec = (long)(t / 1000000);
  173. tv->tv_usec = (long)(t % 1000000);
  174. }
  175. return 0;
  176. }
  177. int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
  178. {
  179. struct timeval timeout, *toptr;
  180. fd_set ifds, ofds, efds, *ip, *op;
  181. int i, rc;
  182. /* Set up the file-descriptor sets in ifds, ofds and efds. */
  183. FD_ZERO(&ifds);
  184. FD_ZERO(&ofds);
  185. FD_ZERO(&efds);
  186. for (i = 0, op = ip = 0; i < nfds; ++i) {
  187. fds[i].revents = 0;
  188. if(fds[i].events & (POLLIN|POLLPRI)) {
  189. ip = &ifds;
  190. FD_SET(fds[i].fd, ip);
  191. }
  192. if(fds[i].events & POLLOUT) {
  193. op = &ofds;
  194. FD_SET(fds[i].fd, op);
  195. }
  196. FD_SET(fds[i].fd, &efds);
  197. }
  198. /* Set up the timeval structure for the timeout parameter */
  199. if(timo < 0) {
  200. toptr = 0;
  201. } else {
  202. toptr = &timeout;
  203. timeout.tv_sec = timo / 1000;
  204. timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
  205. }
  206. #ifdef DEBUG_POLL
  207. printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
  208. (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
  209. #endif
  210. rc = select(0, ip, op, &efds, toptr);
  211. #ifdef DEBUG_POLL
  212. printf("Exiting select rc=%d\n", rc);
  213. #endif
  214. if(rc <= 0)
  215. return rc;
  216. if(rc > 0) {
  217. for (i = 0; i < nfds; ++i) {
  218. int fd = fds[i].fd;
  219. if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
  220. fds[i].revents |= POLLIN;
  221. if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
  222. fds[i].revents |= POLLOUT;
  223. if(FD_ISSET(fd, &efds))
  224. /* Some error was detected ... should be some way to know. */
  225. fds[i].revents |= POLLHUP;
  226. #ifdef DEBUG_POLL
  227. printf("%d %d %d revent = %x\n",
  228. FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
  229. fds[i].revents
  230. );
  231. #endif
  232. }
  233. }
  234. return rc;
  235. }
  236. int win32_close_socket(SOCKET fd) {
  237. int rc;
  238. rc = closesocket(fd);
  239. return 0;
  240. }
  241. static void
  242. set_errno(int winsock_err)
  243. {
  244. switch(winsock_err) {
  245. case WSAEWOULDBLOCK:
  246. errno = EAGAIN;
  247. break;
  248. default:
  249. errno = winsock_err;
  250. break;
  251. }
  252. }
  253. int win32_write_socket(SOCKET fd, void *buf, int n)
  254. {
  255. int rc = send(fd, buf, n, 0);
  256. if(rc == SOCKET_ERROR) {
  257. set_errno(WSAGetLastError());
  258. }
  259. return rc;
  260. }
  261. int win32_read_socket(SOCKET fd, void *buf, int n)
  262. {
  263. int rc = recv(fd, buf, n, 0);
  264. if(rc == SOCKET_ERROR) {
  265. set_errno(WSAGetLastError());
  266. }
  267. return rc;
  268. }
  269. /*
  270. * Set the "non-blocking" flag on socket fd to the value specified by
  271. * the second argument (i.e. if the nonblocking argument is non-zero, the
  272. * socket is set to non-blocking mode). Zero is returned if the operation
  273. * is successful, other -1.
  274. */
  275. int
  276. win32_setnonblocking(SOCKET fd, int nonblocking)
  277. {
  278. int rc;
  279. unsigned long mode = 1;
  280. rc = ioctlsocket(fd, FIONBIO, &mode);
  281. if(rc != 0) {
  282. set_errno(WSAGetLastError());
  283. }
  284. return (rc == 0 ? 0 : -1);
  285. }
  286. /*
  287. * A wrapper around the socket() function. The purpose of this wrapper
  288. * is to ensure that the global errno symbol is set if an error occurs,
  289. * even if we are using winsock.
  290. */
  291. SOCKET
  292. win32_socket(int domain, int type, int protocol)
  293. {
  294. SOCKET fd = socket(domain, type, protocol);
  295. if(fd == INVALID_SOCKET) {
  296. set_errno(WSAGetLastError());
  297. }
  298. return fd;
  299. }
  300. static void
  301. set_connect_errno(int winsock_err)
  302. {
  303. switch(winsock_err) {
  304. case WSAEINVAL:
  305. case WSAEALREADY:
  306. case WSAEWOULDBLOCK:
  307. errno = EINPROGRESS;
  308. break;
  309. default:
  310. errno = winsock_err;
  311. break;
  312. }
  313. }
  314. /*
  315. * A wrapper around the connect() function. The purpose of this wrapper
  316. * is to ensure that the global errno symbol is set if an error occurs,
  317. * even if we are using winsock.
  318. */
  319. int
  320. win32_connect(SOCKET fd, struct sockaddr *addr, socklen_t addr_len)
  321. {
  322. int rc = connect(fd, addr, addr_len);
  323. assert(rc == 0 || rc == SOCKET_ERROR);
  324. if(rc == SOCKET_ERROR) {
  325. set_connect_errno(WSAGetLastError());
  326. }
  327. return rc;
  328. }
  329. /*
  330. * A wrapper around the accept() function. The purpose of this wrapper
  331. * is to ensure that the global errno symbol is set if an error occurs,
  332. * even if we are using winsock.
  333. */
  334. SOCKET
  335. win32_accept(SOCKET fd, struct sockaddr *addr, socklen_t *addr_len)
  336. {
  337. SOCKET newfd = accept(fd, addr, addr_len);
  338. if(newfd == INVALID_SOCKET) {
  339. set_errno(WSAGetLastError());
  340. newfd = -1;
  341. }
  342. return newfd;
  343. }
  344. /*
  345. * A wrapper around the shutdown() function. The purpose of this wrapper
  346. * is to ensure that the global errno symbol is set if an error occurs,
  347. * even if we are using winsock.
  348. */
  349. int
  350. win32_shutdown(SOCKET fd, int mode)
  351. {
  352. int rc = shutdown(fd, mode);
  353. assert(rc == 0 || rc == SOCKET_ERROR);
  354. if(rc == SOCKET_ERROR) {
  355. set_errno(WSAGetLastError());
  356. }
  357. return rc;
  358. }
  359. /*
  360. * A wrapper around the getpeername() function. The purpose of this wrapper
  361. * is to ensure that the global errno symbol is set if an error occurs,
  362. * even if we are using winsock.
  363. */
  364. int
  365. win32_getpeername(SOCKET fd, struct sockaddr *name, socklen_t *namelen)
  366. {
  367. int rc = getpeername(fd, name, namelen);
  368. assert(rc == 0 || rc == SOCKET_ERROR);
  369. if(rc == SOCKET_ERROR) {
  370. set_errno(WSAGetLastError());
  371. }
  372. return rc;
  373. }
  374. /* Stat doesn't work on directories if the name ends in a slash. */
  375. int
  376. win32_stat(const char *filename, struct stat *ss)
  377. {
  378. int len, rc, saved_errno;
  379. char *noslash;
  380. len = strlen(filename);
  381. if(len <= 1 || filename[len - 1] != '/')
  382. return stat(filename, ss);
  383. noslash = malloc(len);
  384. if(noslash == NULL)
  385. return -1;
  386. memcpy(noslash, filename, len - 1);
  387. noslash[len - 1] = '\0';
  388. rc = stat(noslash, ss);
  389. saved_errno = errno;
  390. free(noslash);
  391. errno = saved_errno;
  392. return rc;
  393. }
  394. #endif /* #ifdef WIN32 MINGW */
  395. #ifndef HAVE_READV_WRITEV
  396. int
  397. polipo_writev(int fd, const struct iovec *vector, int count)
  398. {
  399. int rc; /* Return Code */
  400. if(count == 1) {
  401. rc = WRITE(fd, vector->iov_base, vector->iov_len);
  402. } else {
  403. int n = 0; /* Total bytes to write */
  404. char *buf = 0; /* Buffer to copy to before writing */
  405. int i; /* Counter var for looping over vector[] */
  406. int offset = 0; /* Offset for copying to buf */
  407. /* Figure out the required buffer size */
  408. for(i = 0; i < count; i++) {
  409. n += vector[i].iov_len;
  410. }
  411. /* Allocate the buffer. If the allocation fails, bail out */
  412. buf = malloc(n);
  413. if(!buf) {
  414. errno = ENOMEM;
  415. return -1;
  416. }
  417. /* Copy the contents of the vector array to the buffer */
  418. for(i = 0; i < count; i++) {
  419. memcpy(&buf[offset], vector[i].iov_base, vector[i].iov_len);
  420. offset += vector[i].iov_len;
  421. }
  422. assert(offset == n);
  423. /* Write the entire buffer to the socket and free the allocation */
  424. rc = WRITE(fd, buf, n);
  425. free(buf);
  426. }
  427. return rc;
  428. }
  429. int
  430. polipo_readv(int fd, const struct iovec *vector, int count)
  431. {
  432. int ret = 0; /* Return value */
  433. int i;
  434. for(i = 0; i < count; i++) {
  435. int n = vector[i].iov_len;
  436. int rc = READ(fd, vector[i].iov_base, n);
  437. if(rc == n) {
  438. ret += rc;
  439. } else {
  440. if(rc < 0) {
  441. ret = (ret == 0 ? rc : ret);
  442. } else {
  443. ret += rc;
  444. }
  445. break;
  446. }
  447. }
  448. return ret;
  449. }
  450. #endif