compat.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #endif
  12. #include "torint.h"
  13. #include "testsupport.h"
  14. #ifdef HAVE_SYS_PARAM_H
  15. #include <sys/param.h>
  16. #endif
  17. #ifdef HAVE_SYS_TYPES_H
  18. #include <sys/types.h>
  19. #endif
  20. #ifdef HAVE_SYS_TIME_H
  21. #include <sys/time.h>
  22. #endif
  23. #ifdef HAVE_TIME_H
  24. #include <time.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include <stdarg.h>
  30. #ifdef HAVE_SYS_RESOURCE_H
  31. #include <sys/resource.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKET_H
  34. #include <sys/socket.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_NETINET6_IN6_H
  40. #include <netinet6/in6.h>
  41. #endif
  42. #include <stdio.h>
  43. #include <errno.h>
  44. #ifndef NULL_REP_IS_ZERO_BYTES
  45. #error "It seems your platform does not represent NULL as zero. We can't cope."
  46. #endif
  47. #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
  48. #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
  49. #endif
  50. #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
  51. #error "It seems that you encode characters in something other than ASCII."
  52. #endif
  53. /* ===== Compiler compatibility */
  54. /* GCC can check printf and scanf types on arbitrary functions. */
  55. #ifdef __GNUC__
  56. #define CHECK_PRINTF(formatIdx, firstArg) \
  57. __attribute__ ((format(printf, formatIdx, firstArg)))
  58. #else
  59. #define CHECK_PRINTF(formatIdx, firstArg)
  60. #endif
  61. #ifdef __GNUC__
  62. #define CHECK_SCANF(formatIdx, firstArg) \
  63. __attribute__ ((format(scanf, formatIdx, firstArg)))
  64. #else
  65. #define CHECK_SCANF(formatIdx, firstArg)
  66. #endif
  67. /* inline is __inline on windows. */
  68. #ifdef _WIN32
  69. #define inline __inline
  70. #endif
  71. /* Try to get a reasonable __func__ substitute in place. */
  72. #if defined(_MSC_VER)
  73. #define __func__ __FUNCTION__
  74. #else
  75. /* For platforms where autoconf works, make sure __func__ is defined
  76. * sanely. */
  77. #ifndef HAVE_MACRO__func__
  78. #ifdef HAVE_MACRO__FUNCTION__
  79. #define __func__ __FUNCTION__
  80. #elif HAVE_MACRO__FUNC__
  81. #define __func__ __FUNC__
  82. #else
  83. #define __func__ "???"
  84. #endif
  85. #endif /* ifndef MAVE_MACRO__func__ */
  86. #endif /* if not windows */
  87. #define U64_TO_DBL(x) ((double) (x))
  88. #define DBL_TO_U64(x) ((uint64_t) (x))
  89. #ifdef ENUM_VALS_ARE_SIGNED
  90. #define ENUM_BF(t) unsigned
  91. #else
  92. /** Wrapper for having a bitfield of an enumerated type. Where possible, we
  93. * just use the enumerated type (so the compiler can help us and notice
  94. * problems), but if enumerated types are unsigned, we must use unsigned,
  95. * so that the loss of precision doesn't make large values negative. */
  96. #define ENUM_BF(t) t
  97. #endif
  98. /* GCC has several useful attributes. */
  99. #if defined(__GNUC__) && __GNUC__ >= 3
  100. #define ATTR_NORETURN __attribute__((noreturn))
  101. #define ATTR_CONST __attribute__((const))
  102. #define ATTR_MALLOC __attribute__((malloc))
  103. #define ATTR_NORETURN __attribute__((noreturn))
  104. #define ATTR_WUR __attribute__((warn_unused_result))
  105. /* Alas, nonnull is not at present a good idea for us. We'd like to get
  106. * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
  107. * spottily), but we don't want to tell the compiler to make optimizations
  108. * with the assumption that the argument can't be NULL (since this would make
  109. * many of our checks go away, and make our code less robust against
  110. * programming errors). Unfortunately, nonnull currently does both of these
  111. * things, and there's no good way to split them up.
  112. *
  113. * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
  114. #define ATTR_NONNULL(x)
  115. #define ATTR_UNUSED __attribute__ ((unused))
  116. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  117. * of <b>exp</b> will probably be true.
  118. *
  119. * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
  120. * except that it tells the compiler that the branch will be taken most of the
  121. * time. This can generate slightly better code with some CPUs.
  122. */
  123. #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
  124. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  125. * of <b>exp</b> will probably be false.
  126. *
  127. * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
  128. * except that it tells the compiler that the branch will usually not be
  129. * taken. This can generate slightly better code with some CPUs.
  130. */
  131. #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
  132. #else
  133. #define ATTR_NORETURN
  134. #define ATTR_CONST
  135. #define ATTR_MALLOC
  136. #define ATTR_NORETURN
  137. #define ATTR_NONNULL(x)
  138. #define ATTR_UNUSED
  139. #define ATTR_WUR
  140. #define PREDICT_LIKELY(exp) (exp)
  141. #define PREDICT_UNLIKELY(exp) (exp)
  142. #endif
  143. /** Expands to a syntactically valid empty statement. */
  144. #define STMT_NIL (void)0
  145. /** Expands to a syntactically valid empty statement, explicitly (void)ing its
  146. * argument. */
  147. #define STMT_VOID(a) while (0) { (void)(a); }
  148. #ifdef __GNUC__
  149. /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
  150. * the macro can be used as if it were a single C statement. */
  151. #define STMT_BEGIN (void) ({
  152. #define STMT_END })
  153. #elif defined(sun) || defined(__sun__)
  154. #define STMT_BEGIN if (1) {
  155. #define STMT_END } else STMT_NIL
  156. #else
  157. #define STMT_BEGIN do {
  158. #define STMT_END } while (0)
  159. #endif
  160. /* Some tools (like coccinelle) don't like to see operators as macro
  161. * arguments. */
  162. #define OP_LT <
  163. #define OP_GT >
  164. #define OP_GE >=
  165. #define OP_LE <=
  166. #define OP_EQ ==
  167. #define OP_NE !=
  168. /* ===== String compatibility */
  169. #ifdef _WIN32
  170. /* Windows names string functions differently from most other platforms. */
  171. #define strncasecmp _strnicmp
  172. #define strcasecmp _stricmp
  173. #endif
  174. #if defined __APPLE__
  175. /* On OSX 10.9 and later, the overlap-checking code for strlcat would
  176. * appear to have a severe bug that can sometimes cause aborts in Tor.
  177. * Instead, use the non-checking variants. This is sad.
  178. *
  179. * See https://trac.torproject.org/projects/tor/ticket/15205
  180. */
  181. #undef strlcat
  182. #undef strlcpy
  183. #endif
  184. #ifndef HAVE_STRLCAT
  185. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  186. #endif
  187. #ifndef HAVE_STRLCPY
  188. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  189. #endif
  190. #ifdef _MSC_VER
  191. /** Casts the uint64_t value in <b>a</b> to the right type for an argument
  192. * to printf. */
  193. #define U64_PRINTF_ARG(a) (a)
  194. /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
  195. * to scanf. */
  196. #define U64_SCANF_ARG(a) (a)
  197. /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
  198. #define U64_LITERAL(n) (n ## ui64)
  199. #define I64_PRINTF_ARG(a) (a)
  200. #define I64_SCANF_ARG(a) (a)
  201. #define I64_LITERAL(n) (n ## i64)
  202. #else
  203. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  204. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  205. #define U64_LITERAL(n) (n ## llu)
  206. #define I64_PRINTF_ARG(a) ((long long signed int)(a))
  207. #define I64_SCANF_ARG(a) ((long long signed int*)(a))
  208. #define I64_LITERAL(n) (n ## ll)
  209. #endif
  210. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  211. /** The formatting string used to put a uint64_t value in a printf() or
  212. * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
  213. #define U64_FORMAT "%I64u"
  214. #define I64_FORMAT "%I64d"
  215. #else
  216. #define U64_FORMAT "%llu"
  217. #define I64_FORMAT "%lld"
  218. #endif
  219. #if (SIZEOF_INTPTR_T == SIZEOF_INT)
  220. #define INTPTR_T_FORMAT "%d"
  221. #define INTPTR_PRINTF_ARG(x) ((int)(x))
  222. #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
  223. #define INTPTR_T_FORMAT "%ld"
  224. #define INTPTR_PRINTF_ARG(x) ((long)(x))
  225. #elif (SIZEOF_INTPTR_T == 8)
  226. #define INTPTR_T_FORMAT I64_FORMAT
  227. #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
  228. #else
  229. #error Unknown: SIZEOF_INTPTR_T
  230. #endif
  231. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  232. * tor_munmap_file. */
  233. typedef struct tor_mmap_t {
  234. const char *data; /**< Mapping of the file's contents. */
  235. size_t size; /**< Size of the file. */
  236. /* None of the fields below should be accessed from outside compat.c */
  237. #ifdef HAVE_SYS_MMAN_H
  238. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  239. * size, rounded up to the nearest page.) */
  240. #elif defined _WIN32
  241. HANDLE mmap_handle;
  242. #endif
  243. } tor_mmap_t;
  244. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  245. int tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  246. int tor_snprintf(char *str, size_t size, const char *format, ...)
  247. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  248. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  249. CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
  250. int tor_asprintf(char **strp, const char *fmt, ...)
  251. CHECK_PRINTF(2,3);
  252. int tor_vasprintf(char **strp, const char *fmt, va_list args)
  253. CHECK_PRINTF(2,0);
  254. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  255. size_t nlen) ATTR_NONNULL((1,3));
  256. static const void *tor_memstr(const void *haystack, size_t hlen,
  257. const char *needle) ATTR_NONNULL((1,3));
  258. static inline const void *
  259. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  260. {
  261. return tor_memmem(haystack, hlen, needle, strlen(needle));
  262. }
  263. /* Much of the time when we're checking ctypes, we're doing spec compliance,
  264. * which all assumes we're doing ASCII. */
  265. #define DECLARE_CTYPE_FN(name) \
  266. static int TOR_##name(char c); \
  267. extern const uint32_t TOR_##name##_TABLE[]; \
  268. static inline int TOR_##name(char c) { \
  269. uint8_t u = c; \
  270. return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
  271. }
  272. DECLARE_CTYPE_FN(ISALPHA)
  273. DECLARE_CTYPE_FN(ISALNUM)
  274. DECLARE_CTYPE_FN(ISSPACE)
  275. DECLARE_CTYPE_FN(ISDIGIT)
  276. DECLARE_CTYPE_FN(ISXDIGIT)
  277. DECLARE_CTYPE_FN(ISPRINT)
  278. DECLARE_CTYPE_FN(ISLOWER)
  279. DECLARE_CTYPE_FN(ISUPPER)
  280. extern const char TOR_TOUPPER_TABLE[];
  281. extern const char TOR_TOLOWER_TABLE[];
  282. #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
  283. #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
  284. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  285. #ifdef HAVE_STRTOK_R
  286. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  287. #else
  288. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  289. #endif
  290. #ifdef _WIN32
  291. #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
  292. const char *tor_fix_source_file(const char *fname);
  293. #else
  294. #define SHORT_FILE__ (__FILE__)
  295. #define tor_fix_source_file(s) (s)
  296. #endif
  297. /* ===== Time compatibility */
  298. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  299. /** Implementation of timeval for platforms that don't have it. */
  300. struct timeval {
  301. time_t tv_sec;
  302. unsigned int tv_usec;
  303. };
  304. #endif
  305. void tor_gettimeofday(struct timeval *timeval);
  306. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  307. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  308. #ifndef timeradd
  309. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  310. * the sum of tv1 and tv2. */
  311. #define timeradd(tv1,tv2,tvout) \
  312. do { \
  313. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  314. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  315. if ((tvout)->tv_usec >= 1000000) { \
  316. (tvout)->tv_usec -= 1000000; \
  317. (tvout)->tv_sec++; \
  318. } \
  319. } while (0)
  320. #endif
  321. #ifndef timersub
  322. /** Replacement for timersub on platforms that do not have it: sets tvout to
  323. * tv1 minus tv2. */
  324. #define timersub(tv1,tv2,tvout) \
  325. do { \
  326. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  327. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  328. if ((tvout)->tv_usec < 0) { \
  329. (tvout)->tv_usec += 1000000; \
  330. (tvout)->tv_sec--; \
  331. } \
  332. } while (0)
  333. #endif
  334. #ifndef timercmp
  335. /** Replacement for timersub on platforms that do not have it: returns true
  336. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  337. *
  338. * Note that while this definition should work for all boolean opeators, some
  339. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  340. * don't use those.
  341. */
  342. #define timercmp(tv1,tv2,op) \
  343. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  344. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  345. ((tv1)->tv_sec op (tv2)->tv_sec))
  346. #endif
  347. /* ===== File compatibility */
  348. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  349. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  350. int tor_rename(const char *path_old, const char *path_new);
  351. int replace_file(const char *from, const char *to);
  352. int touch_file(const char *fname);
  353. typedef struct tor_lockfile_t tor_lockfile_t;
  354. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  355. int *locked_out);
  356. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  357. off_t tor_fd_getpos(int fd);
  358. int tor_fd_setpos(int fd, off_t pos);
  359. int tor_fd_seekend(int fd);
  360. int tor_ftruncate(int fd);
  361. int64_t tor_get_avail_disk_space(const char *path);
  362. #ifdef _WIN32
  363. #define PATH_SEPARATOR "\\"
  364. #else
  365. #define PATH_SEPARATOR "/"
  366. #endif
  367. /* ===== Net compatibility */
  368. #if (SIZEOF_SOCKLEN_T == 0)
  369. typedef int socklen_t;
  370. #endif
  371. #ifdef _WIN32
  372. /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
  373. * any inadvertant checks for the socket being <= 0 or > 0 will probably
  374. * still work. */
  375. #define tor_socket_t intptr_t
  376. #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
  377. #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
  378. #define TOR_INVALID_SOCKET INVALID_SOCKET
  379. #else
  380. /** Type used for a network socket. */
  381. #define tor_socket_t int
  382. #define TOR_SOCKET_T_FORMAT "%d"
  383. /** Macro: true iff 's' is a possible value for a valid initialized socket. */
  384. #define SOCKET_OK(s) ((s) >= 0)
  385. /** Error/uninitialized value for a tor_socket_t. */
  386. #define TOR_INVALID_SOCKET (-1)
  387. #endif
  388. int tor_close_socket_simple(tor_socket_t s);
  389. int tor_close_socket(tor_socket_t s);
  390. tor_socket_t tor_open_socket_with_extensions(
  391. int domain, int type, int protocol,
  392. int cloexec, int nonblock);
  393. MOCK_DECL(tor_socket_t,
  394. tor_open_socket,(int domain, int type, int protocol));
  395. tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol);
  396. tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr,
  397. socklen_t *len);
  398. tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd,
  399. struct sockaddr *addr,
  400. socklen_t *len);
  401. tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd,
  402. struct sockaddr *addr,
  403. socklen_t *len,
  404. int cloexec, int nonblock);
  405. MOCK_DECL(tor_socket_t,
  406. tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address,
  407. socklen_t address_len));
  408. int get_n_open_sockets(void);
  409. MOCK_DECL(int,
  410. tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
  411. socklen_t *address_len));
  412. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  413. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  414. /** Implementation of struct in6_addr for platforms that do not have it.
  415. * Generally, these platforms are ones without IPv6 support, but we want to
  416. * have a working in6_addr there anyway, so we can use it to parse IPv6
  417. * addresses. */
  418. #if !defined(HAVE_STRUCT_IN6_ADDR)
  419. struct in6_addr
  420. {
  421. union {
  422. uint8_t u6_addr8[16];
  423. uint16_t u6_addr16[8];
  424. uint32_t u6_addr32[4];
  425. } in6_u;
  426. #define s6_addr in6_u.u6_addr8
  427. #define s6_addr16 in6_u.u6_addr16
  428. #define s6_addr32 in6_u.u6_addr32
  429. };
  430. #endif
  431. /** @{ */
  432. /** Many BSD variants seem not to define these. */
  433. #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
  434. || defined(__NetBSD__) || defined(__OpenBSD__)
  435. #ifndef s6_addr16
  436. #define s6_addr16 __u6_addr.__u6_addr16
  437. #endif
  438. #ifndef s6_addr32
  439. #define s6_addr32 __u6_addr.__u6_addr32
  440. #endif
  441. #endif
  442. /** @} */
  443. #ifndef HAVE_SA_FAMILY_T
  444. typedef uint16_t sa_family_t;
  445. #endif
  446. /** @{ */
  447. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  448. * macros get you a pointer to s6_addr32 or local equivalent. */
  449. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  450. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  451. #else
  452. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  453. #endif
  454. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  455. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  456. #else
  457. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  458. #endif
  459. /** @} */
  460. /** Implementation of struct sockaddr_in6 on platforms that do not have
  461. * it. See notes on struct in6_addr. */
  462. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  463. struct sockaddr_in6 {
  464. sa_family_t sin6_family;
  465. uint16_t sin6_port;
  466. // uint32_t sin6_flowinfo;
  467. struct in6_addr sin6_addr;
  468. // uint32_t sin6_scope_id;
  469. };
  470. #endif
  471. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  472. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  473. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  474. int tor_inet_pton(int af, const char *src, void *dst);
  475. MOCK_DECL(int,tor_lookup_hostname,(const char *name, uint32_t *addr));
  476. int set_socket_nonblocking(tor_socket_t socket);
  477. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  478. int network_init(void);
  479. /* For stupid historical reasons, windows sockets have an independent
  480. * set of errnos, and an independent way to get them. Also, you can't
  481. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  482. * errnos against expected values, and use tor_socket_errno to find
  483. * the actual errno after a socket operation fails.
  484. */
  485. #if defined(_WIN32)
  486. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  487. #define SOCK_ERRNO(e) WSA##e
  488. /** Return true if e is EAGAIN or the local equivalent. */
  489. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  490. /** Return true if e is EINPROGRESS or the local equivalent. */
  491. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  492. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  493. * a call to connect(). */
  494. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  495. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  496. /** Return true if e is EAGAIN or another error indicating that a call to
  497. * accept() has no pending connections to return. */
  498. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  499. /** Return true if e is EMFILE or another error indicating that a call to
  500. * accept() has failed because we're out of fds or something. */
  501. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  502. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  503. /** Return true if e is EADDRINUSE or the local equivalent. */
  504. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  505. /** Return true if e is EINTR or the local equivalent */
  506. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  507. int tor_socket_errno(tor_socket_t sock);
  508. const char *tor_socket_strerror(int e);
  509. #else
  510. #define SOCK_ERRNO(e) e
  511. #if EAGAIN == EWOULDBLOCK
  512. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  513. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  514. #else
  515. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  516. #endif
  517. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  518. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  519. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  520. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  521. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  522. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  523. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  524. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  525. #define tor_socket_errno(sock) (errno)
  526. #define tor_socket_strerror(e) strerror(e)
  527. #endif
  528. /** Specified SOCKS5 status codes. */
  529. typedef enum {
  530. SOCKS5_SUCCEEDED = 0x00,
  531. SOCKS5_GENERAL_ERROR = 0x01,
  532. SOCKS5_NOT_ALLOWED = 0x02,
  533. SOCKS5_NET_UNREACHABLE = 0x03,
  534. SOCKS5_HOST_UNREACHABLE = 0x04,
  535. SOCKS5_CONNECTION_REFUSED = 0x05,
  536. SOCKS5_TTL_EXPIRED = 0x06,
  537. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  538. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  539. } socks5_reply_status_t;
  540. /* ===== OS compatibility */
  541. MOCK_DECL(const char *, get_uname, (void));
  542. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  543. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  544. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  545. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  546. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  547. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  548. /* These uint8 variants are defined to make the code more uniform. */
  549. #define get_uint8(cp) (*(const uint8_t*)(cp))
  550. static void set_uint8(void *cp, uint8_t v);
  551. static inline void
  552. set_uint8(void *cp, uint8_t v)
  553. {
  554. *(uint8_t*)cp = v;
  555. }
  556. #if !defined(HAVE_RLIM_T)
  557. typedef unsigned long rlim_t;
  558. #endif
  559. int get_max_sockets(void);
  560. int set_max_file_descriptors(rlim_t limit, int *max);
  561. int tor_disable_debugger_attach(void);
  562. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  563. #define HAVE_LINUX_CAPABILITIES
  564. #endif
  565. int have_capability_support(void);
  566. /** Flag for switch_id; see switch_id() for documentation */
  567. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  568. /** Flag for switch_id; see switch_id() for documentation */
  569. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  570. int switch_id(const char *user, unsigned flags);
  571. #ifdef HAVE_PWD_H
  572. char *get_user_homedir(const char *username);
  573. #endif
  574. #ifndef _WIN32
  575. const struct passwd *tor_getpwnam(const char *username);
  576. const struct passwd *tor_getpwuid(uid_t uid);
  577. #endif
  578. int get_parent_directory(char *fname);
  579. char *make_path_absolute(char *fname);
  580. char **get_environment(void);
  581. int get_total_system_memory(size_t *mem_out);
  582. int compute_num_cpus(void);
  583. int tor_mlockall(void);
  584. /** Macros for MIN/MAX. Never use these when the arguments could have
  585. * side-effects.
  586. * {With GCC extensions we could probably define a safer MIN/MAX. But
  587. * depending on that safety would be dangerous, since not every platform
  588. * has it.}
  589. **/
  590. #ifndef MAX
  591. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  592. #endif
  593. #ifndef MIN
  594. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  595. #endif
  596. /* Platform-specific helpers. */
  597. #ifdef _WIN32
  598. char *format_win32_error(DWORD err);
  599. #endif
  600. /*for some reason my compiler doesn't have these version flags defined
  601. a nice homework assignment for someone one day is to define the rest*/
  602. //these are the values as given on MSDN
  603. #ifdef _WIN32
  604. #ifndef VER_SUITE_EMBEDDEDNT
  605. #define VER_SUITE_EMBEDDEDNT 0x00000040
  606. #endif
  607. #ifndef VER_SUITE_SINGLEUSERTS
  608. #define VER_SUITE_SINGLEUSERTS 0x00000100
  609. #endif
  610. #endif
  611. #ifdef TOR_UNIT_TESTS
  612. void tor_sleep_msec(int msec);
  613. #endif
  614. #ifdef COMPAT_PRIVATE
  615. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  616. #define NEED_ERSATZ_SOCKETPAIR
  617. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  618. tor_socket_t fd[2]);
  619. #endif
  620. #endif
  621. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  622. /* This needs some of the declarations above so we include it here. */
  623. #include "compat_threads.h"
  624. #endif