compat.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef _TOR_COMPAT_H
  6. #define _TOR_COMPAT_H
  7. #include "orconfig.h"
  8. #include "torint.h"
  9. #ifdef MS_WINDOWS
  10. #ifndef WIN32_WINNT
  11. #define WIN32_WINNT 0x400
  12. #endif
  13. #ifndef _WIN32_WINNT
  14. #define _WIN32_WINNT 0x400
  15. #endif
  16. #define WIN32_LEAN_AND_MEAN
  17. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  18. #include <winsock.h>
  19. #else
  20. #include <winsock2.h>
  21. #include <ws2tcpip.h>
  22. #endif
  23. #endif
  24. #ifdef HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif
  27. #ifdef HAVE_SYS_TIME_H
  28. #include <sys/time.h>
  29. #endif
  30. #ifdef HAVE_TIME_H
  31. #include <time.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #if defined(HAVE_PTHREAD_H) && !defined(MS_WINDOWS)
  37. #include <pthread.h>
  38. #endif
  39. #include <stdarg.h>
  40. #ifdef HAVE_SYS_RESOURCE_H
  41. #include <sys/resource.h>
  42. #endif
  43. #ifdef HAVE_SYS_SOCKET_H
  44. #include <sys/socket.h>
  45. #endif
  46. #ifdef HAVE_SYS_TYPES_H
  47. #include <sys/types.h>
  48. #endif
  49. #ifdef HAVE_NETINET_IN_H
  50. #include <netinet/in.h>
  51. #endif
  52. #ifdef HAVE_NETINET6_IN6_H
  53. #include <netinet6/in6.h>
  54. #endif
  55. #include <stdio.h>
  56. #if defined (WINCE)
  57. #include <fcntl.h>
  58. #include <io.h>
  59. #include <math.h>
  60. #include <projects.h>
  61. #define snprintf _snprintf
  62. /* this is not exported as W .... */
  63. #define SHGetPathFromIDListW SHGetPathFromIDList
  64. /* wcecompat has vasprintf */
  65. #define HAVE_VASPRINTF
  66. /* no service here */
  67. #ifdef NT_SERVICE
  68. #undef NT_SERVICE
  69. #endif
  70. #endif // WINCE
  71. #ifndef NULL_REP_IS_ZERO_BYTES
  72. #error "It seems your platform does not represent NULL as zero. We can't cope."
  73. #endif
  74. #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
  75. #error "It seems that you encode characters in something other than ASCII."
  76. #endif
  77. /* ===== Compiler compatibility */
  78. /* GCC can check printf types on arbitrary functions. */
  79. #ifdef __GNUC__
  80. #define CHECK_PRINTF(formatIdx, firstArg) \
  81. __attribute__ ((format(printf, formatIdx, firstArg)))
  82. #else
  83. #define CHECK_PRINTF(formatIdx, firstArg)
  84. #endif
  85. /* inline is __inline on windows. */
  86. #ifdef MS_WINDOWS
  87. #define INLINE __inline
  88. #else
  89. #define INLINE inline
  90. #endif
  91. /* Try to get a reasonable __func__ substitute in place. */
  92. #if defined(_MSC_VER)
  93. /* MSVC compilers before VC7 don't have __func__ at all; later ones call it
  94. * __FUNCTION__. */
  95. #if _MSC_VER < 1300
  96. #define __func__ "???"
  97. #else
  98. #define __func__ __FUNCTION__
  99. #endif
  100. #else
  101. /* For platforms where autoconf works, make sure __func__ is defined
  102. * sanely. */
  103. #ifndef HAVE_MACRO__func__
  104. #ifdef HAVE_MACRO__FUNCTION__
  105. #define __func__ __FUNCTION__
  106. #elif HAVE_MACRO__FUNC__
  107. #define __func__ __FUNC__
  108. #else
  109. #define __func__ "???"
  110. #endif
  111. #endif /* ifndef MAVE_MACRO__func__ */
  112. #endif /* if not windows */
  113. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  114. /* MSVC versions before 7 apparently don't believe that you can cast uint64_t
  115. * to double and really mean it. */
  116. extern INLINE double U64_TO_DBL(uint64_t x) {
  117. int64_t i = (int64_t) x;
  118. return (i < 0) ? ((double) INT64_MAX) : (double) i;
  119. }
  120. #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
  121. #else
  122. #define U64_TO_DBL(x) ((double) (x))
  123. #define DBL_TO_U64(x) ((uint64_t) (x))
  124. #endif
  125. /* GCC has several useful attributes. */
  126. #if defined(__GNUC__) && __GNUC__ >= 3
  127. #define ATTR_NORETURN __attribute__((noreturn))
  128. #define ATTR_PURE __attribute__((pure))
  129. #define ATTR_CONST __attribute__((const))
  130. #define ATTR_MALLOC __attribute__((malloc))
  131. #define ATTR_NORETURN __attribute__((noreturn))
  132. /* Alas, nonnull is not at present a good idea for us. We'd like to get
  133. * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
  134. * spottily), but we don't want to tell the compiler to make optimizations
  135. * with the assumption that the argument can't be NULL (since this would make
  136. * many of our checks go away, and make our code less robust against
  137. * programming errors). Unfortunately, nonnull currently does both of these
  138. * things, and there's no good way to split them up.
  139. *
  140. * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
  141. #define ATTR_NONNULL(x)
  142. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  143. * of <b>exp</b> will probably be true.
  144. *
  145. * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
  146. * except that it tells the compiler that the branch will be taken most of the
  147. * time. This can generate slightly better code with some CPUs.
  148. */
  149. #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
  150. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  151. * of <b>exp</b> will probably be false.
  152. *
  153. * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
  154. * except that it tells the compiler that the branch will usually not be
  155. * taken. This can generate slightly better code with some CPUs.
  156. */
  157. #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
  158. #else
  159. #define ATTR_NORETURN
  160. #define ATTR_PURE
  161. #define ATTR_CONST
  162. #define ATTR_MALLOC
  163. #define ATTR_NORETURN
  164. #define ATTR_NONNULL(x)
  165. #define PREDICT_LIKELY(exp) (exp)
  166. #define PREDICT_UNLIKELY(exp) (exp)
  167. #endif
  168. /** Expands to a syntactically valid empty statement. */
  169. #define STMT_NIL (void)0
  170. #ifdef __GNUC__
  171. /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
  172. * the macro can be used as if it were a single C statement. */
  173. #define STMT_BEGIN (void) ({
  174. #define STMT_END })
  175. #elif defined(sun) || defined(__sun__)
  176. #define STMT_BEGIN if (1) {
  177. #define STMT_END } else STMT_NIL
  178. #else
  179. #define STMT_BEGIN do {
  180. #define STMT_END } while (0)
  181. #endif
  182. /* ===== String compatibility */
  183. #ifdef MS_WINDOWS
  184. /* Windows names string functions differently from most other platforms. */
  185. #define strncasecmp _strnicmp
  186. #define strcasecmp _stricmp
  187. #endif
  188. #ifndef HAVE_STRLCAT
  189. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  190. #endif
  191. #ifndef HAVE_STRLCPY
  192. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  193. #endif
  194. #ifdef _MSC_VER
  195. /** Casts the uint64_t value in <b>a</b> to the right type for an argument
  196. * to printf. */
  197. #define U64_PRINTF_ARG(a) (a)
  198. /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
  199. * to scanf. */
  200. #define U64_SCANF_ARG(a) (a)
  201. /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
  202. #define U64_LITERAL(n) (n ## ui64)
  203. #define I64_PRINTF_ARG(a) (a)
  204. #define I64_SCANF_ARG(a) (a)
  205. #define I64_LITERAL(n) (n ## i64)
  206. #else
  207. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  208. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  209. #define U64_LITERAL(n) (n ## llu)
  210. #define I64_PRINTF_ARG(a) ((long long signed int)(a))
  211. #define I64_SCANF_ARG(a) ((long long signed int*)(a))
  212. #define I64_LITERAL(n) (n ## ll)
  213. #endif
  214. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  215. /** The formatting string used to put a uint64_t value in a printf() or
  216. * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
  217. #define U64_FORMAT "%I64u"
  218. #define I64_FORMAT "%I64d"
  219. #else
  220. #define U64_FORMAT "%llu"
  221. #define I64_FORMAT "%lld"
  222. #endif
  223. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  224. * tor_munmap_file. */
  225. typedef struct tor_mmap_t {
  226. const char *data; /**< Mapping of the file's contents. */
  227. size_t size; /**< Size of the file. */
  228. /* None of the fields below should be accessed from outside compat.c */
  229. #ifdef HAVE_SYS_MMAN_H
  230. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  231. * size, rounded up to the nearest page.) */
  232. #elif defined MS_WINDOWS
  233. HANDLE file_handle;
  234. HANDLE mmap_handle;
  235. #endif
  236. } tor_mmap_t;
  237. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  238. void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  239. int tor_snprintf(char *str, size_t size, const char *format, ...)
  240. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  241. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  242. ATTR_NONNULL((1,3));
  243. int tor_asprintf(char **strp, const char *fmt, ...)
  244. CHECK_PRINTF(2,3);
  245. int tor_vasprintf(char **strp, const char *fmt, va_list args);
  246. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  247. size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
  248. static const void *tor_memstr(const void *haystack, size_t hlen,
  249. const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
  250. static INLINE const void *
  251. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  252. {
  253. return tor_memmem(haystack, hlen, needle, strlen(needle));
  254. }
  255. /* Much of the time when we're checking ctypes, we're doing spec compliance,
  256. * which all assumes we're doing ASCII. */
  257. #define DECLARE_CTYPE_FN(name) \
  258. static int TOR_##name(char c); \
  259. extern const uint32_t TOR_##name##_TABLE[]; \
  260. static INLINE int TOR_##name(char c) { \
  261. uint8_t u = c; \
  262. return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
  263. }
  264. DECLARE_CTYPE_FN(ISALPHA)
  265. DECLARE_CTYPE_FN(ISALNUM)
  266. DECLARE_CTYPE_FN(ISSPACE)
  267. DECLARE_CTYPE_FN(ISDIGIT)
  268. DECLARE_CTYPE_FN(ISXDIGIT)
  269. DECLARE_CTYPE_FN(ISPRINT)
  270. DECLARE_CTYPE_FN(ISLOWER)
  271. DECLARE_CTYPE_FN(ISUPPER)
  272. extern const char TOR_TOUPPER_TABLE[];
  273. extern const char TOR_TOLOWER_TABLE[];
  274. #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
  275. #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
  276. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  277. #ifdef HAVE_STRTOK_R
  278. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  279. #else
  280. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  281. #endif
  282. #ifdef MS_WINDOWS
  283. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  284. const char *tor_fix_source_file(const char *fname);
  285. #else
  286. #define _SHORT_FILE_ (__FILE__)
  287. #define tor_fix_source_file(s) (s)
  288. #endif
  289. /* ===== Time compatibility */
  290. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  291. /** Implementation of timeval for platforms that don't have it. */
  292. struct timeval {
  293. time_t tv_sec;
  294. unsigned int tv_usec;
  295. };
  296. #endif
  297. void tor_gettimeofday(struct timeval *timeval);
  298. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  299. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  300. #ifndef timeradd
  301. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  302. * the sum of tv1 and tv2. */
  303. #define timeradd(tv1,tv2,tvout) \
  304. do { \
  305. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  306. (tvout)->tv_usec = (tv2)->tv_usec + (tv2)->tv_usec; \
  307. if ((tvout)->tv_usec >= 1000000) { \
  308. (tvout)->tv_usec -= 1000000; \
  309. (tvout)->tv_sec++; \
  310. } \
  311. } while (0)
  312. #endif
  313. #ifndef timersub
  314. /** Replacement for timersub on platforms that do not have it: sets tvout to
  315. * tv1 minus tv2. */
  316. #define timersub(tv1,tv2,tvout) \
  317. do { \
  318. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  319. (tvout)->tv_usec = (tv2)->tv_usec - (tv2)->tv_usec; \
  320. if ((tvout)->tv_usec < 0) { \
  321. (tvout)->tv_usec += 1000000; \
  322. (tvout)->tv_sec--; \
  323. } \
  324. } while (0)
  325. #endif
  326. #ifndef timercmp
  327. /** Replacement for timersub on platforms that do not have it: returns true
  328. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  329. *
  330. * Note that while this definition should work for all boolean opeators, some
  331. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  332. * don't use those.
  333. */
  334. #define timercmp(tv1,tv2,op) \
  335. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  336. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  337. ((tv1)->tv_sec op (tv2)->tv_sec))
  338. #endif
  339. /* ===== File compatibility */
  340. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  341. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  342. int replace_file(const char *from, const char *to);
  343. int touch_file(const char *fname);
  344. typedef struct tor_lockfile_t tor_lockfile_t;
  345. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  346. int *locked_out);
  347. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  348. off_t tor_fd_getpos(int fd);
  349. int tor_fd_seekend(int fd);
  350. #ifdef MS_WINDOWS
  351. #define PATH_SEPARATOR "\\"
  352. #else
  353. #define PATH_SEPARATOR "/"
  354. #endif
  355. /* ===== Net compatibility */
  356. #if (SIZEOF_SOCKLEN_T == 0)
  357. typedef int socklen_t;
  358. #endif
  359. #ifdef MS_WINDOWS
  360. #define tor_socket_t intptr_t
  361. #define SOCKET_OK(s) ((s) != INVALID_SOCKET)
  362. #else
  363. #define tor_socket_t int
  364. #define SOCKET_OK(s) ((s) >= 0)
  365. #endif
  366. int tor_close_socket(tor_socket_t s);
  367. tor_socket_t tor_open_socket(int domain, int type, int protocol);
  368. tor_socket_t tor_accept_socket(int sockfd, struct sockaddr *addr,
  369. socklen_t *len);
  370. int get_n_open_sockets(void);
  371. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  372. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  373. /** Implementation of struct in6_addr for platforms that do not have it.
  374. * Generally, these platforms are ones without IPv6 support, but we want to
  375. * have a working in6_addr there anyway, so we can use it to parse IPv6
  376. * addresses. */
  377. #if !defined(HAVE_STRUCT_IN6_ADDR)
  378. struct in6_addr
  379. {
  380. union {
  381. uint8_t u6_addr8[16];
  382. uint16_t u6_addr16[8];
  383. uint32_t u6_addr32[4];
  384. } in6_u;
  385. #define s6_addr in6_u.u6_addr8
  386. #define s6_addr16 in6_u.u6_addr16
  387. #define s6_addr32 in6_u.u6_addr32
  388. };
  389. #endif
  390. /** @{ */
  391. /** Many BSD variants seem not to define these. */
  392. #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
  393. || defined(__NetBSD__) || defined(__OpenBSD__)
  394. #ifndef s6_addr16
  395. #define s6_addr16 __u6_addr.__u6_addr16
  396. #endif
  397. #ifndef s6_addr32
  398. #define s6_addr32 __u6_addr.__u6_addr32
  399. #endif
  400. #endif
  401. /** @} */
  402. #ifndef HAVE_SA_FAMILY_T
  403. typedef uint16_t sa_family_t;
  404. #endif
  405. /** @{ */
  406. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  407. * macros get you a pointer to s6_addr32 or local equivalent. */
  408. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  409. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  410. #else
  411. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  412. #endif
  413. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  414. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  415. #else
  416. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  417. #endif
  418. /** @} */
  419. /** Implementation of struct sockaddr_in6 on platforms that do not have
  420. * it. See notes on struct in6_addr. */
  421. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  422. struct sockaddr_in6 {
  423. sa_family_t sin6_family;
  424. uint16_t sin6_port;
  425. // uint32_t sin6_flowinfo;
  426. struct in6_addr sin6_addr;
  427. // uint32_t sin6_scope_id;
  428. };
  429. #endif
  430. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  431. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  432. int tor_inet_pton(int af, const char *src, void *dst);
  433. int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
  434. void set_socket_nonblocking(tor_socket_t socket);
  435. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  436. int network_init(void);
  437. /* For stupid historical reasons, windows sockets have an independent
  438. * set of errnos, and an independent way to get them. Also, you can't
  439. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  440. * errnos against expected values, and use tor_socket_errno to find
  441. * the actual errno after a socket operation fails.
  442. */
  443. #if defined(MS_WINDOWS)
  444. /** Return true if e is EAGAIN or the local equivalent. */
  445. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  446. /** Return true if e is EINPROGRESS or the local equivalent. */
  447. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  448. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  449. * a call to connect(). */
  450. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  451. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  452. /** Return true if e is EAGAIN or another error indicating that a call to
  453. * accept() has no pending connections to return. */
  454. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  455. /** Return true if e is EMFILE or another error indicating that a call to
  456. * accept() has failed because we're out of fds or something. */
  457. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  458. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  459. /** Return true if e is EADDRINUSE or the local equivalent. */
  460. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  461. int tor_socket_errno(tor_socket_t sock);
  462. const char *tor_socket_strerror(int e);
  463. #else
  464. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  465. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  466. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  467. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  468. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  469. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  470. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  471. #define tor_socket_errno(sock) (errno)
  472. #define tor_socket_strerror(e) strerror(e)
  473. #endif
  474. /** Specified SOCKS5 status codes. */
  475. typedef enum {
  476. SOCKS5_SUCCEEDED = 0x00,
  477. SOCKS5_GENERAL_ERROR = 0x01,
  478. SOCKS5_NOT_ALLOWED = 0x02,
  479. SOCKS5_NET_UNREACHABLE = 0x03,
  480. SOCKS5_HOST_UNREACHABLE = 0x04,
  481. SOCKS5_CONNECTION_REFUSED = 0x05,
  482. SOCKS5_TTL_EXPIRED = 0x06,
  483. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  484. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  485. } socks5_reply_status_t;
  486. /* ===== Insecure rng */
  487. void tor_init_weak_random(unsigned seed);
  488. long tor_weak_random(void);
  489. #define TOR_RAND_MAX (RAND_MAX)
  490. /* ===== OS compatibility */
  491. const char *get_uname(void);
  492. uint16_t get_uint16(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  493. uint32_t get_uint32(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  494. uint64_t get_uint64(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  495. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  496. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  497. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  498. /* These uint8 variants are defined to make the code more uniform. */
  499. #define get_uint8(cp) (*(const uint8_t*)(cp))
  500. static void set_uint8(void *cp, uint8_t v);
  501. static INLINE void
  502. set_uint8(void *cp, uint8_t v)
  503. {
  504. *(uint8_t*)cp = v;
  505. }
  506. #if !defined(HAVE_RLIM_T)
  507. typedef unsigned long rlim_t;
  508. #endif
  509. int set_max_file_descriptors(rlim_t limit, int *max);
  510. int switch_id(const char *user);
  511. #ifdef HAVE_PWD_H
  512. char *get_user_homedir(const char *username);
  513. #endif
  514. int get_parent_directory(char *fname);
  515. int spawn_func(void (*func)(void *), void *data);
  516. void spawn_exit(void) ATTR_NORETURN;
  517. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  518. #define USE_WIN32_THREADS
  519. #define TOR_IS_MULTITHREADED 1
  520. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  521. defined(HAVE_PTHREAD_CREATE))
  522. #define USE_PTHREADS
  523. #define TOR_IS_MULTITHREADED 1
  524. #else
  525. #undef TOR_IS_MULTITHREADED
  526. #endif
  527. int compute_num_cpus(void);
  528. /* Because we use threads instead of processes on most platforms (Windows,
  529. * Linux, etc), we need locking for them. On platforms with poor thread
  530. * support or broken gethostbyname_r, these functions are no-ops. */
  531. /** A generic lock structure for multithreaded builds. */
  532. typedef struct tor_mutex_t {
  533. #if defined(USE_WIN32_THREADS)
  534. /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
  535. CRITICAL_SECTION mutex;
  536. #elif defined(USE_PTHREADS)
  537. /** Pthreads-only: with pthreads, we implement locks with
  538. * pthread_mutex_t. */
  539. pthread_mutex_t mutex;
  540. #else
  541. /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
  542. int _unused;
  543. #endif
  544. } tor_mutex_t;
  545. int tor_mlockall(void);
  546. #ifdef TOR_IS_MULTITHREADED
  547. tor_mutex_t *tor_mutex_new(void);
  548. void tor_mutex_init(tor_mutex_t *m);
  549. void tor_mutex_acquire(tor_mutex_t *m);
  550. void tor_mutex_release(tor_mutex_t *m);
  551. void tor_mutex_free(tor_mutex_t *m);
  552. void tor_mutex_uninit(tor_mutex_t *m);
  553. unsigned long tor_get_thread_id(void);
  554. void tor_threads_init(void);
  555. #else
  556. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  557. #define tor_mutex_init(m) STMT_NIL
  558. #define tor_mutex_acquire(m) STMT_NIL
  559. #define tor_mutex_release(m) STMT_NIL
  560. #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
  561. #define tor_mutex_uninit(m) STMT_NIL
  562. #define tor_get_thread_id() (1UL)
  563. #define tor_threads_init() STMT_NIL
  564. #endif
  565. void set_main_thread(void);
  566. int in_main_thread(void);
  567. #ifdef TOR_IS_MULTITHREADED
  568. #if 0
  569. typedef struct tor_cond_t tor_cond_t;
  570. tor_cond_t *tor_cond_new(void);
  571. void tor_cond_free(tor_cond_t *cond);
  572. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
  573. void tor_cond_signal_one(tor_cond_t *cond);
  574. void tor_cond_signal_all(tor_cond_t *cond);
  575. #endif
  576. #endif
  577. /** Macros for MIN/MAX. Never use these when the arguments could have
  578. * side-effects.
  579. * {With GCC extensions we could probably define a safer MIN/MAX. But
  580. * depending on that safety would be dangerous, since not every platform
  581. * has it.}
  582. **/
  583. #ifndef MAX
  584. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  585. #endif
  586. #ifndef MIN
  587. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  588. #endif
  589. /* Platform-specific helpers. */
  590. #ifdef MS_WINDOWS
  591. char *format_win32_error(DWORD err);
  592. #endif
  593. /*for some reason my compiler doesn't have these version flags defined
  594. a nice homework assignment for someone one day is to define the rest*/
  595. //these are the values as given on MSDN
  596. #ifdef MS_WINDOWS
  597. #ifndef VER_SUITE_EMBEDDEDNT
  598. #define VER_SUITE_EMBEDDEDNT 0x00000040
  599. #endif
  600. #ifndef VER_SUITE_SINGLEUSERTS
  601. #define VER_SUITE_SINGLEUSERTS 0x00000100
  602. #endif
  603. #endif
  604. #endif