compat.h 5.8 KB

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