compat.h 18 KB

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