compat.h 6.1 KB

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