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 "../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. char *tor_strdup(const char *s);
  32. void tor_gettimeofday(struct timeval *timeval);
  33. long tv_udiff(struct timeval *start, struct timeval *end);
  34. void tv_addms(struct timeval *a, long ms);
  35. void tv_add(struct timeval *a, struct timeval *b);
  36. int tv_cmp(struct timeval *a, struct timeval *b);
  37. int write_all(int fd, const void *buf, size_t count);
  38. int read_all(int fd, void *buf, size_t count);
  39. void set_socket_nonblocking(int socket);
  40. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  41. file_status_t file_status(const char *filename);
  42. int check_private_dir(const char *dirname, int create);
  43. int write_str_to_file(const char *fname, const char *str);
  44. char *read_file_to_str(const char *filename);
  45. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  46. int spawn_func(int (*func)(void *), void *data);
  47. void spawn_exit();
  48. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  49. const char *get_uname(void);
  50. /* For stupid historical reasons, windows sockets have an independent set of
  51. * errnos which they use as the fancy strikes them.
  52. */
  53. #ifdef MS_WINDOWS
  54. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  55. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  56. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  57. int correct_socket_errno(int s);
  58. #else
  59. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  60. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  61. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  62. #define correct_socket_errno(s) (errno)
  63. #endif
  64. #endif