compat.h 15 KB

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