compat.h 10 KB

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