compat.h 5.8 KB

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