compat.h 16 KB

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