compat.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* Now that we use libevent, all real sockets are safe for polling ... or
  119. * if they aren't, libevent will help us. */
  120. #define SOCKET_IS_POLLABLE(fd) ((fd)>=0)
  121. struct in_addr;
  122. int tor_inet_aton(const char *cp, struct in_addr *addr);
  123. int tor_lookup_hostname(const char *name, uint32_t *addr);
  124. void set_socket_nonblocking(int socket);
  125. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  126. int network_init(void);
  127. /* For stupid historical reasons, windows sockets have an independent
  128. * set of errnos, and an independent way to get them. Also, you can't
  129. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  130. * errnos against expected values, and use tor_socket_errno to find
  131. * the actual errno after a socket operation fails.
  132. */
  133. #ifdef MS_WINDOWS
  134. /** Return true if e is EAGAIN or the local equivalent. */
  135. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  136. /** Return true if e is EINPROGRESS or the local equivalent. */
  137. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  138. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  139. * a call to connect(). */
  140. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  141. /** Return true if e is EAGAIN or another error indicating that a call to
  142. * accept() has no pending connections to return. */
  143. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  144. /** Return true if e is EMFILE or another error indicating that a call to
  145. * accept() has failed because we're out of fds or something. */
  146. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  147. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  148. int tor_socket_errno(int sock);
  149. const char *tor_socket_strerror(int e);
  150. #else
  151. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  152. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  153. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  154. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  155. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  156. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  157. #define tor_socket_errno(sock) (errno)
  158. #define tor_socket_strerror(e) strerror(e)
  159. #endif
  160. /* ===== OS compatibility */
  161. const char *get_uname(void);
  162. /* Some platforms segfault when you try to access a multi-byte type
  163. * that isn't aligned to a word boundary. The macros and/or functions
  164. * below can be used to access unaligned data on any platform.
  165. */
  166. #ifdef UNALIGNED_INT_ACCESS_OK
  167. #define get_uint16(cp) (*(uint16_t*)(cp))
  168. #define get_uint32(cp) (*(uint32_t*)(cp))
  169. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  170. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  171. #else
  172. uint16_t get_uint16(const char *cp);
  173. uint32_t get_uint32(const char *cp);
  174. void set_uint16(char *cp, uint16_t v);
  175. void set_uint32(char *cp, uint32_t v);
  176. #endif
  177. int set_max_file_descriptors(unsigned long limit, unsigned long cap);
  178. int switch_id(char *user, char *group);
  179. #ifdef HAVE_PWD_H
  180. char *get_user_homedir(const char *username);
  181. #endif
  182. int spawn_func(int (*func)(void *), void *data);
  183. void spawn_exit(void);
  184. #if defined(MS_WINDOWS)
  185. #define USE_WIN32_THREADS
  186. #define TOR_IS_MULTITHREADED 1
  187. #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
  188. #define USE_PTHREADS
  189. #define TOR_IS_MULTITHREADED 1
  190. #else
  191. #undef TOR_IS_MULTITHREADED
  192. #endif
  193. /* Because we use threads instead of processes on Windows, we need locking on
  194. * Windows. On Unixy platforms, these functions are no-ops. */
  195. typedef struct tor_mutex_t tor_mutex_t;
  196. #ifdef TOR_IS_MULTITHREADED
  197. tor_mutex_t *tor_mutex_new(void);
  198. void tor_mutex_acquire(tor_mutex_t *m);
  199. void tor_mutex_release(tor_mutex_t *m);
  200. void tor_mutex_free(tor_mutex_t *m);
  201. unsigned long tor_get_thread_id(void);
  202. #else
  203. #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
  204. #define tor_mutex_acquire(m) do { } while (0)
  205. #define tor_mutex_release(m) do { } while (0)
  206. #define tor_mutex_free(m) do { tor_free(m); } while (0)
  207. #define tor_get_thread_id() (1UL)
  208. #endif
  209. #endif