compat.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, 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. #ifdef _WIN32
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
  12. #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747b
  13. #endif
  14. #endif
  15. #include "lib/cc/torint.h"
  16. #include "common/testsupport.h"
  17. #ifdef HAVE_SYS_PARAM_H
  18. #include <sys/param.h>
  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. #include <stdarg.h>
  33. #ifdef HAVE_SYS_RESOURCE_H
  34. #include <sys/resource.h>
  35. #endif
  36. #ifdef HAVE_SYS_SOCKET_H
  37. #include <sys/socket.h>
  38. #endif
  39. #ifdef HAVE_NETINET_IN_H
  40. #include <netinet/in.h>
  41. #endif
  42. #ifdef HAVE_NETINET6_IN6_H
  43. #include <netinet6/in6.h>
  44. #endif
  45. #include "lib/cc/compat_compiler.h"
  46. #include "common/compat_time.h"
  47. #include <stdio.h>
  48. #include <errno.h>
  49. /* ===== Compiler compatibility */
  50. /* ===== String compatibility */
  51. #ifdef _WIN32
  52. /* Windows names string functions differently from most other platforms. */
  53. #define strncasecmp _strnicmp
  54. #define strcasecmp _stricmp
  55. #endif
  56. #if defined __APPLE__
  57. /* On OSX 10.9 and later, the overlap-checking code for strlcat would
  58. * appear to have a severe bug that can sometimes cause aborts in Tor.
  59. * Instead, use the non-checking variants. This is sad.
  60. *
  61. * See https://trac.torproject.org/projects/tor/ticket/15205
  62. */
  63. #undef strlcat
  64. #undef strlcpy
  65. #endif /* defined __APPLE__ */
  66. #ifndef HAVE_STRLCAT
  67. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  68. #endif
  69. #ifndef HAVE_STRLCPY
  70. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  71. #endif
  72. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  73. * tor_munmap_file. */
  74. typedef struct tor_mmap_t {
  75. const char *data; /**< Mapping of the file's contents. */
  76. size_t size; /**< Size of the file. */
  77. /* None of the fields below should be accessed from outside compat.c */
  78. #ifdef HAVE_MMAP
  79. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  80. * size, rounded up to the nearest page.) */
  81. #elif defined _WIN32
  82. HANDLE mmap_handle;
  83. #endif /* defined(HAVE_MMAP) || ... */
  84. } tor_mmap_t;
  85. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  86. int tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  87. int tor_snprintf(char *str, size_t size, const char *format, ...)
  88. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  89. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  90. CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
  91. int tor_asprintf(char **strp, const char *fmt, ...)
  92. CHECK_PRINTF(2,3);
  93. int tor_vasprintf(char **strp, const char *fmt, va_list args)
  94. CHECK_PRINTF(2,0);
  95. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  96. size_t nlen) ATTR_NONNULL((1,3));
  97. static const void *tor_memstr(const void *haystack, size_t hlen,
  98. const char *needle) ATTR_NONNULL((1,3));
  99. static inline const void *
  100. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  101. {
  102. return tor_memmem(haystack, hlen, needle, strlen(needle));
  103. }
  104. /* Much of the time when we're checking ctypes, we're doing spec compliance,
  105. * which all assumes we're doing ASCII. */
  106. #define DECLARE_CTYPE_FN(name) \
  107. static int TOR_##name(char c); \
  108. extern const uint32_t TOR_##name##_TABLE[]; \
  109. static inline int TOR_##name(char c) { \
  110. uint8_t u = c; \
  111. return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
  112. }
  113. DECLARE_CTYPE_FN(ISALPHA)
  114. DECLARE_CTYPE_FN(ISALNUM)
  115. DECLARE_CTYPE_FN(ISSPACE)
  116. DECLARE_CTYPE_FN(ISDIGIT)
  117. DECLARE_CTYPE_FN(ISXDIGIT)
  118. DECLARE_CTYPE_FN(ISPRINT)
  119. DECLARE_CTYPE_FN(ISLOWER)
  120. DECLARE_CTYPE_FN(ISUPPER)
  121. extern const uint8_t TOR_TOUPPER_TABLE[];
  122. extern const uint8_t TOR_TOLOWER_TABLE[];
  123. #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
  124. #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
  125. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  126. #ifdef HAVE_STRTOK_R
  127. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  128. #else
  129. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  130. #endif
  131. #ifdef _WIN32
  132. #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
  133. const char *tor_fix_source_file(const char *fname);
  134. #else
  135. #define SHORT_FILE__ (__FILE__)
  136. #define tor_fix_source_file(s) (s)
  137. #endif /* defined(_WIN32) */
  138. /* ===== Time compatibility */
  139. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  140. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  141. #ifndef timeradd
  142. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  143. * the sum of tv1 and tv2. */
  144. #define timeradd(tv1,tv2,tvout) \
  145. do { \
  146. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  147. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  148. if ((tvout)->tv_usec >= 1000000) { \
  149. (tvout)->tv_usec -= 1000000; \
  150. (tvout)->tv_sec++; \
  151. } \
  152. } while (0)
  153. #endif /* !defined(timeradd) */
  154. #ifndef timersub
  155. /** Replacement for timersub on platforms that do not have it: sets tvout to
  156. * tv1 minus tv2. */
  157. #define timersub(tv1,tv2,tvout) \
  158. do { \
  159. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  160. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  161. if ((tvout)->tv_usec < 0) { \
  162. (tvout)->tv_usec += 1000000; \
  163. (tvout)->tv_sec--; \
  164. } \
  165. } while (0)
  166. #endif /* !defined(timersub) */
  167. #ifndef timercmp
  168. /** Replacement for timercmp on platforms that do not have it: returns true
  169. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  170. *
  171. * Note that while this definition should work for all boolean operators, some
  172. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  173. * don't use those.
  174. */
  175. #define timercmp(tv1,tv2,op) \
  176. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  177. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  178. ((tv1)->tv_sec op (tv2)->tv_sec))
  179. #endif /* !defined(timercmp) */
  180. /* ===== File compatibility */
  181. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  182. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  183. int tor_rename(const char *path_old, const char *path_new);
  184. int replace_file(const char *from, const char *to);
  185. int touch_file(const char *fname);
  186. typedef struct tor_lockfile_t tor_lockfile_t;
  187. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  188. int *locked_out);
  189. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  190. off_t tor_fd_getpos(int fd);
  191. int tor_fd_setpos(int fd, off_t pos);
  192. int tor_fd_seekend(int fd);
  193. int tor_ftruncate(int fd);
  194. int64_t tor_get_avail_disk_space(const char *path);
  195. #ifdef _WIN32
  196. #define PATH_SEPARATOR "\\"
  197. #else
  198. #define PATH_SEPARATOR "/"
  199. #endif
  200. /* ===== Net compatibility */
  201. #if (SIZEOF_SOCKLEN_T == 0)
  202. typedef int socklen_t;
  203. #endif
  204. #ifdef _WIN32
  205. /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
  206. * any inadvertent checks for the socket being <= 0 or > 0 will probably
  207. * still work. */
  208. #define tor_socket_t intptr_t
  209. #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
  210. #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
  211. #define TOR_INVALID_SOCKET INVALID_SOCKET
  212. #else /* !(defined(_WIN32)) */
  213. /** Type used for a network socket. */
  214. #define tor_socket_t int
  215. #define TOR_SOCKET_T_FORMAT "%d"
  216. /** Macro: true iff 's' is a possible value for a valid initialized socket. */
  217. #define SOCKET_OK(s) ((s) >= 0)
  218. /** Error/uninitialized value for a tor_socket_t. */
  219. #define TOR_INVALID_SOCKET (-1)
  220. #endif /* defined(_WIN32) */
  221. int tor_close_socket_simple(tor_socket_t s);
  222. MOCK_DECL(int, tor_close_socket, (tor_socket_t s));
  223. void tor_take_socket_ownership(tor_socket_t s);
  224. tor_socket_t tor_open_socket_with_extensions(
  225. int domain, int type, int protocol,
  226. int cloexec, int nonblock);
  227. MOCK_DECL(tor_socket_t,
  228. tor_open_socket,(int domain, int type, int protocol));
  229. tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol);
  230. tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr,
  231. socklen_t *len);
  232. tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd,
  233. struct sockaddr *addr,
  234. socklen_t *len);
  235. tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd,
  236. struct sockaddr *addr,
  237. socklen_t *len,
  238. int cloexec, int nonblock);
  239. MOCK_DECL(tor_socket_t,
  240. tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address,
  241. socklen_t address_len));
  242. int get_n_open_sockets(void);
  243. MOCK_DECL(int,
  244. tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
  245. socklen_t *address_len));
  246. struct tor_addr_t;
  247. int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock);
  248. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  249. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  250. /** Implementation of struct in6_addr for platforms that do not have it.
  251. * Generally, these platforms are ones without IPv6 support, but we want to
  252. * have a working in6_addr there anyway, so we can use it to parse IPv6
  253. * addresses. */
  254. #if !defined(HAVE_STRUCT_IN6_ADDR)
  255. struct in6_addr
  256. {
  257. union {
  258. uint8_t u6_addr8[16];
  259. uint16_t u6_addr16[8];
  260. uint32_t u6_addr32[4];
  261. } in6_u;
  262. #define s6_addr in6_u.u6_addr8
  263. #define s6_addr16 in6_u.u6_addr16
  264. #define s6_addr32 in6_u.u6_addr32
  265. };
  266. #endif /* !defined(HAVE_STRUCT_IN6_ADDR) */
  267. /** @{ */
  268. /** Many BSD variants seem not to define these. */
  269. #if defined(__APPLE__) || defined(__darwin__) || \
  270. defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  271. #ifndef s6_addr16
  272. #define s6_addr16 __u6_addr.__u6_addr16
  273. #endif
  274. #ifndef s6_addr32
  275. #define s6_addr32 __u6_addr.__u6_addr32
  276. #endif
  277. #endif /* defined(__APPLE__) || defined(__darwin__) || ... */
  278. /** @} */
  279. #ifndef HAVE_SA_FAMILY_T
  280. typedef uint16_t sa_family_t;
  281. #endif
  282. /** @{ */
  283. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  284. * macros get you a pointer to s6_addr32 or local equivalent. */
  285. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  286. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  287. #else
  288. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  289. #endif
  290. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  291. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  292. #else
  293. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  294. #endif
  295. /** @} */
  296. /** Implementation of struct sockaddr_in6 on platforms that do not have
  297. * it. See notes on struct in6_addr. */
  298. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  299. struct sockaddr_in6 {
  300. sa_family_t sin6_family;
  301. uint16_t sin6_port;
  302. // uint32_t sin6_flowinfo;
  303. struct in6_addr sin6_addr;
  304. // uint32_t sin6_scope_id;
  305. };
  306. #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */
  307. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  308. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  309. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  310. int tor_inet_pton(int af, const char *src, void *dst);
  311. MOCK_DECL(int,tor_lookup_hostname,(const char *name, uint32_t *addr));
  312. int set_socket_nonblocking(tor_socket_t socket);
  313. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  314. int network_init(void);
  315. /* For stupid historical reasons, windows sockets have an independent
  316. * set of errnos, and an independent way to get them. Also, you can't
  317. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  318. * errnos against expected values, and use tor_socket_errno to find
  319. * the actual errno after a socket operation fails.
  320. */
  321. #if defined(_WIN32)
  322. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  323. #define SOCK_ERRNO(e) WSA##e
  324. /** Return true if e is EAGAIN or the local equivalent. */
  325. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  326. /** Return true if e is EINPROGRESS or the local equivalent. */
  327. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  328. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  329. * a call to connect(). */
  330. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  331. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  332. /** Return true if e is EAGAIN or another error indicating that a call to
  333. * accept() has no pending connections to return. */
  334. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  335. /** Return true if e is EMFILE or another error indicating that a call to
  336. * accept() has failed because we're out of fds or something. */
  337. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  338. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  339. /** Return true if e is EADDRINUSE or the local equivalent. */
  340. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  341. /** Return true if e is EINTR or the local equivalent */
  342. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  343. int tor_socket_errno(tor_socket_t sock);
  344. const char *tor_socket_strerror(int e);
  345. #else /* !(defined(_WIN32)) */
  346. #define SOCK_ERRNO(e) e
  347. #if EAGAIN == EWOULDBLOCK
  348. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  349. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  350. #else
  351. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  352. #endif /* EAGAIN == EWOULDBLOCK */
  353. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  354. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  355. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  356. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  357. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  358. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  359. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  360. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  361. #define tor_socket_errno(sock) (errno)
  362. #define tor_socket_strerror(e) strerror(e)
  363. #endif /* defined(_WIN32) */
  364. /** Specified SOCKS5 status codes. */
  365. typedef enum {
  366. SOCKS5_SUCCEEDED = 0x00,
  367. SOCKS5_GENERAL_ERROR = 0x01,
  368. SOCKS5_NOT_ALLOWED = 0x02,
  369. SOCKS5_NET_UNREACHABLE = 0x03,
  370. SOCKS5_HOST_UNREACHABLE = 0x04,
  371. SOCKS5_CONNECTION_REFUSED = 0x05,
  372. SOCKS5_TTL_EXPIRED = 0x06,
  373. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  374. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  375. } socks5_reply_status_t;
  376. /* ===== OS compatibility */
  377. MOCK_DECL(const char *, get_uname, (void));
  378. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  379. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  380. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  381. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  382. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  383. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  384. /* These uint8 variants are defined to make the code more uniform. */
  385. #define get_uint8(cp) (*(const uint8_t*)(cp))
  386. static void set_uint8(void *cp, uint8_t v);
  387. static inline void
  388. set_uint8(void *cp, uint8_t v)
  389. {
  390. *(uint8_t*)cp = v;
  391. }
  392. #if !defined(HAVE_RLIM_T)
  393. typedef unsigned long rlim_t;
  394. #endif
  395. int get_max_sockets(void);
  396. int set_max_file_descriptors(rlim_t limit, int *max);
  397. int tor_disable_debugger_attach(void);
  398. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  399. #define HAVE_LINUX_CAPABILITIES
  400. #endif
  401. int have_capability_support(void);
  402. /** Flag for switch_id; see switch_id() for documentation */
  403. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  404. /** Flag for switch_id; see switch_id() for documentation */
  405. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  406. int switch_id(const char *user, unsigned flags);
  407. #ifdef HAVE_PWD_H
  408. char *get_user_homedir(const char *username);
  409. #endif
  410. #ifndef _WIN32
  411. const struct passwd *tor_getpwnam(const char *username);
  412. const struct passwd *tor_getpwuid(uid_t uid);
  413. #endif
  414. int get_parent_directory(char *fname);
  415. char *make_path_absolute(char *fname);
  416. char **get_environment(void);
  417. MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
  418. int compute_num_cpus(void);
  419. int tor_mlockall(void);
  420. /** Macros for MIN/MAX. Never use these when the arguments could have
  421. * side-effects.
  422. * {With GCC extensions we could probably define a safer MIN/MAX. But
  423. * depending on that safety would be dangerous, since not every platform
  424. * has it.}
  425. **/
  426. #ifndef MAX
  427. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  428. #endif
  429. #ifndef MIN
  430. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  431. #endif
  432. /* Platform-specific helpers. */
  433. #ifdef _WIN32
  434. char *format_win32_error(DWORD err);
  435. #endif
  436. /*for some reason my compiler doesn't have these version flags defined
  437. a nice homework assignment for someone one day is to define the rest*/
  438. //these are the values as given on MSDN
  439. #ifdef _WIN32
  440. #ifndef VER_SUITE_EMBEDDEDNT
  441. #define VER_SUITE_EMBEDDEDNT 0x00000040
  442. #endif
  443. #ifndef VER_SUITE_SINGLEUSERTS
  444. #define VER_SUITE_SINGLEUSERTS 0x00000100
  445. #endif
  446. #endif /* defined(_WIN32) */
  447. #ifdef COMPAT_PRIVATE
  448. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  449. #define NEED_ERSATZ_SOCKETPAIR
  450. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  451. tor_socket_t fd[2]);
  452. #endif
  453. #endif /* defined(COMPAT_PRIVATE) */
  454. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  455. /* This needs some of the declarations above so we include it here. */
  456. #include "common/compat_threads.h"
  457. #endif /* !defined(TOR_COMPAT_H) */