compat.h 20 KB

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