util.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file util.h
  6. * \brief Headers for util.c
  7. **/
  8. #ifndef __UTIL_H
  9. #define __UTIL_H
  10. #include "orconfig.h"
  11. #include "torint.h"
  12. #include <stdio.h>
  13. #ifdef HAVE_SYS_TIME_H
  14. #include <sys/time.h>
  15. #endif
  16. #ifdef HAVE_TIME_H
  17. #include <time.h>
  18. #endif
  19. #if _MSC_VER > 1300
  20. #include <winsock2.h>
  21. #include <ws2tcpip.h>
  22. #elif defined(_MSC_VER)
  23. #include <winsock.h>
  24. #endif
  25. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  26. struct timeval {
  27. time_t tv_sec;
  28. unsigned int tv_usec;
  29. };
  30. #endif
  31. #ifdef MS_WINDOWS
  32. /* Windows names string functions differently from most other platforms. */
  33. #define strncasecmp strnicmp
  34. #define strcasecmp stricmp
  35. /* "inline" is __inline on windows. " */
  36. #define INLINE __inline
  37. /* Windows compilers before VC7 don't have __FUNCTION__. */
  38. #if _MSC_VER < 1300
  39. #define __FUNCTION__ "???"
  40. #endif
  41. #else
  42. #define INLINE inline
  43. #endif
  44. /** Replace assert() with a variant that sends failures to the log before
  45. * calling assert() normally.
  46. */
  47. #ifdef NDEBUG
  48. #define tor_assert(expr) do {} while(0)
  49. #else
  50. #define tor_assert(expr) do { \
  51. if (!(expr)) { \
  52. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  53. __FILE__, __LINE__, __FUNCTION__, #expr); \
  54. assert(expr); /* write to console too. */ \
  55. abort(); /* unreached */ \
  56. } } while (0)
  57. #endif
  58. #ifdef MS_WINDOWS
  59. /** On windows, you have to call close() on fds returned by open(),
  60. * and closesocket() on fds returned by socket(). On Unix, everything
  61. * gets close()'d. We abstract this difference by always using
  62. * tor_close_socket to close sockets, and always using close() on
  63. * files.
  64. */
  65. #define tor_close_socket(s) closesocket(s)
  66. #else
  67. #define tor_close_socket(s) close(s)
  68. #endif
  69. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  70. size_t strlcat(char *dst, const char *src, size_t siz);
  71. size_t strlcpy(char *dst, const char *src, size_t siz);
  72. void *tor_malloc(size_t size);
  73. void *tor_malloc_zero(size_t size);
  74. void *tor_realloc(void *ptr, size_t size);
  75. char *tor_strdup(const char *s);
  76. char *tor_strndup(const char *s, size_t n);
  77. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  78. void tor_strlower(char *s);
  79. int strcmpstart(const char *s1, const char *s2);
  80. int tor_strstrip(char *s, const char *strip);
  81. typedef enum {
  82. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  83. } part_finish_rule_t;
  84. int tor_strpartition(char *dest, size_t dest_len,
  85. const char *s, const char *insert, size_t n,
  86. part_finish_rule_t rule);
  87. long tor_parse_long(const char *s, int base, long min,
  88. long max, int *ok, char **next);
  89. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  90. unsigned long max, int *ok, char **next);
  91. /* Some platforms segfault when you try to access a multi-byte type
  92. * that isn't aligned to a word boundary. The macros and/or functions
  93. * below can be used to access unaligned data on any platform.
  94. */
  95. #ifdef UNALIGNED_INT_ACCESS_OK
  96. #define get_uint16(cp) (*(uint16_t*)(cp))
  97. #define get_uint32(cp) (*(uint32_t*)(cp))
  98. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  99. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  100. #else
  101. uint16_t get_uint16(const char *cp);
  102. uint32_t get_uint32(const char *cp);
  103. void set_uint16(char *cp, uint16_t v);
  104. void set_uint32(char *cp, uint32_t v);
  105. #endif
  106. const char *hex_str(const char *from, size_t fromlen);
  107. /** Generic resizeable array. */
  108. typedef struct smartlist_t smartlist_t;
  109. smartlist_t *smartlist_create();
  110. void smartlist_free(smartlist_t *sl);
  111. void smartlist_set_capacity(smartlist_t *sl, int n);
  112. void smartlist_clear(smartlist_t *sl);
  113. void smartlist_truncate(smartlist_t *sl, int n);
  114. void smartlist_add(smartlist_t *sl, void *element);
  115. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  116. void smartlist_remove(smartlist_t *sl, void *element);
  117. int smartlist_isin(const smartlist_t *sl, void *element);
  118. int smartlist_string_isin(const smartlist_t *sl, const char *element);
  119. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  120. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  121. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  122. void *smartlist_choose(const smartlist_t *sl);
  123. void *smartlist_get(const smartlist_t *sl, int idx);
  124. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  125. void *smartlist_del(smartlist_t *sl, int idx);
  126. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  127. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  128. int smartlist_len(const smartlist_t *sl);
  129. #define SPLIT_SKIP_SPACE 0x01
  130. #define SPLIT_IGNORE_BLANK 0x02
  131. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  132. int flags, int max);
  133. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate);
  134. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  135. do { \
  136. int sl_idx, sl_len=smartlist_len(sl); \
  137. type var; \
  138. for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
  139. var = smartlist_get((sl),sl_idx); \
  140. do {cmd;} while(0); \
  141. } } while (0)
  142. /* Map from const char * to void*. Implemented with a splay tree. */
  143. typedef struct strmap_t strmap_t;
  144. typedef struct strmap_entry_t strmap_entry_t;
  145. typedef struct strmap_entry_t strmap_iter_t;
  146. strmap_t* strmap_new(void);
  147. void* strmap_set(strmap_t *map, const char *key, void *val);
  148. void* strmap_get(strmap_t *map, const char *key);
  149. void* strmap_remove(strmap_t *map, const char *key);
  150. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  151. void* strmap_get_lc(strmap_t *map, const char *key);
  152. void* strmap_remove_lc(strmap_t *map, const char *key);
  153. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  154. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  155. void strmap_free(strmap_t *map, void (*free_val)(void*));
  156. int strmap_isempty(strmap_t *map);
  157. strmap_iter_t *strmap_iter_init(strmap_t *map);
  158. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  159. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  160. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  161. int strmap_iter_done(strmap_iter_t *iter);
  162. /* String manipulation */
  163. const char *eat_whitespace(const char *s);
  164. const char *eat_whitespace_no_nl(const char *s);
  165. const char *find_whitespace(const char *s);
  166. /* Time helpers */
  167. void tor_gettimeofday(struct timeval *timeval);
  168. long tv_udiff(struct timeval *start, struct timeval *end);
  169. void tv_addms(struct timeval *a, long ms);
  170. void tv_add(struct timeval *a, struct timeval *b);
  171. int tv_cmp(struct timeval *a, struct timeval *b);
  172. time_t tor_timegm(struct tm *tm);
  173. #define RFC1123_TIME_LEN 29
  174. void format_rfc1123_time(char *buf, time_t t);
  175. int parse_rfc1123_time(const char *buf, time_t *t);
  176. #define ISO_TIME_LEN 19
  177. void format_iso_time(char *buf, time_t t);
  178. int parse_iso_time(const char *buf, time_t *t);
  179. int write_all(int fd, const char *buf, size_t count, int isSocket);
  180. int read_all(int fd, char *buf, size_t count, int isSocket);
  181. void set_socket_nonblocking(int socket);
  182. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  183. file_status_t file_status(const char *filename);
  184. int check_private_dir(const char *dirname, int create);
  185. int write_str_to_file(const char *fname, const char *str, int bin);
  186. char *read_file_to_str(const char *filename, int bin);
  187. int parse_line_from_file(char *line, size_t maxlen, FILE *f, char **key_out, char **value_out);
  188. char *expand_filename(const char *filename);
  189. int replace_file(const char *from, const char *to);
  190. int spawn_func(int (*func)(void *), void *data);
  191. void spawn_exit();
  192. /* Because we use threads instead of processes on Windows, we need locking on Windows.
  193. * On Unixy platforms, these functions are no-ops. */
  194. typedef struct tor_mutex_t tor_mutex_t;
  195. tor_mutex_t *tor_mutex_new(void);
  196. void tor_mutex_acquire(tor_mutex_t *m);
  197. void tor_mutex_release(tor_mutex_t *m);
  198. void tor_mutex_free(tor_mutex_t *m);
  199. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  200. int is_internal_IP(uint32_t ip);
  201. int is_local_IP(uint32_t ip);
  202. const char *get_uname(void);
  203. void start_daemon(const char *desired_cwd);
  204. void finish_daemon(void);
  205. void write_pidfile(char *filename);
  206. int switch_id(char *user, char *group);
  207. struct in_addr;
  208. int tor_inet_aton(const char *cp, struct in_addr *addr);
  209. int tor_lookup_hostname(const char *name, uint32_t *addr);
  210. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  211. uint16_t *port);
  212. /* For stupid historical reasons, windows sockets have an independent
  213. * set of errnos, and an independent way to get them. Also, you can't
  214. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  215. * errnos against expected values, and use tor_socket_errno to find
  216. * the actual errno after a socket operation fails.
  217. */
  218. #ifdef MS_WINDOWS
  219. /** Return true if e is EAGAIN or the local equivalent. */
  220. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  221. /** Return true if e is EINPROGRESS or the local equivalent. */
  222. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  223. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  224. * a call to connect(). */
  225. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
  226. int tor_socket_errno(int sock);
  227. const char *tor_socket_strerror(int e);
  228. #else
  229. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  230. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  231. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  232. #define tor_socket_errno(sock) (errno)
  233. #define tor_socket_strerror(e) strerror(e)
  234. #endif
  235. #endif
  236. /*
  237. Local Variables:
  238. mode:c
  239. indent-tabs-mode:nil
  240. c-basic-offset:2
  241. End:
  242. */