compat.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /* ===== Time compatibility */
  75. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  76. struct timeval {
  77. time_t tv_sec;
  78. unsigned int tv_usec;
  79. };
  80. #endif
  81. void tor_gettimeofday(struct timeval *timeval);
  82. /* ===== File compatibility */
  83. int replace_file(const char *from, const char *to);
  84. /* ===== Net compatibility */
  85. #ifdef MS_WINDOWS
  86. /** On windows, you have to call close() on fds returned by open(),
  87. * and closesocket() on fds returned by socket(). On Unix, everything
  88. * gets close()'d. We abstract this difference by always using
  89. * tor_close_socket to close sockets, and always using close() on
  90. * files.
  91. */
  92. #define tor_close_socket(s) closesocket(s)
  93. #else
  94. #define tor_close_socket(s) close(s)
  95. #endif
  96. struct in_addr;
  97. int tor_inet_aton(const char *cp, struct in_addr *addr);
  98. void set_socket_nonblocking(int socket);
  99. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  100. /* For stupid historical reasons, windows sockets have an independent
  101. * set of errnos, and an independent way to get them. Also, you can't
  102. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  103. * errnos against expected values, and use tor_socket_errno to find
  104. * the actual errno after a socket operation fails.
  105. */
  106. #ifdef MS_WINDOWS
  107. /** Return true if e is EAGAIN or the local equivalent. */
  108. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  109. /** Return true if e is EINPROGRESS or the local equivalent. */
  110. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  111. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  112. * a call to connect(). */
  113. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  114. /** Return true if e is EAGAIN or another error indicating that a call to
  115. * accept() has no pending connections to return. */
  116. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  117. /** Return true if e is EMFILE or another error indicating that a call to
  118. * accept() has failed because we're out of fds or something. */
  119. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  120. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  121. int tor_socket_errno(int sock);
  122. const char *tor_socket_strerror(int e);
  123. #else
  124. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  125. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  126. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  127. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  128. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  129. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  130. #define tor_socket_errno(sock) (errno)
  131. #define tor_socket_strerror(e) strerror(e)
  132. #endif
  133. /* ===== OS compatibility */
  134. const char *get_uname(void);
  135. /* Some platforms segfault when you try to access a multi-byte type
  136. * that isn't aligned to a word boundary. The macros and/or functions
  137. * below can be used to access unaligned data on any platform.
  138. */
  139. #ifdef UNALIGNED_INT_ACCESS_OK
  140. #define get_uint16(cp) (*(uint16_t*)(cp))
  141. #define get_uint32(cp) (*(uint32_t*)(cp))
  142. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  143. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  144. #else
  145. uint16_t get_uint16(const char *cp);
  146. uint32_t get_uint32(const char *cp);
  147. void set_uint16(char *cp, uint16_t v);
  148. void set_uint32(char *cp, uint32_t v);
  149. #endif
  150. int set_max_file_descriptors(unsigned int required_min);
  151. int switch_id(char *user, char *group);
  152. #ifdef HAVE_PWD_H
  153. char *get_user_homedir(const char *username);
  154. #endif
  155. int spawn_func(int (*func)(void *), void *data);
  156. void spawn_exit(void);
  157. /* Because we use threads instead of processes on Windows, we need locking on
  158. * Windows. On Unixy platforms, these functions are no-ops. */
  159. typedef struct tor_mutex_t tor_mutex_t;
  160. tor_mutex_t *tor_mutex_new(void);
  161. void tor_mutex_acquire(tor_mutex_t *m);
  162. void tor_mutex_release(tor_mutex_t *m);
  163. void tor_mutex_free(tor_mutex_t *m);
  164. #endif