compat.h 5.7 KB

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