compat.h 23 KB

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