compat.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* Copyright (c) 2003-2004, Roger Dingledinex
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007, 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. #else
  131. #if defined(sun) || defined(__sun__)
  132. #define STMT_BEGIN if (1) {
  133. #define STMT_END } else STMT_NIL
  134. #else
  135. #define STMT_BEGIN do {
  136. #define STMT_END } while (0)
  137. #endif
  138. #endif
  139. /* ===== String compatibility */
  140. #ifdef MS_WINDOWS
  141. /* Windows names string functions differently from most other platforms. */
  142. #define strncasecmp strnicmp
  143. #define strcasecmp stricmp
  144. #endif
  145. #ifndef HAVE_STRLCAT
  146. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  147. #endif
  148. #ifndef HAVE_STRLCPY
  149. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  150. #endif
  151. #ifdef _MSC_VER
  152. #define U64_PRINTF_ARG(a) (a)
  153. #define U64_SCANF_ARG(a) (a)
  154. #define U64_LITERAL(n) (n ## ui64)
  155. #else
  156. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  157. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  158. #define U64_LITERAL(n) (n ## llu)
  159. #endif
  160. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  161. #define U64_FORMAT "%I64u"
  162. #else
  163. #define U64_FORMAT "%llu"
  164. #endif
  165. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  166. * tor_munmap_file. */
  167. typedef struct tor_mmap_t {
  168. const char *data; /**< Mapping of the file's contents. */
  169. size_t size; /**< Size of the file. */
  170. } tor_mmap_t;
  171. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  172. void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  173. int tor_snprintf(char *str, size_t size, const char *format, ...)
  174. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  175. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  176. ATTR_NONNULL((1,3));
  177. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  178. size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
  179. static const void *tor_memstr(const void *haystack, size_t hlen,
  180. const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
  181. static INLINE const void *
  182. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  183. {
  184. return tor_memmem(haystack, hlen, needle, strlen(needle));
  185. }
  186. #define TOR_ISALPHA(c) isalpha((int)(unsigned char)(c))
  187. #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
  188. #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
  189. #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
  190. #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
  191. #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
  192. #define TOR_ISLOWER(c) islower((int)(unsigned char)(c))
  193. #define TOR_ISUPPER(c) isupper((int)(unsigned char)(c))
  194. #define TOR_TOLOWER(c) ((char)tolower((int)(unsigned char)(c)))
  195. #define TOR_TOUPPER(c) ((char)toupper((int)(unsigned char)(c)))
  196. #ifdef MS_WINDOWS
  197. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  198. const char *tor_fix_source_file(const char *fname);
  199. #else
  200. #define _SHORT_FILE_ (__FILE__)
  201. #define tor_fix_source_file(s) (s)
  202. #endif
  203. /* ===== Time compatibility */
  204. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  205. struct timeval {
  206. time_t tv_sec;
  207. unsigned int tv_usec;
  208. };
  209. #endif
  210. void tor_gettimeofday(struct timeval *timeval);
  211. #ifdef HAVE_LOCALTIME_R
  212. #define tor_localtime_r localtime_r
  213. #else
  214. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  215. #endif
  216. #ifdef HAVE_GMTIME_R
  217. #define tor_gmtime_r gmtime_r
  218. #else
  219. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  220. #endif
  221. /* ===== File compatibility */
  222. int replace_file(const char *from, const char *to);
  223. int touch_file(const char *fname);
  224. #ifdef MS_WINDOWS
  225. #define PATH_SEPARATOR "\\"
  226. #else
  227. #define PATH_SEPARATOR "/"
  228. #endif
  229. /* ===== Net compatibility */
  230. void tor_close_socket(int s);
  231. int tor_open_socket(int domain, int type, int protocol);
  232. int get_n_open_sockets(void);
  233. #ifdef USE_BSOCKETS
  234. #define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags)
  235. #define tor_socket_recv(s, buf, len, flags) brecv(s, buf, len, flags)
  236. #else
  237. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  238. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  239. #endif
  240. #if (SIZEOF_SOCKLEN_T == 0)
  241. typedef int socklen_t;
  242. #endif
  243. /* XXXX020 detect in6_addr correctly on ms_windows; this is a hack. */
  244. #if !defined(HAVE_STRUCT_IN6_ADDR) && !defined(MS_WINDOWS)
  245. struct in6_addr
  246. {
  247. union {
  248. uint8_t u6_addr8[16];
  249. uint16_t u6_addr16[8];
  250. uint32_t u6_addr32[4];
  251. } in6_u;
  252. #define s6_addr in6_u.u6_addr8
  253. #define s6_addr16 in6_u.u6_addr16
  254. #define s6_addr32 in6_u.u6_addr32
  255. };
  256. #endif
  257. #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
  258. || defined(__NetBSD__) || defined(__OpenBSD__)
  259. /* Many BSD variants seem not to define these. */
  260. #ifndef s6_addr16
  261. #define s6_addr16 __u6_addr.__u6_addr16
  262. #endif
  263. #ifndef s6_addr32
  264. #define s6_addr32 __u6_addr.__u6_addr32
  265. #endif
  266. #endif
  267. #ifndef HAVE_SA_FAMILY_T
  268. typedef uint16_t sa_family_t;
  269. #endif
  270. /* Apparently, MS and Solaris don't define s6_addr16 or s6_addr32. */
  271. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  272. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  273. #else
  274. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  275. #endif
  276. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  277. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  278. #else
  279. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  280. #endif
  281. /* XXXX020 detect sockaddr_in6 correctly on ms_windows; this is also a hack. */
  282. #if !defined(HAVE_STRUCT_SOCKADDR_IN6) && !defined(MS_WINDOWS)
  283. struct sockaddr_in6 {
  284. sa_family_t sin6_family;
  285. uint16_t sin6_port;
  286. // uint32_t sin6_flowinfo;
  287. struct in6_addr sin6_addr;
  288. // uint32_t sin6_scope_id;
  289. };
  290. #endif
  291. typedef uint8_t maskbits_t;
  292. struct in_addr;
  293. typedef struct tor_addr_t
  294. {
  295. sa_family_t family;
  296. union {
  297. struct in_addr in_addr;
  298. struct in6_addr in6_addr;
  299. } addr;
  300. } tor_addr_t;
  301. /* XXXX020 rename these. */
  302. static INLINE uint32_t IPV4IP(const tor_addr_t *a);
  303. static INLINE uint32_t IPV4IPh(const tor_addr_t *a);
  304. static INLINE uint32_t IPV4MAPh(const tor_addr_t *a);
  305. static INLINE uint16_t IN_FAMILY(const tor_addr_t *a);
  306. static INLINE const struct in_addr *IN4_ADDRESS(const tor_addr_t *a);
  307. static INLINE const struct in6_addr *IN6_ADDRESS(const tor_addr_t *a);
  308. static INLINE const struct in6_addr *
  309. IN6_ADDRESS(const tor_addr_t *a)
  310. {
  311. return &a->addr.in6_addr;
  312. }
  313. #define IN6_ADDRESS16(x) S6_ADDR16(*IN6_ADDRESS(x))
  314. #define IN6_ADDRESS32(x) S6_ADDR32(*IN6_ADDRESS(x))
  315. static INLINE uint32_t
  316. IPV4IP(const tor_addr_t *a)
  317. {
  318. return a->addr.in_addr.s_addr;
  319. }
  320. static INLINE uint32_t IPV4IPh(const tor_addr_t *a)
  321. {
  322. /*XXXX020 remove this function */
  323. return ntohl(IPV4IP(a));
  324. }
  325. static INLINE uint32_t
  326. IPV4MAPh(const tor_addr_t *a)
  327. {
  328. return ntohl(IN6_ADDRESS32(a)[3]);
  329. }
  330. static INLINE uint16_t
  331. IN_FAMILY(const tor_addr_t *a)
  332. {
  333. return a->family;
  334. }
  335. static INLINE const struct in_addr *
  336. IN4_ADDRESS(const tor_addr_t *a)
  337. {
  338. return &a->addr.in_addr;
  339. }
  340. #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */
  341. #define TOR_ADDR_BUF_LEN 46 /* ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255 */
  342. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  343. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  344. int tor_inet_pton(int af, const char *src, void *dst);
  345. int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
  346. void set_socket_nonblocking(int socket);
  347. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  348. int network_init(void);
  349. int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
  350. /* For stupid historical reasons, windows sockets have an independent
  351. * set of errnos, and an independent way to get them. Also, you can't
  352. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  353. * errnos against expected values, and use tor_socket_errno to find
  354. * the actual errno after a socket operation fails.
  355. */
  356. #if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
  357. /** Return true if e is EAGAIN or the local equivalent. */
  358. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  359. /** Return true if e is EINPROGRESS or the local equivalent. */
  360. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  361. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  362. * a call to connect(). */
  363. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  364. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  365. /** Return true if e is EAGAIN or another error indicating that a call to
  366. * accept() has no pending connections to return. */
  367. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  368. /** Return true if e is EMFILE or another error indicating that a call to
  369. * accept() has failed because we're out of fds or something. */
  370. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  371. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  372. /** Return true if e is EADDRINUSE or the local equivalent. */
  373. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  374. int tor_socket_errno(int sock);
  375. const char *tor_socket_strerror(int e);
  376. #else
  377. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  378. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  379. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  380. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  381. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  382. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  383. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  384. #define tor_socket_errno(sock) (errno)
  385. #define tor_socket_strerror(e) strerror(e)
  386. #endif
  387. /* ===== OS compatibility */
  388. const char *get_uname(void);
  389. uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  390. uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  391. void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
  392. void set_uint32(char *cp, uint32_t v) ATTR_NONNULL((1));
  393. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  394. int switch_id(const char *user, const char *group);
  395. #ifdef HAVE_PWD_H
  396. char *get_user_homedir(const char *username);
  397. #endif
  398. int spawn_func(void (*func)(void *), void *data);
  399. void spawn_exit(void) ATTR_NORETURN;
  400. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  401. #define USE_WIN32_THREADS
  402. #define TOR_IS_MULTITHREADED 1
  403. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  404. defined(HAVE_PTHREAD_CREATE))
  405. #define USE_PTHREADS
  406. #define TOR_IS_MULTITHREADED 1
  407. #else
  408. #undef TOR_IS_MULTITHREADED
  409. #endif
  410. /* Because we use threads instead of processes on most platforms (Windows,
  411. * Linux, etc), we need locking for them. On platforms with poor thread
  412. * support or broken gethostbyname_r, these functions are no-ops. */
  413. typedef struct tor_mutex_t tor_mutex_t;
  414. #ifdef TOR_IS_MULTITHREADED
  415. tor_mutex_t *tor_mutex_new(void);
  416. void tor_mutex_acquire(tor_mutex_t *m);
  417. void tor_mutex_release(tor_mutex_t *m);
  418. void tor_mutex_free(tor_mutex_t *m);
  419. unsigned long tor_get_thread_id(void);
  420. void tor_threads_init(void);
  421. #else
  422. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  423. #define tor_mutex_acquire(m) STMT_NIL
  424. #define tor_mutex_release(m) STMT_NIL
  425. #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
  426. #define tor_get_thread_id() (1UL)
  427. #define tor_threads_init() STMT_NIL
  428. #endif
  429. #ifdef TOR_IS_MULTITHREADED
  430. typedef struct tor_cond_t tor_cond_t;
  431. tor_cond_t *tor_cond_new(void);
  432. void tor_cond_free(tor_cond_t *cond);
  433. int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
  434. void tor_cond_signal_one(tor_cond_t *cond);
  435. void tor_cond_signal_all(tor_cond_t *cond);
  436. #endif
  437. /* Platform-specific helpers. */
  438. #ifdef MS_WINDOWS
  439. char *format_win32_error(DWORD err);
  440. #endif
  441. /*for some reason my compiler doesn't have these version flags defined
  442. a nice homework assignment for someone one day is to define the rest*/
  443. //these are the values as given on MSDN
  444. #ifdef MS_WINDOWS
  445. #ifndef VER_SUITE_EMBEDDEDNT
  446. #define VER_SUITE_EMBEDDEDNT 0x00000040
  447. #endif
  448. #ifndef VER_SUITE_SINGLEUSERTS
  449. #define VER_SUITE_SINGLEUSERTS 0x00000100
  450. #endif
  451. #endif
  452. #endif