compat.h 24 KB

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