util.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __UTIL_H
  5. #define __UTIL_H
  6. #include "../or/or.h"
  7. #if _MSC_VER > 1300
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #elif defined(_MSC_VER)
  11. #include <winsock.h>
  12. #endif
  13. #ifndef HAVE_GETTIMEOFDAY
  14. #ifdef HAVE_FTIME
  15. #define USING_FAKE_TIMEVAL
  16. #include <sys/timeb.h>
  17. #define timeval timeb
  18. #define tv_sec time
  19. #define tv_usec millitm
  20. #endif
  21. #endif
  22. #ifdef MS_WINDOWS
  23. /* Windows names string functions funnily. */
  24. #define strncasecmp strnicmp
  25. #define strcasecmp stricmp
  26. #define INLINE __inline
  27. #else
  28. #define INLINE inline
  29. #endif
  30. void *tor_malloc(size_t size);
  31. /* Same as gettimeofday, but no need to check exit value. */
  32. void my_gettimeofday(struct timeval *timeval);
  33. /* Returns the number of microseconds between start and end. Requires that
  34. * end >= start, and that the number of microseconds < LONG_MAX. */
  35. long tv_udiff(struct timeval *start, struct timeval *end);
  36. void tv_addms(struct timeval *a, long ms);
  37. void tv_add(struct timeval *a, struct timeval *b);
  38. int tv_cmp(struct timeval *a, struct timeval *b);
  39. int write_all(int fd, const void *buf, size_t count);
  40. int read_all(int fd, void *buf, size_t count);
  41. void set_socket_nonblocking(int socket);
  42. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  43. /* Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
  44. * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
  45. * directory. */
  46. file_status_t file_status(const char *filename);
  47. /* Check whether dirname exists and is private. If yes returns
  48. * 0. Else returns -1.
  49. */
  50. int check_private_dir(const char *dirname, int create);
  51. int write_str_to_file(const char *fname, const char *str);
  52. char *read_file_to_str(const char *filename);
  53. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  54. /* Minimalist interface to run a void function in the background. On
  55. unix calls fork, on win32 calls beginthread. Returns -1 on failure.
  56. func should not return, but rather should call spawn_exit.
  57. */
  58. int spawn_func(int (*func)(void *), void *data);
  59. void spawn_exit();
  60. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  61. const char *get_uname(void);
  62. /* For stupid historical reasons, windows sockets have an independent set of
  63. * errnos which they use as the fancy strikes them.
  64. */
  65. #ifdef MS_WINDOWS
  66. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  67. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  68. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  69. int correct_socket_errno(int s);
  70. #else
  71. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  72. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  73. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  74. #define correct_socket_errno(s) (errno)
  75. #endif
  76. #endif