compat.h 17 KB

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