1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
15 #include "testsupport.h"
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
22 #ifdef HAVE_SYS_TIME_H
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
41 #ifdef HAVE_NETINET6_IN6_H
42 #include <netinet6/in6.h>
45 #include "compat_time.h"
47 #if defined(__has_feature)
48 # if __has_feature(address_sanitizer)
49 /* Some of the fancy glibc strcmp() macros include references to memory that
50 * clang rejects because it is off the end of a less-than-3. Clang hates this,
51 * even though those references never actually happen. */
59 #ifndef NULL_REP_IS_ZERO_BYTES
60 #error "It seems your platform does not represent NULL as zero. We can't cope."
63 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
64 #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
67 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
68 #error "It seems that you encode characters in something other than ASCII."
71 /* ===== Compiler compatibility */
73 /* GCC can check printf and scanf types on arbitrary functions. */
75 #define CHECK_PRINTF(formatIdx, firstArg) \
76 __attribute__ ((format(printf, formatIdx, firstArg)))
78 #define CHECK_PRINTF(formatIdx, firstArg)
81 #define CHECK_SCANF(formatIdx, firstArg) \
82 __attribute__ ((format(scanf, formatIdx, firstArg)))
84 #define CHECK_SCANF(formatIdx, firstArg)
87 /* What GCC do we have? */
89 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
94 /* Temporarily enable and disable warnings. */
96 # define PRAGMA_STRINGIFY_(s) #s
97 # define PRAGMA_JOIN_STRINGIFY_(a,b) PRAGMA_STRINGIFY_(a ## b)
98 /* Support for macro-generated pragmas (c99) */
99 # define PRAGMA_(x) _Pragma (#x)
101 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
103 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
105 # if defined(__clang__) || GCC_VERSION >= 406
106 /* we have push/pop support */
107 # define DISABLE_GCC_WARNING(warningopt) \
108 PRAGMA_DIAGNOSTIC_(push) \
109 PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
110 # define ENABLE_GCC_WARNING(warningopt) \
111 PRAGMA_DIAGNOSTIC_(pop)
113 /* older version of gcc: no push/pop support. */
114 # define DISABLE_GCC_WARNING(warningopt) \
115 PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
116 # define ENABLE_GCC_WARNING(warningopt) \
117 PRAGMA_DIAGNOSTIC_(warning PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
119 #else /* ifdef __GNUC__ */
121 # define DISABLE_GCC_WARNING(warning)
122 # define ENABLE_GCC_WARNING(warning)
125 /* inline is __inline on windows. */
127 #define inline __inline
130 /* Try to get a reasonable __func__ substitute in place. */
131 #if defined(_MSC_VER)
133 #define __func__ __FUNCTION__
136 /* For platforms where autoconf works, make sure __func__ is defined
138 #ifndef HAVE_MACRO__func__
139 #ifdef HAVE_MACRO__FUNCTION__
140 #define __func__ __FUNCTION__
141 #elif HAVE_MACRO__FUNC__
142 #define __func__ __FUNC__
144 #define __func__ "???"
146 #endif /* ifndef MAVE_MACRO__func__ */
147 #endif /* if not windows */
149 #define U64_TO_DBL(x) ((double) (x))
150 #define DBL_TO_U64(x) ((uint64_t) (x))
152 #ifdef ENUM_VALS_ARE_SIGNED
153 #define ENUM_BF(t) unsigned
155 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
156 * just use the enumerated type (so the compiler can help us and notice
157 * problems), but if enumerated types are unsigned, we must use unsigned,
158 * so that the loss of precision doesn't make large values negative. */
162 /* GCC has several useful attributes. */
163 #if defined(__GNUC__) && __GNUC__ >= 3
164 #define ATTR_NORETURN __attribute__((noreturn))
165 #define ATTR_CONST __attribute__((const))
166 #define ATTR_MALLOC __attribute__((malloc))
167 #define ATTR_NORETURN __attribute__((noreturn))
168 #define ATTR_WUR __attribute__((warn_unused_result))
169 /* Alas, nonnull is not at present a good idea for us. We'd like to get
170 * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
171 * spottily), but we don't want to tell the compiler to make optimizations
172 * with the assumption that the argument can't be NULL (since this would make
173 * many of our checks go away, and make our code less robust against
174 * programming errors). Unfortunately, nonnull currently does both of these
175 * things, and there's no good way to split them up.
177 * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
178 #define ATTR_NONNULL(x)
179 #define ATTR_UNUSED __attribute__ ((unused))
181 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
182 * of <b>exp</b> will probably be true.
184 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
185 * except that it tells the compiler that the branch will be taken most of the
186 * time. This can generate slightly better code with some CPUs.
188 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
189 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
190 * of <b>exp</b> will probably be false.
192 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
193 * except that it tells the compiler that the branch will usually not be
194 * taken. This can generate slightly better code with some CPUs.
196 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
198 #define ATTR_NORETURN
201 #define ATTR_NORETURN
202 #define ATTR_NONNULL(x)
205 #define PREDICT_LIKELY(exp) (exp)
206 #define PREDICT_UNLIKELY(exp) (exp)
209 /** Expands to a syntactically valid empty statement. */
210 #define STMT_NIL (void)0
212 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
214 #define STMT_VOID(a) while (0) { (void)(a); }
217 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
218 * the macro can be used as if it were a single C statement. */
219 #define STMT_BEGIN (void) ({
221 #elif defined(sun) || defined(__sun__)
222 #define STMT_BEGIN if (1) {
223 #define STMT_END } else STMT_NIL
225 #define STMT_BEGIN do {
226 #define STMT_END } while (0)
229 /* Some tools (like coccinelle) don't like to see operators as macro
238 /* ===== String compatibility */
240 /* Windows names string functions differently from most other platforms. */
241 #define strncasecmp _strnicmp
242 #define strcasecmp _stricmp
245 #if defined __APPLE__
246 /* On OSX 10.9 and later, the overlap-checking code for strlcat would
247 * appear to have a severe bug that can sometimes cause aborts in Tor.
248 * Instead, use the non-checking variants. This is sad.
250 * See https://trac.torproject.org/projects/tor/ticket/15205
257 size_t strlcat(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
260 size_t strlcpy(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
264 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
266 #define U64_PRINTF_ARG(a) (a)
267 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
269 #define U64_SCANF_ARG(a) (a)
270 /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
271 #define U64_LITERAL(n) (n ## ui64)
272 #define I64_PRINTF_ARG(a) (a)
273 #define I64_SCANF_ARG(a) (a)
274 #define I64_LITERAL(n) (n ## i64)
276 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
277 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
278 #define U64_LITERAL(n) (n ## llu)
279 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
280 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
281 #define I64_LITERAL(n) (n ## ll)
284 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
285 /** The formatting string used to put a uint64_t value in a printf() or
286 * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
287 #define U64_FORMAT "%I64u"
288 #define I64_FORMAT "%I64d"
290 #define U64_FORMAT "%llu"
291 #define I64_FORMAT "%lld"
294 #if (SIZEOF_INTPTR_T == SIZEOF_INT)
295 #define INTPTR_T_FORMAT "%d"
296 #define INTPTR_PRINTF_ARG(x) ((int)(x))
297 #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
298 #define INTPTR_T_FORMAT "%ld"
299 #define INTPTR_PRINTF_ARG(x) ((long)(x))
300 #elif (SIZEOF_INTPTR_T == 8)
301 #define INTPTR_T_FORMAT I64_FORMAT
302 #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
304 #error Unknown: SIZEOF_INTPTR_T
307 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
308 * tor_munmap_file. */
309 typedef struct tor_mmap_t
{
310 const char *data
; /**< Mapping of the file's contents. */
311 size_t size
; /**< Size of the file. */
313 /* None of the fields below should be accessed from outside compat.c */
314 #ifdef HAVE_SYS_MMAN_H
315 size_t mapping_size
; /**< Size of the actual mapping. (This is this file
316 * size, rounded up to the nearest page.) */
323 tor_mmap_t
*tor_mmap_file(const char *filename
) ATTR_NONNULL((1));
324 int tor_munmap_file(tor_mmap_t
*handle
) ATTR_NONNULL((1));
326 int tor_snprintf(char *str
, size_t size
, const char *format
, ...)
327 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
328 int tor_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
329 CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
331 int tor_asprintf(char **strp
, const char *fmt
, ...)
333 int tor_vasprintf(char **strp
, const char *fmt
, va_list args
)
336 const void *tor_memmem(const void *haystack
, size_t hlen
, const void *needle
,
337 size_t nlen
) ATTR_NONNULL((1,3));
338 static const void *tor_memstr(const void *haystack
, size_t hlen
,
339 const char *needle
) ATTR_NONNULL((1,3));
340 static inline const void *
341 tor_memstr(const void *haystack
, size_t hlen
, const char *needle
)
343 return tor_memmem(haystack
, hlen
, needle
, strlen(needle
));
346 /* Much of the time when we're checking ctypes, we're doing spec compliance,
347 * which all assumes we're doing ASCII. */
348 #define DECLARE_CTYPE_FN(name) \
349 static int TOR_##name(char c); \
350 extern const uint32_t TOR_##name##_TABLE[]; \
351 static inline int TOR_##name(char c) { \
353 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
355 DECLARE_CTYPE_FN(ISALPHA
)
356 DECLARE_CTYPE_FN(ISALNUM
)
357 DECLARE_CTYPE_FN(ISSPACE
)
358 DECLARE_CTYPE_FN(ISDIGIT
)
359 DECLARE_CTYPE_FN(ISXDIGIT
)
360 DECLARE_CTYPE_FN(ISPRINT
)
361 DECLARE_CTYPE_FN(ISLOWER
)
362 DECLARE_CTYPE_FN(ISUPPER
)
363 extern const uint8_t TOR_TOUPPER_TABLE
[];
364 extern const uint8_t TOR_TOLOWER_TABLE
[];
365 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
366 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
368 char *tor_strtok_r_impl(char *str
, const char *sep
, char **lasts
);
370 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
372 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
376 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
377 const char *tor_fix_source_file(const char *fname
);
379 #define SHORT_FILE__ (__FILE__)
380 #define tor_fix_source_file(s) (s)
383 /* ===== Time compatibility */
385 struct tm
*tor_localtime_r(const time_t *timep
, struct tm
*result
);
386 struct tm
*tor_gmtime_r(const time_t *timep
, struct tm
*result
);
389 /** Replacement for timeradd on platforms that do not have it: sets tvout to
390 * the sum of tv1 and tv2. */
391 #define timeradd(tv1,tv2,tvout) \
393 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
394 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
395 if ((tvout)->tv_usec >= 1000000) { \
396 (tvout)->tv_usec -= 1000000; \
403 /** Replacement for timersub on platforms that do not have it: sets tvout to
405 #define timersub(tv1,tv2,tvout) \
407 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
408 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
409 if ((tvout)->tv_usec < 0) { \
410 (tvout)->tv_usec += 1000000; \
417 /** Replacement for timersub on platforms that do not have it: returns true
418 * iff the relational operator "op" makes the expression tv1 op tv2 true.
420 * Note that while this definition should work for all boolean opeators, some
421 * platforms' native timercmp definitions do not support >=, <=, or ==. So
424 #define timercmp(tv1,tv2,op) \
425 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
426 ((tv1)->tv_usec op (tv2)->tv_usec) : \
427 ((tv1)->tv_sec op (tv2)->tv_sec))
430 /* ===== File compatibility */
431 int tor_open_cloexec(const char *path
, int flags
, unsigned mode
);
432 FILE *tor_fopen_cloexec(const char *path
, const char *mode
);
433 int tor_rename(const char *path_old
, const char *path_new
);
435 int replace_file(const char *from
, const char *to
);
436 int touch_file(const char *fname
);
438 typedef struct tor_lockfile_t tor_lockfile_t
;
439 tor_lockfile_t
*tor_lockfile_lock(const char *filename
, int blocking
,
441 void tor_lockfile_unlock(tor_lockfile_t
*lockfile
);
443 off_t
tor_fd_getpos(int fd
);
444 int tor_fd_setpos(int fd
, off_t pos
);
445 int tor_fd_seekend(int fd
);
446 int tor_ftruncate(int fd
);
448 int64_t tor_get_avail_disk_space(const char *path
);
451 #define PATH_SEPARATOR "\\"
453 #define PATH_SEPARATOR "/"
456 /* ===== Net compatibility */
458 #if (SIZEOF_SOCKLEN_T == 0)
459 typedef int socklen_t
;
463 /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
464 * any inadvertent checks for the socket being <= 0 or > 0 will probably
466 #define tor_socket_t intptr_t
467 #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
468 #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
469 #define TOR_INVALID_SOCKET INVALID_SOCKET
471 /** Type used for a network socket. */
472 #define tor_socket_t int
473 #define TOR_SOCKET_T_FORMAT "%d"
474 /** Macro: true iff 's' is a possible value for a valid initialized socket. */
475 #define SOCKET_OK(s) ((s) >= 0)
476 /** Error/uninitialized value for a tor_socket_t. */
477 #define TOR_INVALID_SOCKET (-1)
480 int tor_close_socket_simple(tor_socket_t s
);
481 MOCK_DECL(int, tor_close_socket
, (tor_socket_t s
));
482 tor_socket_t
tor_open_socket_with_extensions(
483 int domain
, int type
, int protocol
,
484 int cloexec
, int nonblock
);
485 MOCK_DECL(tor_socket_t
,
486 tor_open_socket
,(int domain
, int type
, int protocol
));
487 tor_socket_t
tor_open_socket_nonblocking(int domain
, int type
, int protocol
);
488 tor_socket_t
tor_accept_socket(tor_socket_t sockfd
, struct sockaddr
*addr
,
490 tor_socket_t
tor_accept_socket_nonblocking(tor_socket_t sockfd
,
491 struct sockaddr
*addr
,
493 tor_socket_t
tor_accept_socket_with_extensions(tor_socket_t sockfd
,
494 struct sockaddr
*addr
,
496 int cloexec
, int nonblock
);
497 MOCK_DECL(tor_socket_t
,
498 tor_connect_socket
,(tor_socket_t socket
,const struct sockaddr
*address
,
499 socklen_t address_len
));
500 int get_n_open_sockets(void);
503 tor_getsockname
,(tor_socket_t socket
, struct sockaddr
*address
,
504 socklen_t
*address_len
));
506 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
507 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
509 /** Implementation of struct in6_addr for platforms that do not have it.
510 * Generally, these platforms are ones without IPv6 support, but we want to
511 * have a working in6_addr there anyway, so we can use it to parse IPv6
513 #if !defined(HAVE_STRUCT_IN6_ADDR)
517 uint8_t u6_addr8
[16];
518 uint16_t u6_addr16
[8];
519 uint32_t u6_addr32
[4];
521 #define s6_addr in6_u.u6_addr8
522 #define s6_addr16 in6_u.u6_addr16
523 #define s6_addr32 in6_u.u6_addr32
528 /** Many BSD variants seem not to define these. */
529 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
530 || defined(__NetBSD__) || defined(__OpenBSD__)
532 #define s6_addr16 __u6_addr.__u6_addr16
535 #define s6_addr32 __u6_addr.__u6_addr32
540 #ifndef HAVE_SA_FAMILY_T
541 typedef uint16_t sa_family_t
;
545 /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
546 * macros get you a pointer to s6_addr32 or local equivalent. */
547 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
548 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
550 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
552 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
553 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
555 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
559 /** Implementation of struct sockaddr_in6 on platforms that do not have
560 * it. See notes on struct in6_addr. */
561 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
562 struct sockaddr_in6
{
563 sa_family_t sin6_family
;
565 // uint32_t sin6_flowinfo;
566 struct in6_addr sin6_addr
;
567 // uint32_t sin6_scope_id;
571 MOCK_DECL(int,tor_gethostname
,(char *name
, size_t namelen
));
572 int tor_inet_aton(const char *cp
, struct in_addr
*addr
) ATTR_NONNULL((1,2));
573 const char *tor_inet_ntop(int af
, const void *src
, char *dst
, size_t len
);
574 int tor_inet_pton(int af
, const char *src
, void *dst
);
575 MOCK_DECL(int,tor_lookup_hostname
,(const char *name
, uint32_t *addr
));
576 int set_socket_nonblocking(tor_socket_t socket
);
577 int tor_socketpair(int family
, int type
, int protocol
, tor_socket_t fd
[2]);
578 int network_init(void);
580 /* For stupid historical reasons, windows sockets have an independent
581 * set of errnos, and an independent way to get them. Also, you can't
582 * always believe WSAEWOULDBLOCK. Use the macros below to compare
583 * errnos against expected values, and use tor_socket_errno to find
584 * the actual errno after a socket operation fails.
587 /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
588 #define SOCK_ERRNO(e) WSA##e
589 /** Return true if e is EAGAIN or the local equivalent. */
590 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
591 /** Return true if e is EINPROGRESS or the local equivalent. */
592 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
593 /** Return true if e is EINPROGRESS or the local equivalent as returned by
594 * a call to connect(). */
595 #define ERRNO_IS_CONN_EINPROGRESS(e) \
596 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
597 /** Return true if e is EAGAIN or another error indicating that a call to
598 * accept() has no pending connections to return. */
599 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
600 /** Return true if e is EMFILE or another error indicating that a call to
601 * accept() has failed because we're out of fds or something. */
602 #define ERRNO_IS_RESOURCE_LIMIT(e) \
603 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
604 /** Return true if e is EADDRINUSE or the local equivalent. */
605 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
606 /** Return true if e is EINTR or the local equivalent */
607 #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
608 int tor_socket_errno(tor_socket_t sock
);
609 const char *tor_socket_strerror(int e
);
611 #define SOCK_ERRNO(e) e
612 #if EAGAIN == EWOULDBLOCK
613 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
614 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
616 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
618 #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
619 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
620 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
621 #define ERRNO_IS_ACCEPT_EAGAIN(e) \
622 (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
623 #define ERRNO_IS_RESOURCE_LIMIT(e) \
624 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
625 #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
626 #define tor_socket_errno(sock) (errno)
627 #define tor_socket_strerror(e) strerror(e)
630 /** Specified SOCKS5 status codes. */
632 SOCKS5_SUCCEEDED
= 0x00,
633 SOCKS5_GENERAL_ERROR
= 0x01,
634 SOCKS5_NOT_ALLOWED
= 0x02,
635 SOCKS5_NET_UNREACHABLE
= 0x03,
636 SOCKS5_HOST_UNREACHABLE
= 0x04,
637 SOCKS5_CONNECTION_REFUSED
= 0x05,
638 SOCKS5_TTL_EXPIRED
= 0x06,
639 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
640 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
641 } socks5_reply_status_t
;
643 /* ===== OS compatibility */
644 MOCK_DECL(const char *, get_uname
, (void));
646 uint16_t get_uint16(const void *cp
) ATTR_NONNULL((1));
647 uint32_t get_uint32(const void *cp
) ATTR_NONNULL((1));
648 uint64_t get_uint64(const void *cp
) ATTR_NONNULL((1));
649 void set_uint16(void *cp
, uint16_t v
) ATTR_NONNULL((1));
650 void set_uint32(void *cp
, uint32_t v
) ATTR_NONNULL((1));
651 void set_uint64(void *cp
, uint64_t v
) ATTR_NONNULL((1));
653 /* These uint8 variants are defined to make the code more uniform. */
654 #define get_uint8(cp) (*(const uint8_t*)(cp))
655 static void set_uint8(void *cp
, uint8_t v
);
657 set_uint8(void *cp
, uint8_t v
)
662 #if !defined(HAVE_RLIM_T)
663 typedef unsigned long rlim_t
;
665 int get_max_sockets(void);
666 int set_max_file_descriptors(rlim_t limit
, int *max
);
667 int tor_disable_debugger_attach(void);
669 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
670 #define HAVE_LINUX_CAPABILITIES
673 int have_capability_support(void);
675 /** Flag for switch_id; see switch_id() for documentation */
676 #define SWITCH_ID_KEEP_BINDLOW (1<<0)
677 /** Flag for switch_id; see switch_id() for documentation */
678 #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
679 int switch_id(const char *user
, unsigned flags
);
681 char *get_user_homedir(const char *username
);
685 const struct passwd
*tor_getpwnam(const char *username
);
686 const struct passwd
*tor_getpwuid(uid_t uid
);
689 int get_parent_directory(char *fname
);
690 char *make_path_absolute(char *fname
);
692 char **get_environment(void);
694 int get_total_system_memory(size_t *mem_out
);
696 int compute_num_cpus(void);
698 int tor_mlockall(void);
700 /** Macros for MIN/MAX. Never use these when the arguments could have
702 * {With GCC extensions we could probably define a safer MIN/MAX. But
703 * depending on that safety would be dangerous, since not every platform
707 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
710 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
713 /* Platform-specific helpers. */
715 char *format_win32_error(DWORD err
);
718 /*for some reason my compiler doesn't have these version flags defined
719 a nice homework assignment for someone one day is to define the rest*/
720 //these are the values as given on MSDN
723 #ifndef VER_SUITE_EMBEDDEDNT
724 #define VER_SUITE_EMBEDDEDNT 0x00000040
727 #ifndef VER_SUITE_SINGLEUSERTS
728 #define VER_SUITE_SINGLEUSERTS 0x00000100
733 #ifdef COMPAT_PRIVATE
734 #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
735 #define NEED_ERSATZ_SOCKETPAIR
736 STATIC
int tor_ersatz_socketpair(int family
, int type
, int protocol
,
741 ssize_t
tor_getpass(const char *prompt
, char *output
, size_t buflen
);
743 /* This needs some of the declarations above so we include it here. */
744 #include "compat_threads.h"