compat.h 23 KB

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