1 /* Copyright 2003-2004 Roger Dingledinex
2 * Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
8 #define COMPAT_H_ID "$Id$"
13 #define WIN32_WINNT 0x400
14 #define _WIN32_WINNT 0x400
15 #define WIN32_LEAN_AND_MEAN
16 #if (_MSC_VER <= 1300)
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_TIME_H
37 #ifndef NULL_REP_IS_ZERO_BYTES
38 #error "It seems your platform does not represent NULL as zero. We can't cope."
41 /* ===== Compiler compatibility */
43 /* GCC can check printf types on arbitrary functions. */
45 #define CHECK_PRINTF(formatIdx, firstArg) \
46 __attribute__ ((format(printf, formatIdx, firstArg)))
48 #define CHECK_PRINTF(formatIdx, firstArg)
51 /* inline is __inline on windows. */
53 #define INLINE __inline
58 /* Try to get a reasonable __func__ substitute in place. */
60 /* MSVC compilers before VC7 don't have __func__ at all; later ones call it
63 #define __func__ "???"
65 #define __func__ __FUNCTION__
69 /* For platforms where autoconf works, make sure __func__ is defined
71 #ifndef HAVE_MACRO__func__
72 #ifdef HAVE_MACRO__FUNCTION__
73 #define __func__ __FUNCTION__
74 #elif HAVE_MACRO__FUNC__
75 #define __func__ __FUNC__
77 #define __func__ "???"
79 #endif /* ifndef MAVE_MACRO__func__ */
80 #endif /* if not windows */
82 #if defined(_MSC_VER) && (_MSC_VER < 1300)
83 /* MSVC versions before 7 apparently don't believe that you can cast uint64_t
84 * to double and really mean it. */
85 extern INLINE
double U64_TO_DBL(uint64_t x
) {
86 int64_t i
= (int64_t) x
;
87 return (i
< 0) ? ((double) INT64_MAX
) : (double) i
;
89 #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
91 #define U64_TO_DBL(x) ((double) (x))
92 #define DBL_TO_U64(x) ((uint64_t) (x))
95 /* GCC has several useful attributes. */
96 #if defined(__GNUC__) && __GNUC__ >= 3
97 #define ATTR_NORETURN __attribute__((noreturn))
98 #define ATTR_PURE __attribute__((pure))
99 #define ATTR_MALLOC __attribute__((malloc))
100 #define ATTR_NONNULL(x) __attribute__((nonnull x))
101 #define PREDICT(exp, val) __builtin_expect((exp), (val))
103 #define ATTR_NORETURN
106 #define ATTR_NONNULL(x)
107 #define PREDICT(exp, val) (exp)
110 /* ===== String compatibility */
112 /* Windows names string functions differently from most other platforms. */
113 #define strncasecmp strnicmp
114 #define strcasecmp stricmp
117 size_t strlcat(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
120 size_t strlcpy(char *dst
, const char *src
, size_t siz
) ATTR_NONNULL((1,2));
124 #define U64_PRINTF_ARG(a) (a)
125 #define U64_SCANF_ARG(a) (a)
126 #define U64_FORMAT "%I64u"
127 #define U64_LITERAL(n) (n ## ui64)
129 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
130 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
131 #define U64_FORMAT "%llu"
132 #define U64_LITERAL(n) (n ## llu)
135 /** Represents an mmaped file. Allocated via tor_mmap_file; freed with
136 * tor_munmap_file. */
137 typedef struct tor_mmap_t
{
138 const char *data
; /**< Mapping of the file's contents. */
139 size_t size
; /**< Size of the file. */
142 tor_mmap_t
*tor_mmap_file(const char *filename
) ATTR_NONNULL((1));
143 void tor_munmap_file(tor_mmap_t
*handle
) ATTR_NONNULL((1));
145 int tor_snprintf(char *str
, size_t size
, const char *format
, ...)
146 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
147 int tor_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
150 const void *tor_memmem(const void *haystack
, size_t hlen
, const void *needle
,
151 size_t nlen
) ATTR_PURE
ATTR_NONNULL((1,3));
153 #define TOR_ISALPHA(c) isalpha((int)(unsigned char)(c))
154 #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
155 #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
156 #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
157 #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
158 #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
159 #define TOR_ISLOWER(c) islower((int)(unsigned char)(c))
160 #define TOR_ISUPPER(c) isupper((int)(unsigned char)(c))
162 #define TOR_TOLOWER(c) ((char)tolower((int)(unsigned char)(c)))
163 #define TOR_TOUPPER(c) ((char)toupper((int)(unsigned char)(c)))
166 #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
167 const char *tor_fix_source_file(const char *fname
);
169 #define _SHORT_FILE_ (__FILE__)
170 #define tor_fix_source_file(s) (s)
173 /* ===== Time compatibility */
174 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
177 unsigned int tv_usec
;
181 void tor_gettimeofday(struct timeval
*timeval
);
183 #ifdef HAVE_LOCALTIME_R
184 #define tor_localtime_r localtime_r
186 struct tm
*tor_localtime_r(const time_t *timep
, struct tm
*result
);
190 #define tor_gmtime_r gmtime_r
192 struct tm
*tor_gmtime_r(const time_t *timep
, struct tm
*result
);
195 /* ===== File compatibility */
196 int replace_file(const char *from
, const char *to
);
197 int touch_file(const char *fname
);
200 #define PATH_SEPARATOR "\\"
202 #define PATH_SEPARATOR "/"
205 /* ===== Net compatibility */
207 #define tor_close_socket(s) bclose(s)
208 #elif defined(MS_WINDOWS)
209 /** On Windows, you have to call close() on fds returned by open(),
210 * and closesocket() on fds returned by socket(). On Unix, everything
211 * gets close()'d. We abstract this difference by always using
212 * tor_close_socket to close sockets, and always using close() on
215 #define tor_close_socket(s) closesocket(s)
217 #define tor_close_socket(s) close(s)
221 #define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags)
222 #define tor_socket_recv(s, buf, len, flags) brecv(s, buf, len, flags)
224 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
225 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
228 #if (SIZEOF_SOCKLEN_T == 0)
229 typedef int socklen_t
;
233 int tor_inet_aton(const char *cp
, struct in_addr
*addr
) ATTR_NONNULL((1,2));
234 int tor_lookup_hostname(const char *name
, uint32_t *addr
) ATTR_NONNULL((1,2));
235 void set_socket_nonblocking(int socket
);
236 int tor_socketpair(int family
, int type
, int protocol
, int fd
[2]);
237 int network_init(void);
238 /* For stupid historical reasons, windows sockets have an independent
239 * set of errnos, and an independent way to get them. Also, you can't
240 * always believe WSAEWOULDBLOCK. Use the macros below to compare
241 * errnos against expected values, and use tor_socket_errno to find
242 * the actual errno after a socket operation fails.
244 #if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
245 /** Return true if e is EAGAIN or the local equivalent. */
246 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
247 /** Return true if e is EINPROGRESS or the local equivalent. */
248 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
249 /** Return true if e is EINPROGRESS or the local equivalent as returned by
250 * a call to connect(). */
251 #define ERRNO_IS_CONN_EINPROGRESS(e) \
252 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
253 /** Return true if e is EAGAIN or another error indicating that a call to
254 * accept() has no pending connections to return. */
255 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
256 /** Return true if e is EMFILE or another error indicating that a call to
257 * accept() has failed because we're out of fds or something. */
258 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
259 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
260 /** Return true if e is EADDRINUSE or the local equivalent. */
261 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
262 int tor_socket_errno(int sock
);
263 const char *tor_socket_strerror(int e
);
265 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
266 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
267 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
268 #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
269 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
270 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
271 #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
272 #define tor_socket_errno(sock) (errno)
273 #define tor_socket_strerror(e) strerror(e)
276 /* ===== OS compatibility */
277 const char *get_uname(void);
279 uint16_t get_uint16(const char *cp
) ATTR_PURE
ATTR_NONNULL((1));
280 uint32_t get_uint32(const char *cp
) ATTR_PURE
ATTR_NONNULL((1));
281 void set_uint16(char *cp
, uint16_t v
) ATTR_NONNULL((1));
282 void set_uint32(char *cp
, uint32_t v
) ATTR_NONNULL((1));
284 int set_max_file_descriptors(unsigned long limit
, unsigned long cap
);
285 int switch_id(char *user
, char *group
);
287 char *get_user_homedir(const char *username
);
290 int spawn_func(void (*func
)(void *), void *data
);
291 void spawn_exit(void) ATTR_NORETURN
;
293 #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
294 #define USE_WIN32_THREADS
295 #define TOR_IS_MULTITHREADED 1
296 #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
297 defined(HAVE_PTHREAD_CREATE))
299 #define TOR_IS_MULTITHREADED 1
301 #undef TOR_IS_MULTITHREADED
304 /* Because we use threads instead of processes on most platforms (Windows,
305 * Linux, etc), we need locking for them. On platforms with poor thread
306 * support or broken gethostbyname_r, these functions are no-ops. */
308 typedef struct tor_mutex_t tor_mutex_t
;
309 #ifdef TOR_IS_MULTITHREADED
310 tor_mutex_t
*tor_mutex_new(void);
311 void tor_mutex_acquire(tor_mutex_t
*m
);
312 void tor_mutex_release(tor_mutex_t
*m
);
313 void tor_mutex_free(tor_mutex_t
*m
);
314 unsigned long tor_get_thread_id(void);
316 #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
317 #define tor_mutex_acquire(m) do { } while (0)
318 #define tor_mutex_release(m) do { } while (0)
319 #define tor_mutex_free(m) do { tor_free(m); } while (0)
320 #define tor_get_thread_id() (1UL)
323 /*for some reason my compiler doesn't have these version flags defined
324 a nice homework assignment for someone one day is to define the rest*/
325 //these are the values as given on MSDN
328 #ifndef VER_SUITE_EMBEDDEDNT
329 #define VER_SUITE_EMBEDDEDNT 0x00000040
332 #ifndef VER_SUITE_SINGLEUSERTS
333 #define VER_SUITE_SINGLEUSERTS 0x00000100