compat.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright 2003-2004 Roger Dingledinex
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. #ifndef __COMPAT_H
  6. #define __COMPAT_H
  7. #define COMPAT_H_ID "$Id$"
  8. #include "orconfig.h"
  9. #include "torint.h"
  10. #ifdef MS_WINDOWS
  11. #define WIN32_WINNT 0x400
  12. #define _WIN32_WINNT 0x400
  13. #define WIN32_LEAN_AND_MEAN
  14. #if (_MSC_VER <= 1300)
  15. #include <winsock.h>
  16. #else
  17. #include <winsock2.h>
  18. #include <ws2tcpip.h>
  19. #endif
  20. #endif
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #ifdef HAVE_SYS_TIME_H
  25. #include <sys/time.h>
  26. #endif
  27. #include <stdarg.h>
  28. #ifndef NULL_REP_IS_ZERO_BYTES
  29. #error "It seems your platform does not represent NULL as zero. We can't cope."
  30. #endif
  31. /* ===== Compiler compatibility */
  32. /* GCC can check printf types on arbitrary functions. */
  33. #ifdef __GNUC__
  34. #define CHECK_PRINTF(formatIdx, firstArg) \
  35. __attribute__ ((format(printf, formatIdx, firstArg)))
  36. #else
  37. #define CHECK_PRINTF(formatIdx, firstArg)
  38. #endif
  39. /* inline is __inline on windows. */
  40. #ifdef MS_WINDOWS
  41. #define INLINE __inline
  42. #else
  43. #define INLINE inline
  44. #endif
  45. /* Windows compilers before VC7 don't have __FUNCTION__. */
  46. #if defined(_MSC_VER) && _MSC_VER < 1300
  47. #define __FUNCTION__ "???"
  48. #endif
  49. #if defined(__sgi) && !defined(__GNUC__) && defined(__c99)
  50. #define __FUNCTION__ __func__
  51. #endif
  52. /* ===== String compatibility */
  53. #ifdef MS_WINDOWS
  54. /* Windows names string functions differently from most other platforms. */
  55. #define strncasecmp strnicmp
  56. #define strcasecmp stricmp
  57. #endif
  58. #ifndef HAVE_STRLCAT
  59. size_t strlcat(char *dst, const char *src, size_t siz);
  60. #endif
  61. #ifndef HAVE_STRLCPY
  62. size_t strlcpy(char *dst, const char *src, size_t siz);
  63. #endif
  64. #ifdef MS_WINDOWS
  65. #define U64_PRINTF_ARG(a) (a)
  66. #define U64_SCANF_ARG(a) (a)
  67. #define U64_FORMAT "%I64u"
  68. #define U64_LITERAL(n) (n ## ui64)
  69. #else
  70. #define U64_PRINTF_ARG(a) ((long long unsigned int)a)
  71. #define U64_SCANF_ARG(a) ((long long unsigned int*)a)
  72. #define U64_FORMAT "%llu"
  73. #define U64_LITERAL(n) (n ## llu)
  74. #endif
  75. int tor_snprintf(char *str, size_t size, const char *format, ...)
  76. CHECK_PRINTF(3,4);
  77. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);
  78. #define TOR_ISAPLHA(c) isalpha((int)(unsigned char)(c))
  79. #define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
  80. #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
  81. #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
  82. #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
  83. #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
  84. #define _SHORT_FILE_ (_tor_fix_source_file(__FILE__))
  85. const char *_tor_fix_source_file(const char *fname);
  86. /* ===== Time compatibility */
  87. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  88. struct timeval {
  89. time_t tv_sec;
  90. unsigned int tv_usec;
  91. };
  92. #endif
  93. void tor_gettimeofday(struct timeval *timeval);
  94. #ifdef HAVE_LOCALTIME_R
  95. #define tor_localtime_r localtime_r
  96. #else
  97. struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
  98. #endif
  99. #ifdef HAVE_GMTIME_R
  100. #define tor_gmtime_r gmtime_r
  101. #else
  102. struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
  103. #endif
  104. /* ===== File compatibility */
  105. int replace_file(const char *from, const char *to);
  106. /* ===== Net compatibility */
  107. #ifdef MS_WINDOWS
  108. /** On windows, you have to call close() on fds returned by open(),
  109. * and closesocket() on fds returned by socket(). On Unix, everything
  110. * gets close()'d. We abstract this difference by always using
  111. * tor_close_socket to close sockets, and always using close() on
  112. * files.
  113. */
  114. #define tor_close_socket(s) closesocket(s)
  115. #else
  116. #define tor_close_socket(s) close(s)
  117. #endif
  118. #if (SIZEOF_SOCKLEN_T == 0)
  119. typedef int socklen_t;
  120. #endif
  121. /* Now that we use libevent, all real sockets are safe for polling ... or
  122. * if they aren't, libevent will help us. */
  123. #define SOCKET_IS_POLLABLE(fd) ((fd)>=0)
  124. struct in_addr;
  125. int tor_inet_aton(const char *cp, struct in_addr *addr);
  126. int tor_lookup_hostname(const char *name, uint32_t *addr);
  127. void set_socket_nonblocking(int socket);
  128. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  129. int network_init(void);
  130. /* For stupid historical reasons, windows sockets have an independent
  131. * set of errnos, and an independent way to get them. Also, you can't
  132. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  133. * errnos against expected values, and use tor_socket_errno to find
  134. * the actual errno after a socket operation fails.
  135. */
  136. #ifdef MS_WINDOWS
  137. /** Return true if e is EAGAIN or the local equivalent. */
  138. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  139. /** Return true if e is EINPROGRESS or the local equivalent. */
  140. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  141. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  142. * a call to connect(). */
  143. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  144. /** Return true if e is EAGAIN or another error indicating that a call to
  145. * accept() has no pending connections to return. */
  146. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  147. /** Return true if e is EMFILE or another error indicating that a call to
  148. * accept() has failed because we're out of fds or something. */
  149. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  150. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  151. int tor_socket_errno(int sock);
  152. const char *tor_socket_strerror(int e);
  153. #else
  154. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  155. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  156. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  157. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  158. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  159. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  160. #define tor_socket_errno(sock) (errno)
  161. #define tor_socket_strerror(e) strerror(e)
  162. #endif
  163. /* ===== OS compatibility */
  164. const char *get_uname(void);
  165. /* Some platforms segfault when you try to access a multi-byte type
  166. * that isn't aligned to a word boundary. The macros and/or functions
  167. * below can be used to access unaligned data on any platform.
  168. */
  169. #ifdef UNALIGNED_INT_ACCESS_OK
  170. #define get_uint16(cp) (*(uint16_t*)(cp))
  171. #define get_uint32(cp) (*(uint32_t*)(cp))
  172. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  173. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  174. #else
  175. uint16_t get_uint16(const char *cp);
  176. uint32_t get_uint32(const char *cp);
  177. void set_uint16(char *cp, uint16_t v);
  178. void set_uint32(char *cp, uint32_t v);
  179. #endif
  180. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  181. int switch_id(char *user, char *group);
  182. #ifdef HAVE_PWD_H
  183. char *get_user_homedir(const char *username);
  184. #endif
  185. int spawn_func(int (*func)(void *), void *data);
  186. void spawn_exit(void);
  187. #if defined(ENABLE_THREADS) && defined(MS_WINDOWS)
  188. #define USE_WIN32_THREADS
  189. #define TOR_IS_MULTITHREADED 1
  190. #elif defined(ENABLE_THREADS) && defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  191. #define USE_PTHREADS
  192. #define TOR_IS_MULTITHREADED 1
  193. #else
  194. #undef TOR_IS_MULTITHREADED
  195. #endif
  196. /* Because we use threads instead of processes on Windows, we need locking on
  197. * Windows. On Unixy platforms, these functions are no-ops. */
  198. typedef struct tor_mutex_t tor_mutex_t;
  199. #ifdef TOR_IS_MULTITHREADED
  200. tor_mutex_t *tor_mutex_new(void);
  201. void tor_mutex_acquire(tor_mutex_t *m);
  202. void tor_mutex_release(tor_mutex_t *m);
  203. void tor_mutex_free(tor_mutex_t *m);
  204. unsigned long tor_get_thread_id(void);
  205. #else
  206. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  207. #define tor_mutex_acquire(m) do { } while (0)
  208. #define tor_mutex_release(m) do { } while (0)
  209. #define tor_mutex_free(m) do { tor_free(m); } while (0)
  210. #define tor_get_thread_id() (1UL)
  211. #endif
  212. #endif