util.h 2.3 KB

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