compat.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /* ===== String compatibility */
  113. #ifdef MS_WINDOWS
  114. /* Windows names string functions differently from most other platforms. */
  115. #define strncasecmp strnicmp
  116. #define strcasecmp stricmp
  117. #endif
  118. #ifndef HAVE_STRLCAT
  119. size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  120. #endif
  121. #ifndef HAVE_STRLCPY
  122. size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
  123. #endif
  124. #ifdef _MSC_VER
  125. #define U64_PRINTF_ARG(a) (a)
  126. #define U64_SCANF_ARG(a) (a)
  127. #define U64_LITERAL(n) (n ## ui64)
  128. #else
  129. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  130. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  131. #define U64_LITERAL(n) (n ## llu)
  132. #endif
  133. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
  134. #define U64_FORMAT "%I64u"
  135. #else
  136. #define U64_FORMAT "%llu"
  137. #endif
  138. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  139. * tor_munmap_file. */
  140. typedef struct tor_mmap_t {
  141. const char *data; /**< Mapping of the file's contents. */
  142. size_t size; /**< Size of the file. */
  143. } tor_mmap_t;
  144. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  145. void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  146. int tor_snprintf(char *str, size_t size, const char *format, ...)
  147. CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
  148. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
  149. ATTR_NONNULL((1,3));
  150. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  151. size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
  152. static const void *tor_memstr(const void *haystack, size_t hlen,
  153. const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
  154. static INLINE const void *
  155. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  156. {
  157. return tor_memmem(haystack, hlen, needle, strlen(needle));
  158. }
  159. #define TOR_ISALPHA(c) isalpha((int)(unsigned char)(c))
  160. #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
  161. #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
  162. #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
  163. #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
  164. #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
  165. #define TOR_ISLOWER(c) islower((int)(unsigned char)(c))
  166. #define TOR_ISUPPER(c) isupper((int)(unsigned char)(c))
  167. #define TOR_TOLOWER(c) ((char)tolower((int)(unsigned char)(c)))
  168. #define TOR_TOUPPER(c) ((char)toupper((int)(unsigned char)(c)))
  169. #ifdef MS_WINDOWS
  170. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  171. const char *tor_fix_source_file(const char *fname);
  172. #else
  173. #define _SHORT_FILE_ (__FILE__)
  174. #define tor_fix_source_file(s) (s)
  175. #endif
  176. /* ===== Time compatibility */
  177. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  178. struct timeval {
  179. time_t tv_sec;
  180. unsigned int tv_usec;
  181. };
  182. #endif
  183. void tor_gettimeofday(struct timeval *timeval);
  184. #ifdef HAVE_LOCALTIME_R
  185. #define tor_localtime_r localtime_r
  186. #else
  187. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  188. #endif
  189. #ifdef HAVE_GMTIME_R
  190. #define tor_gmtime_r gmtime_r
  191. #else
  192. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  193. #endif
  194. /* ===== File compatibility */
  195. int replace_file(const char *from, const char *to);
  196. int touch_file(const char *fname);
  197. #ifdef MS_WINDOWS
  198. #define PATH_SEPARATOR "\\"
  199. #else
  200. #define PATH_SEPARATOR "/"
  201. #endif
  202. /* ===== Net compatibility */
  203. void tor_close_socket(int s);
  204. int tor_open_socket(int domain, int type, int protocol);
  205. int get_n_open_sockets(void);
  206. #ifdef USE_BSOCKETS
  207. #define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags)
  208. #define tor_socket_recv(s, buf, len, flags) brecv(s, buf, len, flags)
  209. #else
  210. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  211. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  212. #endif
  213. #if (SIZEOF_SOCKLEN_T == 0)
  214. typedef int socklen_t;
  215. #endif
  216. /* XXXX020 detect in6_addr correctly on ms_windows; this is a hack. */
  217. #if !defined(HAVE_STRUCT_IN6_ADDR) && !defined(MS_WINDOWS)
  218. struct in6_addr
  219. {
  220. uint8_t s6_addr[16];
  221. };
  222. #endif
  223. struct in_addr;
  224. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  225. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  226. int tor_inet_pton(int af, const char *src, void *dst);
  227. int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
  228. void set_socket_nonblocking(int socket);
  229. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  230. int network_init(void);
  231. /* For stupid historical reasons, windows sockets have an independent
  232. * set of errnos, and an independent way to get them. Also, you can't
  233. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  234. * errnos against expected values, and use tor_socket_errno to find
  235. * the actual errno after a socket operation fails.
  236. */
  237. #if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
  238. /** Return true if e is EAGAIN or the local equivalent. */
  239. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  240. /** Return true if e is EINPROGRESS or the local equivalent. */
  241. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  242. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  243. * a call to connect(). */
  244. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  245. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  246. /** Return true if e is EAGAIN or another error indicating that a call to
  247. * accept() has no pending connections to return. */
  248. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  249. /** Return true if e is EMFILE or another error indicating that a call to
  250. * accept() has failed because we're out of fds or something. */
  251. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  252. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  253. /** Return true if e is EADDRINUSE or the local equivalent. */
  254. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  255. int tor_socket_errno(int sock);
  256. const char *tor_socket_strerror(int e);
  257. #else
  258. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  259. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  260. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  261. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  262. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  263. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  264. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  265. #define tor_socket_errno(sock) (errno)
  266. #define tor_socket_strerror(e) strerror(e)
  267. #endif
  268. /* ===== OS compatibility */
  269. const char *get_uname(void);
  270. uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  271. uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
  272. void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
  273. void set_uint32(char *cp, uint32_t v) ATTR_NONNULL((1));
  274. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  275. int switch_id(const char *user, const char *group);
  276. #ifdef HAVE_PWD_H
  277. char *get_user_homedir(const char *username);
  278. #endif
  279. int spawn_func(void (*func)(void *), void *data);
  280. void spawn_exit(void) ATTR_NORETURN;
  281. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  282. #define USE_WIN32_THREADS
  283. #define TOR_IS_MULTITHREADED 1
  284. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  285. defined(HAVE_PTHREAD_CREATE))
  286. #define USE_PTHREADS
  287. #define TOR_IS_MULTITHREADED 1
  288. #else
  289. #undef TOR_IS_MULTITHREADED
  290. #endif
  291. /* Because we use threads instead of processes on most platforms (Windows,
  292. * Linux, etc), we need locking for them. On platforms with poor thread
  293. * support or broken gethostbyname_r, these functions are no-ops. */
  294. typedef struct tor_mutex_t tor_mutex_t;
  295. #ifdef TOR_IS_MULTITHREADED
  296. tor_mutex_t *tor_mutex_new(void);
  297. void tor_mutex_acquire(tor_mutex_t *m);
  298. void tor_mutex_release(tor_mutex_t *m);
  299. void tor_mutex_free(tor_mutex_t *m);
  300. unsigned long tor_get_thread_id(void);
  301. #else
  302. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  303. #define tor_mutex_acquire(m) do { } while (0)
  304. #define tor_mutex_release(m) do { } while (0)
  305. #define tor_mutex_free(m) do { tor_free(m); } while (0)
  306. #define tor_get_thread_id() (1UL)
  307. #endif
  308. /* Platform-specific helpers. */
  309. #ifdef MS_WINDOWS
  310. char *format_win32_error(DWORD err);
  311. #endif
  312. /*for some reason my compiler doesn't have these version flags defined
  313. a nice homework assignment for someone one day is to define the rest*/
  314. //these are the values as given on MSDN
  315. #ifdef MS_WINDOWS
  316. #ifndef VER_SUITE_EMBEDDEDNT
  317. #define VER_SUITE_EMBEDDEDNT 0x00000040
  318. #endif
  319. #ifndef VER_SUITE_SINGLEUSERTS
  320. #define VER_SUITE_SINGLEUSERTS 0x00000100
  321. #endif
  322. #endif
  323. #endif