1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, 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>
48 #ifndef NULL_REP_IS_ZERO_BYTES
49 #error "It seems your platform does not represent NULL as zero. We can't cope."
52 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
53 #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
56 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
57 #error "It seems that you encode characters in something other than ASCII."
60 /* ===== Compiler compatibility */
62 /* GCC can check printf and scanf types on arbitrary functions. */
64 #define CHECK_PRINTF(formatIdx, firstArg) \
65 __attribute__ ((format(printf, formatIdx, firstArg)))
67 #define CHECK_PRINTF(formatIdx, firstArg)
70 #define CHECK_SCANF(formatIdx, firstArg) \
71 __attribute__ ((format(scanf, formatIdx, firstArg)))
73 #define CHECK_SCANF(formatIdx, firstArg)
76 /* inline is __inline on windows. */
78 #define INLINE __inline
83 /* Try to get a reasonable __func__ substitute in place. */
86 #define __func__ __FUNCTION__
89 /* For platforms where autoconf works, make sure __func__ is defined
91 #ifndef HAVE_MACRO__func__
92 #ifdef HAVE_MACRO__FUNCTION__
93 #define __func__ __FUNCTION__
94 #elif HAVE_MACRO__FUNC__
95 #define __func__ __FUNC__
97 #define __func__ "???"
99 #endif /* ifndef MAVE_MACRO__func__ */
100 #endif /* if not windows */
102 #define U64_TO_DBL(x) ((double) (x))
103 #define DBL_TO_U64(x) ((uint64_t) (x))
105 #ifdef ENUM_VALS_ARE_SIGNED
106 #define ENUM_BF(t) unsigned
108 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
109 * just use the enumerated type (so the compiler can help us and notice
110 * problems), but if enumerated types are unsigned, we must use unsigned,
111 * so that the loss of precision doesn't make large values negative. */
115 /* GCC has several useful attributes. */
116 #if defined(__GNUC__) && __GNUC__ >= 3
117 #define ATTR_NORETURN __attribute__((noreturn))
118 #define ATTR_CONST __attribute__((const))
119 #define ATTR_MALLOC __attribute__((malloc))
120 #define ATTR_NORETURN __attribute__((noreturn))
121 /* Alas, nonnull is not at present a good idea for us. We'd like to get
122 * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
123 * spottily), but we don't want to tell the compiler to make optimizations
124 * with the assumption that the argument can't be NULL (since this would make
125 * many of our checks go away, and make our code less robust against
126 * programming errors). Unfortunately, nonnull currently does both of these
127 * things, and there's no good way to split them up.
129 * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
130 #define ATTR_NONNULL(x)
131 #define ATTR_UNUSED __attribute__ ((unused))
133 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
134 * of <b>exp</b> will probably be true.
136 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
137 * except that it tells the compiler that the branch will be taken most of the
138 * time. This can generate slightly better code with some CPUs.
140 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
141 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
142 * of <b>exp</b> will probably be false.
144 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
145 * except that it tells the compiler that the branch will usually not be
146 * taken. This can generate slightly better code with some CPUs.
148 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
150 #define ATTR_NORETURN
153 #define ATTR_NORETURN
154 #define ATTR_NONNULL(x)
156 #define PREDICT_LIKELY(exp) (exp)
157 #define PREDICT_UNLIKELY(exp) (exp)
160 /** Expands to a syntactically valid empty statement. */
161 #define STMT_NIL (void)0
163 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
165 #define STMT_VOID(a) while (0) { (void)(a); }
168 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
169 * the macro can be used as if it were a single C statement. */
170 #define STMT_BEGIN (void) ({
172 #elif defined(sun) || defined(__sun__)
173 #define STMT_BEGIN if (1) {
174 #define STMT_END } else STMT_NIL
176 #define STMT_BEGIN do {
177 #define STMT_END } while (0)
180 /* Some tools (like coccinelle) don't like to see operators as macro
189 /* ===== String compatibility */
191 /* Windows names string functions differently from most other platforms. */
192 #define strncasecmp _strnicmp
193 #define strcasecmp _stricmp
196 #if defined __APPLE__
197 /* On OSX 10.9 and later, the overlap-checking code for strlcat would
198 * appear to have a severe bug that can sometimes cause aborts in Tor.
199 * Instead, use the non-checking variants. This is sad.
201 * See https://trac.torproject.org/projects/tor/ticket/15205
208 size_t strlcat(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
211 size_t strlcpy(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
215 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
217 #define U64_PRINTF_ARG(a) (a)
218 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
220 #define U64_SCANF_ARG(a) (a)
221 /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
222 #define U64_LITERAL(n) (n ## ui64)
223 #define I64_PRINTF_ARG(a) (a)
224 #define I64_SCANF_ARG(a) (a)
225 #define I64_LITERAL(n) (n ## i64)
227 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
228 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
229 #define U64_LITERAL(n) (n ## llu)
230 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
231 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
232 #define I64_LITERAL(n) (n ## ll)
235 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
236 /** The formatting string used to put a uint64_t value in a printf() or
237 * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
238 #define U64_FORMAT "%I64u"
239 #define I64_FORMAT "%I64d"
241 #define U64_FORMAT "%llu"
242 #define I64_FORMAT "%lld"
245 #if (SIZEOF_INTPTR_T == SIZEOF_INT)
246 #define INTPTR_T_FORMAT "%d"
247 #define INTPTR_PRINTF_ARG(x) ((int)(x))
248 #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
249 #define INTPTR_T_FORMAT "%ld"
250 #define INTPTR_PRINTF_ARG(x) ((long)(x))
251 #elif (SIZEOF_INTPTR_T == 8)
252 #define INTPTR_T_FORMAT I64_FORMAT
253 #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
255 #error Unknown: SIZEOF_INTPTR_T
258 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
259 * tor_munmap_file. */
260 typedef struct tor_mmap_t
{
261 const char *data
; /**< Mapping of the file's contents. */
262 size_t size
; /**< Size of the file. */
264 /* None of the fields below should be accessed from outside compat.c */
265 #ifdef HAVE_SYS_MMAN_H
266 size_t mapping_size
; /**< Size of the actual mapping. (This is this file
267 * size, rounded up to the nearest page.) */
274 tor_mmap_t
*tor_mmap_file(const char *filename
) ATTR_NONNULL((1));
275 int tor_munmap_file(tor_mmap_t
*handle
) ATTR_NONNULL((1));
277 int tor_snprintf(char *str
, size_t size
, const char *format
, ...)
278 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
279 int tor_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
280 CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
282 int tor_asprintf(char **strp
, const char *fmt
, ...)
284 int tor_vasprintf(char **strp
, const char *fmt
, va_list args
)
287 const void *tor_memmem(const void *haystack
, size_t hlen
, const void *needle
,
288 size_t nlen
) ATTR_NONNULL((1,3));
289 static const void *tor_memstr(const void *haystack
, size_t hlen
,
290 const char *needle
) ATTR_NONNULL((1,3));
291 static INLINE
const void *
292 tor_memstr(const void *haystack
, size_t hlen
, const char *needle
)
294 return tor_memmem(haystack
, hlen
, needle
, strlen(needle
));
297 /* Much of the time when we're checking ctypes, we're doing spec compliance,
298 * which all assumes we're doing ASCII. */
299 #define DECLARE_CTYPE_FN(name) \
300 static int TOR_##name(char c); \
301 extern const uint32_t TOR_##name##_TABLE[]; \
302 static INLINE int TOR_##name(char c) { \
304 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31))); \
306 DECLARE_CTYPE_FN(ISALPHA
)
307 DECLARE_CTYPE_FN(ISALNUM
)
308 DECLARE_CTYPE_FN(ISSPACE
)
309 DECLARE_CTYPE_FN(ISDIGIT
)
310 DECLARE_CTYPE_FN(ISXDIGIT
)
311 DECLARE_CTYPE_FN(ISPRINT
)
312 DECLARE_CTYPE_FN(ISLOWER
)
313 DECLARE_CTYPE_FN(ISUPPER
)
314 extern const char TOR_TOUPPER_TABLE
[];
315 extern const char TOR_TOLOWER_TABLE
[];
316 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
317 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
319 char *tor_strtok_r_impl(char *str
, const char *sep
, char **lasts
);
321 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
323 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
327 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
328 const char *tor_fix_source_file(const char *fname
);
330 #define SHORT_FILE__ (__FILE__)
331 #define tor_fix_source_file(s) (s)
334 /* ===== Time compatibility */
335 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
336 /** Implementation of timeval for platforms that don't have it. */
339 unsigned int tv_usec
;
343 void tor_gettimeofday(struct timeval
*timeval
);
345 struct tm
*tor_localtime_r(const time_t *timep
, struct tm
*result
);
346 struct tm
*tor_gmtime_r(const time_t *timep
, struct tm
*result
);
349 /** Replacement for timeradd on platforms that do not have it: sets tvout to
350 * the sum of tv1 and tv2. */
351 #define timeradd(tv1,tv2,tvout) \
353 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
354 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
355 if ((tvout)->tv_usec >= 1000000) { \
356 (tvout)->tv_usec -= 1000000; \
363 /** Replacement for timersub on platforms that do not have it: sets tvout to
365 #define timersub(tv1,tv2,tvout) \
367 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
368 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
369 if ((tvout)->tv_usec < 0) { \
370 (tvout)->tv_usec += 1000000; \
377 /** Replacement for timersub on platforms that do not have it: returns true
378 * iff the relational operator "op" makes the expression tv1 op tv2 true.
380 * Note that while this definition should work for all boolean opeators, some
381 * platforms' native timercmp definitions do not support >=, <=, or ==. So
384 #define timercmp(tv1,tv2,op) \
385 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
386 ((tv1)->tv_usec op (tv2)->tv_usec) : \
387 ((tv1)->tv_sec op (tv2)->tv_sec))
390 /* ===== File compatibility */
391 int tor_open_cloexec(const char *path
, int flags
, unsigned mode
);
392 FILE *tor_fopen_cloexec(const char *path
, const char *mode
);
393 int tor_rename(const char *path_old
, const char *path_new
);
395 int replace_file(const char *from
, const char *to
);
396 int touch_file(const char *fname
);
398 typedef struct tor_lockfile_t tor_lockfile_t
;
399 tor_lockfile_t
*tor_lockfile_lock(const char *filename
, int blocking
,
401 void tor_lockfile_unlock(tor_lockfile_t
*lockfile
);
403 off_t
tor_fd_getpos(int fd
);
404 int tor_fd_setpos(int fd
, off_t pos
);
405 int tor_fd_seekend(int fd
);
406 int tor_ftruncate(int fd
);
408 int64_t tor_get_avail_disk_space(const char *path
);
411 #define PATH_SEPARATOR "\\"
413 #define PATH_SEPARATOR "/"
416 /* ===== Net compatibility */
418 #if (SIZEOF_SOCKLEN_T == 0)
419 typedef int socklen_t
;
423 /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
424 * any inadvertant checks for the socket being <= 0 or > 0 will probably
426 #define tor_socket_t intptr_t
427 #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
428 #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
429 #define TOR_INVALID_SOCKET INVALID_SOCKET
431 /** Type used for a network socket. */
432 #define tor_socket_t int
433 #define TOR_SOCKET_T_FORMAT "%d"
434 /** Macro: true iff 's' is a possible value for a valid initialized socket. */
435 #define SOCKET_OK(s) ((s) >= 0)
436 /** Error/uninitialized value for a tor_socket_t. */
437 #define TOR_INVALID_SOCKET (-1)
440 int tor_close_socket_simple(tor_socket_t s
);
441 int tor_close_socket(tor_socket_t s
);
442 tor_socket_t
tor_open_socket_with_extensions(
443 int domain
, int type
, int protocol
,
444 int cloexec
, int nonblock
);
445 MOCK_DECL(tor_socket_t
,
446 tor_open_socket
,(int domain
, int type
, int protocol
));
447 tor_socket_t
tor_open_socket_nonblocking(int domain
, int type
, int protocol
);
448 tor_socket_t
tor_accept_socket(tor_socket_t sockfd
, struct sockaddr
*addr
,
450 tor_socket_t
tor_accept_socket_nonblocking(tor_socket_t sockfd
,
451 struct sockaddr
*addr
,
453 tor_socket_t
tor_accept_socket_with_extensions(tor_socket_t sockfd
,
454 struct sockaddr
*addr
,
456 int cloexec
, int nonblock
);
457 MOCK_DECL(tor_socket_t
,
458 tor_connect_socket
,(tor_socket_t socket
,const struct sockaddr
*address
,
459 socklen_t address_len
));
460 int get_n_open_sockets(void);
463 tor_getsockname
,(tor_socket_t socket
, struct sockaddr
*address
,
464 socklen_t
*address_len
));
466 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
467 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
469 /** Implementation of struct in6_addr for platforms that do not have it.
470 * Generally, these platforms are ones without IPv6 support, but we want to
471 * have a working in6_addr there anyway, so we can use it to parse IPv6
473 #if !defined(HAVE_STRUCT_IN6_ADDR)
477 uint8_t u6_addr8
[16];
478 uint16_t u6_addr16
[8];
479 uint32_t u6_addr32
[4];
481 #define s6_addr in6_u.u6_addr8
482 #define s6_addr16 in6_u.u6_addr16
483 #define s6_addr32 in6_u.u6_addr32
488 /** Many BSD variants seem not to define these. */
489 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
490 || defined(__NetBSD__) || defined(__OpenBSD__)
492 #define s6_addr16 __u6_addr.__u6_addr16
495 #define s6_addr32 __u6_addr.__u6_addr32
500 #ifndef HAVE_SA_FAMILY_T
501 typedef uint16_t sa_family_t
;
505 /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
506 * macros get you a pointer to s6_addr32 or local equivalent. */
507 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
508 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
510 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
512 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
513 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
515 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
519 /** Implementation of struct sockaddr_in6 on platforms that do not have
520 * it. See notes on struct in6_addr. */
521 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
522 struct sockaddr_in6
{
523 sa_family_t sin6_family
;
525 // uint32_t sin6_flowinfo;
526 struct in6_addr sin6_addr
;
527 // uint32_t sin6_scope_id;
531 MOCK_DECL(int,tor_gethostname
,(char *name
, size_t namelen
));
532 int tor_inet_aton(const char *cp
, struct in_addr
*addr
) ATTR_NONNULL((1,2));
533 const char *tor_inet_ntop(int af
, const void *src
, char *dst
, size_t len
);
534 int tor_inet_pton(int af
, const char *src
, void *dst
);
535 MOCK_DECL(int,tor_lookup_hostname
,(const char *name
, uint32_t *addr
));
536 int set_socket_nonblocking(tor_socket_t socket
);
537 int tor_socketpair(int family
, int type
, int protocol
, tor_socket_t fd
[2]);
538 int network_init(void);
540 /* For stupid historical reasons, windows sockets have an independent
541 * set of errnos, and an independent way to get them. Also, you can't
542 * always believe WSAEWOULDBLOCK. Use the macros below to compare
543 * errnos against expected values, and use tor_socket_errno to find
544 * the actual errno after a socket operation fails.
547 /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
548 #define SOCK_ERRNO(e) WSA##e
549 /** Return true if e is EAGAIN or the local equivalent. */
550 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
551 /** Return true if e is EINPROGRESS or the local equivalent. */
552 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
553 /** Return true if e is EINPROGRESS or the local equivalent as returned by
554 * a call to connect(). */
555 #define ERRNO_IS_CONN_EINPROGRESS(e) \
556 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
557 /** Return true if e is EAGAIN or another error indicating that a call to
558 * accept() has no pending connections to return. */
559 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
560 /** Return true if e is EMFILE or another error indicating that a call to
561 * accept() has failed because we're out of fds or something. */
562 #define ERRNO_IS_RESOURCE_LIMIT(e) \
563 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
564 /** Return true if e is EADDRINUSE or the local equivalent. */
565 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
566 /** Return true if e is EINTR or the local equivalent */
567 #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
568 int tor_socket_errno(tor_socket_t sock
);
569 const char *tor_socket_strerror(int e
);
571 #define SOCK_ERRNO(e) e
572 #if EAGAIN == EWOULDBLOCK
573 /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
574 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
576 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
578 #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
579 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
580 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
581 #define ERRNO_IS_ACCEPT_EAGAIN(e) \
582 (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
583 #define ERRNO_IS_RESOURCE_LIMIT(e) \
584 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
585 #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
586 #define tor_socket_errno(sock) (errno)
587 #define tor_socket_strerror(e) strerror(e)
590 /** Specified SOCKS5 status codes. */
592 SOCKS5_SUCCEEDED
= 0x00,
593 SOCKS5_GENERAL_ERROR
= 0x01,
594 SOCKS5_NOT_ALLOWED
= 0x02,
595 SOCKS5_NET_UNREACHABLE
= 0x03,
596 SOCKS5_HOST_UNREACHABLE
= 0x04,
597 SOCKS5_CONNECTION_REFUSED
= 0x05,
598 SOCKS5_TTL_EXPIRED
= 0x06,
599 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
600 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
601 } socks5_reply_status_t
;
603 /* ===== OS compatibility */
604 const char *get_uname(void);
606 uint16_t get_uint16(const void *cp
) ATTR_NONNULL((1));
607 uint32_t get_uint32(const void *cp
) ATTR_NONNULL((1));
608 uint64_t get_uint64(const void *cp
) ATTR_NONNULL((1));
609 void set_uint16(void *cp
, uint16_t v
) ATTR_NONNULL((1));
610 void set_uint32(void *cp
, uint32_t v
) ATTR_NONNULL((1));
611 void set_uint64(void *cp
, uint64_t v
) ATTR_NONNULL((1));
613 /* These uint8 variants are defined to make the code more uniform. */
614 #define get_uint8(cp) (*(const uint8_t*)(cp))
615 static void set_uint8(void *cp
, uint8_t v
);
617 set_uint8(void *cp
, uint8_t v
)
622 #if !defined(HAVE_RLIM_T)
623 typedef unsigned long rlim_t
;
625 int get_max_sockets(void);
626 int set_max_file_descriptors(rlim_t limit
, int *max
);
627 int tor_disable_debugger_attach(void);
628 int switch_id(const char *user
);
630 char *get_user_homedir(const char *username
);
634 const struct passwd
*tor_getpwnam(const char *username
);
635 const struct passwd
*tor_getpwuid(uid_t uid
);
638 int get_parent_directory(char *fname
);
639 char *make_path_absolute(char *fname
);
641 char **get_environment(void);
643 int get_total_system_memory(size_t *mem_out
);
645 int compute_num_cpus(void);
647 int tor_mlockall(void);
649 /** Macros for MIN/MAX. Never use these when the arguments could have
651 * {With GCC extensions we could probably define a safer MIN/MAX. But
652 * depending on that safety would be dangerous, since not every platform
656 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
659 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
662 /* Platform-specific helpers. */
664 char *format_win32_error(DWORD err
);
667 /*for some reason my compiler doesn't have these version flags defined
668 a nice homework assignment for someone one day is to define the rest*/
669 //these are the values as given on MSDN
672 #ifndef VER_SUITE_EMBEDDEDNT
673 #define VER_SUITE_EMBEDDEDNT 0x00000040
676 #ifndef VER_SUITE_SINGLEUSERTS
677 #define VER_SUITE_SINGLEUSERTS 0x00000100
682 #ifdef TOR_UNIT_TESTS
683 void tor_sleep_msec(int msec
);
686 #ifdef COMPAT_PRIVATE
687 #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
688 #define NEED_ERSATZ_SOCKETPAIR
689 STATIC
int tor_ersatz_socketpair(int family
, int type
, int protocol
,
694 ssize_t
tor_getpass(const char *prompt
, char *output
, size_t buflen
);
696 /* This needs some of the declarations above so we include it here. */
697 #include "compat_threads.h"