compat.h 6.3 KB

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