compat.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /* ===== String compatibility */
  71. #ifdef MS_WINDOWS
  72. /* Windows names string functions differently from most other platforms. */
  73. #define strncasecmp strnicmp
  74. #define strcasecmp stricmp
  75. #endif
  76. #ifndef HAVE_STRLCAT
  77. size_t strlcat(char *dst, const char *src, size_t siz);
  78. #endif
  79. #ifndef HAVE_STRLCPY
  80. size_t strlcpy(char *dst, const char *src, size_t siz);
  81. #endif
  82. #ifdef MS_WINDOWS
  83. #define U64_PRINTF_ARG(a) (a)
  84. #define U64_SCANF_ARG(a) (a)
  85. #define U64_FORMAT "%I64u"
  86. #define U64_LITERAL(n) (n ## ui64)
  87. #else
  88. #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
  89. #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
  90. #define U64_FORMAT "%llu"
  91. #define U64_LITERAL(n) (n ## llu)
  92. #endif
  93. int tor_snprintf(char *str, size_t size, const char *format, ...)
  94. CHECK_PRINTF(3,4);
  95. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);
  96. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  97. size_t nlen);
  98. #define TOR_ISALPHA(c) isalpha((int)(unsigned char)(c))
  99. #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
  100. #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
  101. #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
  102. #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
  103. #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
  104. #ifdef MS_WINDOWS
  105. #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
  106. const char *tor_fix_source_file(const char *fname);
  107. #else
  108. #define _SHORT_FILE_ (__FILE__)
  109. #define tor_fix_source_file(s) (s)
  110. #endif
  111. /* ===== Time compatibility */
  112. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  113. struct timeval {
  114. time_t tv_sec;
  115. unsigned int tv_usec;
  116. };
  117. #endif
  118. void tor_gettimeofday(struct timeval *timeval);
  119. #ifdef HAVE_LOCALTIME_R
  120. #define tor_localtime_r localtime_r
  121. #else
  122. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  123. #endif
  124. #ifdef HAVE_GMTIME_R
  125. #define tor_gmtime_r gmtime_r
  126. #else
  127. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  128. #endif
  129. /* ===== File compatibility */
  130. int replace_file(const char *from, const char *to);
  131. int touch_file(const char *fname);
  132. #ifdef MS_WINDOWS
  133. #define PATH_SEPARATOR "\\"
  134. #else
  135. #define PATH_SEPARATOR "/"
  136. #endif
  137. /* ===== Net compatibility */
  138. #ifdef MS_WINDOWS
  139. /** On windows, you have to call close() on fds returned by open(),
  140. * and closesocket() on fds returned by socket(). On Unix, everything
  141. * gets close()'d. We abstract this difference by always using
  142. * tor_close_socket to close sockets, and always using close() on
  143. * files.
  144. */
  145. #define tor_close_socket(s) closesocket(s)
  146. #else
  147. #define tor_close_socket(s) close(s)
  148. #endif
  149. #if (SIZEOF_SOCKLEN_T == 0)
  150. typedef int socklen_t;
  151. #endif
  152. /* Now that we use libevent, all real sockets are safe for polling ... or
  153. * if they aren't, libevent will help us. */
  154. #define SOCKET_IS_POLLABLE(fd) ((fd)>=0)
  155. struct in_addr;
  156. int tor_inet_aton(const char *cp, struct in_addr *addr);
  157. int tor_lookup_hostname(const char *name, uint32_t *addr);
  158. void set_socket_nonblocking(int socket);
  159. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  160. int network_init(void);
  161. /* For stupid historical reasons, windows sockets have an independent
  162. * set of errnos, and an independent way to get them. Also, you can't
  163. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  164. * errnos against expected values, and use tor_socket_errno to find
  165. * the actual errno after a socket operation fails.
  166. */
  167. #ifdef MS_WINDOWS
  168. /** Return true if e is EAGAIN or the local equivalent. */
  169. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  170. /** Return true if e is EINPROGRESS or the local equivalent. */
  171. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  172. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  173. * a call to connect(). */
  174. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  175. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  176. /** Return true if e is EAGAIN or another error indicating that a call to
  177. * accept() has no pending connections to return. */
  178. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  179. /** Return true if e is EMFILE or another error indicating that a call to
  180. * accept() has failed because we're out of fds or something. */
  181. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  182. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  183. /** Return true if e is EADDRINUSE or the local equivalent. */
  184. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  185. int tor_socket_errno(int sock);
  186. const char *tor_socket_strerror(int e);
  187. #else
  188. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  189. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  190. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  191. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  192. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  193. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  194. #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
  195. #define tor_socket_errno(sock) (errno)
  196. #define tor_socket_strerror(e) strerror(e)
  197. #endif
  198. /* ===== OS compatibility */
  199. const char *get_uname(void);
  200. uint16_t get_uint16(const char *cp);
  201. uint32_t get_uint32(const char *cp);
  202. void set_uint16(char *cp, uint16_t v);
  203. void set_uint32(char *cp, uint32_t v);
  204. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  205. int switch_id(char *user, char *group);
  206. #ifdef HAVE_PWD_H
  207. char *get_user_homedir(const char *username);
  208. #endif
  209. int spawn_func(int (*func)(void *), void *data);
  210. void spawn_exit(void);
  211. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  212. #define USE_WIN32_THREADS
  213. #define TOR_IS_MULTITHREADED 1
  214. #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
  215. defined(HAVE_PTHREAD_CREATE))
  216. #define USE_PTHREADS
  217. #define TOR_IS_MULTITHREADED 1
  218. #else
  219. #undef TOR_IS_MULTITHREADED
  220. #endif
  221. /* Because we use threads instead of processes on Windows, we need locking on
  222. * Windows. On Unixy platforms, these functions are no-ops. */
  223. typedef struct tor_mutex_t tor_mutex_t;
  224. #ifdef TOR_IS_MULTITHREADED
  225. tor_mutex_t *tor_mutex_new(void);
  226. void tor_mutex_acquire(tor_mutex_t *m);
  227. void tor_mutex_release(tor_mutex_t *m);
  228. void tor_mutex_free(tor_mutex_t *m);
  229. unsigned long tor_get_thread_id(void);
  230. #else
  231. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  232. #define tor_mutex_acquire(m) do { } while (0)
  233. #define tor_mutex_release(m) do { } while (0)
  234. #define tor_mutex_free(m) do { tor_free(m); } while (0)
  235. #define tor_get_thread_id() (1UL)
  236. #endif
  237. #endif