00001
00002
00003
00004
00005
00006 #ifndef _TOR_COMPAT_H
00007 #define _TOR_COMPAT_H
00008
00009 #include "orconfig.h"
00010 #include "torint.h"
00011 #ifdef MS_WINDOWS
00012 #define WIN32_WINNT 0x400
00013 #define _WIN32_WINNT 0x400
00014 #define WIN32_LEAN_AND_MEAN
00015 #if defined(_MSC_VER) && (_MSC_VER < 1300)
00016 #include <winsock.h>
00017 #else
00018 #include <winsock2.h>
00019 #include <ws2tcpip.h>
00020 #endif
00021 #endif
00022 #ifdef HAVE_SYS_TYPES_H
00023 #include <sys/types.h>
00024 #endif
00025 #ifdef HAVE_SYS_TIME_H
00026 #include <sys/time.h>
00027 #endif
00028 #ifdef HAVE_TIME_H
00029 #include <time.h>
00030 #endif
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 #ifdef HAVE_PTHREAD_H
00035 #include <pthread.h>
00036 #endif
00037 #include <stdarg.h>
00038 #ifdef HAVE_SYS_RESOURCE_H
00039 #include <sys/resource.h>
00040 #endif
00041 #ifdef HAVE_SYS_SOCKET_H
00042 #include <sys/socket.h>
00043 #endif
00044 #ifdef HAVE_SYS_TYPES_H
00045 #include <sys/types.h>
00046 #endif
00047 #ifdef HAVE_NETINET_IN_H
00048 #include <netinet/in.h>
00049 #endif
00050 #ifdef HAVE_NETINET6_IN6_H
00051 #include <netinet6/in6.h>
00052 #endif
00053
00054 #ifndef NULL_REP_IS_ZERO_BYTES
00055 #error "It seems your platform does not represent NULL as zero. We can't cope."
00056 #endif
00057
00058 #if 'a'!=97 || 'z'!=122 || 'A'!=65 || ' '!=32
00059 #error "It seems that you encode characters in something other than ASCII."
00060 #endif
00061
00062
00063
00064
00065 #ifdef __GNUC__
00066 #define CHECK_PRINTF(formatIdx, firstArg) \
00067 __attribute__ ((format(printf, formatIdx, firstArg)))
00068 #else
00069 #define CHECK_PRINTF(formatIdx, firstArg)
00070 #endif
00071
00072
00073 #ifdef MS_WINDOWS
00074 #define INLINE __inline
00075 #else
00076 #define INLINE inline
00077 #endif
00078
00079
00080 #if defined(_MSC_VER)
00081
00082
00083 #if _MSC_VER < 1300
00084 #define __func__ "???"
00085 #else
00086 #define __func__ __FUNCTION__
00087 #endif
00088
00089 #else
00090
00091
00092 #ifndef HAVE_MACRO__func__
00093 #ifdef HAVE_MACRO__FUNCTION__
00094 #define __func__ __FUNCTION__
00095 #elif HAVE_MACRO__FUNC__
00096 #define __func__ __FUNC__
00097 #else
00098 #define __func__ "???"
00099 #endif
00100 #endif
00101 #endif
00102
00103 #if defined(_MSC_VER) && (_MSC_VER < 1300)
00104
00105
00106 extern INLINE double U64_TO_DBL(uint64_t x) {
00107 int64_t i = (int64_t) x;
00108 return (i < 0) ? ((double) INT64_MAX) : (double) i;
00109 }
00110 #define DBL_TO_U64(x) ((uint64_t)(int64_t) (x))
00111 #else
00112 #define U64_TO_DBL(x) ((double) (x))
00113 #define DBL_TO_U64(x) ((uint64_t) (x))
00114 #endif
00115
00116
00117 #if defined(__GNUC__) && __GNUC__ >= 3
00118 #define ATTR_NORETURN __attribute__((noreturn))
00119 #define ATTR_PURE __attribute__((pure))
00120 #define ATTR_CONST __attribute__((const))
00121 #define ATTR_MALLOC __attribute__((malloc))
00122 #define ATTR_NORETURN __attribute__((noreturn))
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 #define ATTR_NONNULL(x)
00133
00141 #define PREDICT_LIKELY(exp) __builtin_expect(!!(exp), 1)
00142
00149 #define PREDICT_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
00150 #else
00151 #define ATTR_NORETURN
00152 #define ATTR_PURE
00153 #define ATTR_CONST
00154 #define ATTR_MALLOC
00155 #define ATTR_NORETURN
00156 #define ATTR_NONNULL(x)
00157 #define PREDICT_LIKELY(exp) (exp)
00158 #define PREDICT_UNLIKELY(exp) (exp)
00159 #endif
00160
00162 #define STMT_NIL (void)0
00163
00164 #ifdef __GNUC__
00165
00167 #define STMT_BEGIN (void) ({
00168 #define STMT_END })
00169 #elif defined(sun) || defined(__sun__)
00170 #define STMT_BEGIN if (1) {
00171 #define STMT_END } else STMT_NIL
00172 #else
00173 #define STMT_BEGIN do {
00174 #define STMT_END } while (0)
00175 #endif
00176
00177
00178 #ifdef MS_WINDOWS
00179
00180 #define strncasecmp strnicmp
00181 #define strcasecmp stricmp
00182 #endif
00183 #ifndef HAVE_STRLCAT
00184 size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
00185 #endif
00186 #ifndef HAVE_STRLCPY
00187 size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
00188 #endif
00189
00190 #ifdef _MSC_VER
00191
00193 #define U64_PRINTF_ARG(a) (a)
00194
00196 #define U64_SCANF_ARG(a) (a)
00197
00198 #define U64_LITERAL(n) (n ## ui64)
00199 #define I64_PRINTF_ARG(a) (a)
00200 #define I64_SCANF_ARG(a) (a)
00201 #define I64_LITERAL(n) (n ## i64)
00202 #else
00203 #define U64_PRINTF_ARG(a) ((long long unsigned int)(a))
00204 #define U64_SCANF_ARG(a) ((long long unsigned int*)(a))
00205 #define U64_LITERAL(n) (n ## llu)
00206 #define I64_PRINTF_ARG(a) ((long long signed int)(a))
00207 #define I64_SCANF_ARG(a) ((long long signed int*)(a))
00208 #define I64_LITERAL(n) (n ## ll)
00209 #endif
00210
00211 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
00212
00214 #define U64_FORMAT "%I64u"
00215 #define I64_FORMAT "%I64d"
00216 #else
00217 #define U64_FORMAT "%llu"
00218 #define I64_FORMAT "%lld"
00219 #endif
00220
00223 typedef struct tor_mmap_t {
00224 const char *data;
00225 size_t size;
00227
00228 #ifdef HAVE_SYS_MMAN_H
00229 size_t mapping_size;
00231 #elif defined MS_WINDOWS
00232 HANDLE file_handle;
00233 HANDLE mmap_handle;
00234 #endif
00235
00236 } tor_mmap_t;
00237
00238 tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
00239 void tor_munmap_file(tor_mmap_t *handle) ATTR_NONNULL((1));
00240
00241 int tor_snprintf(char *str, size_t size, const char *format, ...)
00242 CHECK_PRINTF(3,4) ATTR_NONNULL((1,3));
00243 int tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
00244 ATTR_NONNULL((1,3));
00245
00246 int tor_asprintf(char **strp, const char *fmt, ...)
00247 CHECK_PRINTF(2,3);
00248 int tor_vasprintf(char **strp, const char *fmt, va_list args);
00249
00250 const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
00251 size_t nlen) ATTR_PURE ATTR_NONNULL((1,3));
00252 static const void *tor_memstr(const void *haystack, size_t hlen,
00253 const char *needle) ATTR_PURE ATTR_NONNULL((1,3));
00254 static INLINE const void *
00255 tor_memstr(const void *haystack, size_t hlen, const char *needle)
00256 {
00257 return tor_memmem(haystack, hlen, needle, strlen(needle));
00258 }
00259
00260
00261
00262 #define DECLARE_CTYPE_FN(name) \
00263 static int TOR_##name(char c); \
00264 extern const uint32_t TOR_##name##_TABLE[]; \
00265 static INLINE int TOR_##name(char c) { \
00266 uint8_t u = c; \
00267 return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
00268 }
00269 DECLARE_CTYPE_FN(ISALPHA)
00270 DECLARE_CTYPE_FN(ISALNUM)
00271 DECLARE_CTYPE_FN(ISSPACE)
00272 DECLARE_CTYPE_FN(ISDIGIT)
00273 DECLARE_CTYPE_FN(ISXDIGIT)
00274 DECLARE_CTYPE_FN(ISPRINT)
00275 DECLARE_CTYPE_FN(ISLOWER)
00276 DECLARE_CTYPE_FN(ISUPPER)
00277 extern const char TOR_TOUPPER_TABLE[];
00278 extern const char TOR_TOLOWER_TABLE[];
00279 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
00280 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
00281
00282 char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
00283 #ifdef HAVE_STRTOK_R
00284 #define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
00285 #else
00286 #define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
00287 #endif
00288
00289 #ifdef MS_WINDOWS
00290 #define _SHORT_FILE_ (tor_fix_source_file(__FILE__))
00291 const char *tor_fix_source_file(const char *fname);
00292 #else
00293 #define _SHORT_FILE_ (__FILE__)
00294 #define tor_fix_source_file(s) (s)
00295 #endif
00296
00297
00298 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
00299 struct timeval {
00300 time_t tv_sec;
00301 unsigned int tv_usec;
00302 };
00303 #endif
00304
00305 void tor_gettimeofday(struct timeval *timeval);
00306
00307 #ifdef HAVE_LOCALTIME_R
00308 #define tor_localtime_r localtime_r
00309 #else
00310 struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
00311 #endif
00312
00313 #ifdef HAVE_GMTIME_R
00314 #define tor_gmtime_r gmtime_r
00315 #else
00316 struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
00317 #endif
00318
00319
00320 int replace_file(const char *from, const char *to);
00321 int touch_file(const char *fname);
00322
00323 typedef struct tor_lockfile_t tor_lockfile_t;
00324 tor_lockfile_t *tor_lockfile_lock(const char *filename, int blocking,
00325 int *locked_out);
00326 void tor_lockfile_unlock(tor_lockfile_t *lockfile);
00327
00328 off_t tor_fd_getpos(int fd);
00329 int tor_fd_seekend(int fd);
00330
00331 #ifdef MS_WINDOWS
00332 #define PATH_SEPARATOR "\\"
00333 #else
00334 #define PATH_SEPARATOR "/"
00335 #endif
00336
00337
00338
00339 #if (SIZEOF_SOCKLEN_T == 0)
00340 typedef int socklen_t;
00341 #endif
00342
00343 int tor_close_socket(int s);
00344 int tor_open_socket(int domain, int type, int protocol);
00345 int tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len);
00346 int get_n_open_sockets(void);
00347
00348 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
00349 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
00350
00351
00352
00353
00354
00355 #if !defined(HAVE_STRUCT_IN6_ADDR)
00356 struct in6_addr
00357 {
00358 union {
00359 uint8_t u6_addr8[16];
00360 uint16_t u6_addr16[8];
00361 uint32_t u6_addr32[4];
00362 } in6_u;
00363 #define s6_addr in6_u.u6_addr8
00364 #define s6_addr16 in6_u.u6_addr16
00365 #define s6_addr32 in6_u.u6_addr32
00366 };
00367 #endif
00368
00369 #if defined(__APPLE__) || defined(__darwin__) || defined(__FreeBSD__) \
00370 || defined(__NetBSD__) || defined(__OpenBSD__)
00371
00372 #ifndef s6_addr16
00373 #define s6_addr16 __u6_addr.__u6_addr16
00374 #endif
00375 #ifndef s6_addr32
00376 #define s6_addr32 __u6_addr.__u6_addr32
00377 #endif
00378 #endif
00379
00380 #ifndef HAVE_SA_FAMILY_T
00381 typedef uint16_t sa_family_t;
00382 #endif
00383
00384
00385 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
00386 #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
00387 #else
00388 #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
00389 #endif
00390 #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
00391 #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
00392 #else
00393 #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
00394 #endif
00395
00396
00397
00398 #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
00399 struct sockaddr_in6 {
00400 sa_family_t sin6_family;
00401 uint16_t sin6_port;
00402
00403 struct in6_addr sin6_addr;
00404
00405 };
00406 #endif
00407
00408 int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
00409 const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
00410 int tor_inet_pton(int af, const char *src, void *dst);
00411 int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
00412 void set_socket_nonblocking(int socket);
00413 int tor_socketpair(int family, int type, int protocol, int fd[2]);
00414 int network_init(void);
00415
00416
00417
00418
00419
00420
00421
00422 #if defined(MS_WINDOWS)
00423
00424 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
00425
00426 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
00427
00429 #define ERRNO_IS_CONN_EINPROGRESS(e) \
00430 ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
00431
00433 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
00434
00436 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
00437 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
00438
00439 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
00440 int tor_socket_errno(int sock);
00441 const char *tor_socket_strerror(int e);
00442 #else
00443 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
00444 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
00445 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
00446 #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
00447 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
00448 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
00449 #define ERRNO_IS_EADDRINUSE(e) ((e) == EADDRINUSE)
00450 #define tor_socket_errno(sock) (errno)
00451 #define tor_socket_strerror(e) strerror(e)
00452 #endif
00453
00455 typedef enum {
00456 SOCKS5_SUCCEEDED = 0x00,
00457 SOCKS5_GENERAL_ERROR = 0x01,
00458 SOCKS5_NOT_ALLOWED = 0x02,
00459 SOCKS5_NET_UNREACHABLE = 0x03,
00460 SOCKS5_HOST_UNREACHABLE = 0x04,
00461 SOCKS5_CONNECTION_REFUSED = 0x05,
00462 SOCKS5_TTL_EXPIRED = 0x06,
00463 SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
00464 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
00465 } socks5_reply_status_t;
00466
00467
00468 const char *get_uname(void);
00469
00470 uint16_t get_uint16(const char *cp) ATTR_PURE ATTR_NONNULL((1));
00471 uint32_t get_uint32(const char *cp) ATTR_PURE ATTR_NONNULL((1));
00472 uint64_t get_uint64(const char *cp) ATTR_PURE ATTR_NONNULL((1));
00473 void set_uint16(char *cp, uint16_t v) ATTR_NONNULL((1));
00474 void set_uint32(char *cp, uint32_t v) ATTR_NONNULL((1));
00475 void set_uint64(char *cp, uint64_t v) ATTR_NONNULL((1));
00476
00477
00478 #define get_uint8(cp) (*(const uint8_t*)(cp))
00479 static void set_uint8(char *cp, uint8_t v);
00480 static INLINE void
00481 set_uint8(char *cp, uint8_t v)
00482 {
00483 *(uint8_t*)cp = v;
00484 }
00485
00486 #if !defined(HAVE_RLIM_T)
00487 typedef unsigned long rlim_t;
00488 #endif
00489 int set_max_file_descriptors(rlim_t limit, int *max);
00490 int switch_id(const char *user);
00491 #ifdef HAVE_PWD_H
00492 char *get_user_homedir(const char *username);
00493 #endif
00494
00495 int spawn_func(void (*func)(void *), void *data);
00496 void spawn_exit(void) ATTR_NORETURN;
00497
00498 #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
00499 #define USE_WIN32_THREADS
00500 #define TOR_IS_MULTITHREADED 1
00501 #elif (defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && \
00502 defined(HAVE_PTHREAD_CREATE))
00503 #define USE_PTHREADS
00504 #define TOR_IS_MULTITHREADED 1
00505 #else
00506 #undef TOR_IS_MULTITHREADED
00507 #endif
00508
00509
00510
00511
00512
00514 typedef struct tor_mutex_t {
00515 #if defined(USE_WIN32_THREADS)
00516 CRITICAL_SECTION mutex;
00517 #elif defined(USE_PTHREADS)
00518 pthread_mutex_t mutex;
00519 #else
00520 int _unused;
00521 #endif
00522 } tor_mutex_t;
00523
00524 int tor_mlockall(void);
00525
00526 #ifdef TOR_IS_MULTITHREADED
00527 tor_mutex_t *tor_mutex_new(void);
00528 void tor_mutex_init(tor_mutex_t *m);
00529 void tor_mutex_acquire(tor_mutex_t *m);
00530 void tor_mutex_release(tor_mutex_t *m);
00531 void tor_mutex_free(tor_mutex_t *m);
00532 void tor_mutex_uninit(tor_mutex_t *m);
00533 unsigned long tor_get_thread_id(void);
00534 void tor_threads_init(void);
00535 #else
00536 #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
00537 #define tor_mutex_init(m) STMT_NIL
00538 #define tor_mutex_acquire(m) STMT_NIL
00539 #define tor_mutex_release(m) STMT_NIL
00540 #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
00541 #define tor_mutex_uninit(m) STMT_NIL
00542 #define tor_get_thread_id() (1UL)
00543 #define tor_threads_init() STMT_NIL
00544 #endif
00545
00546 void set_main_thread(void);
00547 int in_main_thread(void);
00548
00549 #ifdef TOR_IS_MULTITHREADED
00550 #if 0
00551 typedef struct tor_cond_t tor_cond_t;
00552 tor_cond_t *tor_cond_new(void);
00553 void tor_cond_free(tor_cond_t *cond);
00554 int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex);
00555 void tor_cond_signal_one(tor_cond_t *cond);
00556 void tor_cond_signal_all(tor_cond_t *cond);
00557 #endif
00558 #endif
00559
00566 #ifndef MAX
00567 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
00568 #endif
00569 #ifndef MIN
00570 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
00571 #endif
00572
00573
00574 #ifdef MS_WINDOWS
00575 char *format_win32_error(DWORD err);
00576 #endif
00577
00578
00579
00580
00581 #ifdef MS_WINDOWS
00582
00583 #ifndef VER_SUITE_EMBEDDEDNT
00584 #define VER_SUITE_EMBEDDEDNT 0x00000040
00585 #endif
00586
00587 #ifndef VER_SUITE_SINGLEUSERTS
00588 #define VER_SUITE_SINGLEUSERTS 0x00000100
00589 #endif
00590
00591 #endif
00592
00593 #endif
00594