compat.h 8.0 KB

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