compat.h 5.7 KB

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