compat.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #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. #else
  184. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  185. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  186. #define U64_LITERAL(n) (n ## llu)
  187. #endif
  188. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  189. /** The formatting string used to put a uint64_t value in a printf() or
  190. * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
  191. #define U64_FORMAT "%I64u"
  192. #else
  193. #define U64_FORMAT "%llu"
  194. #endif
  195. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  196. * tor_munmap_file. */
  197. typedef struct tor_mmap_t {
  198. const char *data; /**< Mapping of the file's contents. */
  199. size_t size; /**< Size of the file. */
  200. /* None of the fields below should be accessed from outside compat.c */
  201. #ifdef HAVE_SYS_MMAN_H
  202. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  203. * size, rounded up to the nearest page.) */
  204. #elif defined MS_WINDOWS
  205. HANDLE file_handle;
  206. HANDLE mmap_handle;
  207. #endif
  208. } tor_mmap_t;
  209. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  210. void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  211. int tor_snprintf(char *str, size_t size, const char *format, ...)
  212. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  213. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  214. ATTR_NONNULL((1,3));
  215. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  216. size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
  217. static const void *tor_memstr(const void *haystack, size_t hlen,
  218. const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
  219. static INLINE const void *
  220. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  221. {
  222. return tor_memmem(haystack, hlen, needle, strlen(needle));
  223. }
  224. /* Much of the time when we're checking ctypes, we're doing spec compliance,
  225. * which all assumes we're doing ASCII. */
  226. #define DECLARE_CTYPE_FN(name) \
  227. static int TOR_##name(char c); \
  228. extern const uint32_t TOR_##name##_TABLE[]; \
  229. static INLINE int TOR_##name(char c) { \
  230. uint8_t u = c; \
  231. return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
  232. }
  233. DECLARE_CTYPE_FN(ISALPHA)
  234. DECLARE_CTYPE_FN(ISALNUM)
  235. DECLARE_CTYPE_FN(ISSPACE)
  236. DECLARE_CTYPE_FN(ISDIGIT)
  237. DECLARE_CTYPE_FN(ISXDIGIT)
  238. DECLARE_CTYPE_FN(ISPRINT)
  239. DECLARE_CTYPE_FN(ISLOWER)
  240. DECLARE_CTYPE_FN(ISUPPER)
  241. extern const char TOR_TOUPPER_TABLE[];
  242. extern const char TOR_TOLOWER_TABLE[];
  243. #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
  244. #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
  245. #ifdef MS_WINDOWS
  246. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  247. const char *tor_fix_source_file(const char *fname);
  248. #else
  249. #define _SHORT_FILE_ (__FILE__)
  250. #define tor_fix_source_file(s) (s)
  251. #endif
  252. /* ===== Time compatibility */
  253. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  254. struct timeval {
  255. time_t tv_sec;
  256. unsigned int tv_usec;
  257. };
  258. #endif
  259. void tor_gettimeofday(struct timeval *timeval);
  260. #ifdef HAVE_LOCALTIME_R
  261. #define tor_localtime_r localtime_r
  262. #else
  263. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  264. #endif
  265. #ifdef HAVE_GMTIME_R
  266. #define tor_gmtime_r gmtime_r
  267. #else
  268. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  269. #endif
  270. /* ===== File compatibility */
  271. int replace_file(const char *from, const char *to);
  272. int touch_file(const char *fname);
  273. typedef struct tor_lockfile_t tor_lockfile_t;
  274. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  275. int *locked_out);
  276. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  277. off_t tor_fd_getpos(int fd);
  278. int tor_fd_seekend(int fd);
  279. #ifdef MS_WINDOWS
  280. #define PATH_SEPARATOR "\\"
  281. #else
  282. #define PATH_SEPARATOR "/"
  283. #endif
  284. /* ===== Net compatibility */
  285. #if (SIZEOF_SOCKLEN_T == 0)
  286. typedef int socklen_t;
  287. #endif
  288. int tor_close_socket(int s);
  289. int tor_open_socket(int domain, int type, int protocol);
  290. int tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len);
  291. int get_n_open_sockets(void);
  292. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  293. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  294. /* Define struct in6_addr on platforms that do not have it. Generally,
  295. * these platforms are ones without IPv6 support, but we want to have
  296. * a working in6_addr there anyway, so we can use it to parse IPv6
  297. * addresses. */
  298. #if !defined(HAVE_STRUCT_IN6_ADDR)
  299. struct in6_addr
  300. {
  301. union {
  302. uint8_t u6_addr8[16];
  303. uint16_t u6_addr16[8];
  304. uint32_t u6_addr32[4];
  305. } in6_u;
  306. #define s6_addr in6_u.u6_addr8
  307. #define s6_addr16 in6_u.u6_addr16
  308. #define s6_addr32 in6_u.u6_addr32
  309. };
  310. #endif
  311. #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
  312. || defined(__NetBSD__) || defined(__OpenBSD__)
  313. /* Many BSD variants seem not to define these. */
  314. #ifndef s6_addr16
  315. #define s6_addr16 __u6_addr.__u6_addr16
  316. #endif
  317. #ifndef s6_addr32
  318. #define s6_addr32 __u6_addr.__u6_addr32
  319. #endif
  320. #endif
  321. #ifndef HAVE_SA_FAMILY_T
  322. typedef uint16_t sa_family_t;
  323. #endif
  324. /* Apparently, MS and Solaris don't define s6_addr16 or s6_addr32. */
  325. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  326. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  327. #else
  328. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  329. #endif
  330. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  331. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  332. #else
  333. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  334. #endif
  335. /* Define struct sockaddr_in6 on platforms that do not have it. See notes
  336. * on struct in6_addr. */
  337. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  338. struct sockaddr_in6 {
  339. sa_family_t sin6_family;
  340. uint16_t sin6_port;
  341. // uint32_t sin6_flowinfo;
  342. struct in6_addr sin6_addr;
  343. // uint32_t sin6_scope_id;
  344. };
  345. #endif
  346. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  347. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  348. int tor_inet_pton(int af, const char *src, void *dst);
  349. int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
  350. void set_socket_nonblocking(int socket);
  351. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  352. int network_init(void);
  353. /* For stupid historical reasons, windows sockets have an independent
  354. * set of errnos, and an independent way to get them. Also, you can't
  355. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  356. * errnos against expected values, and use tor_socket_errno to find
  357. * the actual errno after a socket operation fails.
  358. */
  359. #if defined(MS_WINDOWS)
  360. /** Return true if e is EAGAIN or the local equivalent. */
  361. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  362. /** Return true if e is EINPROGRESS or the local equivalent. */
  363. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  364. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  365. * a call to connect(). */
  366. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  367. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  368. /** Return true if e is EAGAIN or another error indicating that a call to
  369. * accept() has no pending connections to return. */
  370. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  371. /** Return true if e is EMFILE or another error indicating that a call to
  372. * accept() has failed because we're out of fds or something. */
  373. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  374. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  375. /** Return true if e is EADDRINUSE or the local equivalent. */
  376. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  377. int tor_socket_errno(int sock);
  378. const char *tor_socket_strerror(int e);
  379. #else
  380. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  381. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  382. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  383. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  384. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  385. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  386. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  387. #define tor_socket_errno(sock) (errno)
  388. #define tor_socket_strerror(e) strerror(e)
  389. #endif
  390. /** Specified SOCKS5 status codes. */
  391. typedef enum {
  392. SOCKS5_SUCCEEDED = 0x00,
  393. SOCKS5_GENERAL_ERROR = 0x01,
  394. SOCKS5_NOT_ALLOWED = 0x02,
  395. SOCKS5_NET_UNREACHABLE = 0x03,
  396. SOCKS5_HOST_UNREACHABLE = 0x04,
  397. SOCKS5_CONNECTION_REFUSED = 0x05,
  398. SOCKS5_TTL_EXPIRED = 0x06,
  399. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  400. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  401. } socks5_reply_status_t;
  402. /* ===== OS compatibility */
  403. const char *get_uname(void);
  404. uint16_t get_uint16(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  405. uint32_t get_uint32(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  406. uint64_t get_uint64(const void *cp) ATTR_PURE ATTR_NONNULL((1));
  407. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  408. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  409. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  410. /* These uint8 variants are defined to make the code more uniform. */
  411. #define get_uint8(cp) (*(const uint8_t*)(cp))
  412. static void set_uint8(void *cp, uint8_t v);
  413. static INLINE void
  414. set_uint8(void *cp, uint8_t v)
  415. {
  416. *(uint8_t*)cp = v;
  417. }
  418. #if !defined(HAVE_RLIM_T)
  419. typedef unsigned long rlim_t;
  420. #endif
  421. int set_max_file_descriptors(rlim_t limit, int *max);
  422. int switch_id(const char *user);
  423. #ifdef HAVE_PWD_H
  424. char *get_user_homedir(const char *username);
  425. #endif
  426. int spawn_func(void (*func)(void *), void *data);
  427. void spawn_exit(void) ATTR_NORETURN;
  428. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  429. #define USE_WIN32_THREADS
  430. #define TOR_IS_MULTITHREADED 1
  431. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  432. defined(HAVE_PTHREAD_CREATE))
  433. #define USE_PTHREADS
  434. #define TOR_IS_MULTITHREADED 1
  435. #else
  436. #undef TOR_IS_MULTITHREADED
  437. #endif
  438. /* Because we use threads instead of processes on most platforms (Windows,
  439. * Linux, etc), we need locking for them. On platforms with poor thread
  440. * support or broken gethostbyname_r, these functions are no-ops. */
  441. /** A generic lock structure for multithreaded builds. */
  442. typedef struct tor_mutex_t {
  443. #if defined(USE_WIN32_THREADS)
  444. CRITICAL_SECTION mutex;
  445. #elif defined(USE_PTHREADS)
  446. pthread_mutex_t mutex;
  447. #else
  448. int _unused;
  449. #endif
  450. } tor_mutex_t;
  451. #ifdef TOR_IS_MULTITHREADED
  452. tor_mutex_t *tor_mutex_new(void);
  453. void tor_mutex_init(tor_mutex_t *m);
  454. void tor_mutex_acquire(tor_mutex_t *m);
  455. void tor_mutex_release(tor_mutex_t *m);
  456. void tor_mutex_free(tor_mutex_t *m);
  457. void tor_mutex_uninit(tor_mutex_t *m);
  458. unsigned long tor_get_thread_id(void);
  459. void tor_threads_init(void);
  460. #else
  461. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  462. #define tor_mutex_init(m) STMT_NIL
  463. #define tor_mutex_acquire(m) STMT_NIL
  464. #define tor_mutex_release(m) STMT_NIL
  465. #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
  466. #define tor_mutex_uninit(m) STMT_NIL
  467. #define tor_get_thread_id() (1UL)
  468. #define tor_threads_init() STMT_NIL
  469. #endif
  470. void set_main_thread(void);
  471. int in_main_thread(void);
  472. #ifdef TOR_IS_MULTITHREADED
  473. #if 0
  474. typedef struct tor_cond_t tor_cond_t;
  475. tor_cond_t *tor_cond_new(void);
  476. void tor_cond_free(tor_cond_t *cond);
  477. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
  478. void tor_cond_signal_one(tor_cond_t *cond);
  479. void tor_cond_signal_all(tor_cond_t *cond);
  480. #endif
  481. #endif
  482. /* Platform-specific helpers. */
  483. #ifdef MS_WINDOWS
  484. char *format_win32_error(DWORD err);
  485. #endif
  486. /*for some reason my compiler doesn't have these version flags defined
  487. a nice homework assignment for someone one day is to define the rest*/
  488. //these are the values as given on MSDN
  489. #ifdef MS_WINDOWS
  490. #ifndef VER_SUITE_EMBEDDEDNT
  491. #define VER_SUITE_EMBEDDEDNT 0x00000040
  492. #endif
  493. #ifndef VER_SUITE_SINGLEUSERTS
  494. #define VER_SUITE_SINGLEUSERTS 0x00000100
  495. #endif
  496. #endif
  497. #endif