util.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. size_t strlcat(char *dst, const char *src, size_t siz);
  31. size_t strlcpy(char *dst, const char *src, size_t siz);
  32. void *tor_malloc(size_t size);
  33. void *tor_malloc_zero(size_t size);
  34. void *tor_realloc(void *ptr, size_t size);
  35. char *tor_strdup(const char *s);
  36. char *tor_strndup(const char *s, size_t n);
  37. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  38. typedef struct {
  39. void **list;
  40. int num_used;
  41. int max;
  42. } smartlist_t;
  43. smartlist_t *smartlist_create(int max_elements);
  44. void smartlist_free(smartlist_t *sl);
  45. void smartlist_add(smartlist_t *sl, void *element);
  46. void smartlist_remove(smartlist_t *sl, void *element);
  47. int smartlist_isin(smartlist_t *sl, void *element);
  48. int smartlist_overlap(smartlist_t *sl1, smartlist_t *sl2);
  49. void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2);
  50. void smartlist_subtract(smartlist_t *sl1, smartlist_t *sl2);
  51. void *smartlist_choose(smartlist_t *sl);
  52. /* Map from const char * to void*. Implemented with a splay tree. */
  53. typedef struct strmap_t strmap_t;
  54. typedef struct strmap_entry_t strmap_entry_t;
  55. typedef struct strmap_entry_t strmap_iter_t;
  56. strmap_t* strmap_new(void);
  57. void* strmap_set(strmap_t *map, const char *key, void *val);
  58. void* strmap_get(strmap_t *map, const char *key);
  59. void* strmap_remove(strmap_t *map, const char *key);
  60. void strmap_foreach(strmap_t *map,
  61. void* (*fn)(const char *key, void *val, void *data),
  62. void *data);
  63. void strmap_free(strmap_t *map, void (*free_val)(void*));
  64. strmap_iter_t *strmap_iter_init(strmap_t *map);
  65. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  66. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  67. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  68. int strmap_iter_done(strmap_iter_t *iter);
  69. const char *eat_whitespace(const char *s);
  70. const char *eat_whitespace_no_nl(const char *s);
  71. const char *find_whitespace(const char *s);
  72. void tor_gettimeofday(struct timeval *timeval);
  73. long tv_udiff(struct timeval *start, struct timeval *end);
  74. void tv_addms(struct timeval *a, long ms);
  75. void tv_add(struct timeval *a, struct timeval *b);
  76. int tv_cmp(struct timeval *a, struct timeval *b);
  77. time_t tor_timegm (struct tm *tm);
  78. int write_all(int fd, const char *buf, size_t count, int isSocket);
  79. int read_all(int fd, char *buf, size_t count, int isSocket);
  80. void set_socket_nonblocking(int socket);
  81. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  82. file_status_t file_status(const char *filename);
  83. int check_private_dir(const char *dirname, int create);
  84. int write_str_to_file(const char *fname, const char *str);
  85. char *read_file_to_str(const char *filename);
  86. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  87. int spawn_func(int (*func)(void *), void *data);
  88. void spawn_exit();
  89. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  90. int is_internal_IP(uint32_t ip);
  91. const char *get_uname(void);
  92. /* Start putting the process into daemon mode: fork and drop all resources
  93. * except standard fds. The parent process never returns, but stays around
  94. * until finish_daemon is called. (Note: it's safe to call this more
  95. * than once: calls after the first are ignored.)
  96. */
  97. void start_daemon(char *desired_cwd);
  98. /* Finish putting the process into daemon mode: drop standard fds, and tell
  99. * the parent process to exit. (Note: it's safe to call this more than once:
  100. * calls after the first are ignored. Calls start_daemon first if it hasn't
  101. * been called already.)
  102. */
  103. void finish_daemon(void);
  104. void write_pidfile(char *filename);
  105. int switch_id(char *user, char *group);
  106. struct in_addr;
  107. int tor_inet_aton(const char *cp, struct in_addr *addr);
  108. /* For stupid historical reasons, windows sockets have an independent set of
  109. * errnos which they use as the fancy strikes them.
  110. */
  111. #ifdef MS_WINDOWS
  112. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  113. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  114. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  115. int correct_socket_errno(int s);
  116. #else
  117. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  118. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  119. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  120. #define correct_socket_errno(s) (errno)
  121. #endif
  122. #endif