compat.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* Copyright (c) 2003-2004, Roger Dingledinex
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. #ifndef __COMPAT_H
  7. #define __COMPAT_H
  8. #define COMPAT_H_ID "$Id$"
  9. #include "orconfig.h"
  10. #include "torint.h"
  11. #ifdef MS_WINDOWS
  12. #define WIN32_WINNT 0x400
  13. #define _WIN32_WINNT 0x400
  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_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #ifdef HAVE_SYS_TIME_H
  26. #include <sys/time.h>
  27. #endif
  28. #ifdef HAVE_TIME_H
  29. #include <time.h>
  30. #endif
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. #ifdef HAVE_CTYPE_H
  35. #include <ctype.h>
  36. #endif
  37. #include <stdarg.h>
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #ifdef HAVE_NETINET_IN_H
  42. #include <netinet/in.h>
  43. #endif
  44. #ifdef HAVE_NETINET6_IN6_H
  45. #include <netinet6/in6.h>
  46. #endif
  47. #ifndef NULL_REP_IS_ZERO_BYTES
  48. #error "It seems your platform does not represent NULL as zero. We can't cope."
  49. #endif
  50. #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
  51. #error "It seems that you encode characters in something other than ASCII."
  52. #endif
  53. /* ===== Compiler compatibility */
  54. /* GCC can check printf types on arbitrary functions. */
  55. #ifdef __GNUC__
  56. #define CHECK_PRINTF(formatIdx, firstArg) \
  57. __attribute__ ((format(printf, formatIdx, firstArg)))
  58. #else
  59. #define CHECK_PRINTF(formatIdx, firstArg)
  60. #endif
  61. /* inline is __inline on windows. */
  62. #ifdef MS_WINDOWS
  63. #define INLINE __inline
  64. #else
  65. #define INLINE inline
  66. #endif
  67. /* Try to get a reasonable __func__ substitute in place. */
  68. #if defined(_MSC_VER)
  69. /* MSVC compilers before VC7 don't have __func__ at all; later ones call it
  70. * __FUNCTION__. */
  71. #if _MSC_VER < 1300
  72. #define __func__ "???"
  73. #else
  74. #define __func__ __FUNCTION__
  75. #endif
  76. #else
  77. /* For platforms where autoconf works, make sure __func__ is defined
  78. * sanely. */
  79. #ifndef HAVE_MACRO__func__
  80. #ifdef HAVE_MACRO__FUNCTION__
  81. #define __func__ __FUNCTION__
  82. #elif HAVE_MACRO__FUNC__
  83. #define __func__ __FUNC__
  84. #else
  85. #define __func__ "???"
  86. #endif
  87. #endif /* ifndef MAVE_MACRO__func__ */
  88. #endif /* if not windows */
  89. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  90. /* MSVC versions before 7 apparently don't believe that you can cast uint64_t
  91. * to double and really mean it. */
  92. extern INLINE double U64_TO_DBL(uint64_t x) {
  93. int64_t i = (int64_t) x;
  94. return (i < 0) ? ((double) INT64_MAX) : (double) i;
  95. }
  96. #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
  97. #else
  98. #define U64_TO_DBL(x) ((double) (x))
  99. #define DBL_TO_U64(x) ((uint64_t) (x))
  100. #endif
  101. /* GCC has several useful attributes. */
  102. #if defined(__GNUC__) && __GNUC__ >= 3
  103. #define ATTR_NORETURN __attribute__((noreturn))
  104. #define ATTR_PURE __attribute__((pure))
  105. #define ATTR_CONST __attribute__((const))
  106. #define ATTR_MALLOC __attribute__((malloc))
  107. #define ATTR_NORETURN __attribute__((noreturn))
  108. #define ATTR_NONNULL(x) __attribute__((nonnull x))
  109. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  110. * of <b>exp</b> will probably be true. */
  111. #define PREDICT_LIKELY(exp) __builtin_expect((exp), 1)
  112. /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
  113. * of <b>exp</b> will probably be false. */
  114. #define PREDICT_UNLIKELY(exp) __builtin_expect((exp), 0)
  115. #else
  116. #define ATTR_NORETURN
  117. #define ATTR_PURE
  118. #define ATTR_CONST
  119. #define ATTR_MALLOC
  120. #define ATTR_NORETURN
  121. #define ATTR_NONNULL(x)
  122. #define PREDICT_LIKELY(exp) (exp)
  123. #define PREDICT_UNLIKELY(exp) (exp)
  124. #endif
  125. /* Ways to declare macros. */
  126. #define STMT_NIL (void)0
  127. #ifdef __GNUC__
  128. #define STMT_BEGIN (void) ({
  129. #define STMT_END })
  130. #elif defined(sun) || defined(__sun__)
  131. #define STMT_BEGIN if (1) {
  132. #define STMT_END } else STMT_NIL
  133. #else
  134. #define STMT_BEGIN do {
  135. #define STMT_END } while (0)
  136. #endif
  137. /* ===== String compatibility */
  138. #ifdef MS_WINDOWS
  139. /* Windows names string functions differently from most other platforms. */
  140. #define strncasecmp strnicmp
  141. #define strcasecmp stricmp
  142. #endif
  143. #ifndef HAVE_STRLCAT
  144. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  145. #endif
  146. #ifndef HAVE_STRLCPY
  147. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  148. #endif
  149. #ifdef _MSC_VER
  150. #define U64_PRINTF_ARG(a) (a)
  151. #define U64_SCANF_ARG(a) (a)
  152. #define U64_LITERAL(n) (n ## ui64)
  153. #else
  154. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  155. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  156. #define U64_LITERAL(n) (n ## llu)
  157. #endif
  158. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  159. #define U64_FORMAT "%I64u"
  160. #else
  161. #define U64_FORMAT "%llu"
  162. #endif
  163. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  164. * tor_munmap_file. */
  165. typedef struct tor_mmap_t {
  166. const char *data; /**< Mapping of the file's contents. */
  167. size_t size; /**< Size of the file. */
  168. } tor_mmap_t;
  169. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  170. void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  171. int tor_snprintf(char *str, size_t size, const char *format, ...)
  172. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  173. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  174. ATTR_NONNULL((1,3));
  175. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  176. size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
  177. static const void *tor_memstr(const void *haystack, size_t hlen,
  178. const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
  179. static INLINE const void *
  180. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  181. {
  182. return tor_memmem(haystack, hlen, needle, strlen(needle));
  183. }
  184. #define TOR_ISALPHA(c) isalpha((int)(unsigned char)(c))
  185. #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
  186. #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
  187. #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
  188. #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
  189. #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
  190. #define TOR_ISLOWER(c) islower((int)(unsigned char)(c))
  191. #define TOR_ISUPPER(c) isupper((int)(unsigned char)(c))
  192. #define TOR_TOLOWER(c) ((char)tolower((int)(unsigned char)(c)))
  193. #define TOR_TOUPPER(c) ((char)toupper((int)(unsigned char)(c)))
  194. #ifdef MS_WINDOWS
  195. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  196. const char *tor_fix_source_file(const char *fname);
  197. #else
  198. #define _SHORT_FILE_ (__FILE__)
  199. #define tor_fix_source_file(s) (s)
  200. #endif
  201. /* ===== Time compatibility */
  202. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  203. struct timeval {
  204. time_t tv_sec;
  205. unsigned int tv_usec;
  206. };
  207. #endif
  208. void tor_gettimeofday(struct timeval *timeval);
  209. #ifdef HAVE_LOCALTIME_R
  210. #define tor_localtime_r localtime_r
  211. #else
  212. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  213. #endif
  214. #ifdef HAVE_GMTIME_R
  215. #define tor_gmtime_r gmtime_r
  216. #else
  217. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  218. #endif
  219. /* ===== File compatibility */
  220. int replace_file(const char *from, const char *to);
  221. int touch_file(const char *fname);
  222. #ifdef MS_WINDOWS
  223. #define PATH_SEPARATOR "\\"
  224. #else
  225. #define PATH_SEPARATOR "/"
  226. #endif
  227. /* ===== Net compatibility */
  228. void tor_close_socket(int s);
  229. int tor_open_socket(int domain, int type, int protocol);
  230. int get_n_open_sockets(void);
  231. #ifdef USE_BSOCKETS
  232. #define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags)
  233. #define tor_socket_recv(s, buf, len, flags) brecv(s, buf, len, flags)
  234. #else
  235. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  236. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  237. #endif
  238. #if (SIZEOF_SOCKLEN_T == 0)
  239. typedef int socklen_t;
  240. #endif
  241. /* Define struct in6_addr on platforms that do not have it. Generally,
  242. * these platforms are ones without IPv6 support, but we want to have
  243. * a working in6_addr there anyway, so we can use it to parse IPv6
  244. * addresses. */
  245. #if !defined(HAVE_STRUCT_IN6_ADDR)
  246. struct in6_addr
  247. {
  248. union {
  249. uint8_t u6_addr8[16];
  250. uint16_t u6_addr16[8];
  251. uint32_t u6_addr32[4];
  252. } in6_u;
  253. #define s6_addr in6_u.u6_addr8
  254. #define s6_addr16 in6_u.u6_addr16
  255. #define s6_addr32 in6_u.u6_addr32
  256. };
  257. #endif
  258. #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
  259. || defined(__NetBSD__) || defined(__OpenBSD__)
  260. /* Many BSD variants seem not to define these. */
  261. #ifndef s6_addr16
  262. #define s6_addr16 __u6_addr.__u6_addr16
  263. #endif
  264. #ifndef s6_addr32
  265. #define s6_addr32 __u6_addr.__u6_addr32
  266. #endif
  267. #endif
  268. #ifndef HAVE_SA_FAMILY_T
  269. typedef uint16_t sa_family_t;
  270. #endif
  271. /* Apparently, MS and Solaris don't define s6_addr16 or s6_addr32. */
  272. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  273. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  274. #else
  275. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  276. #endif
  277. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  278. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  279. #else
  280. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  281. #endif
  282. /* Define struct sockaddr_in6 on platforms that do not have it. See notes
  283. * on struct in6_addr. */
  284. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  285. struct sockaddr_in6 {
  286. sa_family_t sin6_family;
  287. uint16_t sin6_port;
  288. // uint32_t sin6_flowinfo;
  289. struct in6_addr sin6_addr;
  290. // uint32_t sin6_scope_id;
  291. };
  292. #endif
  293. typedef uint8_t maskbits_t;
  294. struct in_addr;
  295. typedef struct tor_addr_t
  296. {
  297. sa_family_t family;
  298. union {
  299. struct in_addr in_addr;
  300. struct in6_addr in6_addr;
  301. } addr;
  302. } tor_addr_t;
  303. /* XXXX020 rename these. */
  304. static INLINE uint32_t IPV4IP(const tor_addr_t *a);
  305. static INLINE uint32_t IPV4IPh(const tor_addr_t *a);
  306. static INLINE uint32_t IPV4MAPh(const tor_addr_t *a);
  307. static INLINE uint16_t IN_FAMILY(const tor_addr_t *a);
  308. static INLINE const struct in_addr *IN4_ADDRESS(const tor_addr_t *a);
  309. static INLINE const struct in6_addr *IN6_ADDRESS(const tor_addr_t *a);
  310. static INLINE const struct in6_addr *
  311. IN6_ADDRESS(const tor_addr_t *a)
  312. {
  313. return &a->addr.in6_addr;
  314. }
  315. #define IN6_ADDRESS16(x) S6_ADDR16(*IN6_ADDRESS(x))
  316. #define IN6_ADDRESS32(x) S6_ADDR32(*IN6_ADDRESS(x))
  317. static INLINE uint32_t
  318. IPV4IP(const tor_addr_t *a)
  319. {
  320. return a->addr.in_addr.s_addr;
  321. }
  322. static INLINE uint32_t IPV4IPh(const tor_addr_t *a)
  323. {
  324. return ntohl(IPV4IP(a));
  325. }
  326. static INLINE uint32_t
  327. IPV4MAPh(const tor_addr_t *a)
  328. {
  329. return ntohl(IN6_ADDRESS32(a)[3]);
  330. }
  331. static INLINE uint16_t
  332. IN_FAMILY(const tor_addr_t *a)
  333. {
  334. return a->family;
  335. }
  336. static INLINE const struct in_addr *
  337. IN4_ADDRESS(const tor_addr_t *a)
  338. {
  339. return &a->addr.in_addr;
  340. }
  341. #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
  342. #define TOR_ADDR_BUF_LEN 46 /* ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255 */
  343. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  344. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  345. int tor_inet_pton(int af, const char *src, void *dst);
  346. int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
  347. void set_socket_nonblocking(int socket);
  348. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  349. int network_init(void);
  350. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  351. /* For stupid historical reasons, windows sockets have an independent
  352. * set of errnos, and an independent way to get them. Also, you can't
  353. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  354. * errnos against expected values, and use tor_socket_errno to find
  355. * the actual errno after a socket operation fails.
  356. */
  357. #if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
  358. /** Return true if e is EAGAIN or the local equivalent. */
  359. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  360. /** Return true if e is EINPROGRESS or the local equivalent. */
  361. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  362. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  363. * a call to connect(). */
  364. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  365. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  366. /** Return true if e is EAGAIN or another error indicating that a call to
  367. * accept() has no pending connections to return. */
  368. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  369. /** Return true if e is EMFILE or another error indicating that a call to
  370. * accept() has failed because we're out of fds or something. */
  371. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  372. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  373. /** Return true if e is EADDRINUSE or the local equivalent. */
  374. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  375. int tor_socket_errno(int sock);
  376. const char *tor_socket_strerror(int e);
  377. #else
  378. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  379. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  380. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  381. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  382. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  383. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  384. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  385. #define tor_socket_errno(sock) (errno)
  386. #define tor_socket_strerror(e) strerror(e)
  387. #endif
  388. /* ===== OS compatibility */
  389. const char *get_uname(void);
  390. uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  391. uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  392. void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
  393. void set_uint32(char *cp, uint32_t v) ATTR_NONNULL((1));
  394. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  395. int switch_id(const char *user, const char *group);
  396. #ifdef HAVE_PWD_H
  397. char *get_user_homedir(const char *username);
  398. #endif
  399. int spawn_func(void (*func)(void *), void *data);
  400. void spawn_exit(void) ATTR_NORETURN;
  401. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  402. #define USE_WIN32_THREADS
  403. #define TOR_IS_MULTITHREADED 1
  404. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  405. defined(HAVE_PTHREAD_CREATE))
  406. #define USE_PTHREADS
  407. #define TOR_IS_MULTITHREADED 1
  408. #else
  409. #undef TOR_IS_MULTITHREADED
  410. #endif
  411. /* Because we use threads instead of processes on most platforms (Windows,
  412. * Linux, etc), we need locking for them. On platforms with poor thread
  413. * support or broken gethostbyname_r, these functions are no-ops. */
  414. typedef struct tor_mutex_t tor_mutex_t;
  415. #ifdef TOR_IS_MULTITHREADED
  416. tor_mutex_t *tor_mutex_new(void);
  417. void tor_mutex_acquire(tor_mutex_t *m);
  418. void tor_mutex_release(tor_mutex_t *m);
  419. void tor_mutex_free(tor_mutex_t *m);
  420. unsigned long tor_get_thread_id(void);
  421. void tor_threads_init(void);
  422. #else
  423. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  424. #define tor_mutex_acquire(m) STMT_NIL
  425. #define tor_mutex_release(m) STMT_NIL
  426. #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
  427. #define tor_get_thread_id() (1UL)
  428. #define tor_threads_init() STMT_NIL
  429. #endif
  430. #ifdef TOR_IS_MULTITHREADED
  431. #if 0
  432. typedef struct tor_cond_t tor_cond_t;
  433. tor_cond_t *tor_cond_new(void);
  434. void tor_cond_free(tor_cond_t *cond);
  435. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
  436. void tor_cond_signal_one(tor_cond_t *cond);
  437. void tor_cond_signal_all(tor_cond_t *cond);
  438. #endif
  439. #endif
  440. /* Platform-specific helpers. */
  441. #ifdef MS_WINDOWS
  442. char *format_win32_error(DWORD err);
  443. #endif
  444. /*for some reason my compiler doesn't have these version flags defined
  445. a nice homework assignment for someone one day is to define the rest*/
  446. //these are the values as given on MSDN
  447. #ifdef MS_WINDOWS
  448. #ifndef VER_SUITE_EMBEDDEDNT
  449. #define VER_SUITE_EMBEDDEDNT 0x00000040
  450. #endif
  451. #ifndef VER_SUITE_SINGLEUSERTS
  452. #define VER_SUITE_SINGLEUSERTS 0x00000100
  453. #endif
  454. #endif
  455. #endif