compat.h 17 KB

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