util.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __UTIL_H
  5. #define __UTIL_H
  6. #include "orconfig.h"
  7. #ifdef HAVE_SYS_TIME_H
  8. #include <sys/time.h>
  9. #endif
  10. #ifdef HAVE_TIME_H
  11. #include <time.h>
  12. #endif
  13. #if _MSC_VER > 1300
  14. #include <winsock2.h>
  15. #include <ws2tcpip.h>
  16. #elif defined(_MSC_VER)
  17. #include <winsock.h>
  18. #endif
  19. #include <errno.h>
  20. #ifndef HAVE_GETTIMEOFDAY
  21. #ifdef HAVE_FTIME
  22. #define USING_FAKE_TIMEVAL
  23. #include <sys/timeb.h>
  24. #define timeval timeb
  25. #define tv_sec time
  26. #define tv_usec millitm
  27. #endif
  28. #endif
  29. #ifdef MS_WINDOWS
  30. /* Windows names string functions funnily. */
  31. #define strncasecmp strnicmp
  32. #define strcasecmp stricmp
  33. #define INLINE __inline
  34. #else
  35. #define INLINE inline
  36. #endif
  37. void *tor_malloc(size_t size);
  38. /* Same as gettimeofday, but no need to check exit value. */
  39. void my_gettimeofday(struct timeval *timeval);
  40. /* Returns the number of microseconds between start and end. Requires that
  41. * end >= start, and that the number of microseconds < LONG_MAX. */
  42. long tv_udiff(struct timeval *start, struct timeval *end);
  43. void tv_addms(struct timeval *a, long ms);
  44. void tv_add(struct timeval *a, struct timeval *b);
  45. int tv_cmp(struct timeval *a, struct timeval *b);
  46. void set_socket_nonblocking(int socket);
  47. /* Minimalist interface to run a void function in the background. On
  48. unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  49. func should not return, but rather should call spawn_exit.
  50. */
  51. int spawn_func(int (*func)(void *), void *data);
  52. void spawn_exit();
  53. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  54. /* For stupid historical reasons, windows sockets have an independent set of
  55. * errnos which they use as the fancy strikes them.
  56. */
  57. #ifdef MS_WINDOWS
  58. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  59. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  60. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  61. int correct_socket_errno(int s);
  62. #else
  63. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  64. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  65. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  66. #define correct_socket_errno(s) (errno)
  67. #endif
  68. #endif