compat.h 6.5 KB

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