Use a 64-bit type to hold sockets on win64.
[tor.git] / src / common / compat.h
blob8f558e0ce518225fc0a4105cec39768dfc70c5f3
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #ifndef _TOR_COMPAT_H
7 #define _TOR_COMPAT_H
9 #include "orconfig.h"
10 #include "torint.h"
11 #ifdef MS_WINDOWS
12 #define WIN32_WINNT 0x400
13 #define _WIN32_WINNT 0x400
14 #define WIN32_LEAN_AND_MEAN
15 #if defined(_MSC_VER) && (_MSC_VER < 1300)
16 #include <winsock.h>
17 #else
18 #include <winsock2.h>
19 #include <ws2tcpip.h>
20 #endif
21 #endif
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #ifdef HAVE_TIME_H
29 #include <time.h>
30 #endif
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #if defined(HAVE_PTHREAD_H) && !defined(MS_WINDOWS)
35 #include <pthread.h>
36 #endif
37 #include <stdarg.h>
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
40 #endif
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
46 #endif
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50 #ifdef HAVE_NETINET6_IN6_H
51 #include <netinet6/in6.h>
52 #endif
54 #if defined (WINCE)
55 #include <fcntl.h>
56 #include <io.h>
57 #include <math.h>
58 #include <projects.h>
59 #define snprintf _snprintf
60 /* this is not exported as W .... */
61 #define SHGetPathFromIDListW SHGetPathFromIDList
62 /* wcecompat has vasprintf */
63 #define HAVE_VASPRINTF
64 /* no service here */
65 #ifdef NT_SERVICE
66 #undef NT_SERVICE
67 #endif
68 #endif // WINCE
70 #ifndef NULL_REP_IS_ZERO_BYTES
71 #error "It seems your platform does not represent NULL as zero. We can't cope."
72 #endif
74 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
75 #error "It seems that you encode characters in something other than ASCII."
76 #endif
78 /* ===== Compiler compatibility */
80 /* GCC can check printf types on arbitrary functions. */
81 #ifdef __GNUC__
82 #define CHECK_PRINTF(formatIdx, firstArg) \
83 __attribute__ ((format(printf, formatIdx, firstArg)))
84 #else
85 #define CHECK_PRINTF(formatIdx, firstArg)
86 #endif
88 /* inline is __inline on windows. */
89 #ifdef MS_WINDOWS
90 #define INLINE __inline
91 #else
92 #define INLINE inline
93 #endif
95 /* Try to get a reasonable __func__ substitute in place. */
96 #if defined(_MSC_VER)
97 /* MSVC compilers before VC7 don't have __func__ at all; later ones call it
98 * __FUNCTION__. */
99 #if _MSC_VER < 1300
100 #define __func__ "???"
101 #else
102 #define __func__ __FUNCTION__
103 #endif
105 #else
106 /* For platforms where autoconf works, make sure __func__ is defined
107 * sanely. */
108 #ifndef HAVE_MACRO__func__
109 #ifdef HAVE_MACRO__FUNCTION__
110 #define __func__ __FUNCTION__
111 #elif HAVE_MACRO__FUNC__
112 #define __func__ __FUNC__
113 #else
114 #define __func__ "???"
115 #endif
116 #endif /* ifndef MAVE_MACRO__func__ */
117 #endif /* if not windows */
119 #if defined(_MSC_VER) && (_MSC_VER < 1300)
120 /* MSVC versions before 7 apparently don't believe that you can cast uint64_t
121 * to double and really mean it. */
122 extern INLINE double U64_TO_DBL(uint64_t x) {
123 int64_t i = (int64_t) x;
124 return (i < 0) ? ((double) INT64_MAX) : (double) i;
126 #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
127 #else
128 #define U64_TO_DBL(x) ((double) (x))
129 #define DBL_TO_U64(x) ((uint64_t) (x))
130 #endif
132 /* GCC has several useful attributes. */
133 #if defined(__GNUC__) && __GNUC__ >= 3
134 #define ATTR_NORETURN __attribute__((noreturn))
135 #define ATTR_PURE __attribute__((pure))
136 #define ATTR_CONST __attribute__((const))
137 #define ATTR_MALLOC __attribute__((malloc))
138 #define ATTR_NORETURN __attribute__((noreturn))
139 /* Alas, nonnull is not at present a good idea for us. We'd like to get
140 * warnings when we pass NULL where we shouldn't (which nonnull does, albeit
141 * spottily), but we don't want to tell the compiler to make optimizations
142 * with the assumption that the argument can't be NULL (since this would make
143 * many of our checks go away, and make our code less robust against
144 * programming errors). Unfortunately, nonnull currently does both of these
145 * things, and there's no good way to split them up.
147 * #define ATTR_NONNULL(x) __attribute__((nonnull x)) */
148 #define ATTR_NONNULL(x)
150 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
151 * of <b>exp</b> will probably be true.
153 * In other words, "if (PREDICT_LIKELY(foo))" is the same as "if (foo)",
154 * except that it tells the compiler that the branch will be taken most of the
155 * time. This can generate slightly better code with some CPUs.
157 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
158 /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
159 * of <b>exp</b> will probably be false.
161 * In other words, "if (PREDICT_UNLIKELY(foo))" is the same as "if (foo)",
162 * except that it tells the compiler that the branch will usually not be
163 * taken. This can generate slightly better code with some CPUs.
165 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
166 #else
167 #define ATTR_NORETURN
168 #define ATTR_PURE
169 #define ATTR_CONST
170 #define ATTR_MALLOC
171 #define ATTR_NORETURN
172 #define ATTR_NONNULL(x)
173 #define PREDICT_LIKELY(exp) (exp)
174 #define PREDICT_UNLIKELY(exp) (exp)
175 #endif
177 /** Expands to a syntactically valid empty statement. */
178 #define STMT_NIL (void)0
180 #ifdef __GNUC__
181 /** STMT_BEGIN and STMT_END are used to wrap blocks inside macros so that
182 * the macro can be used as if it were a single C statement. */
183 #define STMT_BEGIN (void) ({
184 #define STMT_END })
185 #elif defined(sun) || defined(__sun__)
186 #define STMT_BEGIN if (1) {
187 #define STMT_END } else STMT_NIL
188 #else
189 #define STMT_BEGIN do {
190 #define STMT_END } while (0)
191 #endif
193 /* ===== String compatibility */
194 #ifdef MS_WINDOWS
195 /* Windows names string functions differently from most other platforms. */
196 #define strncasecmp _strnicmp
197 #define strcasecmp _stricmp
198 #endif
199 #ifndef HAVE_STRLCAT
200 size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
201 #endif
202 #ifndef HAVE_STRLCPY
203 size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
204 #endif
206 #ifdef _MSC_VER
207 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
208 * to printf. */
209 #define U64_PRINTF_ARG(a) (a)
210 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
211 * to scanf. */
212 #define U64_SCANF_ARG(a) (a)
213 /** Expands to a literal uint64_t-typed constant for the value <b>n</b>. */
214 #define U64_LITERAL(n) (n ## ui64)
215 #define I64_PRINTF_ARG(a) (a)
216 #define I64_SCANF_ARG(a) (a)
217 #define I64_LITERAL(n) (n ## i64)
218 #else
219 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
220 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
221 #define U64_LITERAL(n) (n ## llu)
222 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
223 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
224 #define I64_LITERAL(n) (n ## ll)
225 #endif
227 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
228 /** The formatting string used to put a uint64_t value in a printf() or
229 * scanf() function. See also U64_PRINTF_ARG and U64_SCANF_ARG. */
230 #define U64_FORMAT "%I64u"
231 #define I64_FORMAT "%I64d"
232 #else
233 #define U64_FORMAT "%llu"
234 #define I64_FORMAT "%lld"
235 #endif
237 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
238 * tor_munmap_file. */
239 typedef struct tor_mmap_t {
240 const char *data; /**< Mapping of the file's contents. */
241 size_t size; /**< Size of the file. */
243 /* None of the fields below should be accessed from outside compat.c */
244 #ifdef HAVE_SYS_MMAN_H
245 size_t mapping_size; /**< Size of the actual mapping. (This is this file
246 * size, rounded up to the nearest page.) */
247 #elif defined MS_WINDOWS
248 HANDLE file_handle;
249 HANDLE mmap_handle;
250 #endif
252 } tor_mmap_t;
254 tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
255 void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
257 int tor_snprintf(char *str, size_t size, const char *format, ...)
258 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
259 int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
260 ATTR_NONNULL((1,3));
262 int tor_asprintf(char **strp, const char *fmt, ...)
263 CHECK_PRINTF(2,3);
264 int tor_vasprintf(char **strp, const char *fmt, va_list args);
266 const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
267 size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
268 static const void *tor_memstr(const void *haystack, size_t hlen,
269 const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
270 static INLINE const void *
271 tor_memstr(const void *haystack, size_t hlen, const char *needle)
273 return tor_memmem(haystack, hlen, needle, strlen(needle));
276 /* Much of the time when we're checking ctypes, we're doing spec compliance,
277 * which all assumes we're doing ASCII. */
278 #define DECLARE_CTYPE_FN(name) \
279 static int TOR_##name(char c); \
280 extern const uint32_t TOR_##name##_TABLE[]; \
281 static INLINE int TOR_##name(char c) { \
282 uint8_t u = c; \
283 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
285 DECLARE_CTYPE_FN(ISALPHA)
286 DECLARE_CTYPE_FN(ISALNUM)
287 DECLARE_CTYPE_FN(ISSPACE)
288 DECLARE_CTYPE_FN(ISDIGIT)
289 DECLARE_CTYPE_FN(ISXDIGIT)
290 DECLARE_CTYPE_FN(ISPRINT)
291 DECLARE_CTYPE_FN(ISLOWER)
292 DECLARE_CTYPE_FN(ISUPPER)
293 extern const char TOR_TOUPPER_TABLE[];
294 extern const char TOR_TOLOWER_TABLE[];
295 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
296 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
298 char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
299 #ifdef HAVE_STRTOK_R
300 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
301 #else
302 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
303 #endif
305 #ifdef MS_WINDOWS
306 #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
307 const char *tor_fix_source_file(const char *fname);
308 #else
309 #define _SHORT_FILE_ (__FILE__)
310 #define tor_fix_source_file(s) (s)
311 #endif
313 /* ===== Time compatibility */
314 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
315 /** Implementation of timeval for platforms that don't have it. */
316 struct timeval {
317 time_t tv_sec;
318 unsigned int tv_usec;
320 #endif
322 void tor_gettimeofday(struct timeval *timeval);
324 struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
325 struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
327 #ifndef timeradd
328 /** Replacement for timeradd on platforms that do not have it: sets tvout to
329 * the sum of tv1 and tv2. */
330 #define timeradd(tv1,tv2,tvout) \
331 do { \
332 (tvout)->tv_sec = (tv1)->tv_sec + (tv2)->tv_sec; \
333 (tvout)->tv_usec = (tv2)->tv_usec + (tv2)->tv_usec; \
334 if ((tvout)->tv_usec >= 1000000) { \
335 (tvout)->tv_usec -= 1000000; \
336 (tvout)->tv_sec++; \
338 } while (0)
339 #endif
341 #ifndef timersub
342 /** Replacement for timersub on platforms that do not have it: sets tvout to
343 * tv1 minus tv2. */
344 #define timersub(tv1,tv2,tvout) \
345 do { \
346 (tvout)->tv_sec = (tv1)->tv_sec - (tv2)->tv_sec; \
347 (tvout)->tv_usec = (tv2)->tv_usec - (tv2)->tv_usec; \
348 if ((tvout)->tv_usec < 0) { \
349 (tvout)->tv_usec += 1000000; \
350 (tvout)->tv_sec--; \
352 } while (0)
353 #endif
355 #ifndef timercmp
356 /** Replacement for timersub on platforms that do not have it: returns true
357 * iff the relational operator "op" makes the expression tv1 op tv2 true.
359 * Note that while this definition should work for all boolean opeators, some
360 * platforms' native timercmp definitions do not support >=, <=, or ==. So
361 * don't use those.
363 #define timercmp(tv1,tv2,op) \
364 (((tv1)->tv_sec == (tv2)->tv_sec) ? \
365 ((tv1)->tv_usec op (tv2)->tv_usec) : \
366 ((tv1)->tv_sec op (tv2)->tv_sec))
367 #endif
369 /* ===== File compatibility */
370 int replace_file(const char *from, const char *to);
371 int touch_file(const char *fname);
373 typedef struct tor_lockfile_t tor_lockfile_t;
374 tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
375 int *locked_out);
376 void tor_lockfile_unlock(tor_lockfile_t *lockfile);
378 off_t tor_fd_getpos(int fd);
379 int tor_fd_seekend(int fd);
381 #ifdef MS_WINDOWS
382 #define PATH_SEPARATOR "\\"
383 #else
384 #define PATH_SEPARATOR "/"
385 #endif
387 /* ===== Net compatibility */
389 #if (SIZEOF_SOCKLEN_T == 0)
390 typedef int socklen_t;
391 #endif
393 #ifdef MS_WINDOWS
394 #define tor_socket_t intptr_t
395 #define SOCKET_OK(s) ((s) != INVALID_SOCKET)
396 #else
397 #define tor_socket_t int
398 #define SOCKET_OK(s) ((s) >= 0)
399 #endif
401 int tor_close_socket(tor_socket_t s);
402 tor_socket_t tor_open_socket(int domain, int type, int protocol);
403 tor_socket_t tor_accept_socket(int sockfd, struct sockaddr *addr,
404 socklen_t *len);
405 int get_n_open_sockets(void);
407 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
408 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
410 /** Implementatino of struct in6_addr for platforms that do not have it.
411 * Generally, these platforms are ones without IPv6 support, but we want to
412 * have a working in6_addr there anyway, so we can use it to parse IPv6
413 * addresses. */
414 #if !defined(HAVE_STRUCT_IN6_ADDR)
415 struct in6_addr
417 union {
418 uint8_t u6_addr8[16];
419 uint16_t u6_addr16[8];
420 uint32_t u6_addr32[4];
421 } in6_u;
422 #define s6_addr in6_u.u6_addr8
423 #define s6_addr16 in6_u.u6_addr16
424 #define s6_addr32 in6_u.u6_addr32
426 #endif
428 /** @{ */
429 /** Many BSD variants seem not to define these. */
430 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
431 || defined(__NetBSD__) || defined(__OpenBSD__)
432 #ifndef s6_addr16
433 #define s6_addr16 __u6_addr.__u6_addr16
434 #endif
435 #ifndef s6_addr32
436 #define s6_addr32 __u6_addr.__u6_addr32
437 #endif
438 #endif
439 /** @} */
441 #ifndef HAVE_SA_FAMILY_T
442 typedef uint16_t sa_family_t;
443 #endif
445 /** @{ */
446 /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
447 * macros get you a pointer to s6_addr32 or local equivalent. */
448 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
449 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
450 #else
451 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
452 #endif
453 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
454 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
455 #else
456 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
457 #endif
458 /** @} */
460 /** Implementation of struct sockaddr_in6 on platforms that do not have
461 * it. See notes on struct in6_addr. */
462 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
463 struct sockaddr_in6 {
464 sa_family_t sin6_family;
465 uint16_t sin6_port;
466 // uint32_t sin6_flowinfo;
467 struct in6_addr sin6_addr;
468 // uint32_t sin6_scope_id;
470 #endif
472 int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
473 const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
474 int tor_inet_pton(int af, const char *src, void *dst);
475 int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
476 void set_socket_nonblocking(tor_socket_t socket);
477 int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
478 int network_init(void);
480 /* For stupid historical reasons, windows sockets have an independent
481 * set of errnos, and an independent way to get them. Also, you can't
482 * always believe WSAEWOULDBLOCK. Use the macros below to compare
483 * errnos against expected values, and use tor_socket_errno to find
484 * the actual errno after a socket operation fails.
486 #if defined(MS_WINDOWS)
487 /** Return true if e is EAGAIN or the local equivalent. */
488 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
489 /** Return true if e is EINPROGRESS or the local equivalent. */
490 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
491 /** Return true if e is EINPROGRESS or the local equivalent as returned by
492 * a call to connect(). */
493 #define ERRNO_IS_CONN_EINPROGRESS(e) \
494 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
495 /** Return true if e is EAGAIN or another error indicating that a call to
496 * accept() has no pending connections to return. */
497 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
498 /** Return true if e is EMFILE or another error indicating that a call to
499 * accept() has failed because we're out of fds or something. */
500 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
501 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
502 /** Return true if e is EADDRINUSE or the local equivalent. */
503 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
504 int tor_socket_errno(tor_socket_t sock);
505 const char *tor_socket_strerror(int e);
506 #else
507 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
508 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
509 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
510 #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
511 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
512 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
513 #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
514 #define tor_socket_errno(sock) (errno)
515 #define tor_socket_strerror(e) strerror(e)
516 #endif
518 /** Specified SOCKS5 status codes. */
519 typedef enum {
520 SOCKS5_SUCCEEDED = 0x00,
521 SOCKS5_GENERAL_ERROR = 0x01,
522 SOCKS5_NOT_ALLOWED = 0x02,
523 SOCKS5_NET_UNREACHABLE = 0x03,
524 SOCKS5_HOST_UNREACHABLE = 0x04,
525 SOCKS5_CONNECTION_REFUSED = 0x05,
526 SOCKS5_TTL_EXPIRED = 0x06,
527 SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
528 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
529 } socks5_reply_status_t;
531 /* ===== Insecure rng */
532 void tor_init_weak_random(unsigned seed);
533 long tor_weak_random(void);
534 #define TOR_RAND_MAX (RAND_MAX)
536 /* ===== OS compatibility */
537 const char *get_uname(void);
539 uint16_t get_uint16(const void *cp) ATTR_PURE ATTR_NONNULL((1));
540 uint32_t get_uint32(const void *cp) ATTR_PURE ATTR_NONNULL((1));
541 uint64_t get_uint64(const void *cp) ATTR_PURE ATTR_NONNULL((1));
542 void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
543 void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
544 void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
546 /* These uint8 variants are defined to make the code more uniform. */
547 #define get_uint8(cp) (*(const uint8_t*)(cp))
548 static void set_uint8(void *cp, uint8_t v);
549 static INLINE void
550 set_uint8(void *cp, uint8_t v)
552 *(uint8_t*)cp = v;
555 #if !defined(HAVE_RLIM_T)
556 typedef unsigned long rlim_t;
557 #endif
558 int set_max_file_descriptors(rlim_t limit, int *max);
559 int switch_id(const char *user);
560 #ifdef HAVE_PWD_H
561 char *get_user_homedir(const char *username);
562 #endif
564 int get_parent_directory(char *fname);
566 int spawn_func(void (*func)(void *), void *data);
567 void spawn_exit(void) ATTR_NORETURN;
569 #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
570 #define USE_WIN32_THREADS
571 #define TOR_IS_MULTITHREADED 1
572 #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
573 defined(HAVE_PTHREAD_CREATE))
574 #define USE_PTHREADS
575 #define TOR_IS_MULTITHREADED 1
576 #else
577 #undef TOR_IS_MULTITHREADED
578 #endif
580 /* Because we use threads instead of processes on most platforms (Windows,
581 * Linux, etc), we need locking for them. On platforms with poor thread
582 * support or broken gethostbyname_r, these functions are no-ops. */
584 /** A generic lock structure for multithreaded builds. */
585 typedef struct tor_mutex_t {
586 #if defined(USE_WIN32_THREADS)
587 /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
588 CRITICAL_SECTION mutex;
589 #elif defined(USE_PTHREADS)
590 /** Pthreads-only: with pthreads, we implement locks with
591 * pthread_mutex_t. */
592 pthread_mutex_t mutex;
593 #else
594 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
595 int _unused;
596 #endif
597 } tor_mutex_t;
599 int tor_mlockall(void);
601 #ifdef TOR_IS_MULTITHREADED
602 tor_mutex_t *tor_mutex_new(void);
603 void tor_mutex_init(tor_mutex_t *m);
604 void tor_mutex_acquire(tor_mutex_t *m);
605 void tor_mutex_release(tor_mutex_t *m);
606 void tor_mutex_free(tor_mutex_t *m);
607 void tor_mutex_uninit(tor_mutex_t *m);
608 unsigned long tor_get_thread_id(void);
609 void tor_threads_init(void);
610 #else
611 #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
612 #define tor_mutex_init(m) STMT_NIL
613 #define tor_mutex_acquire(m) STMT_NIL
614 #define tor_mutex_release(m) STMT_NIL
615 #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
616 #define tor_mutex_uninit(m) STMT_NIL
617 #define tor_get_thread_id() (1UL)
618 #define tor_threads_init() STMT_NIL
619 #endif
621 void set_main_thread(void);
622 int in_main_thread(void);
624 #ifdef TOR_IS_MULTITHREADED
625 #if 0
626 typedef struct tor_cond_t tor_cond_t;
627 tor_cond_t *tor_cond_new(void);
628 void tor_cond_free(tor_cond_t *cond);
629 int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
630 void tor_cond_signal_one(tor_cond_t *cond);
631 void tor_cond_signal_all(tor_cond_t *cond);
632 #endif
633 #endif
635 /** Macros for MIN/MAX. Never use these when the arguments could have
636 * side-effects.
637 * {With GCC extensions we could probably define a safer MIN/MAX. But
638 * depending on that safety would be dangerous, since not every platform
639 * has it.}
641 #ifndef MAX
642 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
643 #endif
644 #ifndef MIN
645 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
646 #endif
648 /* Platform-specific helpers. */
649 #ifdef MS_WINDOWS
650 char *format_win32_error(DWORD err);
651 #endif
653 /*for some reason my compiler doesn't have these version flags defined
654 a nice homework assignment for someone one day is to define the rest*/
655 //these are the values as given on MSDN
656 #ifdef MS_WINDOWS
658 #ifndef VER_SUITE_EMBEDDEDNT
659 #define VER_SUITE_EMBEDDEDNT 0x00000040
660 #endif
662 #ifndef VER_SUITE_SINGLEUSERTS
663 #define VER_SUITE_SINGLEUSERTS 0x00000100
664 #endif
666 #endif
668 #endif