compat.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <stdio.h>
  51. #include <errno.h>
  52. /* ===== Compiler compatibility */
  53. /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
  54. * tor_munmap_file. */
  55. typedef struct tor_mmap_t {
  56. const char *data; /**< Mapping of the file's contents. */
  57. size_t size; /**< Size of the file. */
  58. /* None of the fields below should be accessed from outside compat.c */
  59. #ifdef HAVE_MMAP
  60. size_t mapping_size; /**< Size of the actual mapping. (This is this file
  61. * size, rounded up to the nearest page.) */
  62. #elif defined _WIN32
  63. HANDLE mmap_handle;
  64. #endif /* defined(HAVE_MMAP) || ... */
  65. } tor_mmap_t;
  66. tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
  67. int tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
  68. const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
  69. size_t nlen) ATTR_NONNULL((1,3));
  70. static const void *tor_memstr(const void *haystack, size_t hlen,
  71. const char *needle) ATTR_NONNULL((1,3));
  72. static inline const void *
  73. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  74. {
  75. return tor_memmem(haystack, hlen, needle, strlen(needle));
  76. }
  77. char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
  78. #ifdef HAVE_STRTOK_R
  79. #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
  80. #else
  81. #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
  82. #endif
  83. #ifdef _WIN32
  84. #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
  85. const char *tor_fix_source_file(const char *fname);
  86. #else
  87. #define SHORT_FILE__ (__FILE__)
  88. #define tor_fix_source_file(s) (s)
  89. #endif /* defined(_WIN32) */
  90. /* ===== Time compatibility */
  91. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  92. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  93. #ifndef timeradd
  94. /** Replacement for timeradd on platforms that do not have it: sets tvout to
  95. * the sum of tv1 and tv2. */
  96. #define timeradd(tv1,tv2,tvout) \
  97. do { \
  98. (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
  99. (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
  100. if ((tvout)->tv_usec >= 1000000) { \
  101. (tvout)->tv_usec -= 1000000; \
  102. (tvout)->tv_sec++; \
  103. } \
  104. } while (0)
  105. #endif /* !defined(timeradd) */
  106. #ifndef timersub
  107. /** Replacement for timersub on platforms that do not have it: sets tvout to
  108. * tv1 minus tv2. */
  109. #define timersub(tv1,tv2,tvout) \
  110. do { \
  111. (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
  112. (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
  113. if ((tvout)->tv_usec < 0) { \
  114. (tvout)->tv_usec += 1000000; \
  115. (tvout)->tv_sec--; \
  116. } \
  117. } while (0)
  118. #endif /* !defined(timersub) */
  119. #ifndef timercmp
  120. /** Replacement for timercmp on platforms that do not have it: returns true
  121. * iff the relational operator "op" makes the expression tv1 op tv2 true.
  122. *
  123. * Note that while this definition should work for all boolean operators, some
  124. * platforms' native timercmp definitions do not support >=, <=, or ==. So
  125. * don't use those.
  126. */
  127. #define timercmp(tv1,tv2,op) \
  128. (((tv1)->tv_sec == (tv2)->tv_sec) ? \
  129. ((tv1)->tv_usec op (tv2)->tv_usec) : \
  130. ((tv1)->tv_sec op (tv2)->tv_sec))
  131. #endif /* !defined(timercmp) */
  132. /* ===== File compatibility */
  133. int tor_open_cloexec(const char *path, int flags, unsigned mode);
  134. FILE *tor_fopen_cloexec(const char *path, const char *mode);
  135. int tor_rename(const char *path_old, const char *path_new);
  136. int replace_file(const char *from, const char *to);
  137. int touch_file(const char *fname);
  138. typedef struct tor_lockfile_t tor_lockfile_t;
  139. tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
  140. int *locked_out);
  141. void tor_lockfile_unlock(tor_lockfile_t *lockfile);
  142. int64_t tor_get_avail_disk_space(const char *path);
  143. #ifdef _WIN32
  144. #define PATH_SEPARATOR "\\"
  145. #else
  146. #define PATH_SEPARATOR "/"
  147. #endif
  148. /* ===== Net compatibility */
  149. #if (SIZEOF_SOCKLEN_T == 0)
  150. typedef int socklen_t;
  151. #endif
  152. #ifdef _WIN32
  153. /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
  154. * any inadvertent checks for the socket being <= 0 or > 0 will probably
  155. * still work. */
  156. #define tor_socket_t intptr_t
  157. #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
  158. #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
  159. #define TOR_INVALID_SOCKET INVALID_SOCKET
  160. #else /* !(defined(_WIN32)) */
  161. /** Type used for a network socket. */
  162. #define tor_socket_t int
  163. #define TOR_SOCKET_T_FORMAT "%d"
  164. /** Macro: true iff 's' is a possible value for a valid initialized socket. */
  165. #define SOCKET_OK(s) ((s) >= 0)
  166. /** Error/uninitialized value for a tor_socket_t. */
  167. #define TOR_INVALID_SOCKET (-1)
  168. #endif /* defined(_WIN32) */
  169. int tor_close_socket_simple(tor_socket_t s);
  170. MOCK_DECL(int, tor_close_socket, (tor_socket_t s));
  171. void tor_take_socket_ownership(tor_socket_t s);
  172. tor_socket_t tor_open_socket_with_extensions(
  173. int domain, int type, int protocol,
  174. int cloexec, int nonblock);
  175. MOCK_DECL(tor_socket_t,
  176. tor_open_socket,(int domain, int type, int protocol));
  177. tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol);
  178. tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr,
  179. socklen_t *len);
  180. tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd,
  181. struct sockaddr *addr,
  182. socklen_t *len);
  183. tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd,
  184. struct sockaddr *addr,
  185. socklen_t *len,
  186. int cloexec, int nonblock);
  187. MOCK_DECL(tor_socket_t,
  188. tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address,
  189. socklen_t address_len));
  190. int get_n_open_sockets(void);
  191. MOCK_DECL(int,
  192. tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
  193. socklen_t *address_len));
  194. struct tor_addr_t;
  195. int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock);
  196. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  197. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  198. /** Implementation of struct in6_addr for platforms that do not have it.
  199. * Generally, these platforms are ones without IPv6 support, but we want to
  200. * have a working in6_addr there anyway, so we can use it to parse IPv6
  201. * addresses. */
  202. #if !defined(HAVE_STRUCT_IN6_ADDR)
  203. struct in6_addr
  204. {
  205. union {
  206. uint8_t u6_addr8[16];
  207. uint16_t u6_addr16[8];
  208. uint32_t u6_addr32[4];
  209. } in6_u;
  210. #define s6_addr in6_u.u6_addr8
  211. #define s6_addr16 in6_u.u6_addr16
  212. #define s6_addr32 in6_u.u6_addr32
  213. };
  214. #endif /* !defined(HAVE_STRUCT_IN6_ADDR) */
  215. /** @{ */
  216. /** Many BSD variants seem not to define these. */
  217. #if defined(__APPLE__) || defined(__darwin__) || \
  218. defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  219. #ifndef s6_addr16
  220. #define s6_addr16 __u6_addr.__u6_addr16
  221. #endif
  222. #ifndef s6_addr32
  223. #define s6_addr32 __u6_addr.__u6_addr32
  224. #endif
  225. #endif /* defined(__APPLE__) || defined(__darwin__) || ... */
  226. /** @} */
  227. #ifndef HAVE_SA_FAMILY_T
  228. typedef uint16_t sa_family_t;
  229. #endif
  230. /** @{ */
  231. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  232. * macros get you a pointer to s6_addr32 or local equivalent. */
  233. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  234. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  235. #else
  236. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  237. #endif
  238. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  239. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  240. #else
  241. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  242. #endif
  243. /** @} */
  244. /** Implementation of struct sockaddr_in6 on platforms that do not have
  245. * it. See notes on struct in6_addr. */
  246. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  247. struct sockaddr_in6 {
  248. sa_family_t sin6_family;
  249. uint16_t sin6_port;
  250. // uint32_t sin6_flowinfo;
  251. struct in6_addr sin6_addr;
  252. // uint32_t sin6_scope_id;
  253. };
  254. #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */
  255. MOCK_DECL(int,tor_gethostname,(char *name, size_t namelen));
  256. int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
  257. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  258. int tor_inet_pton(int af, const char *src, void *dst);
  259. MOCK_DECL(int,tor_lookup_hostname,(const char *name, uint32_t *addr));
  260. int set_socket_nonblocking(tor_socket_t socket);
  261. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  262. int network_init(void);
  263. /* For stupid historical reasons, windows sockets have an independent
  264. * set of errnos, and an independent way to get them. Also, you can't
  265. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  266. * errnos against expected values, and use tor_socket_errno to find
  267. * the actual errno after a socket operation fails.
  268. */
  269. #if defined(_WIN32)
  270. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  271. #define SOCK_ERRNO(e) WSA##e
  272. /** Return true if e is EAGAIN or the local equivalent. */
  273. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  274. /** Return true if e is EINPROGRESS or the local equivalent. */
  275. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  276. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  277. * a call to connect(). */
  278. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  279. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  280. /** Return true if e is EAGAIN or another error indicating that a call to
  281. * accept() has no pending connections to return. */
  282. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  283. /** Return true if e is EMFILE or another error indicating that a call to
  284. * accept() has failed because we're out of fds or something. */
  285. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  286. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  287. /** Return true if e is EADDRINUSE or the local equivalent. */
  288. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  289. /** Return true if e is EINTR or the local equivalent */
  290. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  291. int tor_socket_errno(tor_socket_t sock);
  292. const char *tor_socket_strerror(int e);
  293. #else /* !(defined(_WIN32)) */
  294. #define SOCK_ERRNO(e) e
  295. #if EAGAIN == EWOULDBLOCK
  296. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  297. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  298. #else
  299. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  300. #endif /* EAGAIN == EWOULDBLOCK */
  301. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  302. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  303. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  304. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  305. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  306. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  307. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  308. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  309. #define tor_socket_errno(sock) (errno)
  310. #define tor_socket_strerror(e) strerror(e)
  311. #endif /* defined(_WIN32) */
  312. /** Specified SOCKS5 status codes. */
  313. typedef enum {
  314. SOCKS5_SUCCEEDED = 0x00,
  315. SOCKS5_GENERAL_ERROR = 0x01,
  316. SOCKS5_NOT_ALLOWED = 0x02,
  317. SOCKS5_NET_UNREACHABLE = 0x03,
  318. SOCKS5_HOST_UNREACHABLE = 0x04,
  319. SOCKS5_CONNECTION_REFUSED = 0x05,
  320. SOCKS5_TTL_EXPIRED = 0x06,
  321. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  322. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  323. } socks5_reply_status_t;
  324. /* ===== OS compatibility */
  325. MOCK_DECL(const char *, get_uname, (void));
  326. uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
  327. uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
  328. uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
  329. void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
  330. void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
  331. void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
  332. /* These uint8 variants are defined to make the code more uniform. */
  333. #define get_uint8(cp) (*(const uint8_t*)(cp))
  334. static void set_uint8(void *cp, uint8_t v);
  335. static inline void
  336. set_uint8(void *cp, uint8_t v)
  337. {
  338. *(uint8_t*)cp = v;
  339. }
  340. #if !defined(HAVE_RLIM_T)
  341. typedef unsigned long rlim_t;
  342. #endif
  343. int get_max_sockets(void);
  344. int set_max_file_descriptors(rlim_t limit, int *max);
  345. int tor_disable_debugger_attach(void);
  346. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  347. #define HAVE_LINUX_CAPABILITIES
  348. #endif
  349. int have_capability_support(void);
  350. /** Flag for switch_id; see switch_id() for documentation */
  351. #define SWITCH_ID_KEEP_BINDLOW (1<<0)
  352. /** Flag for switch_id; see switch_id() for documentation */
  353. #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
  354. int switch_id(const char *user, unsigned flags);
  355. #ifdef HAVE_PWD_H
  356. char *get_user_homedir(const char *username);
  357. #endif
  358. #ifndef _WIN32
  359. const struct passwd *tor_getpwnam(const char *username);
  360. const struct passwd *tor_getpwuid(uid_t uid);
  361. #endif
  362. int get_parent_directory(char *fname);
  363. char *make_path_absolute(char *fname);
  364. char **get_environment(void);
  365. MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
  366. int compute_num_cpus(void);
  367. int tor_mlockall(void);
  368. /** Macros for MIN/MAX. Never use these when the arguments could have
  369. * side-effects.
  370. * {With GCC extensions we could probably define a safer MIN/MAX. But
  371. * depending on that safety would be dangerous, since not every platform
  372. * has it.}
  373. **/
  374. #ifndef MAX
  375. #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
  376. #endif
  377. #ifndef MIN
  378. #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
  379. #endif
  380. /* Platform-specific helpers. */
  381. #ifdef _WIN32
  382. char *format_win32_error(DWORD err);
  383. #endif
  384. /*for some reason my compiler doesn't have these version flags defined
  385. a nice homework assignment for someone one day is to define the rest*/
  386. //these are the values as given on MSDN
  387. #ifdef _WIN32
  388. #ifndef VER_SUITE_EMBEDDEDNT
  389. #define VER_SUITE_EMBEDDEDNT 0x00000040
  390. #endif
  391. #ifndef VER_SUITE_SINGLEUSERTS
  392. #define VER_SUITE_SINGLEUSERTS 0x00000100
  393. #endif
  394. #endif /* defined(_WIN32) */
  395. #ifdef COMPAT_PRIVATE
  396. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  397. #define NEED_ERSATZ_SOCKETPAIR
  398. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  399. tor_socket_t fd[2]);
  400. #endif
  401. #endif /* defined(COMPAT_PRIVATE) */
  402. ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
  403. /* This needs some of the declarations above so we include it here. */
  404. #include "common/compat_threads.h"
  405. #endif /* !defined(TOR_COMPAT_H) */