socket.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file socket.c
  7. * \brief Compatibility and utility functions for working with network
  8. * sockets.
  9. **/
  10. #define SOCKET_PRIVATE
  11. #include "lib/net/socket.h"
  12. #include "lib/net/socketpair.h"
  13. #include "lib/net/address.h"
  14. #include "lib/cc/compat_compiler.h"
  15. #include "lib/err/torerr.h"
  16. #include "lib/lock/compat_mutex.h"
  17. #include "lib/log/log.h"
  18. #include "lib/log/util_bug.h"
  19. #ifdef _WIN32
  20. #include <winsock2.h>
  21. #include <windows.h>
  22. #endif
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #endif
  29. #include <stddef.h>
  30. #include <string.h>
  31. /** Called before we make any calls to network-related functions.
  32. * (Some operating systems require their network libraries to be
  33. * initialized.) */
  34. int
  35. network_init(void)
  36. {
  37. #ifdef _WIN32
  38. /* This silly exercise is necessary before windows will allow
  39. * gethostbyname to work. */
  40. WSADATA WSAData;
  41. int r;
  42. r = WSAStartup(0x101,&WSAData);
  43. if (r) {
  44. log_warn(LD_NET,"Error initializing windows network layer: code was %d",r);
  45. return -1;
  46. }
  47. if (sizeof(SOCKET) != sizeof(tor_socket_t)) {
  48. log_warn(LD_BUG,"The tor_socket_t type does not match SOCKET in size; Tor "
  49. "might not work. (Sizes are %d and %d respectively.)",
  50. (int)sizeof(tor_socket_t), (int)sizeof(SOCKET));
  51. }
  52. /* WSAData.iMaxSockets might show the max sockets we're allowed to use.
  53. * We might use it to complain if we're trying to be a server but have
  54. * too few sockets available. */
  55. #endif /* defined(_WIN32) */
  56. return 0;
  57. }
  58. /* When set_max_file_sockets() is called, update this with the max file
  59. * descriptor value so we can use it to check the limit when opening a new
  60. * socket. Default value is what Debian sets as the default hard limit. */
  61. static int max_sockets = 1024;
  62. /** Return the maximum number of allowed sockets. */
  63. int
  64. get_max_sockets(void)
  65. {
  66. return max_sockets;
  67. }
  68. /** Set the maximum number of allowed sockets to <b>n</b> */
  69. void
  70. set_max_sockets(int n)
  71. {
  72. max_sockets = n;
  73. }
  74. #undef DEBUG_SOCKET_COUNTING
  75. #ifdef DEBUG_SOCKET_COUNTING
  76. #include "lib/container/bitarray.h"
  77. /** A bitarray of all fds that should be passed to tor_socket_close(). Only
  78. * used if DEBUG_SOCKET_COUNTING is defined. */
  79. static bitarray_t *open_sockets = NULL;
  80. /** The size of <b>open_sockets</b>, in bits. */
  81. static int max_socket = -1;
  82. #endif /* defined(DEBUG_SOCKET_COUNTING) */
  83. /** Count of number of sockets currently open. (Undercounts sockets opened by
  84. * eventdns and libevent.) */
  85. static int n_sockets_open = 0;
  86. /** Mutex to protect open_sockets, max_socket, and n_sockets_open. */
  87. static tor_mutex_t *socket_accounting_mutex = NULL;
  88. /** Helper: acquire the socket accounting lock. */
  89. static inline void
  90. socket_accounting_lock(void)
  91. {
  92. if (PREDICT_UNLIKELY(!socket_accounting_mutex))
  93. socket_accounting_mutex = tor_mutex_new();
  94. tor_mutex_acquire(socket_accounting_mutex);
  95. }
  96. /** Helper: release the socket accounting lock. */
  97. static inline void
  98. socket_accounting_unlock(void)
  99. {
  100. tor_mutex_release(socket_accounting_mutex);
  101. }
  102. /** As close(), but guaranteed to work for sockets across platforms (including
  103. * Windows, where close()ing a socket doesn't work. Returns 0 on success and
  104. * the socket error code on failure. */
  105. int
  106. tor_close_socket_simple(tor_socket_t s)
  107. {
  108. int r = 0;
  109. /* On Windows, you have to call close() on fds returned by open(),
  110. * and closesocket() on fds returned by socket(). On Unix, everything
  111. * gets close()'d. We abstract this difference by always using
  112. * tor_close_socket to close sockets, and always using close() on
  113. * files.
  114. */
  115. #if defined(_WIN32)
  116. r = closesocket(s);
  117. #else
  118. r = close(s);
  119. #endif
  120. if (r != 0) {
  121. int err = tor_socket_errno(-1);
  122. log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
  123. return err;
  124. }
  125. return r;
  126. }
  127. /** As tor_close_socket_simple(), but keeps track of the number
  128. * of open sockets. Returns 0 on success, -1 on failure. */
  129. MOCK_IMPL(int,
  130. tor_close_socket,(tor_socket_t s))
  131. {
  132. int r = tor_close_socket_simple(s);
  133. socket_accounting_lock();
  134. #ifdef DEBUG_SOCKET_COUNTING
  135. if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
  136. log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
  137. "socket(), or that was already closed or something.", s);
  138. } else {
  139. tor_assert(open_sockets && s <= max_socket);
  140. bitarray_clear(open_sockets, s);
  141. }
  142. #endif /* defined(DEBUG_SOCKET_COUNTING) */
  143. if (r == 0) {
  144. --n_sockets_open;
  145. } else {
  146. #ifdef _WIN32
  147. if (r != WSAENOTSOCK)
  148. --n_sockets_open;
  149. #else
  150. if (r != EBADF)
  151. --n_sockets_open; // LCOV_EXCL_LINE -- EIO and EINTR too hard to force.
  152. #endif /* defined(_WIN32) */
  153. r = -1;
  154. }
  155. tor_assert_nonfatal(n_sockets_open >= 0);
  156. socket_accounting_unlock();
  157. return r;
  158. }
  159. /** @{ */
  160. #ifdef DEBUG_SOCKET_COUNTING
  161. /** Helper: if DEBUG_SOCKET_COUNTING is enabled, remember that <b>s</b> is
  162. * now an open socket. */
  163. static inline void
  164. mark_socket_open(tor_socket_t s)
  165. {
  166. /* XXXX This bitarray business will NOT work on windows: sockets aren't
  167. small ints there. */
  168. if (s > max_socket) {
  169. if (max_socket == -1) {
  170. open_sockets = bitarray_init_zero(s+128);
  171. max_socket = s+128;
  172. } else {
  173. open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
  174. max_socket = s+128;
  175. }
  176. }
  177. if (bitarray_is_set(open_sockets, s)) {
  178. log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
  179. "gave it to me!", s);
  180. }
  181. bitarray_set(open_sockets, s);
  182. }
  183. #else /* !(defined(DEBUG_SOCKET_COUNTING)) */
  184. #define mark_socket_open(s) ((void) (s))
  185. #endif /* defined(DEBUG_SOCKET_COUNTING) */
  186. /** @} */
  187. /** As socket(), but counts the number of open sockets. */
  188. MOCK_IMPL(tor_socket_t,
  189. tor_open_socket,(int domain, int type, int protocol))
  190. {
  191. return tor_open_socket_with_extensions(domain, type, protocol, 1, 0);
  192. }
  193. /** Mockable wrapper for connect(). */
  194. MOCK_IMPL(tor_socket_t,
  195. tor_connect_socket,(tor_socket_t sock, const struct sockaddr *address,
  196. socklen_t address_len))
  197. {
  198. return connect(sock,address,address_len);
  199. }
  200. /** As socket(), but creates a nonblocking socket and
  201. * counts the number of open sockets. */
  202. tor_socket_t
  203. tor_open_socket_nonblocking(int domain, int type, int protocol)
  204. {
  205. return tor_open_socket_with_extensions(domain, type, protocol, 1, 1);
  206. }
  207. /** As socket(), but counts the number of open sockets and handles
  208. * socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
  209. * <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
  210. * if the corresponding extension should be used.*/
  211. tor_socket_t
  212. tor_open_socket_with_extensions(int domain, int type, int protocol,
  213. int cloexec, int nonblock)
  214. {
  215. tor_socket_t s;
  216. /* We are about to create a new file descriptor so make sure we have
  217. * enough of them. */
  218. if (get_n_open_sockets() >= max_sockets - 1) {
  219. #ifdef _WIN32
  220. WSASetLastError(WSAEMFILE);
  221. #else
  222. errno = EMFILE;
  223. #endif
  224. return TOR_INVALID_SOCKET;
  225. }
  226. #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
  227. int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
  228. (nonblock ? SOCK_NONBLOCK : 0);
  229. s = socket(domain, type|ext_flags, protocol);
  230. if (SOCKET_OK(s))
  231. goto socket_ok;
  232. /* If we got an error, see if it is EINVAL. EINVAL might indicate that,
  233. * even though we were built on a system with SOCK_CLOEXEC and SOCK_NONBLOCK
  234. * support, we are running on one without. */
  235. if (errno != EINVAL)
  236. return s;
  237. #endif /* defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) */
  238. s = socket(domain, type, protocol);
  239. if (! SOCKET_OK(s))
  240. return s;
  241. #if defined(FD_CLOEXEC)
  242. if (cloexec) {
  243. if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
  244. log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
  245. tor_close_socket_simple(s);
  246. return TOR_INVALID_SOCKET;
  247. }
  248. }
  249. #else /* !(defined(FD_CLOEXEC)) */
  250. (void)cloexec;
  251. #endif /* defined(FD_CLOEXEC) */
  252. if (nonblock) {
  253. if (set_socket_nonblocking(s) == -1) {
  254. tor_close_socket_simple(s);
  255. return TOR_INVALID_SOCKET;
  256. }
  257. }
  258. goto socket_ok; /* So that socket_ok will not be unused. */
  259. socket_ok:
  260. tor_take_socket_ownership(s);
  261. return s;
  262. }
  263. /**
  264. * For socket accounting: remember that we are the owner of the socket
  265. * <b>s</b>. This will prevent us from overallocating sockets, and prevent us
  266. * from asserting later when we close the socket <b>s</b>.
  267. */
  268. void
  269. tor_take_socket_ownership(tor_socket_t s)
  270. {
  271. socket_accounting_lock();
  272. ++n_sockets_open;
  273. mark_socket_open(s);
  274. socket_accounting_unlock();
  275. }
  276. /** As accept(), but counts the number of open sockets. */
  277. tor_socket_t
  278. tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
  279. {
  280. return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 0);
  281. }
  282. /** As accept(), but returns a nonblocking socket and
  283. * counts the number of open sockets. */
  284. tor_socket_t
  285. tor_accept_socket_nonblocking(tor_socket_t sockfd, struct sockaddr *addr,
  286. socklen_t *len)
  287. {
  288. return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 1);
  289. }
  290. /** As accept(), but counts the number of open sockets and handles
  291. * socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
  292. * <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
  293. * if the corresponding extension should be used.*/
  294. tor_socket_t
  295. tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr,
  296. socklen_t *len, int cloexec, int nonblock)
  297. {
  298. tor_socket_t s;
  299. /* We are about to create a new file descriptor so make sure we have
  300. * enough of them. */
  301. if (get_n_open_sockets() >= max_sockets - 1) {
  302. #ifdef _WIN32
  303. WSASetLastError(WSAEMFILE);
  304. #else
  305. errno = EMFILE;
  306. #endif
  307. return TOR_INVALID_SOCKET;
  308. }
  309. #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) \
  310. && defined(SOCK_NONBLOCK)
  311. int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
  312. (nonblock ? SOCK_NONBLOCK : 0);
  313. s = accept4(sockfd, addr, len, ext_flags);
  314. if (SOCKET_OK(s))
  315. goto socket_ok;
  316. /* If we got an error, see if it is ENOSYS. ENOSYS indicates that,
  317. * even though we were built on a system with accept4 support, we
  318. * are running on one without. Also, check for EINVAL, which indicates that
  319. * we are missing SOCK_CLOEXEC/SOCK_NONBLOCK support. */
  320. if (errno != EINVAL && errno != ENOSYS)
  321. return s;
  322. #endif /* defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) ... */
  323. s = accept(sockfd, addr, len);
  324. if (!SOCKET_OK(s))
  325. return s;
  326. #if defined(FD_CLOEXEC)
  327. if (cloexec) {
  328. if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
  329. log_warn(LD_NET, "Couldn't set FD_CLOEXEC: %s", strerror(errno));
  330. tor_close_socket_simple(s);
  331. return TOR_INVALID_SOCKET;
  332. }
  333. }
  334. #else /* !(defined(FD_CLOEXEC)) */
  335. (void)cloexec;
  336. #endif /* defined(FD_CLOEXEC) */
  337. if (nonblock) {
  338. if (set_socket_nonblocking(s) == -1) {
  339. tor_close_socket_simple(s);
  340. return TOR_INVALID_SOCKET;
  341. }
  342. }
  343. goto socket_ok; /* So that socket_ok will not be unused. */
  344. socket_ok:
  345. tor_take_socket_ownership(s);
  346. return s;
  347. }
  348. /** Return the number of sockets we currently have opened. */
  349. int
  350. get_n_open_sockets(void)
  351. {
  352. int n;
  353. socket_accounting_lock();
  354. n = n_sockets_open;
  355. socket_accounting_unlock();
  356. return n;
  357. }
  358. /**
  359. * Allocate a pair of connected sockets. (Like socketpair(family,
  360. * type,protocol,fd), but works on systems that don't have
  361. * socketpair.)
  362. *
  363. * Currently, only (AF_UNIX, SOCK_STREAM, 0) sockets are supported.
  364. *
  365. * Note that on systems without socketpair, this call will fail if
  366. * localhost is inaccessible (for example, if the networking
  367. * stack is down). And even if it succeeds, the socket pair will not
  368. * be able to read while localhost is down later (the socket pair may
  369. * even close, depending on OS-specific timeouts).
  370. *
  371. * Returns 0 on success and -errno on failure; do not rely on the value
  372. * of errno or WSAGetLastError().
  373. **/
  374. /* It would be nicer just to set errno, but that won't work for windows. */
  375. int
  376. tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
  377. {
  378. //don't use win32 socketpairs (they are always bad)
  379. #if defined(HAVE_SOCKETPAIR) && !defined(_WIN32)
  380. int r;
  381. #ifdef SOCK_CLOEXEC
  382. r = socketpair(family, type|SOCK_CLOEXEC, protocol, fd);
  383. if (r == 0)
  384. goto sockets_ok;
  385. /* If we got an error, see if it is EINVAL. EINVAL might indicate that,
  386. * even though we were built on a system with SOCK_CLOEXEC support, we
  387. * are running on one without. */
  388. if (errno != EINVAL)
  389. return -errno;
  390. #endif /* defined(SOCK_CLOEXEC) */
  391. r = socketpair(family, type, protocol, fd);
  392. if (r < 0)
  393. return -errno;
  394. #if defined(FD_CLOEXEC)
  395. if (SOCKET_OK(fd[0])) {
  396. r = fcntl(fd[0], F_SETFD, FD_CLOEXEC);
  397. if (r == -1) {
  398. close(fd[0]);
  399. close(fd[1]);
  400. return -errno;
  401. }
  402. }
  403. if (SOCKET_OK(fd[1])) {
  404. r = fcntl(fd[1], F_SETFD, FD_CLOEXEC);
  405. if (r == -1) {
  406. close(fd[0]);
  407. close(fd[1]);
  408. return -errno;
  409. }
  410. }
  411. #endif /* defined(FD_CLOEXEC) */
  412. goto sockets_ok; /* So that sockets_ok will not be unused. */
  413. sockets_ok:
  414. socket_accounting_lock();
  415. if (SOCKET_OK(fd[0])) {
  416. ++n_sockets_open;
  417. mark_socket_open(fd[0]);
  418. }
  419. if (SOCKET_OK(fd[1])) {
  420. ++n_sockets_open;
  421. mark_socket_open(fd[1]);
  422. }
  423. socket_accounting_unlock();
  424. return 0;
  425. #else /* !(defined(HAVE_SOCKETPAIR) && !defined(_WIN32)) */
  426. return tor_ersatz_socketpair(family, type, protocol, fd);
  427. #endif /* defined(HAVE_SOCKETPAIR) && !defined(_WIN32) */
  428. }
  429. /** Mockable wrapper for getsockname(). */
  430. MOCK_IMPL(int,
  431. tor_getsockname,(tor_socket_t sock, struct sockaddr *address,
  432. socklen_t *address_len))
  433. {
  434. return getsockname(sock, address, address_len);
  435. }
  436. /**
  437. * Find the local address associated with the socket <b>sock</b>, and
  438. * place it in *<b>addr_out</b>. Return 0 on success, -1 on failure.
  439. *
  440. * (As tor_getsockname, but instead places the result in a tor_addr_t.) */
  441. int
  442. tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock)
  443. {
  444. struct sockaddr_storage ss;
  445. socklen_t ss_len = sizeof(ss);
  446. memset(&ss, 0, sizeof(ss));
  447. if (tor_getsockname(sock, (struct sockaddr *) &ss, &ss_len) < 0)
  448. return -1;
  449. return tor_addr_from_sockaddr(addr_out, (struct sockaddr *)&ss, NULL);
  450. }
  451. /** Turn <b>socket</b> into a nonblocking socket. Return 0 on success, -1
  452. * on failure.
  453. */
  454. int
  455. set_socket_nonblocking(tor_socket_t sock)
  456. {
  457. #if defined(_WIN32)
  458. unsigned long nonblocking = 1;
  459. ioctlsocket(sock, FIONBIO, (unsigned long*) &nonblocking);
  460. #else
  461. int flags;
  462. flags = fcntl(sock, F_GETFL, 0);
  463. if (flags == -1) {
  464. log_warn(LD_NET, "Couldn't get file status flags: %s", strerror(errno));
  465. return -1;
  466. }
  467. flags |= O_NONBLOCK;
  468. if (fcntl(sock, F_SETFL, flags) == -1) {
  469. log_warn(LD_NET, "Couldn't set file status flags: %s", strerror(errno));
  470. return -1;
  471. }
  472. #endif /* defined(_WIN32) */
  473. return 0;
  474. }
  475. /** Read from <b>sock</b> to <b>buf</b>, until we get <b>count</b> bytes or
  476. * reach the end of the file. Return the number of bytes read, or -1 on
  477. * error. Only use if fd is a blocking fd. */
  478. ssize_t
  479. read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
  480. {
  481. size_t numread = 0;
  482. ssize_t result;
  483. if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
  484. errno = EINVAL;
  485. return -1;
  486. }
  487. while (numread < count) {
  488. result = tor_socket_recv(sock, buf+numread, count-numread, 0);
  489. if (result<0)
  490. return -1;
  491. else if (result == 0)
  492. break;
  493. numread += result;
  494. }
  495. return (ssize_t)numread;
  496. }
  497. /** Write <b>count</b> bytes from <b>buf</b> to <b>sock</b>. Return the number
  498. * of bytes written, or -1 on error. Only use if fd is a blocking fd. */
  499. ssize_t
  500. write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
  501. {
  502. size_t written = 0;
  503. ssize_t result;
  504. raw_assert(count < SSIZE_MAX);
  505. while (written != count) {
  506. result = tor_socket_send(fd, buf+written, count-written, 0);
  507. if (result<0)
  508. return -1;
  509. written += result;
  510. }
  511. return (ssize_t)count;
  512. }
  513. /**
  514. * On Windows, WSAEWOULDBLOCK is not always correct: when you see it,
  515. * you need to ask the socket for its actual errno. Also, you need to
  516. * get your errors from WSAGetLastError, not errno. (If you supply a
  517. * socket of -1, we check WSAGetLastError, but don't correct
  518. * WSAEWOULDBLOCKs.)
  519. *
  520. * The upshot of all of this is that when a socket call fails, you
  521. * should call tor_socket_errno <em>at most once</em> on the failing
  522. * socket to get the error.
  523. */
  524. #if defined(_WIN32)
  525. int
  526. tor_socket_errno(tor_socket_t sock)
  527. {
  528. int optval, optvallen=sizeof(optval);
  529. int err = WSAGetLastError();
  530. if (err == WSAEWOULDBLOCK && SOCKET_OK(sock)) {
  531. if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
  532. return err;
  533. if (optval)
  534. return optval;
  535. }
  536. return err;
  537. }
  538. #endif /* defined(_WIN32) */
  539. #if defined(_WIN32)
  540. #define E(code, s) { code, (s " [" #code " ]") }
  541. struct { int code; const char *msg; } windows_socket_errors[] = {
  542. E(WSAEINTR, "Interrupted function call"),
  543. E(WSAEACCES, "Permission denied"),
  544. E(WSAEFAULT, "Bad address"),
  545. E(WSAEINVAL, "Invalid argument"),
  546. E(WSAEMFILE, "Too many open files"),
  547. E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
  548. E(WSAEINPROGRESS, "Operation now in progress"),
  549. E(WSAEALREADY, "Operation already in progress"),
  550. E(WSAENOTSOCK, "Socket operation on nonsocket"),
  551. E(WSAEDESTADDRREQ, "Destination address required"),
  552. E(WSAEMSGSIZE, "Message too long"),
  553. E(WSAEPROTOTYPE, "Protocol wrong for socket"),
  554. E(WSAENOPROTOOPT, "Bad protocol option"),
  555. E(WSAEPROTONOSUPPORT, "Protocol not supported"),
  556. E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
  557. /* What's the difference between NOTSUPP and NOSUPPORT? :) */
  558. E(WSAEOPNOTSUPP, "Operation not supported"),
  559. E(WSAEPFNOSUPPORT, "Protocol family not supported"),
  560. E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
  561. E(WSAEADDRINUSE, "Address already in use"),
  562. E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
  563. E(WSAENETDOWN, "Network is down"),
  564. E(WSAENETUNREACH, "Network is unreachable"),
  565. E(WSAENETRESET, "Network dropped connection on reset"),
  566. E(WSAECONNABORTED, "Software caused connection abort"),
  567. E(WSAECONNRESET, "Connection reset by peer"),
  568. E(WSAENOBUFS, "No buffer space available"),
  569. E(WSAEISCONN, "Socket is already connected"),
  570. E(WSAENOTCONN, "Socket is not connected"),
  571. E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
  572. E(WSAETIMEDOUT, "Connection timed out"),
  573. E(WSAECONNREFUSED, "Connection refused"),
  574. E(WSAEHOSTDOWN, "Host is down"),
  575. E(WSAEHOSTUNREACH, "No route to host"),
  576. E(WSAEPROCLIM, "Too many processes"),
  577. /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
  578. E(WSASYSNOTREADY, "Network subsystem is unavailable"),
  579. E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
  580. E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
  581. E(WSAEDISCON, "Graceful shutdown now in progress"),
  582. #ifdef WSATYPE_NOT_FOUND
  583. E(WSATYPE_NOT_FOUND, "Class type not found"),
  584. #endif
  585. E(WSAHOST_NOT_FOUND, "Host not found"),
  586. E(WSATRY_AGAIN, "Nonauthoritative host not found"),
  587. E(WSANO_RECOVERY, "This is a nonrecoverable error"),
  588. E(WSANO_DATA, "Valid name, no data record of requested type)"),
  589. /* There are some more error codes whose numeric values are marked
  590. * <b>OS dependent</b>. They start with WSA_, apparently for the same
  591. * reason that practitioners of some craft traditions deliberately
  592. * introduce imperfections into their baskets and rugs "to allow the
  593. * evil spirits to escape." If we catch them, then our binaries
  594. * might not report consistent results across versions of Windows.
  595. * Thus, I'm going to let them all fall through.
  596. */
  597. { -1, NULL },
  598. };
  599. /** There does not seem to be a strerror equivalent for Winsock errors.
  600. * Naturally, we have to roll our own.
  601. */
  602. const char *
  603. tor_socket_strerror(int e)
  604. {
  605. int i;
  606. for (i=0; windows_socket_errors[i].code >= 0; ++i) {
  607. if (e == windows_socket_errors[i].code)
  608. return windows_socket_errors[i].msg;
  609. }
  610. return strerror(e);
  611. }
  612. #endif /* defined(_WIN32) */