compat.h 23 KB

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