1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
13 #define _WIN32_WINNT 0x0501
15 #define WIN32_LEAN_AND_MEAN
16 #if defined(_MSC_VER) && (_MSC_VER < 1300)
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_TIME_H
38 #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
42 #ifdef HAVE_SYS_RESOURCE_H
43 #include <sys/resource.h>
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
51 #ifdef HAVE_NETINET6_IN6_H
52 #include <netinet6/in6.h>
63 /* this is not exported as W .... */
64 #define SHGetPathFromIDListW SHGetPathFromIDList
65 /* wcecompat has vasprintf */
66 #define HAVE_VASPRINTF
73 #ifndef NULL_REP_IS_ZERO_BYTES
74 #error "It seems your platform does not represent NULL as zero. We can't cope."
77 #ifndef DOUBLE_0_REP_IS_ZERO_BYTES
78 #error "It seems your platform does not represent 0.0 as zeros. We can't cope."
81 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
82 #error "It seems that you encode characters in something other than ASCII."
85 /* ===== Compiler compatibility */
87 /* GCC can check printf types on arbitrary functions. */
89 #define CHECK_PRINTF(formatIdx, firstArg) \
90 __attribute__ ((format(printf, formatIdx, firstArg)))
92 #define CHECK_PRINTF(formatIdx, firstArg)
95 /* inline is __inline on windows. */
97 #define INLINE __inline
102 /* Try to get a reasonable __func__ substitute in place. */
103 #if defined(_MSC_VER)
104 /* MSVC compilers before VC7 don't have __func__ at all; later ones call it
107 #define __func__ "???"
109 #define __func__ __FUNCTION__
113 /* For platforms where autoconf works, make sure __func__ is defined
115 #ifndef HAVE_MACRO__func__
116 #ifdef HAVE_MACRO__FUNCTION__
117 #define __func__ __FUNCTION__
118 #elif HAVE_MACRO__FUNC__
119 #define __func__ __FUNC__
121 #define __func__ "???"
123 #endif /* ifndef MAVE_MACRO__func__ */
124 #endif /* if not windows */
126 #if defined(_MSC_VER) && (_MSC_VER < 1300)
127 /* MSVC versions before 7 apparently don't believe that you can cast uint64_t
128 * to double and really mean it. */
129 extern INLINE
double U64_TO_DBL(uint64_t x
) {
130 int64_t i
= (int64_t) x
;
131 return (i
< 0) ? ((double) INT64_MAX
) : (double) i
;
133 #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
135 #define U64_TO_DBL(x) ((double) (x))
136 #define DBL_TO_U64(x) ((uint64_t) (x))
139 #ifdef ENUM_VALS_ARE_SIGNED
140 #define ENUM_BF(t) unsigned
142 /** Wrapper for having a bitfield of an enumerated type. Where possible, we
143 * just use the enumerated type (so the compiler can help us and notice
144 * problems), but if enumerated types are unsigned, we must use unsigned,
145 * so that the loss of precision doesn't make large values negative. */
149 /* GCC has several useful attributes. */
150 #if defined(__GNUC__) && __GNUC__ >= 3
151 #define ATTR_NORETURN __attribute__((noreturn))
152 #define ATTR_CONST __attribute__((const))
153 #define ATTR_MALLOC __attribute__((malloc))
154 #define ATTR_NORETURN __attribute__((noreturn))
155 /* Alas, nonnull is not at present a good idea for us. We'd like to get
156 * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
157 * spottily), but we don't want to tell the compiler to make optimizations
158 * with the assumption that the argument can't be NULL (since this would make
159 * many of our checks go away, and make our code less robust against
160 * programming errors). Unfortunately, nonnull currently does both of these
161 * things, and there's no good way to split them up.
163 * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
164 #define ATTR_NONNULL(x)
166 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
167 * of <b>exp</b> will probably be true.
169 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
170 * except that it tells the compiler that the branch will be taken most of the
171 * time. This can generate slightly better code with some CPUs.
173 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
174 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
175 * of <b>exp</b> will probably be false.
177 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
178 * except that it tells the compiler that the branch will usually not be
179 * taken. This can generate slightly better code with some CPUs.
181 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
183 #define ATTR_NORETURN
186 #define ATTR_NORETURN
187 #define ATTR_NONNULL(x)
188 #define PREDICT_LIKELY(exp) (exp)
189 #define PREDICT_UNLIKELY(exp) (exp)
192 /** Expands to a syntactically valid empty statement. */
193 #define STMT_NIL (void)0
195 /** Expands to a syntactically valid empty statement, explicitly (void)ing its
197 #define STMT_VOID(a) while (0) { (void)(a); }
200 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
201 * the macro can be used as if it were a single C statement. */
202 #define STMT_BEGIN (void) ({
204 #elif defined(sun) || defined(__sun__)
205 #define STMT_BEGIN if (1) {
206 #define STMT_END } else STMT_NIL
208 #define STMT_BEGIN do {
209 #define STMT_END } while (0)
212 /* ===== String compatibility */
214 /* Windows names string functions differently from most other platforms. */
215 #define strncasecmp _strnicmp
216 #define strcasecmp _stricmp
219 size_t strlcat(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
222 size_t strlcpy(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
226 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
228 #define U64_PRINTF_ARG(a) (a)
229 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
231 #define U64_SCANF_ARG(a) (a)
232 /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
233 #define U64_LITERAL(n) (n ## ui64)
234 #define I64_PRINTF_ARG(a) (a)
235 #define I64_SCANF_ARG(a) (a)
236 #define I64_LITERAL(n) (n ## i64)
238 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
239 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
240 #define U64_LITERAL(n) (n ## llu)
241 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
242 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
243 #define I64_LITERAL(n) (n ## ll)
246 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
247 /** The formatting string used to put a uint64_t value in a printf() or
248 * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
249 #define U64_FORMAT "%I64u"
250 #define I64_FORMAT "%I64d"
252 #define U64_FORMAT "%llu"
253 #define I64_FORMAT "%lld"
256 #if (SIZEOF_INTPTR_T == SIZEOF_INT)
257 #define INTPTR_T_FORMAT "%d"
258 #define INTPTR_PRINTF_ARG(x) ((int)(x))
259 #elif (SIZEOF_INTPTR_T == SIZEOF_LONG)
260 #define INTPTR_T_FORMAT "%ld"
261 #define INTPTR_PRINTF_ARG(x) ((long)(x))
262 #elif (SIZEOF_INTPTR_T == 8)
263 #define INTPTR_T_FORMAT I64_FORMAT
264 #define INTPTR_PRINTF_ARG(x) I64_PRINTF_ARG(x)
266 #error Unknown: SIZEOF_INTPTR_T
269 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
270 * tor_munmap_file. */
271 typedef struct tor_mmap_t
{
272 const char *data
; /**< Mapping of the file's contents. */
273 size_t size
; /**< Size of the file. */
275 /* None of the fields below should be accessed from outside compat.c */
276 #ifdef HAVE_SYS_MMAN_H
277 size_t mapping_size
; /**< Size of the actual mapping. (This is this file
278 * size, rounded up to the nearest page.) */
285 tor_mmap_t
*tor_mmap_file(const char *filename
) ATTR_NONNULL((1));
286 void tor_munmap_file(tor_mmap_t
*handle
) ATTR_NONNULL((1));
288 int tor_snprintf(char *str
, size_t size
, const char *format
, ...)
289 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
290 int tor_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
291 CHECK_PRINTF(3,0) ATTR_NONNULL((1,3));
293 int tor_asprintf(char **strp
, const char *fmt
, ...)
295 int tor_vasprintf(char **strp
, const char *fmt
, va_list args
)
298 const void *tor_memmem(const void *haystack
, size_t hlen
, const void *needle
,
299 size_t nlen
) ATTR_NONNULL((1,3));
300 static const void *tor_memstr(const void *haystack
, size_t hlen
,
301 const char *needle
) ATTR_NONNULL((1,3));
302 static INLINE
const void *
303 tor_memstr(const void *haystack
, size_t hlen
, const char *needle
)
305 return tor_memmem(haystack
, hlen
, needle
, strlen(needle
));
308 /* Much of the time when we're checking ctypes, we're doing spec compliance,
309 * which all assumes we're doing ASCII. */
310 #define DECLARE_CTYPE_FN(name) \
311 static int TOR_##name(char c); \
312 extern const uint32_t TOR_##name##_TABLE[]; \
313 static INLINE int TOR_##name(char c) { \
315 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
317 DECLARE_CTYPE_FN(ISALPHA
)
318 DECLARE_CTYPE_FN(ISALNUM
)
319 DECLARE_CTYPE_FN(ISSPACE
)
320 DECLARE_CTYPE_FN(ISDIGIT
)
321 DECLARE_CTYPE_FN(ISXDIGIT
)
322 DECLARE_CTYPE_FN(ISPRINT
)
323 DECLARE_CTYPE_FN(ISLOWER
)
324 DECLARE_CTYPE_FN(ISUPPER
)
325 extern const char TOR_TOUPPER_TABLE
[];
326 extern const char TOR_TOLOWER_TABLE
[];
327 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
328 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
330 char *tor_strtok_r_impl(char *str
, const char *sep
, char **lasts
);
332 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
334 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
338 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
339 const char *tor_fix_source_file(const char *fname
);
341 #define SHORT_FILE__ (__FILE__)
342 #define tor_fix_source_file(s) (s)
345 /* ===== Time compatibility */
346 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
347 /** Implementation of timeval for platforms that don't have it. */
350 unsigned int tv_usec
;
354 void tor_gettimeofday(struct timeval
*timeval
);
356 struct tm
*tor_localtime_r(const time_t *timep
, struct tm
*result
);
357 struct tm
*tor_gmtime_r(const time_t *timep
, struct tm
*result
);
360 /** Replacement for timeradd on platforms that do not have it: sets tvout to
361 * the sum of tv1 and tv2. */
362 #define timeradd(tv1,tv2,tvout) \
364 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
365 (tvout)->tv_usec = (tv1)->tv_usec + (tv2)->tv_usec; \
366 if ((tvout)->tv_usec >= 1000000) { \
367 (tvout)->tv_usec -= 1000000; \
374 /** Replacement for timersub on platforms that do not have it: sets tvout to
376 #define timersub(tv1,tv2,tvout) \
378 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
379 (tvout)->tv_usec = (tv1)->tv_usec - (tv2)->tv_usec; \
380 if ((tvout)->tv_usec < 0) { \
381 (tvout)->tv_usec += 1000000; \
388 /** Replacement for timersub on platforms that do not have it: returns true
389 * iff the relational operator "op" makes the expression tv1 op tv2 true.
391 * Note that while this definition should work for all boolean opeators, some
392 * platforms' native timercmp definitions do not support >=, <=, or ==. So
395 #define timercmp(tv1,tv2,op) \
396 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
397 ((tv1)->tv_usec op (tv2)->tv_usec) : \
398 ((tv1)->tv_sec op (tv2)->tv_sec))
401 /* ===== File compatibility */
402 int tor_open_cloexec(const char *path
, int flags
, unsigned mode
);
403 FILE *tor_fopen_cloexec(const char *path
, const char *mode
);
405 int replace_file(const char *from
, const char *to
);
406 int touch_file(const char *fname
);
408 typedef struct tor_lockfile_t tor_lockfile_t
;
409 tor_lockfile_t
*tor_lockfile_lock(const char *filename
, int blocking
,
411 void tor_lockfile_unlock(tor_lockfile_t
*lockfile
);
413 off_t
tor_fd_getpos(int fd
);
414 int tor_fd_setpos(int fd
, off_t pos
);
415 int tor_fd_seekend(int fd
);
418 #define PATH_SEPARATOR "\\"
420 #define PATH_SEPARATOR "/"
423 /* ===== Net compatibility */
425 #if (SIZEOF_SOCKLEN_T == 0)
426 typedef int socklen_t
;
430 /* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
431 * any inadvertant checks for the socket being <= 0 or > 0 will probably
433 #define tor_socket_t intptr_t
434 #define TOR_SOCKET_T_FORMAT INTPTR_T_FORMAT
435 #define SOCKET_OK(s) ((SOCKET)(s) != INVALID_SOCKET)
436 #define TOR_INVALID_SOCKET INVALID_SOCKET
438 /** Type used for a network socket. */
439 #define tor_socket_t int
440 #define TOR_SOCKET_T_FORMAT "%d"
441 /** Macro: true iff 's' is a possible value for a valid initialized socket. */
442 #define SOCKET_OK(s) ((s) >= 0)
443 /** Error/uninitialized value for a tor_socket_t. */
444 #define TOR_INVALID_SOCKET (-1)
447 int tor_close_socket(tor_socket_t s
);
448 tor_socket_t
tor_open_socket(int domain
, int type
, int protocol
);
449 tor_socket_t
tor_accept_socket(tor_socket_t sockfd
, struct sockaddr
*addr
,
451 int get_n_open_sockets(void);
453 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
454 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
456 /** Implementation of struct in6_addr for platforms that do not have it.
457 * Generally, these platforms are ones without IPv6 support, but we want to
458 * have a working in6_addr there anyway, so we can use it to parse IPv6
460 #if !defined(HAVE_STRUCT_IN6_ADDR)
464 uint8_t u6_addr8
[16];
465 uint16_t u6_addr16
[8];
466 uint32_t u6_addr32
[4];
468 #define s6_addr in6_u.u6_addr8
469 #define s6_addr16 in6_u.u6_addr16
470 #define s6_addr32 in6_u.u6_addr32
475 /** Many BSD variants seem not to define these. */
476 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
477 || defined(__NetBSD__) || defined(__OpenBSD__)
479 #define s6_addr16 __u6_addr.__u6_addr16
482 #define s6_addr32 __u6_addr.__u6_addr32
487 #ifndef HAVE_SA_FAMILY_T
488 typedef uint16_t sa_family_t
;
492 /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
493 * macros get you a pointer to s6_addr32 or local equivalent. */
494 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
495 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
497 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
499 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
500 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
502 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
506 /** Implementation of struct sockaddr_in6 on platforms that do not have
507 * it. See notes on struct in6_addr. */
508 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
509 struct sockaddr_in6
{
510 sa_family_t sin6_family
;
512 // uint32_t sin6_flowinfo;
513 struct in6_addr sin6_addr
;
514 // uint32_t sin6_scope_id;
518 int tor_inet_aton(const char *cp
, struct in_addr
*addr
) ATTR_NONNULL((1,2));
519 const char *tor_inet_ntop(int af
, const void *src
, char *dst
, size_t len
);
520 int tor_inet_pton(int af
, const char *src
, void *dst
);
521 int tor_lookup_hostname(const char *name
, uint32_t *addr
) ATTR_NONNULL((1,2));
522 int set_socket_nonblocking(tor_socket_t socket
);
523 int tor_socketpair(int family
, int type
, int protocol
, tor_socket_t fd
[2]);
524 int network_init(void);
526 /* For stupid historical reasons, windows sockets have an independent
527 * set of errnos, and an independent way to get them. Also, you can't
528 * always believe WSAEWOULDBLOCK. Use the macros below to compare
529 * errnos against expected values, and use tor_socket_errno to find
530 * the actual errno after a socket operation fails.
533 /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
534 #define SOCK_ERRNO(e) WSA##e
535 /** Return true if e is EAGAIN or the local equivalent. */
536 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
537 /** Return true if e is EINPROGRESS or the local equivalent. */
538 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
539 /** Return true if e is EINPROGRESS or the local equivalent as returned by
540 * a call to connect(). */
541 #define ERRNO_IS_CONN_EINPROGRESS(e) \
542 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
543 /** Return true if e is EAGAIN or another error indicating that a call to
544 * accept() has no pending connections to return. */
545 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
546 /** Return true if e is EMFILE or another error indicating that a call to
547 * accept() has failed because we're out of fds or something. */
548 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
549 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
550 /** Return true if e is EADDRINUSE or the local equivalent. */
551 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
552 int tor_socket_errno(tor_socket_t sock
);
553 const char *tor_socket_strerror(int e
);
555 #define SOCK_ERRNO(e) e
556 #if EAGAIN == EWOULDBLOCK
557 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
559 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
561 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
562 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
563 #define ERRNO_IS_ACCEPT_EAGAIN(e) \
564 (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
565 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
566 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
567 #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
568 #define tor_socket_errno(sock) (errno)
569 #define tor_socket_strerror(e) strerror(e)
572 /** Specified SOCKS5 status codes. */
574 SOCKS5_SUCCEEDED
= 0x00,
575 SOCKS5_GENERAL_ERROR
= 0x01,
576 SOCKS5_NOT_ALLOWED
= 0x02,
577 SOCKS5_NET_UNREACHABLE
= 0x03,
578 SOCKS5_HOST_UNREACHABLE
= 0x04,
579 SOCKS5_CONNECTION_REFUSED
= 0x05,
580 SOCKS5_TTL_EXPIRED
= 0x06,
581 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
582 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
583 } socks5_reply_status_t
;
585 /* ===== OS compatibility */
586 const char *get_uname(void);
588 uint16_t get_uint16(const void *cp
) ATTR_NONNULL((1));
589 uint32_t get_uint32(const void *cp
) ATTR_NONNULL((1));
590 uint64_t get_uint64(const void *cp
) ATTR_NONNULL((1));
591 void set_uint16(void *cp
, uint16_t v
) ATTR_NONNULL((1));
592 void set_uint32(void *cp
, uint32_t v
) ATTR_NONNULL((1));
593 void set_uint64(void *cp
, uint64_t v
) ATTR_NONNULL((1));
595 /* These uint8 variants are defined to make the code more uniform. */
596 #define get_uint8(cp) (*(const uint8_t*)(cp))
597 static void set_uint8(void *cp
, uint8_t v
);
599 set_uint8(void *cp
, uint8_t v
)
604 #if !defined(HAVE_RLIM_T)
605 typedef unsigned long rlim_t
;
607 int set_max_file_descriptors(rlim_t limit
, int *max
);
608 int tor_disable_debugger_attach(void);
609 int switch_id(const char *user
);
611 char *get_user_homedir(const char *username
);
614 int get_parent_directory(char *fname
);
615 char *make_path_absolute(char *fname
);
617 char **get_environment(void);
619 int spawn_func(void (*func
)(void *), void *data
);
620 void spawn_exit(void) ATTR_NORETURN
;
622 #if defined(ENABLE_THREADS) && defined(_WIN32)
623 #define USE_WIN32_THREADS
624 #define TOR_IS_MULTITHREADED 1
625 #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
626 defined(HAVE_PTHREAD_CREATE))
628 #define TOR_IS_MULTITHREADED 1
630 #undef TOR_IS_MULTITHREADED
633 int compute_num_cpus(void);
635 /* Because we use threads instead of processes on most platforms (Windows,
636 * Linux, etc), we need locking for them. On platforms with poor thread
637 * support or broken gethostbyname_r, these functions are no-ops. */
639 /** A generic lock structure for multithreaded builds. */
640 typedef struct tor_mutex_t
{
641 #if defined(USE_WIN32_THREADS)
642 /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
643 CRITICAL_SECTION mutex
;
644 #elif defined(USE_PTHREADS)
645 /** Pthreads-only: with pthreads, we implement locks with
646 * pthread_mutex_t. */
647 pthread_mutex_t mutex
;
649 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
654 int tor_mlockall(void);
656 #ifdef TOR_IS_MULTITHREADED
657 tor_mutex_t
*tor_mutex_new(void);
658 void tor_mutex_init(tor_mutex_t
*m
);
659 void tor_mutex_acquire(tor_mutex_t
*m
);
660 void tor_mutex_release(tor_mutex_t
*m
);
661 void tor_mutex_free(tor_mutex_t
*m
);
662 void tor_mutex_uninit(tor_mutex_t
*m
);
663 unsigned long tor_get_thread_id(void);
664 void tor_threads_init(void);
666 #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
667 #define tor_mutex_init(m) STMT_NIL
668 #define tor_mutex_acquire(m) STMT_VOID(m)
669 #define tor_mutex_release(m) STMT_NIL
670 #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
671 #define tor_mutex_uninit(m) STMT_NIL
672 #define tor_get_thread_id() (1UL)
673 #define tor_threads_init() STMT_NIL
676 void set_main_thread(void);
677 int in_main_thread(void);
679 #ifdef TOR_IS_MULTITHREADED
681 typedef struct tor_cond_t tor_cond_t
;
682 tor_cond_t
*tor_cond_new(void);
683 void tor_cond_free(tor_cond_t
*cond
);
684 int tor_cond_wait(tor_cond_t
*cond
, tor_mutex_t
*mutex
);
685 void tor_cond_signal_one(tor_cond_t
*cond
);
686 void tor_cond_signal_all(tor_cond_t
*cond
);
690 /** Macros for MIN/MAX. Never use these when the arguments could have
692 * {With GCC extensions we could probably define a safer MIN/MAX. But
693 * depending on that safety would be dangerous, since not every platform
697 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
700 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
703 /* Platform-specific helpers. */
705 char *format_win32_error(DWORD err
);
708 /*for some reason my compiler doesn't have these version flags defined
709 a nice homework assignment for someone one day is to define the rest*/
710 //these are the values as given on MSDN
713 #ifndef VER_SUITE_EMBEDDEDNT
714 #define VER_SUITE_EMBEDDEDNT 0x00000040
717 #ifndef VER_SUITE_SINGLEUSERTS
718 #define VER_SUITE_SINGLEUSERTS 0x00000100