compat.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/log/win32err.h"
  51. #include "lib/net/socket.h"
  52. #include "lib/net/ipv4.h"
  53. #include "lib/net/ipv6.h"
  54. #include "lib/net/resolve.h"
  55. #include "lib/fs/files.h"
  56. #include "lib/fs/mmap.h"
  57. #include "lib/fs/userdb.h"
  58. #include <stdio.h>
  59. #include <errno.h>
  60. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  61. size_t nlen) ATTR_NONNULL((1,3));
  62. static const void *tor_memstr(const void *haystack, size_t hlen,
  63. const char *needle) ATTR_NONNULL((1,3));
  64. static inline const void *
  65. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  66. {
  67. return tor_memmem(haystack, hlen, needle, strlen(needle));
  68. }
  69. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  70. #ifdef HAVE_STRTOK_R
  71. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  72. #else
  73. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  74. #endif
  75. /* ===== Time compatibility */
  76. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  77. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  78. #ifndef timeradd
  79. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  80. * the sum of tv1 and tv2. */
  81. #define timeradd(tv1,tv2,tvout) \
  82. do { \
  83. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  84. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  85. if ((tvout)->tv_usec >= 1000000) { \
  86. (tvout)->tv_usec -= 1000000; \
  87. (tvout)->tv_sec++; \
  88. } \
  89. } while (0)
  90. #endif /* !defined(timeradd) */
  91. #ifndef timersub
  92. /** Replacement for timersub on platforms that do not have it: sets tvout to
  93. * tv1 minus tv2. */
  94. #define timersub(tv1,tv2,tvout) \
  95. do { \
  96. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  97. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  98. if ((tvout)->tv_usec < 0) { \
  99. (tvout)->tv_usec += 1000000; \
  100. (tvout)->tv_sec--; \
  101. } \
  102. } while (0)
  103. #endif /* !defined(timersub) */
  104. #ifndef timercmp
  105. /** Replacement for timercmp on platforms that do not have it: returns true
  106. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  107. *
  108. * Note that while this definition should work for all boolean operators, some
  109. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  110. * don't use those.
  111. */
  112. #define timercmp(tv1,tv2,op) \
  113. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  114. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  115. ((tv1)->tv_sec op (tv2)->tv_sec))
  116. #endif /* !defined(timercmp) */
  117. /* ===== File compatibility */
  118. typedef struct tor_lockfile_t tor_lockfile_t;
  119. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  120. int *locked_out);
  121. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  122. /* ===== Net compatibility */
  123. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  124. /** Specified SOCKS5 status codes. */
  125. typedef enum {
  126. SOCKS5_SUCCEEDED = 0x00,
  127. SOCKS5_GENERAL_ERROR = 0x01,
  128. SOCKS5_NOT_ALLOWED = 0x02,
  129. SOCKS5_NET_UNREACHABLE = 0x03,
  130. SOCKS5_HOST_UNREACHABLE = 0x04,
  131. SOCKS5_CONNECTION_REFUSED = 0x05,
  132. SOCKS5_TTL_EXPIRED = 0x06,
  133. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  134. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  135. } socks5_reply_status_t;
  136. /* ===== OS compatibility */
  137. MOCK_DECL(const char *, get_uname, (void));
  138. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  139. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  140. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  141. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  142. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  143. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  144. /* These uint8 variants are defined to make the code more uniform. */
  145. #define get_uint8(cp) (*(const uint8_t*)(cp))
  146. static void set_uint8(void *cp, uint8_t v);
  147. static inline void
  148. set_uint8(void *cp, uint8_t v)
  149. {
  150. *(uint8_t*)cp = v;
  151. }
  152. #if !defined(HAVE_RLIM_T)
  153. typedef unsigned long rlim_t;
  154. #endif
  155. int set_max_file_descriptors(rlim_t limit, int *max);
  156. int tor_disable_debugger_attach(void);
  157. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  158. #define HAVE_LINUX_CAPABILITIES
  159. #endif
  160. int have_capability_support(void);
  161. /** Flag for switch_id; see switch_id() for documentation */
  162. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  163. /** Flag for switch_id; see switch_id() for documentation */
  164. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  165. int switch_id(const char *user, unsigned flags);
  166. char **get_environment(void);
  167. MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
  168. int compute_num_cpus(void);
  169. int tor_mlockall(void);
  170. /** Macros for MIN/MAX. Never use these when the arguments could have
  171. * side-effects.
  172. * {With GCC extensions we could probably define a safer MIN/MAX. But
  173. * depending on that safety would be dangerous, since not every platform
  174. * has it.}
  175. **/
  176. #ifndef MAX
  177. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  178. #endif
  179. #ifndef MIN
  180. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  181. #endif
  182. /*for some reason my compiler doesn't have these version flags defined
  183. a nice homework assignment for someone one day is to define the rest*/
  184. //these are the values as given on MSDN
  185. #ifdef _WIN32
  186. #ifndef VER_SUITE_EMBEDDEDNT
  187. #define VER_SUITE_EMBEDDEDNT 0x00000040
  188. #endif
  189. #ifndef VER_SUITE_SINGLEUSERTS
  190. #define VER_SUITE_SINGLEUSERTS 0x00000100
  191. #endif
  192. #endif /* defined(_WIN32) */
  193. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  194. /* This needs some of the declarations above so we include it here. */
  195. #include "common/compat_threads.h"
  196. #endif /* !defined(TOR_COMPAT_H) */