compat.h 12 KB

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