compat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char compat_c_id[] = "$Id$";
  5. /* This is required on rh7 to make strptime not complain.
  6. */
  7. #define _GNU_SOURCE
  8. #include "orconfig.h"
  9. #include "compat.h"
  10. #ifdef MS_WINDOWS
  11. #include <process.h>
  12. #endif
  13. #ifdef HAVE_UNAME
  14. #include <sys/utsname.h>
  15. #endif
  16. #ifdef HAVE_SYS_TIME_H
  17. #include <sys/time.h>
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifdef HAVE_SYS_FCNTL_H
  23. #include <sys/fcntl.h>
  24. #endif
  25. #ifdef HAVE_PWD_H
  26. #include <pwd.h>
  27. #endif
  28. #ifdef HAVE_GRP_H
  29. #include <grp.h>
  30. #endif
  31. #ifdef HAVE_FCNTL_H
  32. #include <fcntl.h>
  33. #endif
  34. #ifdef HAVE_SYS_RESOURCE_H
  35. #include <sys/resource.h>
  36. #endif
  37. #ifdef HAVE_ERRNO_H
  38. #include <errno.h>
  39. #endif
  40. #ifdef HAVE_NETINET_IN_H
  41. #include <netinet/in.h>
  42. #endif
  43. #ifdef HAVE_ARPA_INET_H
  44. #include <arpa/inet.h>
  45. #endif
  46. #ifndef HAVE_GETTIMEOFDAY
  47. #ifdef HAVE_FTIME
  48. #include <sys/timeb.h>
  49. #endif
  50. #endif
  51. #ifdef HAVE_SYS_SOCKET_H
  52. #include <sys/socket.h>
  53. #endif
  54. #ifdef HAVE_SYS_PARAM_H
  55. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  56. #endif
  57. #include <stdarg.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <assert.h>
  62. #include "log.h"
  63. #include "util.h"
  64. /* Inline the strl functions if the platform doesn't have them. */
  65. #ifndef HAVE_STRLCPY
  66. #include "strlcpy.c"
  67. #endif
  68. #ifndef HAVE_STRLCAT
  69. #include "strlcat.c"
  70. #endif
  71. /** Replacement for snprintf. Differs from platform snprintf in two
  72. * ways: First, always NUL-terminates its output. Second, always
  73. * returns -1 if the result is truncated. (Note that this return
  74. * behavior does <i>not</i> conform to C99; it just happens to be the
  75. * easiest to emulate "return -1" with conformant implementations than
  76. * it is to emulate "return number that would be written" with
  77. * non-conformant implementations.) */
  78. int tor_snprintf(char *str, size_t size, const char *format, ...)
  79. {
  80. va_list ap;
  81. int r;
  82. va_start(ap,format);
  83. r = tor_vsnprintf(str,size,format,ap);
  84. va_end(ap);
  85. return r;
  86. }
  87. /** Replacement for vsnprintf; behavior differs as tor_snprintf differs from
  88. * snprintf.
  89. */
  90. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  91. {
  92. int r;
  93. if (size == 0)
  94. return -1; /* no place for the NUL */
  95. #ifdef MS_WINDOWS
  96. r = _vsnprintf(str, size, format, args);
  97. #else
  98. r = vsnprintf(str, size, format, args);
  99. #endif
  100. str[size-1] = '\0';
  101. if (r < 0 || ((size_t)r) >= size)
  102. return -1;
  103. return r;
  104. }
  105. #ifndef UNALIGNED_INT_ACCESS_OK
  106. /**
  107. * Read a 16-bit value beginning at <b>cp</b>. Equaivalent to
  108. * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
  109. * unaligned memory access.
  110. */
  111. uint16_t get_uint16(const char *cp)
  112. {
  113. uint16_t v;
  114. memcpy(&v,cp,2);
  115. return v;
  116. }
  117. /**
  118. * Read a 32-bit value beginning at <b>cp</b>. Equaivalent to
  119. * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
  120. * unaligned memory access.
  121. */
  122. uint32_t get_uint32(const char *cp)
  123. {
  124. uint32_t v;
  125. memcpy(&v,cp,4);
  126. return v;
  127. }
  128. /**
  129. * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
  130. * *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
  131. * unaligned memory access. */
  132. void set_uint16(char *cp, uint16_t v)
  133. {
  134. memcpy(cp,&v,2);
  135. }
  136. /**
  137. * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
  138. * *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
  139. * unaligned memory access. */
  140. void set_uint32(char *cp, uint32_t v)
  141. {
  142. memcpy(cp,&v,4);
  143. }
  144. #endif
  145. /**
  146. * Rename the file 'from' to the file 'to'. On unix, this is the same as
  147. * rename(2). On windows, this removes 'to' first if it already exists.
  148. * Returns 0 on success. Returns -1 and sets errno on failure.
  149. */
  150. int replace_file(const char *from, const char *to)
  151. {
  152. #ifndef MS_WINDOWS
  153. return rename(from,to);
  154. #else
  155. switch (file_status(to))
  156. {
  157. case FN_NOENT:
  158. break;
  159. case FN_FILE:
  160. if (unlink(to)) return -1;
  161. break;
  162. case FN_ERROR:
  163. return -1;
  164. case FN_DIR:
  165. errno = EISDIR;
  166. return -1;
  167. }
  168. return rename(from,to);
  169. #endif
  170. }
  171. /** Turn <b>socket</b> into a nonblocking socket.
  172. */
  173. void set_socket_nonblocking(int socket)
  174. {
  175. #ifdef MS_WINDOWS
  176. int nonblocking = 1;
  177. ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
  178. #else
  179. fcntl(socket, F_SETFL, O_NONBLOCK);
  180. #endif
  181. }
  182. /**
  183. * Allocate a pair of connected sockets. (Like socketpair(family,
  184. * type,protocol,fd), but works on systems that don't have
  185. * socketpair.)
  186. *
  187. * Currently, only (AF_UNIX, SOCK_STREAM, 0 ) sockets are supported.
  188. *
  189. * Note that on systems without socketpair, this call will fail if
  190. * localhost is inaccessible (for example, if the networking
  191. * stack is down). And even if it succeeds, the socket pair will not
  192. * be able to read while localhost is down later (the socket pair may
  193. * even close, depending on OS-specific timeouts).
  194. **/
  195. int
  196. tor_socketpair(int family, int type, int protocol, int fd[2])
  197. {
  198. #ifdef HAVE_SOCKETPAIR
  199. return socketpair(family, type, protocol, fd);
  200. #else
  201. /* This socketpair does not work when localhost is down. So
  202. * it's really not the same thing at all. But it's close enough
  203. * for now, and really, when localhost is down sometimes, we
  204. * have other problems too.
  205. */
  206. int listener = -1;
  207. int connector = -1;
  208. int acceptor = -1;
  209. struct sockaddr_in listen_addr;
  210. struct sockaddr_in connect_addr;
  211. int size;
  212. if (protocol
  213. #ifdef AF_UNIX
  214. || family != AF_UNIX
  215. #endif
  216. ) {
  217. #ifdef MS_WINDOWS
  218. errno = WSAEAFNOSUPPORT;
  219. #else
  220. errno = EAFNOSUPPORT;
  221. #endif
  222. return -1;
  223. }
  224. if (!fd) {
  225. errno = EINVAL;
  226. return -1;
  227. }
  228. listener = socket(AF_INET, type, 0);
  229. if (listener == -1)
  230. return -1;
  231. memset(&listen_addr, 0, sizeof(listen_addr));
  232. listen_addr.sin_family = AF_INET;
  233. listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  234. listen_addr.sin_port = 0; /* kernel choses port. */
  235. if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
  236. == -1)
  237. goto tidy_up_and_fail;
  238. if (listen(listener, 1) == -1)
  239. goto tidy_up_and_fail;
  240. connector = socket(AF_INET, type, 0);
  241. if (connector == -1)
  242. goto tidy_up_and_fail;
  243. /* We want to find out the port number to connect to. */
  244. size = sizeof(connect_addr);
  245. if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
  246. goto tidy_up_and_fail;
  247. if (size != sizeof (connect_addr))
  248. goto abort_tidy_up_and_fail;
  249. if (connect(connector, (struct sockaddr *) &connect_addr,
  250. sizeof(connect_addr)) == -1)
  251. goto tidy_up_and_fail;
  252. size = sizeof(listen_addr);
  253. acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
  254. if (acceptor == -1)
  255. goto tidy_up_and_fail;
  256. if (size != sizeof(listen_addr))
  257. goto abort_tidy_up_and_fail;
  258. tor_close_socket(listener);
  259. /* Now check we are talking to ourself by matching port and host on the
  260. two sockets. */
  261. if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
  262. goto tidy_up_and_fail;
  263. if (size != sizeof (connect_addr)
  264. || listen_addr.sin_family != connect_addr.sin_family
  265. || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
  266. || listen_addr.sin_port != connect_addr.sin_port) {
  267. goto abort_tidy_up_and_fail;
  268. }
  269. fd[0] = connector;
  270. fd[1] = acceptor;
  271. return 0;
  272. abort_tidy_up_and_fail:
  273. #ifdef MS_WINDOWS
  274. errno = WSAECONNABORTED;
  275. #else
  276. errno = ECONNABORTED; /* I hope this is portable and appropriate. */
  277. #endif
  278. tidy_up_and_fail:
  279. {
  280. int save_errno = errno;
  281. if (listener != -1)
  282. tor_close_socket(listener);
  283. if (connector != -1)
  284. tor_close_socket(connector);
  285. if (acceptor != -1)
  286. tor_close_socket(acceptor);
  287. errno = save_errno;
  288. return -1;
  289. }
  290. #endif
  291. }
  292. /** Get the maximum allowed number of file descriptors. (Some systems
  293. * have a low soft limit.) Make sure we set it to at least
  294. * <b>required_min</b>. Return 0 if we can, or -1 if we fail. */
  295. int set_max_file_descriptors(unsigned int required_min) {
  296. #ifndef HAVE_GETRLIMIT
  297. log_fn(LOG_INFO,"This platform is missing getrlimit(). Proceeding.");
  298. return 0; /* hope we'll be ok */
  299. #else
  300. struct rlimit rlim;
  301. if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  302. log_fn(LOG_WARN, "Could not get maximum number of file descriptors: %s",
  303. strerror(errno));
  304. return -1;
  305. }
  306. if (required_min > rlim.rlim_max) {
  307. log_fn(LOG_WARN,"We need %u file descriptors available, and we're limited to %lu. Please change your ulimit.", required_min, (unsigned long int)rlim.rlim_max);
  308. return -1;
  309. }
  310. if (required_min > rlim.rlim_cur) {
  311. log_fn(LOG_INFO,"Raising max file descriptors from %lu to %lu.",
  312. (unsigned long int)rlim.rlim_cur, (unsigned long int)rlim.rlim_max);
  313. }
  314. rlim.rlim_cur = rlim.rlim_max;
  315. if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
  316. log_fn(LOG_WARN, "Could not set maximum number of file descriptors: %s",
  317. strerror(errno));
  318. return -1;
  319. }
  320. return 0;
  321. #endif
  322. }
  323. /** Call setuid and setgid to run as <b>user</b>:<b>group</b>. Return 0 on
  324. * success. On failure, log and return -1.
  325. */
  326. int switch_id(char *user, char *group) {
  327. #ifndef MS_WINDOWS
  328. struct passwd *pw = NULL;
  329. struct group *gr = NULL;
  330. if (user) {
  331. pw = getpwnam(user);
  332. if (pw == NULL) {
  333. log_fn(LOG_ERR,"User '%s' not found.", user);
  334. return -1;
  335. }
  336. }
  337. /* switch the group first, while we still have the privileges to do so */
  338. if (group) {
  339. gr = getgrnam(group);
  340. if (gr == NULL) {
  341. log_fn(LOG_ERR,"Group '%s' not found.", group);
  342. return -1;
  343. }
  344. if (setgid(gr->gr_gid) != 0) {
  345. log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
  346. return -1;
  347. }
  348. } else if (user) {
  349. if (setgid(pw->pw_gid) != 0) {
  350. log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
  351. return -1;
  352. }
  353. }
  354. /* now that the group is switched, we can switch users and lose
  355. privileges */
  356. if (user) {
  357. if (setuid(pw->pw_uid) != 0) {
  358. log_fn(LOG_ERR,"Error setting UID: %s", strerror(errno));
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. #endif
  364. log_fn(LOG_ERR,
  365. "User or group specified, but switching users is not supported.");
  366. return -1;
  367. }
  368. #ifdef HAVE_PWD_H
  369. /** Allocate and return a string containing the home directory for the
  370. * user <b>username</b>. Only works on posix-like systems */
  371. char *
  372. get_user_homedir(const char *username)
  373. {
  374. struct passwd *pw;
  375. tor_assert(username);
  376. if (!(pw = getpwnam(username))) {
  377. log_fn(LOG_ERR,"User '%s' not found.", username);
  378. return NULL;
  379. }
  380. return tor_strdup(pw->pw_dir);
  381. }
  382. #endif
  383. /** Set *addr to the IP address (in dotted-quad notation) stored in c.
  384. * Return 1 on success, 0 if c is badly formatted. (Like inet_aton(c,addr),
  385. * but works on Windows and Solaris.)
  386. */
  387. int tor_inet_aton(const char *c, struct in_addr* addr)
  388. {
  389. #ifdef HAVE_INET_ATON
  390. return inet_aton(c, addr);
  391. #else
  392. uint32_t r;
  393. tor_assert(c);
  394. tor_assert(addr);
  395. if (strcmp(c, "255.255.255.255") == 0) {
  396. addr->s_addr = 0xFFFFFFFFu;
  397. return 1;
  398. }
  399. r = inet_addr(c);
  400. if (r == INADDR_NONE)
  401. return 0;
  402. addr->s_addr = r;
  403. return 1;
  404. #endif
  405. }
  406. /* Hold the result of our call to <b>uname</b>. */
  407. static char uname_result[256];
  408. /* True iff uname_result is set. */
  409. static int uname_result_is_set = 0;
  410. /* Return a pointer to a description of our platform.
  411. */
  412. const char *
  413. get_uname(void)
  414. {
  415. #ifdef HAVE_UNAME
  416. struct utsname u;
  417. #endif
  418. if (!uname_result_is_set) {
  419. #ifdef HAVE_UNAME
  420. if (uname(&u) != -1) {
  421. /* (linux says 0 is success, solaris says 1 is success) */
  422. tor_snprintf(uname_result, sizeof(uname_result), "%s %s %s",
  423. u.sysname, u.nodename, u.machine);
  424. } else
  425. #endif
  426. {
  427. strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
  428. }
  429. uname_result_is_set = 1;
  430. }
  431. return uname_result;
  432. }
  433. /*
  434. * Process control
  435. */
  436. /** Minimalist interface to run a void function in the background. On
  437. * unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  438. * func should not return, but rather should call spawn_exit.
  439. */
  440. int
  441. spawn_func(int (*func)(void *), void *data)
  442. {
  443. #ifdef MS_WINDOWS
  444. int rv;
  445. rv = _beginthread(func, 0, data);
  446. if (rv == (unsigned long) -1)
  447. return -1;
  448. return 0;
  449. #else
  450. pid_t pid;
  451. pid = fork();
  452. if (pid<0)
  453. return -1;
  454. if (pid==0) {
  455. /* Child */
  456. func(data);
  457. tor_assert(0); /* Should never reach here. */
  458. return 0; /* suppress "control-reaches-end-of-non-void" warning. */
  459. } else {
  460. /* Parent */
  461. return 0;
  462. }
  463. #endif
  464. }
  465. /** End the current thread/process.
  466. */
  467. void spawn_exit()
  468. {
  469. #ifdef MS_WINDOWS
  470. _endthread();
  471. #else
  472. exit(0);
  473. #endif
  474. }
  475. /** Set *timeval to the current time of day. On error, log and terminate.
  476. * (Same as gettimeofday(timeval,NULL), but never returns -1.)
  477. */
  478. void tor_gettimeofday(struct timeval *timeval) {
  479. #ifdef HAVE_GETTIMEOFDAY
  480. if (gettimeofday(timeval, NULL)) {
  481. log_fn(LOG_ERR, "gettimeofday failed.");
  482. /* If gettimeofday dies, we have either given a bad timezone (we didn't),
  483. or segfaulted.*/
  484. exit(1);
  485. }
  486. #elif defined(HAVE_FTIME)
  487. struct timeb tb;
  488. ftime(&tb);
  489. timeval->tv_sec = tb.time;
  490. timeval->tv_usec = tb.millitm * 1000;
  491. #else
  492. #error "No way to get time."
  493. #endif
  494. return;
  495. }
  496. #ifndef MS_WINDOWS
  497. struct tor_mutex_t {
  498. };
  499. tor_mutex_t *tor_mutex_new(void) { return NULL; }
  500. void tor_mutex_acquire(tor_mutex_t *m) { }
  501. void tor_mutex_release(tor_mutex_t *m) { }
  502. void tor_mutex_free(tor_mutex_t *m) { }
  503. #else
  504. struct tor_mutex_t {
  505. HANDLE handle;
  506. };
  507. tor_mutex_t *tor_mutex_new(void)
  508. {
  509. tor_mutex_t *m;
  510. m = tor_malloc_zero(sizeof(tor_mutex_t));
  511. m->handle = CreateMutex(NULL, FALSE, NULL);
  512. tor_assert(m->handle != NULL);
  513. return m;
  514. }
  515. void tor_mutex_free(tor_mutex_t *m)
  516. {
  517. CloseHandle(m->handle);
  518. tor_free(m);
  519. }
  520. void tor_mutex_acquire(tor_mutex_t *m)
  521. {
  522. DWORD r;
  523. r = WaitForSingleObject(m->handle, INFINITE);
  524. switch (r) {
  525. case WAIT_ABANDONED: /* holding thread exited. */
  526. case WAIT_OBJECT_0: /* we got the mutex normally. */
  527. break;
  528. case WAIT_TIMEOUT: /* Should never happen. */
  529. tor_assert(0);
  530. break;
  531. case WAIT_FAILED:
  532. log_fn(LOG_WARN, "Failed to acquire mutex: %d", GetLastError());
  533. }
  534. }
  535. void tor_mutex_release(tor_mutex_t *m)
  536. {
  537. BOOL r;
  538. r = ReleaseMutex(m->handle);
  539. if (!r) {
  540. log_fn(LOG_WARN, "Failed to release mutex: %d", GetLastError());
  541. }
  542. }
  543. #endif
  544. /**
  545. * On Windows, WSAEWOULDBLOCK is not always correct: when you see it,
  546. * you need to ask the socket for its actual errno. Also, you need to
  547. * get your errors from WSAGetLastError, not errno. (If you supply a
  548. * socket of -1, we check WSAGetLastError, but don't correct
  549. * WSAEWOULDBLOCKs.)
  550. *
  551. * The upshot of all of this is that when a socket call fails, you
  552. * should call tor_socket_errno <em>at most once</em> on the failing
  553. * socket to get the error.
  554. */
  555. #ifdef MS_WINDOWS
  556. int tor_socket_errno(int sock)
  557. {
  558. int optval, optvallen=sizeof(optval);
  559. int err = WSAGetLastError();
  560. if (err == WSAEWOULDBLOCK && sock >= 0) {
  561. if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
  562. return err;
  563. if (optval)
  564. return optval;
  565. }
  566. return err;
  567. }
  568. #endif
  569. #ifdef MS_WINDOWS
  570. #define E(code, s) { code, (s " [" #code " ]") }
  571. struct { int code; const char *msg; } windows_socket_errors[] = {
  572. E(WSAEINTR, "Interrupted function call"),
  573. E(WSAEACCES, "Permission denied"),
  574. E(WSAEFAULT, "Bad address"),
  575. E(WSAEINVAL, "Invalid argument"),
  576. E(WSAEMFILE, "Too many open files"),
  577. E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
  578. E(WSAEINPROGRESS, "Operation now in progress"),
  579. E(WSAEALREADY, "Operation already in progress"),
  580. E(WSAENOTSOCK, "Socket operation on nonsocket"),
  581. E(WSAEDESTADDRREQ, "Destination address required"),
  582. E(WSAEMSGSIZE, "Message too long"),
  583. E(WSAEPROTOTYPE, "Protocol wrong for socket"),
  584. E(WSAENOPROTOOPT, "Bad protocol option"),
  585. E(WSAEPROTONOSUPPORT, "Protocol not supported"),
  586. E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
  587. /* What's the difference between NOTSUPP and NOSUPPORT? :) */
  588. E(WSAEOPNOTSUPP, "Operation not supported"),
  589. E(WSAEPFNOSUPPORT, "Protocol family not supported"),
  590. E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
  591. E(WSAEADDRINUSE, "Address already in use"),
  592. E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
  593. E(WSAENETDOWN, "Network is down"),
  594. E(WSAENETUNREACH, "Network is unreachable"),
  595. E(WSAENETRESET, "Network dropped connection on reset"),
  596. E(WSAECONNABORTED, "Software caused connection abort"),
  597. E(WSAECONNRESET, "Connection reset by peer"),
  598. E(WSAENOBUFS, "No buffer space avaialable"),
  599. E(WSAEISCONN, "Socket is already connected"),
  600. E(WSAENOTCONN, "Socket is not connected"),
  601. E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
  602. E(WSAETIMEDOUT, "Connection timed out"),
  603. E(WSAECONNREFUSED, "Connection refused"),
  604. E(WSAEHOSTDOWN, "Host is down"),
  605. E(WSAEHOSTUNREACH, "No route to host"),
  606. E(WSAEPROCLIM, "Too many processes"),
  607. /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
  608. E(WSASYSNOTREADY, "Network subsystem is unavailable"),
  609. E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
  610. E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
  611. E(WSAEDISCON, "Graceful shutdown now in progress"),
  612. #ifdef WSATYPE_NOT_FOUND
  613. E(WSATYPE_NOT_FOUND, "Class type not found"),
  614. #endif
  615. E(WSAHOST_NOT_FOUND, "Host not found"),
  616. E(WSATRY_AGAIN, "Nonauthoritative host not found"),
  617. E(WSANO_RECOVERY, "This is a nonrecoverable error"),
  618. E(WSANO_DATA, "Valid name, no data record of requested type)"),
  619. /* There are some more error codes whose numeric values are marked
  620. * <b>OS dependent</b>. They start with WSA_, apparently for the same
  621. * reason that practitioners of some craft traditions deliberately
  622. * introduce imperfections into their baskets and rugs "to allow the
  623. * evil spirits to escape." If we catch them, then our binaries
  624. * might not report consistent results across versions of Windows.
  625. * Thus, I'm going to let them all fall through.
  626. */
  627. { -1, NULL },
  628. };
  629. /** There does not seem to be a strerror equivalent for winsock errors.
  630. * Naturally, we have to roll our own.
  631. */
  632. const char *tor_socket_strerror(int e)
  633. {
  634. int i;
  635. for (i=0; windows_socket_errors[i].code >= 0; ++i) {
  636. if (e == windows_socket_errors[i].code)
  637. return windows_socket_errors[i].msg;
  638. }
  639. return strerror(e);
  640. }
  641. #endif