compat.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #ifdef _MSC_VER
  73. /** Casts the uint64_t value in <b>a</b> to the right type for an argument
  74. * to printf. */
  75. #define U64_PRINTF_ARG(a) (a)
  76. /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
  77. * to scanf. */
  78. #define U64_SCANF_ARG(a) (a)
  79. /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
  80. #define U64_LITERAL(n) (n ## ui64)
  81. #define I64_PRINTF_ARG(a) (a)
  82. #define I64_SCANF_ARG(a) (a)
  83. #define I64_LITERAL(n) (n ## i64)
  84. #else /* !(defined(_MSC_VER)) */
  85. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  86. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  87. #define U64_LITERAL(n) (n ## llu)
  88. #define I64_PRINTF_ARG(a) ((long long signed int)(a))
  89. #define I64_SCANF_ARG(a) ((long long signed int*)(a))
  90. #define I64_LITERAL(n) (n ## ll)
  91. #endif /* defined(_MSC_VER) */
  92. #if defined(__MINGW32__) || defined(__MINGW64__)
  93. #define MINGW_ANY
  94. #endif
  95. #if defined(_MSC_VER) || defined(MINGW_ANY)
  96. /** The formatting string used to put a uint64_t value in a printf() or
  97. * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
  98. #define U64_FORMAT "%I64u"
  99. #define I64_FORMAT "%I64d"
  100. #else /* !(defined(_MSC_VER) || defined(MINGW_ANY)) */
  101. #define U64_FORMAT "%llu"
  102. #define I64_FORMAT "%lld"
  103. #endif /* defined(_MSC_VER) || defined(MINGW_ANY) */
  104. #if (SIZEOF_INTPTR_T == SIZEOF_INT)
  105. #define INTPTR_T_FORMAT "%d"
  106. #define INTPTR_PRINTF_ARG(x) ((int)(x))
  107. #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
  108. #define INTPTR_T_FORMAT "%ld"
  109. #define INTPTR_PRINTF_ARG(x) ((long)(x))
  110. #elif (SIZEOF_INTPTR_T == 8)
  111. #define INTPTR_T_FORMAT I64_FORMAT
  112. #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
  113. #else
  114. #error Unknown: SIZEOF_INTPTR_T
  115. #endif /* (SIZEOF_INTPTR_T == SIZEOF_INT) || ... */
  116. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  117. * tor_munmap_file. */
  118. typedef struct tor_mmap_t {
  119. const char *data; /**< Mapping of the file's contents. */
  120. size_t size; /**< Size of the file. */
  121. /* None of the fields below should be accessed from outside compat.c */
  122. #ifdef HAVE_MMAP
  123. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  124. * size, rounded up to the nearest page.) */
  125. #elif defined _WIN32
  126. HANDLE mmap_handle;
  127. #endif /* defined(HAVE_MMAP) || ... */
  128. } tor_mmap_t;
  129. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  130. int tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  131. int tor_snprintf(char *str, size_t size, const char *format, ...)
  132. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  133. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  134. CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
  135. int tor_asprintf(char **strp, const char *fmt, ...)
  136. CHECK_PRINTF(2,3);
  137. int tor_vasprintf(char **strp, const char *fmt, va_list args)
  138. CHECK_PRINTF(2,0);
  139. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  140. size_t nlen) ATTR_NONNULL((1,3));
  141. static const void *tor_memstr(const void *haystack, size_t hlen,
  142. const char *needle) ATTR_NONNULL((1,3));
  143. static inline const void *
  144. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  145. {
  146. return tor_memmem(haystack, hlen, needle, strlen(needle));
  147. }
  148. /* Much of the time when we're checking ctypes, we're doing spec compliance,
  149. * which all assumes we're doing ASCII. */
  150. #define DECLARE_CTYPE_FN(name) \
  151. static int TOR_##name(char c); \
  152. extern const uint32_t TOR_##name##_TABLE[]; \
  153. static inline int TOR_##name(char c) { \
  154. uint8_t u = c; \
  155. return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
  156. }
  157. DECLARE_CTYPE_FN(ISALPHA)
  158. DECLARE_CTYPE_FN(ISALNUM)
  159. DECLARE_CTYPE_FN(ISSPACE)
  160. DECLARE_CTYPE_FN(ISDIGIT)
  161. DECLARE_CTYPE_FN(ISXDIGIT)
  162. DECLARE_CTYPE_FN(ISPRINT)
  163. DECLARE_CTYPE_FN(ISLOWER)
  164. DECLARE_CTYPE_FN(ISUPPER)
  165. extern const uint8_t TOR_TOUPPER_TABLE[];
  166. extern const uint8_t TOR_TOLOWER_TABLE[];
  167. #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
  168. #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
  169. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  170. #ifdef HAVE_STRTOK_R
  171. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  172. #else
  173. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  174. #endif
  175. #ifdef _WIN32
  176. #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
  177. const char *tor_fix_source_file(const char *fname);
  178. #else
  179. #define SHORT_FILE__ (__FILE__)
  180. #define tor_fix_source_file(s) (s)
  181. #endif /* defined(_WIN32) */
  182. /* ===== Time compatibility */
  183. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  184. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  185. #ifndef timeradd
  186. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  187. * the sum of tv1 and tv2. */
  188. #define timeradd(tv1,tv2,tvout) \
  189. do { \
  190. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  191. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  192. if ((tvout)->tv_usec >= 1000000) { \
  193. (tvout)->tv_usec -= 1000000; \
  194. (tvout)->tv_sec++; \
  195. } \
  196. } while (0)
  197. #endif /* !defined(timeradd) */
  198. #ifndef timersub
  199. /** Replacement for timersub on platforms that do not have it: sets tvout to
  200. * tv1 minus tv2. */
  201. #define timersub(tv1,tv2,tvout) \
  202. do { \
  203. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  204. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  205. if ((tvout)->tv_usec < 0) { \
  206. (tvout)->tv_usec += 1000000; \
  207. (tvout)->tv_sec--; \
  208. } \
  209. } while (0)
  210. #endif /* !defined(timersub) */
  211. #ifndef timercmp
  212. /** Replacement for timercmp on platforms that do not have it: returns true
  213. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  214. *
  215. * Note that while this definition should work for all boolean operators, some
  216. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  217. * don't use those.
  218. */
  219. #define timercmp(tv1,tv2,op) \
  220. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  221. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  222. ((tv1)->tv_sec op (tv2)->tv_sec))
  223. #endif /* !defined(timercmp) */
  224. /* ===== File compatibility */
  225. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  226. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  227. int tor_rename(const char *path_old, const char *path_new);
  228. int replace_file(const char *from, const char *to);
  229. int touch_file(const char *fname);
  230. typedef struct tor_lockfile_t tor_lockfile_t;
  231. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  232. int *locked_out);
  233. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  234. off_t tor_fd_getpos(int fd);
  235. int tor_fd_setpos(int fd, off_t pos);
  236. int tor_fd_seekend(int fd);
  237. int tor_ftruncate(int fd);
  238. int64_t tor_get_avail_disk_space(const char *path);
  239. #ifdef _WIN32
  240. #define PATH_SEPARATOR "\\"
  241. #else
  242. #define PATH_SEPARATOR "/"
  243. #endif
  244. /* ===== Net compatibility */
  245. #if (SIZEOF_SOCKLEN_T == 0)
  246. typedef int socklen_t;
  247. #endif
  248. #ifdef _WIN32
  249. /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
  250. * any inadvertent checks for the socket being <= 0 or > 0 will probably
  251. * still work. */
  252. #define tor_socket_t intptr_t
  253. #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
  254. #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
  255. #define TOR_INVALID_SOCKET INVALID_SOCKET
  256. #else /* !(defined(_WIN32)) */
  257. /** Type used for a network socket. */
  258. #define tor_socket_t int
  259. #define TOR_SOCKET_T_FORMAT "%d"
  260. /** Macro: true iff 's' is a possible value for a valid initialized socket. */
  261. #define SOCKET_OK(s) ((s) >= 0)
  262. /** Error/uninitialized value for a tor_socket_t. */
  263. #define TOR_INVALID_SOCKET (-1)
  264. #endif /* defined(_WIN32) */
  265. int tor_close_socket_simple(tor_socket_t s);
  266. MOCK_DECL(int, tor_close_socket, (tor_socket_t s));
  267. void tor_take_socket_ownership(tor_socket_t s);
  268. tor_socket_t tor_open_socket_with_extensions(
  269. int domain, int type, int protocol,
  270. int cloexec, int nonblock);
  271. MOCK_DECL(tor_socket_t,
  272. tor_open_socket,(int domain, int type, int protocol));
  273. tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol);
  274. tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr,
  275. socklen_t *len);
  276. tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd,
  277. struct sockaddr *addr,
  278. socklen_t *len);
  279. tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd,
  280. struct sockaddr *addr,
  281. socklen_t *len,
  282. int cloexec, int nonblock);
  283. MOCK_DECL(tor_socket_t,
  284. tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address,
  285. socklen_t address_len));
  286. int get_n_open_sockets(void);
  287. MOCK_DECL(int,
  288. tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
  289. socklen_t *address_len));
  290. struct tor_addr_t;
  291. int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock);
  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. /** Implementation of struct in6_addr for platforms that do not have it.
  295. * Generally, these platforms are ones without IPv6 support, but we want to
  296. * have 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 /* !defined(HAVE_STRUCT_IN6_ADDR) */
  311. /** @{ */
  312. /** Many BSD variants seem not to define these. */
  313. #if defined(__APPLE__) || defined(__darwin__) || \
  314. defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  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 /* defined(__APPLE__) || defined(__darwin__) || ... */
  322. /** @} */
  323. #ifndef HAVE_SA_FAMILY_T
  324. typedef uint16_t sa_family_t;
  325. #endif
  326. /** @{ */
  327. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  328. * macros get you a pointer to s6_addr32 or local equivalent. */
  329. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  330. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  331. #else
  332. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  333. #endif
  334. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  335. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  336. #else
  337. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  338. #endif
  339. /** @} */
  340. /** Implementation of struct sockaddr_in6 on platforms that do not have
  341. * it. See notes on struct in6_addr. */
  342. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  343. struct sockaddr_in6 {
  344. sa_family_t sin6_family;
  345. uint16_t sin6_port;
  346. // uint32_t sin6_flowinfo;
  347. struct in6_addr sin6_addr;
  348. // uint32_t sin6_scope_id;
  349. };
  350. #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */
  351. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  352. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  353. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  354. int tor_inet_pton(int af, const char *src, void *dst);
  355. MOCK_DECL(int,tor_lookup_hostname,(const char *name, uint32_t *addr));
  356. int set_socket_nonblocking(tor_socket_t socket);
  357. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  358. int network_init(void);
  359. /* For stupid historical reasons, windows sockets have an independent
  360. * set of errnos, and an independent way to get them. Also, you can't
  361. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  362. * errnos against expected values, and use tor_socket_errno to find
  363. * the actual errno after a socket operation fails.
  364. */
  365. #if defined(_WIN32)
  366. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  367. #define SOCK_ERRNO(e) WSA##e
  368. /** Return true if e is EAGAIN or the local equivalent. */
  369. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  370. /** Return true if e is EINPROGRESS or the local equivalent. */
  371. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  372. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  373. * a call to connect(). */
  374. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  375. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  376. /** Return true if e is EAGAIN or another error indicating that a call to
  377. * accept() has no pending connections to return. */
  378. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  379. /** Return true if e is EMFILE or another error indicating that a call to
  380. * accept() has failed because we're out of fds or something. */
  381. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  382. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  383. /** Return true if e is EADDRINUSE or the local equivalent. */
  384. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  385. /** Return true if e is EINTR or the local equivalent */
  386. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  387. int tor_socket_errno(tor_socket_t sock);
  388. const char *tor_socket_strerror(int e);
  389. #else /* !(defined(_WIN32)) */
  390. #define SOCK_ERRNO(e) e
  391. #if EAGAIN == EWOULDBLOCK
  392. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  393. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  394. #else
  395. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  396. #endif /* EAGAIN == EWOULDBLOCK */
  397. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  398. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  399. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  400. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  401. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  402. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  403. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  404. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  405. #define tor_socket_errno(sock) (errno)
  406. #define tor_socket_strerror(e) strerror(e)
  407. #endif /* defined(_WIN32) */
  408. /** Specified SOCKS5 status codes. */
  409. typedef enum {
  410. SOCKS5_SUCCEEDED = 0x00,
  411. SOCKS5_GENERAL_ERROR = 0x01,
  412. SOCKS5_NOT_ALLOWED = 0x02,
  413. SOCKS5_NET_UNREACHABLE = 0x03,
  414. SOCKS5_HOST_UNREACHABLE = 0x04,
  415. SOCKS5_CONNECTION_REFUSED = 0x05,
  416. SOCKS5_TTL_EXPIRED = 0x06,
  417. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  418. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  419. } socks5_reply_status_t;
  420. /* ===== OS compatibility */
  421. MOCK_DECL(const char *, get_uname, (void));
  422. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  423. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  424. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  425. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  426. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  427. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  428. /* These uint8 variants are defined to make the code more uniform. */
  429. #define get_uint8(cp) (*(const uint8_t*)(cp))
  430. static void set_uint8(void *cp, uint8_t v);
  431. static inline void
  432. set_uint8(void *cp, uint8_t v)
  433. {
  434. *(uint8_t*)cp = v;
  435. }
  436. #if !defined(HAVE_RLIM_T)
  437. typedef unsigned long rlim_t;
  438. #endif
  439. int get_max_sockets(void);
  440. int set_max_file_descriptors(rlim_t limit, int *max);
  441. int tor_disable_debugger_attach(void);
  442. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  443. #define HAVE_LINUX_CAPABILITIES
  444. #endif
  445. int have_capability_support(void);
  446. /** Flag for switch_id; see switch_id() for documentation */
  447. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  448. /** Flag for switch_id; see switch_id() for documentation */
  449. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  450. int switch_id(const char *user, unsigned flags);
  451. #ifdef HAVE_PWD_H
  452. char *get_user_homedir(const char *username);
  453. #endif
  454. #ifndef _WIN32
  455. const struct passwd *tor_getpwnam(const char *username);
  456. const struct passwd *tor_getpwuid(uid_t uid);
  457. #endif
  458. int get_parent_directory(char *fname);
  459. char *make_path_absolute(char *fname);
  460. char **get_environment(void);
  461. MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
  462. int compute_num_cpus(void);
  463. int tor_mlockall(void);
  464. /** Macros for MIN/MAX. Never use these when the arguments could have
  465. * side-effects.
  466. * {With GCC extensions we could probably define a safer MIN/MAX. But
  467. * depending on that safety would be dangerous, since not every platform
  468. * has it.}
  469. **/
  470. #ifndef MAX
  471. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  472. #endif
  473. #ifndef MIN
  474. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  475. #endif
  476. /* Platform-specific helpers. */
  477. #ifdef _WIN32
  478. char *format_win32_error(DWORD err);
  479. #endif
  480. /*for some reason my compiler doesn't have these version flags defined
  481. a nice homework assignment for someone one day is to define the rest*/
  482. //these are the values as given on MSDN
  483. #ifdef _WIN32
  484. #ifndef VER_SUITE_EMBEDDEDNT
  485. #define VER_SUITE_EMBEDDEDNT 0x00000040
  486. #endif
  487. #ifndef VER_SUITE_SINGLEUSERTS
  488. #define VER_SUITE_SINGLEUSERTS 0x00000100
  489. #endif
  490. #endif /* defined(_WIN32) */
  491. #ifdef COMPAT_PRIVATE
  492. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  493. #define NEED_ERSATZ_SOCKETPAIR
  494. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  495. tor_socket_t fd[2]);
  496. #endif
  497. #endif /* defined(COMPAT_PRIVATE) */
  498. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  499. /* This needs some of the declarations above so we include it here. */
  500. #include "common/compat_threads.h"
  501. #endif /* !defined(TOR_COMPAT_H) */