compat.h 15 KB

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