compat.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_COMPAT_H
  6. #define TOR_COMPAT_H
  7. #include "orconfig.h"
  8. #ifdef _WIN32
  9. #include <winsock2.h>
  10. #include <ws2tcpip.h>
  11. #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
  12. #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747b
  13. #endif
  14. #endif
  15. #include "lib/cc/torint.h"
  16. #include "lib/testsupport/testsupport.h"
  17. #ifdef HAVE_SYS_PARAM_H
  18. #include <sys/param.h>
  19. #endif
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. #ifdef HAVE_SYS_TIME_H
  24. #include <sys/time.h>
  25. #endif
  26. #ifdef HAVE_TIME_H
  27. #include <time.h>
  28. #endif
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #endif
  32. #include <stdarg.h>
  33. #ifdef HAVE_SYS_RESOURCE_H
  34. #include <sys/resource.h>
  35. #endif
  36. #ifdef HAVE_SYS_SOCKET_H
  37. #include <sys/socket.h>
  38. #endif
  39. #ifdef HAVE_NETINET_IN_H
  40. #include <netinet/in.h>
  41. #endif
  42. #ifdef HAVE_NETINET6_IN6_H
  43. #include <netinet6/in6.h>
  44. #endif
  45. #include "lib/cc/compat_compiler.h"
  46. #include "common/compat_time.h"
  47. #include "lib/string/compat_ctype.h"
  48. #include "lib/string/compat_string.h"
  49. #include "lib/string/printf.h"
  50. #include "lib/net/socket.h"
  51. #include "lib/net/ipv4.h"
  52. #include "lib/net/ipv6.h"
  53. #include "lib/net/resolve.h"
  54. #include <stdio.h>
  55. #include <errno.h>
  56. /* ===== Compiler compatibility */
  57. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  58. * tor_munmap_file. */
  59. typedef struct tor_mmap_t {
  60. const char *data; /**< Mapping of the file's contents. */
  61. size_t size; /**< Size of the file. */
  62. /* None of the fields below should be accessed from outside compat.c */
  63. #ifdef HAVE_MMAP
  64. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  65. * size, rounded up to the nearest page.) */
  66. #elif defined _WIN32
  67. HANDLE mmap_handle;
  68. #endif /* defined(HAVE_MMAP) || ... */
  69. } tor_mmap_t;
  70. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  71. int tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  72. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  73. size_t nlen) ATTR_NONNULL((1,3));
  74. static const void *tor_memstr(const void *haystack, size_t hlen,
  75. const char *needle) ATTR_NONNULL((1,3));
  76. static inline const void *
  77. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  78. {
  79. return tor_memmem(haystack, hlen, needle, strlen(needle));
  80. }
  81. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  82. #ifdef HAVE_STRTOK_R
  83. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  84. #else
  85. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  86. #endif
  87. /* ===== Time compatibility */
  88. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  89. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  90. #ifndef timeradd
  91. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  92. * the sum of tv1 and tv2. */
  93. #define timeradd(tv1,tv2,tvout) \
  94. do { \
  95. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  96. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  97. if ((tvout)->tv_usec >= 1000000) { \
  98. (tvout)->tv_usec -= 1000000; \
  99. (tvout)->tv_sec++; \
  100. } \
  101. } while (0)
  102. #endif /* !defined(timeradd) */
  103. #ifndef timersub
  104. /** Replacement for timersub on platforms that do not have it: sets tvout to
  105. * tv1 minus tv2. */
  106. #define timersub(tv1,tv2,tvout) \
  107. do { \
  108. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  109. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  110. if ((tvout)->tv_usec < 0) { \
  111. (tvout)->tv_usec += 1000000; \
  112. (tvout)->tv_sec--; \
  113. } \
  114. } while (0)
  115. #endif /* !defined(timersub) */
  116. #ifndef timercmp
  117. /** Replacement for timercmp on platforms that do not have it: returns true
  118. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  119. *
  120. * Note that while this definition should work for all boolean operators, some
  121. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  122. * don't use those.
  123. */
  124. #define timercmp(tv1,tv2,op) \
  125. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  126. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  127. ((tv1)->tv_sec op (tv2)->tv_sec))
  128. #endif /* !defined(timercmp) */
  129. /* ===== File compatibility */
  130. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  131. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  132. int tor_rename(const char *path_old, const char *path_new);
  133. int replace_file(const char *from, const char *to);
  134. int touch_file(const char *fname);
  135. typedef struct tor_lockfile_t tor_lockfile_t;
  136. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  137. int *locked_out);
  138. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  139. int64_t tor_get_avail_disk_space(const char *path);
  140. #ifdef _WIN32
  141. #define PATH_SEPARATOR "\\"
  142. #else
  143. #define PATH_SEPARATOR "/"
  144. #endif
  145. /* ===== Net compatibility */
  146. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  147. /** Specified SOCKS5 status codes. */
  148. typedef enum {
  149. SOCKS5_SUCCEEDED = 0x00,
  150. SOCKS5_GENERAL_ERROR = 0x01,
  151. SOCKS5_NOT_ALLOWED = 0x02,
  152. SOCKS5_NET_UNREACHABLE = 0x03,
  153. SOCKS5_HOST_UNREACHABLE = 0x04,
  154. SOCKS5_CONNECTION_REFUSED = 0x05,
  155. SOCKS5_TTL_EXPIRED = 0x06,
  156. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  157. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  158. } socks5_reply_status_t;
  159. /* ===== OS compatibility */
  160. MOCK_DECL(const char *, get_uname, (void));
  161. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  162. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  163. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  164. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  165. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  166. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  167. /* These uint8 variants are defined to make the code more uniform. */
  168. #define get_uint8(cp) (*(const uint8_t*)(cp))
  169. static void set_uint8(void *cp, uint8_t v);
  170. static inline void
  171. set_uint8(void *cp, uint8_t v)
  172. {
  173. *(uint8_t*)cp = v;
  174. }
  175. #if !defined(HAVE_RLIM_T)
  176. typedef unsigned long rlim_t;
  177. #endif
  178. int set_max_file_descriptors(rlim_t limit, int *max);
  179. int tor_disable_debugger_attach(void);
  180. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  181. #define HAVE_LINUX_CAPABILITIES
  182. #endif
  183. int have_capability_support(void);
  184. /** Flag for switch_id; see switch_id() for documentation */
  185. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  186. /** Flag for switch_id; see switch_id() for documentation */
  187. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  188. int switch_id(const char *user, unsigned flags);
  189. #ifdef HAVE_PWD_H
  190. char *get_user_homedir(const char *username);
  191. #endif
  192. #ifndef _WIN32
  193. const struct passwd *tor_getpwnam(const char *username);
  194. const struct passwd *tor_getpwuid(uid_t uid);
  195. #endif
  196. int get_parent_directory(char *fname);
  197. char *make_path_absolute(char *fname);
  198. char **get_environment(void);
  199. MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
  200. int compute_num_cpus(void);
  201. int tor_mlockall(void);
  202. /** Macros for MIN/MAX. Never use these when the arguments could have
  203. * side-effects.
  204. * {With GCC extensions we could probably define a safer MIN/MAX. But
  205. * depending on that safety would be dangerous, since not every platform
  206. * has it.}
  207. **/
  208. #ifndef MAX
  209. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  210. #endif
  211. #ifndef MIN
  212. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  213. #endif
  214. /* Platform-specific helpers. */
  215. #ifdef _WIN32
  216. char *format_win32_error(DWORD err);
  217. #endif
  218. /*for some reason my compiler doesn't have these version flags defined
  219. a nice homework assignment for someone one day is to define the rest*/
  220. //these are the values as given on MSDN
  221. #ifdef _WIN32
  222. #ifndef VER_SUITE_EMBEDDEDNT
  223. #define VER_SUITE_EMBEDDEDNT 0x00000040
  224. #endif
  225. #ifndef VER_SUITE_SINGLEUSERTS
  226. #define VER_SUITE_SINGLEUSERTS 0x00000100
  227. #endif
  228. #endif /* defined(_WIN32) */
  229. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  230. /* This needs some of the declarations above so we include it here. */
  231. #include "common/compat_threads.h"
  232. #endif /* !defined(TOR_COMPAT_H) */