Reformat changelog a little to follow arma rules on spacing
[tor.git] / src / common / compat.h
blob8ab7190526334113c9ec0b336d31950f0560878b
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 */
6 #ifndef TOR_COMPAT_H
7 #define TOR_COMPAT_H
9 #include "orconfig.h"
10 #include "torint.h"
11 #ifdef _WIN32
12 #ifndef _WIN32_WINNT
13 #define _WIN32_WINNT 0x0501
14 #endif
15 #define WIN32_LEAN_AND_MEAN
16 #if defined(_MSC_VER) && (_MSC_VER < 1300)
17 #include <winsock.h>
18 #else
19 #include <winsock2.h>
20 #include <ws2tcpip.h>
21 #endif
22 #endif
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
25 #endif
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 #ifdef HAVE_TIME_H
33 #include <time.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38 #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
39 #include <pthread.h>
40 #endif
41 #include <stdarg.h>
42 #ifdef HAVE_SYS_RESOURCE_H
43 #include <sys/resource.h>
44 #endif
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
50 #endif
51 #ifdef HAVE_NETINET6_IN6_H
52 #include <netinet6/in6.h>
53 #endif
55 #include <stdio.h>
56 #include <errno.h>
58 #if defined (WINCE)
59 #include <fcntl.h>
60 #include <io.h>
61 #include <math.h>
62 #include <projects.h>
63 /* this is not exported as W .... */
64 #define SHGetPathFromIDListW SHGetPathFromIDList
65 /* wcecompat has vasprintf */
66 #define HAVE_VASPRINTF
67 /* no service here */
68 #ifdef NT_SERVICE
69 #undef NT_SERVICE
70 #endif
71 #endif // WINCE
73 #ifndef NULL_REP_IS_ZERO_BYTES
74 #error "It seems your platform does not represent NULL as zero. We can't cope."
75 #endif
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."
79 #endif
81 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
82 #error "It seems that you encode characters in something other than ASCII."
83 #endif
85 /* ===== Compiler compatibility */
87 /* GCC can check printf types on arbitrary functions. */
88 #ifdef __GNUC__
89 #define CHECK_PRINTF(formatIdx, firstArg) \
90 __attribute__ ((format(printf, formatIdx, firstArg)))
91 #else
92 #define CHECK_PRINTF(formatIdx, firstArg)
93 #endif
95 /* inline is __inline on windows. */
96 #ifdef _WIN32
97 #define INLINE __inline
98 #else
99 #define INLINE inline
100 #endif
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
105 * __FUNCTION__. */
106 #if _MSC_VER < 1300
107 #define __func__ "???"
108 #else
109 #define __func__ __FUNCTION__
110 #endif
112 #else
113 /* For platforms where autoconf works, make sure __func__ is defined
114 * sanely. */
115 #ifndef HAVE_MACRO__func__
116 #ifdef HAVE_MACRO__FUNCTION__
117 #define __func__ __FUNCTION__
118 #elif HAVE_MACRO__FUNC__
119 #define __func__ __FUNC__
120 #else
121 #define __func__ "???"
122 #endif
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))
134 #else
135 #define U64_TO_DBL(x) ((double) (x))
136 #define DBL_TO_U64(x) ((uint64_t) (x))
137 #endif
139 #ifdef ENUM_VALS_ARE_SIGNED
140 #define ENUM_BF(t) unsigned
141 #else
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. */
146 #define ENUM_BF(t) t
147 #endif
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)
182 #else
183 #define ATTR_NORETURN
184 #define ATTR_CONST
185 #define ATTR_MALLOC
186 #define ATTR_NORETURN
187 #define ATTR_NONNULL(x)
188 #define PREDICT_LIKELY(exp) (exp)
189 #define PREDICT_UNLIKELY(exp) (exp)
190 #endif
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
196 * argument. */
197 #define STMT_VOID(a) while (0) { (void)(a); }
199 #ifdef __GNUC__
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) ({
203 #define STMT_END })
204 #elif defined(sun) || defined(__sun__)
205 #define STMT_BEGIN if (1) {
206 #define STMT_END } else STMT_NIL
207 #else
208 #define STMT_BEGIN do {
209 #define STMT_END } while (0)
210 #endif
212 /* ===== String compatibility */
213 #ifdef _WIN32
214 /* Windows names string functions differently from most other platforms. */
215 #define strncasecmp _strnicmp
216 #define strcasecmp _stricmp
217 #endif
218 #ifndef HAVE_STRLCAT
219 size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
220 #endif
221 #ifndef HAVE_STRLCPY
222 size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
223 #endif
225 #ifdef _MSC_VER
226 /** Casts the uint64_t value in <b>a</b> to the right type for an argument
227 * to printf. */
228 #define U64_PRINTF_ARG(a) (a)
229 /** Casts the uint64_t* value in <b>a</b> to the right type for an argument
230 * to scanf. */
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)
237 #else
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)
244 #endif
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"
251 #else
252 #define U64_FORMAT "%llu"
253 #define I64_FORMAT "%lld"
254 #endif
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)
265 #else
266 #error Unknown: SIZEOF_INTPTR_T
267 #endif
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.) */
279 #elif defined _WIN32
280 HANDLE mmap_handle;
281 #endif
283 } tor_mmap_t;
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, ...)
294 CHECK_PRINTF(2,3);
295 int tor_vasprintf(char **strp, const char *fmt, va_list args)
296 CHECK_PRINTF(2,0);
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) { \
314 uint8_t u = 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);
331 #ifdef HAVE_STRTOK_R
332 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
333 #else
334 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
335 #endif
337 #ifdef _WIN32
338 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))
339 const char *tor_fix_source_file(const char *fname);
340 #else
341 #define SHORT_FILE__ (__FILE__)
342 #define tor_fix_source_file(s) (s)
343 #endif
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. */
348 struct timeval {
349 time_t tv_sec;
350 unsigned int tv_usec;
352 #endif
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);
359 #ifndef timeradd
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) \
363 do { \
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; \
368 (tvout)->tv_sec++; \
370 } while (0)
371 #endif
373 #ifndef timersub
374 /** Replacement for timersub on platforms that do not have it: sets tvout to
375 * tv1 minus tv2. */
376 #define timersub(tv1,tv2,tvout) \
377 do { \
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; \
382 (tvout)->tv_sec--; \
384 } while (0)
385 #endif
387 #ifndef timercmp
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
393 * don't use those.
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))
399 #endif
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,
410 int *locked_out);
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);
417 #ifdef _WIN32
418 #define PATH_SEPARATOR "\\"
419 #else
420 #define PATH_SEPARATOR "/"
421 #endif
423 /* ===== Net compatibility */
425 #if (SIZEOF_SOCKLEN_T == 0)
426 typedef int socklen_t;
427 #endif
429 #ifdef _WIN32
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
432 * still work. */
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
437 #else
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)
445 #endif
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,
450 socklen_t *len);
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
459 * addresses. */
460 #if !defined(HAVE_STRUCT_IN6_ADDR)
461 struct in6_addr
463 union {
464 uint8_t u6_addr8[16];
465 uint16_t u6_addr16[8];
466 uint32_t u6_addr32[4];
467 } in6_u;
468 #define s6_addr in6_u.u6_addr8
469 #define s6_addr16 in6_u.u6_addr16
470 #define s6_addr32 in6_u.u6_addr32
472 #endif
474 /** @{ */
475 /** Many BSD variants seem not to define these. */
476 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
477 || defined(__NetBSD__) || defined(__OpenBSD__)
478 #ifndef s6_addr16
479 #define s6_addr16 __u6_addr.__u6_addr16
480 #endif
481 #ifndef s6_addr32
482 #define s6_addr32 __u6_addr.__u6_addr32
483 #endif
484 #endif
485 /** @} */
487 #ifndef HAVE_SA_FAMILY_T
488 typedef uint16_t sa_family_t;
489 #endif
491 /** @{ */
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)
496 #else
497 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
498 #endif
499 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
500 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
501 #else
502 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
503 #endif
504 /** @} */
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;
511 uint16_t sin6_port;
512 // uint32_t sin6_flowinfo;
513 struct in6_addr sin6_addr;
514 // uint32_t sin6_scope_id;
516 #endif
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.
532 #if defined(_WIN32)
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);
554 #else
555 #define SOCK_ERRNO(e) e
556 #if EAGAIN == EWOULDBLOCK
557 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
558 #else
559 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
560 #endif
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)
570 #endif
572 /** Specified SOCKS5 status codes. */
573 typedef enum {
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);
598 static INLINE void
599 set_uint8(void *cp, uint8_t v)
601 *(uint8_t*)cp = v;
604 #if !defined(HAVE_RLIM_T)
605 typedef unsigned long rlim_t;
606 #endif
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);
610 #ifdef HAVE_PWD_H
611 char *get_user_homedir(const char *username);
612 #endif
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))
627 #define USE_PTHREADS
628 #define TOR_IS_MULTITHREADED 1
629 #else
630 #undef TOR_IS_MULTITHREADED
631 #endif
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;
648 #else
649 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
650 int _unused;
651 #endif
652 } tor_mutex_t;
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);
665 #else
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
674 #endif
676 void set_main_thread(void);
677 int in_main_thread(void);
679 #ifdef TOR_IS_MULTITHREADED
680 #if 0
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);
687 #endif
688 #endif
690 /** Macros for MIN/MAX. Never use these when the arguments could have
691 * side-effects.
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
694 * has it.}
696 #ifndef MAX
697 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
698 #endif
699 #ifndef MIN
700 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
701 #endif
703 /* Platform-specific helpers. */
704 #ifdef _WIN32
705 char *format_win32_error(DWORD err);
706 #endif
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
711 #ifdef _WIN32
713 #ifndef VER_SUITE_EMBEDDEDNT
714 #define VER_SUITE_EMBEDDEDNT 0x00000040
715 #endif
717 #ifndef VER_SUITE_SINGLEUSERTS
718 #define VER_SUITE_SINGLEUSERTS 0x00000100
719 #endif
721 #endif
723 #endif