util.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. int write_all(int fd, const void *buf, size_t count);
  47. void set_socket_nonblocking(int socket);
  48. /* Minimalist interface to run a void function in the background. On
  49. unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  50. func should not return, but rather should call spawn_exit.
  51. */
  52. int spawn_func(int (*func)(void *), void *data);
  53. void spawn_exit();
  54. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  55. /* For stupid historical reasons, windows sockets have an independent set of
  56. * errnos which they use as the fancy strikes them.
  57. */
  58. #ifdef MS_WINDOWS
  59. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  60. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  61. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  62. int correct_socket_errno(int s);
  63. #else
  64. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  65. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  66. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  67. #define correct_socket_errno(s) (errno)
  68. #endif
  69. #endif