1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
13 #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
14 #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747b
18 #include "testsupport.h"
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_TIME_H
35 #ifdef HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
44 #ifdef HAVE_NETINET6_IN6_H
45 #include <netinet6/in6.h>
48 #include "compat_time.h"
50 #if defined(__has_feature)
51 # if __has_feature(address_sanitizer)
52 /* Some of the fancy glibc strcmp() macros include references to memory that
53 * clang rejects because it is off the end of a less-than-3. Clang hates this,
54 * even though those references never actually happen. */
62 #ifndef NULL_REP_IS_ZERO_BYTES
63 #error "It seems your platform does not represent NULL as zero. We can't cope."
66 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
67 #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
70 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
71 #error "It seems that you encode characters in something other than ASCII."
74 /* ===== Compiler compatibility */
76 /* GCC can check printf and scanf types on arbitrary functions. */
78 #define CHECK_PRINTF(formatIdx, firstArg) \
79 __attribute__ ((format(printf, formatIdx, firstArg)))
81 #define CHECK_PRINTF(formatIdx, firstArg)
84 #define CHECK_SCANF(formatIdx, firstArg) \
85 __attribute__ ((format(scanf, formatIdx, firstArg)))
87 #define CHECK_SCANF(formatIdx, firstArg)
90 /* What GCC do we have? */
92 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
97 /* Temporarily enable and disable warnings. */
99 # define PRAGMA_STRINGIFY_(s) #s
100 # define PRAGMA_JOIN_STRINGIFY_(a,b) PRAGMA_STRINGIFY_(a ## b)
101 /* Support for macro-generated pragmas (c99) */
102 # define PRAGMA_(x) _Pragma (#x)
104 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(clang diagnostic x)
106 # define PRAGMA_DIAGNOSTIC_(x) PRAGMA_(GCC diagnostic x)
108 # if defined(__clang__) || GCC_VERSION >= 406
109 /* we have push/pop support */
110 # define DISABLE_GCC_WARNING(warningopt) \
111 PRAGMA_DIAGNOSTIC_(push) \
112 PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
113 # define ENABLE_GCC_WARNING(warningopt) \
114 PRAGMA_DIAGNOSTIC_(pop)
116 /* older version of gcc: no push/pop support. */
117 # define DISABLE_GCC_WARNING(warningopt) \
118 PRAGMA_DIAGNOSTIC_(ignored PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
119 # define ENABLE_GCC_WARNING(warningopt) \
120 PRAGMA_DIAGNOSTIC_(warning PRAGMA_JOIN_STRINGIFY_(-W,warningopt))
122 #else /* ifdef __GNUC__ */
124 # define DISABLE_GCC_WARNING(warning)
125 # define ENABLE_GCC_WARNING(warning)
128 /* inline is __inline on windows. */
130 #define inline __inline
133 /* Try to get a reasonable __func__ substitute in place. */
134 #if defined(_MSC_VER)
136 #define __func__ __FUNCTION__
139 /* For platforms where autoconf works, make sure __func__ is defined
141 #ifndef HAVE_MACRO__func__
142 #ifdef HAVE_MACRO__FUNCTION__
143 #define __func__ __FUNCTION__
144 #elif HAVE_MACRO__FUNC__
145 #define __func__ __FUNC__
147 #define __func__ "???"
149 #endif /* ifndef MAVE_MACRO__func__ */
150 #endif /* if not windows */
152 #define U64_TO_DBL(x) ((double) (x))
153 #define DBL_TO_U64(x) ((uint64_t) (x))
155 #ifdef ENUM_VALS_ARE_SIGNED
156 #define ENUM_BF(t) unsigned
158 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
159 * just use the enumerated type (so the compiler can help us and notice
160 * problems), but if enumerated types are unsigned, we must use unsigned,
161 * so that the loss of precision doesn't make large values negative. */
165 /* GCC has several useful attributes. */
166 #if defined(__GNUC__) && __GNUC__ >= 3
167 #define ATTR_NORETURN __attribute__((noreturn))
168 #define ATTR_CONST __attribute__((const))
169 #define ATTR_MALLOC __attribute__((malloc))
170 #define ATTR_NORETURN __attribute__((noreturn))
171 #define ATTR_WUR __attribute__((warn_unused_result))
172 /* Alas, nonnull is not at present a good idea for us. We'd like to get
173 * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
174 * spottily), but we don't want to tell the compiler to make optimizations
175 * with the assumption that the argument can't be NULL (since this would make
176 * many of our checks go away, and make our code less robust against
177 * programming errors). Unfortunately, nonnull currently does both of these
178 * things, and there's no good way to split them up.
180 * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
181 #define ATTR_NONNULL(x)
182 #define ATTR_UNUSED __attribute__ ((unused))
184 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
185 * of <b>exp</b> will probably be true.
187 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
188 * except that it tells the compiler that the branch will be taken most of the
189 * time. This can generate slightly better code with some CPUs.
191 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
192 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
193 * of <b>exp</b> will probably be false.
195 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
196 * except that it tells the compiler that the branch will usually not be
197 * taken. This can generate slightly better code with some CPUs.
199 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
201 #define ATTR_NORETURN
204 #define ATTR_NORETURN
205 #define ATTR_NONNULL(x)
208 #define PREDICT_LIKELY(exp) (exp)
209 #define PREDICT_UNLIKELY(exp) (exp)
212 /** Expands to a syntactically valid empty statement. */
213 #define STMT_NIL (void)0
215 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
217 #define STMT_VOID(a) while (0) { (void)(a); }
220 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
221 * the macro can be used as if it were a single C statement. */
222 #define STMT_BEGIN (void) ({
224 #elif defined(sun) || defined(__sun__)
225 #define STMT_BEGIN if (1) {
226 #define STMT_END } else STMT_NIL
228 #define STMT_BEGIN do {
229 #define STMT_END } while (0)
232 /* Some tools (like coccinelle) don't like to see operators as macro
241 /* ===== String compatibility */
243 /* Windows names string functions differently from most other platforms. */
244 #define strncasecmp _strnicmp
245 #define strcasecmp _stricmp
248 #if defined __APPLE__
249 /* On OSX 10.9 and later, the overlap-checking code for strlcat would
250 * appear to have a severe bug that can sometimes cause aborts in Tor.
251 * Instead, use the non-checking variants. This is sad.
253 * See https://trac.torproject.org/projects/tor/ticket/15205
260 size_t strlcat(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
263 size_t strlcpy(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
267 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
269 #define U64_PRINTF_ARG(a) (a)
270 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
272 #define U64_SCANF_ARG(a) (a)
273 /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
274 #define U64_LITERAL(n) (n ## ui64)
275 #define I64_PRINTF_ARG(a) (a)
276 #define I64_SCANF_ARG(a) (a)
277 #define I64_LITERAL(n) (n ## i64)
279 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
280 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
281 #define U64_LITERAL(n) (n ## llu)
282 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
283 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
284 #define I64_LITERAL(n) (n ## ll)
287 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
288 /** The formatting string used to put a uint64_t value in a printf() or
289 * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
290 #define U64_FORMAT "%I64u"
291 #define I64_FORMAT "%I64d"
293 #define U64_FORMAT "%llu"
294 #define I64_FORMAT "%lld"
297 #if (SIZEOF_INTPTR_T == SIZEOF_INT)
298 #define INTPTR_T_FORMAT "%d"
299 #define INTPTR_PRINTF_ARG(x) ((int)(x))
300 #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
301 #define INTPTR_T_FORMAT "%ld"
302 #define INTPTR_PRINTF_ARG(x) ((long)(x))
303 #elif (SIZEOF_INTPTR_T == 8)
304 #define INTPTR_T_FORMAT I64_FORMAT
305 #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
307 #error Unknown: SIZEOF_INTPTR_T
310 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
311 * tor_munmap_file. */
312 typedef struct tor_mmap_t
{
313 const char *data
; /**< Mapping of the file's contents. */
314 size_t size
; /**< Size of the file. */
316 /* None of the fields below should be accessed from outside compat.c */
317 #ifdef HAVE_SYS_MMAN_H
318 size_t mapping_size
; /**< Size of the actual mapping. (This is this file
319 * size, rounded up to the nearest page.) */
326 tor_mmap_t
*tor_mmap_file(const char *filename
) ATTR_NONNULL((1));
327 int tor_munmap_file(tor_mmap_t
*handle
) ATTR_NONNULL((1));
329 int tor_snprintf(char *str
, size_t size
, const char *format
, ...)
330 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
331 int tor_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
332 CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
334 int tor_asprintf(char **strp
, const char *fmt
, ...)
336 int tor_vasprintf(char **strp
, const char *fmt
, va_list args
)
339 const void *tor_memmem(const void *haystack
, size_t hlen
, const void *needle
,
340 size_t nlen
) ATTR_NONNULL((1,3));
341 static const void *tor_memstr(const void *haystack
, size_t hlen
,
342 const char *needle
) ATTR_NONNULL((1,3));
343 static inline const void *
344 tor_memstr(const void *haystack
, size_t hlen
, const char *needle
)
346 return tor_memmem(haystack
, hlen
, needle
, strlen(needle
));
349 /* Much of the time when we're checking ctypes, we're doing spec compliance,
350 * which all assumes we're doing ASCII. */
351 #define DECLARE_CTYPE_FN(name) \
352 static int TOR_##name(char c); \
353 extern const uint32_t TOR_##name##_TABLE[]; \
354 static inline int TOR_##name(char c) { \
356 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
358 DECLARE_CTYPE_FN(ISALPHA
)
359 DECLARE_CTYPE_FN(ISALNUM
)
360 DECLARE_CTYPE_FN(ISSPACE
)
361 DECLARE_CTYPE_FN(ISDIGIT
)
362 DECLARE_CTYPE_FN(ISXDIGIT
)
363 DECLARE_CTYPE_FN(ISPRINT
)
364 DECLARE_CTYPE_FN(ISLOWER
)
365 DECLARE_CTYPE_FN(ISUPPER
)
366 extern const uint8_t TOR_TOUPPER_TABLE
[];
367 extern const uint8_t TOR_TOLOWER_TABLE
[];
368 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
369 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
371 char *tor_strtok_r_impl(char *str
, const char *sep
, char **lasts
);
373 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
375 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
379 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
380 const char *tor_fix_source_file(const char *fname
);
382 #define SHORT_FILE__ (__FILE__)
383 #define tor_fix_source_file(s) (s)
386 /* ===== Time compatibility */
388 struct tm
*tor_localtime_r(const time_t *timep
, struct tm
*result
);
389 struct tm
*tor_gmtime_r(const time_t *timep
, struct tm
*result
);
392 /** Replacement for timeradd on platforms that do not have it: sets tvout to
393 * the sum of tv1 and tv2. */
394 #define timeradd(tv1,tv2,tvout) \
396 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
397 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
398 if ((tvout)->tv_usec >= 1000000) { \
399 (tvout)->tv_usec -= 1000000; \
406 /** Replacement for timersub on platforms that do not have it: sets tvout to
408 #define timersub(tv1,tv2,tvout) \
410 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
411 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
412 if ((tvout)->tv_usec < 0) { \
413 (tvout)->tv_usec += 1000000; \
420 /** Replacement for timersub on platforms that do not have it: returns true
421 * iff the relational operator "op" makes the expression tv1 op tv2 true.
423 * Note that while this definition should work for all boolean opeators, some
424 * platforms' native timercmp definitions do not support >=, <=, or ==. So
427 #define timercmp(tv1,tv2,op) \
428 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
429 ((tv1)->tv_usec op (tv2)->tv_usec) : \
430 ((tv1)->tv_sec op (tv2)->tv_sec))
433 /* ===== File compatibility */
434 int tor_open_cloexec(const char *path
, int flags
, unsigned mode
);
435 FILE *tor_fopen_cloexec(const char *path
, const char *mode
);
436 int tor_rename(const char *path_old
, const char *path_new
);
438 int replace_file(const char *from
, const char *to
);
439 int touch_file(const char *fname
);
441 typedef struct tor_lockfile_t tor_lockfile_t
;
442 tor_lockfile_t
*tor_lockfile_lock(const char *filename
, int blocking
,
444 void tor_lockfile_unlock(tor_lockfile_t
*lockfile
);
446 off_t
tor_fd_getpos(int fd
);
447 int tor_fd_setpos(int fd
, off_t pos
);
448 int tor_fd_seekend(int fd
);
449 int tor_ftruncate(int fd
);
451 int64_t tor_get_avail_disk_space(const char *path
);
454 #define PATH_SEPARATOR "\\"
456 #define PATH_SEPARATOR "/"
459 /* ===== Net compatibility */
461 #if (SIZEOF_SOCKLEN_T == 0)
462 typedef int socklen_t
;
466 /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
467 * any inadvertent checks for the socket being <= 0 or > 0 will probably
469 #define tor_socket_t intptr_t
470 #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
471 #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
472 #define TOR_INVALID_SOCKET INVALID_SOCKET
474 /** Type used for a network socket. */
475 #define tor_socket_t int
476 #define TOR_SOCKET_T_FORMAT "%d"
477 /** Macro: true iff 's' is a possible value for a valid initialized socket. */
478 #define SOCKET_OK(s) ((s) >= 0)
479 /** Error/uninitialized value for a tor_socket_t. */
480 #define TOR_INVALID_SOCKET (-1)
483 int tor_close_socket_simple(tor_socket_t s
);
484 MOCK_DECL(int, tor_close_socket
, (tor_socket_t s
));
485 tor_socket_t
tor_open_socket_with_extensions(
486 int domain
, int type
, int protocol
,
487 int cloexec
, int nonblock
);
488 MOCK_DECL(tor_socket_t
,
489 tor_open_socket
,(int domain
, int type
, int protocol
));
490 tor_socket_t
tor_open_socket_nonblocking(int domain
, int type
, int protocol
);
491 tor_socket_t
tor_accept_socket(tor_socket_t sockfd
, struct sockaddr
*addr
,
493 tor_socket_t
tor_accept_socket_nonblocking(tor_socket_t sockfd
,
494 struct sockaddr
*addr
,
496 tor_socket_t
tor_accept_socket_with_extensions(tor_socket_t sockfd
,
497 struct sockaddr
*addr
,
499 int cloexec
, int nonblock
);
500 MOCK_DECL(tor_socket_t
,
501 tor_connect_socket
,(tor_socket_t socket
,const struct sockaddr
*address
,
502 socklen_t address_len
));
503 int get_n_open_sockets(void);
506 tor_getsockname
,(tor_socket_t socket
, struct sockaddr
*address
,
507 socklen_t
*address_len
));
509 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
510 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
512 /** Implementation of struct in6_addr for platforms that do not have it.
513 * Generally, these platforms are ones without IPv6 support, but we want to
514 * have a working in6_addr there anyway, so we can use it to parse IPv6
516 #if !defined(HAVE_STRUCT_IN6_ADDR)
520 uint8_t u6_addr8
[16];
521 uint16_t u6_addr16
[8];
522 uint32_t u6_addr32
[4];
524 #define s6_addr in6_u.u6_addr8
525 #define s6_addr16 in6_u.u6_addr16
526 #define s6_addr32 in6_u.u6_addr32
531 /** Many BSD variants seem not to define these. */
532 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
533 || defined(__NetBSD__) || defined(__OpenBSD__)
535 #define s6_addr16 __u6_addr.__u6_addr16
538 #define s6_addr32 __u6_addr.__u6_addr32
543 #ifndef HAVE_SA_FAMILY_T
544 typedef uint16_t sa_family_t
;
548 /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
549 * macros get you a pointer to s6_addr32 or local equivalent. */
550 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
551 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
553 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
555 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
556 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
558 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
562 /** Implementation of struct sockaddr_in6 on platforms that do not have
563 * it. See notes on struct in6_addr. */
564 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
565 struct sockaddr_in6
{
566 sa_family_t sin6_family
;
568 // uint32_t sin6_flowinfo;
569 struct in6_addr sin6_addr
;
570 // uint32_t sin6_scope_id;
574 MOCK_DECL(int,tor_gethostname
,(char *name
, size_t namelen
));
575 int tor_inet_aton(const char *cp
, struct in_addr
*addr
) ATTR_NONNULL((1,2));
576 const char *tor_inet_ntop(int af
, const void *src
, char *dst
, size_t len
);
577 int tor_inet_pton(int af
, const char *src
, void *dst
);
578 MOCK_DECL(int,tor_lookup_hostname
,(const char *name
, uint32_t *addr
));
579 int set_socket_nonblocking(tor_socket_t socket
);
580 int tor_socketpair(int family
, int type
, int protocol
, tor_socket_t fd
[2]);
581 int network_init(void);
583 /* For stupid historical reasons, windows sockets have an independent
584 * set of errnos, and an independent way to get them. Also, you can't
585 * always believe WSAEWOULDBLOCK. Use the macros below to compare
586 * errnos against expected values, and use tor_socket_errno to find
587 * the actual errno after a socket operation fails.
590 /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
591 #define SOCK_ERRNO(e) WSA##e
592 /** Return true if e is EAGAIN or the local equivalent. */
593 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
594 /** Return true if e is EINPROGRESS or the local equivalent. */
595 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
596 /** Return true if e is EINPROGRESS or the local equivalent as returned by
597 * a call to connect(). */
598 #define ERRNO_IS_CONN_EINPROGRESS(e) \
599 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
600 /** Return true if e is EAGAIN or another error indicating that a call to
601 * accept() has no pending connections to return. */
602 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
603 /** Return true if e is EMFILE or another error indicating that a call to
604 * accept() has failed because we're out of fds or something. */
605 #define ERRNO_IS_RESOURCE_LIMIT(e) \
606 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
607 /** Return true if e is EADDRINUSE or the local equivalent. */
608 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
609 /** Return true if e is EINTR or the local equivalent */
610 #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
611 int tor_socket_errno(tor_socket_t sock
);
612 const char *tor_socket_strerror(int e
);
614 #define SOCK_ERRNO(e) e
615 #if EAGAIN == EWOULDBLOCK
616 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
617 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
619 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
621 #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
622 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
623 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
624 #define ERRNO_IS_ACCEPT_EAGAIN(e) \
625 (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
626 #define ERRNO_IS_RESOURCE_LIMIT(e) \
627 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
628 #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
629 #define tor_socket_errno(sock) (errno)
630 #define tor_socket_strerror(e) strerror(e)
633 /** Specified SOCKS5 status codes. */
635 SOCKS5_SUCCEEDED
= 0x00,
636 SOCKS5_GENERAL_ERROR
= 0x01,
637 SOCKS5_NOT_ALLOWED
= 0x02,
638 SOCKS5_NET_UNREACHABLE
= 0x03,
639 SOCKS5_HOST_UNREACHABLE
= 0x04,
640 SOCKS5_CONNECTION_REFUSED
= 0x05,
641 SOCKS5_TTL_EXPIRED
= 0x06,
642 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
643 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
644 } socks5_reply_status_t
;
646 /* ===== OS compatibility */
647 MOCK_DECL(const char *, get_uname
, (void));
649 uint16_t get_uint16(const void *cp
) ATTR_NONNULL((1));
650 uint32_t get_uint32(const void *cp
) ATTR_NONNULL((1));
651 uint64_t get_uint64(const void *cp
) ATTR_NONNULL((1));
652 void set_uint16(void *cp
, uint16_t v
) ATTR_NONNULL((1));
653 void set_uint32(void *cp
, uint32_t v
) ATTR_NONNULL((1));
654 void set_uint64(void *cp
, uint64_t v
) ATTR_NONNULL((1));
656 /* These uint8 variants are defined to make the code more uniform. */
657 #define get_uint8(cp) (*(const uint8_t*)(cp))
658 static void set_uint8(void *cp
, uint8_t v
);
660 set_uint8(void *cp
, uint8_t v
)
665 #if !defined(HAVE_RLIM_T)
666 typedef unsigned long rlim_t
;
668 int get_max_sockets(void);
669 int set_max_file_descriptors(rlim_t limit
, int *max
);
670 int tor_disable_debugger_attach(void);
672 #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
673 #define HAVE_LINUX_CAPABILITIES
676 int have_capability_support(void);
678 /** Flag for switch_id; see switch_id() for documentation */
679 #define SWITCH_ID_KEEP_BINDLOW (1<<0)
680 /** Flag for switch_id; see switch_id() for documentation */
681 #define SWITCH_ID_WARN_IF_NO_CAPS (1<<1)
682 int switch_id(const char *user
, unsigned flags
);
684 char *get_user_homedir(const char *username
);
688 const struct passwd
*tor_getpwnam(const char *username
);
689 const struct passwd
*tor_getpwuid(uid_t uid
);
692 int get_parent_directory(char *fname
);
693 char *make_path_absolute(char *fname
);
695 char **get_environment(void);
697 int get_total_system_memory(size_t *mem_out
);
699 int compute_num_cpus(void);
701 int tor_mlockall(void);
703 /** Macros for MIN/MAX. Never use these when the arguments could have
705 * {With GCC extensions we could probably define a safer MIN/MAX. But
706 * depending on that safety would be dangerous, since not every platform
710 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
713 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
716 /* Platform-specific helpers. */
718 char *format_win32_error(DWORD err
);
721 /*for some reason my compiler doesn't have these version flags defined
722 a nice homework assignment for someone one day is to define the rest*/
723 //these are the values as given on MSDN
726 #ifndef VER_SUITE_EMBEDDEDNT
727 #define VER_SUITE_EMBEDDEDNT 0x00000040
730 #ifndef VER_SUITE_SINGLEUSERTS
731 #define VER_SUITE_SINGLEUSERTS 0x00000100
736 #ifdef COMPAT_PRIVATE
737 #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
738 #define NEED_ERSATZ_SOCKETPAIR
739 STATIC
int tor_ersatz_socketpair(int family
, int type
, int protocol
,
744 ssize_t
tor_getpass(const char *prompt
, char *output
, size_t buflen
);
746 /* This needs some of the declarations above so we include it here. */
747 #include "compat_threads.h"