util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright 2003-2004 Roger Dingledine
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. /**
  6. * \file util.h
  7. * \brief Headers for util.c
  8. **/
  9. #ifndef __UTIL_H
  10. #define __UTIL_H
  11. #define UTIL_H_ID "$Id$"
  12. #include "orconfig.h"
  13. #include "torint.h"
  14. #include "compat.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. /* Replace assert() with a variant that sends failures to the log before
  18. * calling assert() normally.
  19. */
  20. #ifdef NDEBUG
  21. /* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
  22. * be outside the critical path anyway, so it's silly to disable bugchecking
  23. * throughout the entire program just because a few asserts are slowing you
  24. * down. Profile, optimize the critical path, and keep debugging on.
  25. *
  26. * And I'm not just saying that because some of our asserts check
  27. * security-critical properties.
  28. */
  29. #error "Sorry; we don't support building with NDEBUG."
  30. #else
  31. #ifdef __GNUC__
  32. /* Give an int-valued version of !x that won't confuse PREDICT_UNLIKELY. */
  33. #define IS_FALSE_AS_INT(x) ((x) == ((typeof(x)) 0))
  34. #else
  35. #define IS_FALSE_AS_INT(x) !(x)
  36. #endif
  37. /** Like assert(3), but send assertion failures to the log as well as to
  38. * stderr. */
  39. #define tor_assert(expr) STMT_BEGIN \
  40. if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \
  41. log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
  42. _SHORT_FILE_, __LINE__, __func__, #expr); \
  43. fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
  44. _SHORT_FILE_, __LINE__, __func__, #expr); \
  45. abort(); \
  46. } STMT_END
  47. #endif
  48. #ifdef USE_DMALLOC
  49. #define DMALLOC_PARAMS , const char *file, const int line
  50. #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
  51. #else
  52. #define DMALLOC_PARAMS
  53. #define DMALLOC_ARGS
  54. #endif
  55. /** Define this if you want Tor to crash when any problem comes up,
  56. * so you can get a coredump and track things down. */
  57. // #define tor_fragile_assert() tor_assert(0)
  58. #define tor_fragile_assert()
  59. /* Memory management */
  60. void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
  61. void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
  62. void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
  63. char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
  64. char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
  65. ATTR_MALLOC ATTR_NONNULL((1));
  66. void *_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
  67. ATTR_MALLOC ATTR_NONNULL((1));
  68. void _tor_free(void *mem);
  69. #ifdef USE_DMALLOC
  70. extern int dmalloc_free(const char *file, const int line, void *pnt,
  71. const int func_id);
  72. #define tor_free(p) STMT_BEGIN \
  73. if (PREDICT_LIKELY((p)!=NULL)) { \
  74. dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
  75. (p)=NULL; \
  76. } \
  77. STMT_END
  78. #else
  79. #define tor_free(p) STMT_BEGIN \
  80. if (PREDICT_LIKELY((p)!=NULL)) { \
  81. free(p); \
  82. (p)=NULL; \
  83. } \
  84. STMT_END
  85. #endif
  86. #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
  87. #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
  88. #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
  89. #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
  90. #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
  91. #define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
  92. void tor_log_mallinfo(int severity);
  93. /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
  94. #if defined(__GNUC__) && __GNUC__ > 3
  95. #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
  96. #else
  97. #define STRUCT_OFFSET(tp, member) \
  98. ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  99. #endif
  100. /** Macro: yield a pointer to the field at position <b>off</b> within the
  101. * structure <b>st</b>. Example:
  102. * <pre>
  103. * struct a { int foo; int bar; } x;
  104. * off_t bar_offset = STRUCT_OFFSET(struct a, bar);
  105. * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
  106. * *bar_p = 3;
  107. * </pre>
  108. */
  109. #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
  110. /** Macro: yield a pointer to an enclosing structure given a pointer to
  111. * a substructure at offset <b>off</b>. Example:
  112. * <pre>
  113. * struct base { ... };
  114. * struct subtype { int x; struct base b; } x;
  115. * struct base *bp = &x.base;
  116. * struct *sp = SUBTYPE_P(bp, struct subtype, b);
  117. * </pre>
  118. */
  119. #define SUBTYPE_P(p, subtype, basemember) \
  120. ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
  121. /* Logic */
  122. /** Macro: true if two values have the same boolean value. */
  123. #define bool_eq(a,b) (!(a)==!(b))
  124. /** Macro: true if two values have different boolean values. */
  125. #define bool_neq(a,b) (!(a)!=!(b))
  126. /* Math functions */
  127. int tor_log2(uint64_t u64) ATTR_CONST;
  128. uint64_t round_to_power_of_2(uint64_t u64);
  129. /* String manipulation */
  130. /** Allowable characters in a hexadecimal string. */
  131. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  132. void tor_strlower(char *s) ATTR_NONNULL((1));
  133. void tor_strupper(char *s) ATTR_NONNULL((1));
  134. int tor_strisprint(const char *s) ATTR_PURE ATTR_NONNULL((1));
  135. int tor_strisnonupper(const char *s) ATTR_PURE ATTR_NONNULL((1));
  136. int strcmpstart(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
  137. int strcmp_len(const char *s1, const char *s2, size_t len)
  138. ATTR_PURE ATTR_NONNULL((1,2));
  139. int strcasecmpstart(const char *s1, const char *s2)
  140. ATTR_PURE ATTR_NONNULL((1,2));
  141. int strcmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
  142. int strcasecmpend(const char *s1, const char *s2)
  143. ATTR_PURE ATTR_NONNULL((1,2));
  144. int tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
  145. int tor_strpartition(char *dest, size_t dest_len,
  146. const char *s, const char *insert, size_t n);
  147. long tor_parse_long(const char *s, int base, long min,
  148. long max, int *ok, char **next);
  149. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  150. unsigned long max, int *ok, char **next);
  151. uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
  152. uint64_t max, int *ok, char **next);
  153. const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
  154. const char *eat_whitespace(const char *s) ATTR_PURE;
  155. const char *eat_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
  156. const char *eat_whitespace_no_nl(const char *s) ATTR_PURE;
  157. const char *eat_whitespace_eos_no_nl(const char *s, const char *eos) ATTR_PURE;
  158. const char *find_whitespace(const char *s) ATTR_PURE;
  159. const char *find_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
  160. int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
  161. int tor_digest_is_zero(const char *digest) ATTR_PURE;
  162. char *esc_for_log(const char *string) ATTR_MALLOC;
  163. const char *escaped(const char *string);
  164. struct smartlist_t;
  165. void wrap_string(struct smartlist_t *out, const char *string, size_t width,
  166. const char *prefix0, const char *prefixRest);
  167. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  168. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  169. /* Time helpers */
  170. long tv_udiff(const struct timeval *start, const struct timeval *end);
  171. void tv_addms(struct timeval *a, long ms);
  172. void tv_add(struct timeval *a, const struct timeval *b);
  173. int tv_cmp(const struct timeval *a, const struct timeval *b);
  174. time_t tor_timegm(struct tm *tm);
  175. #define RFC1123_TIME_LEN 29
  176. void format_rfc1123_time(char *buf, time_t t);
  177. int parse_rfc1123_time(const char *buf, time_t *t);
  178. #define ISO_TIME_LEN 19
  179. void format_local_iso_time(char *buf, time_t t);
  180. void format_iso_time(char *buf, time_t t);
  181. int parse_iso_time(const char *buf, time_t *t);
  182. int parse_http_time(const char *buf, struct tm *tm);
  183. /* File helpers */
  184. int write_all(int fd, const char *buf, size_t count, int isSocket);
  185. int read_all(int fd, char *buf, size_t count, int isSocket);
  186. /** Return values from file_status(); see that function's documentation
  187. * for details. */
  188. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR } file_status_t;
  189. file_status_t file_status(const char *filename);
  190. /** Possible behaviors for check_private_dir() on encountering a nonexistent
  191. * directory; see that function's documentation for details. */
  192. typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
  193. int check_private_dir(const char *dirname, cpd_check_t check);
  194. int write_str_to_file(const char *fname, const char *str, int bin);
  195. int write_bytes_to_file(const char *fname, const char *str, size_t len,
  196. int bin);
  197. /** An ad-hoc type to hold a string of characters and a count; used by
  198. * write_chunks_to_file. */
  199. typedef struct sized_chunk_t {
  200. const char *bytes;
  201. size_t len;
  202. } sized_chunk_t;
  203. int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
  204. int bin);
  205. int append_bytes_to_file(const char *fname, const char *str, size_t len,
  206. int bin);
  207. /** Flag for read_file_to_str: open the file in binary mode. */
  208. #define RFTS_BIN 1
  209. /** Flag for read_file_to_str: it's okay if the file doesn't exist. */
  210. #define RFTS_IGNORE_MISSING 2
  211. struct stat;
  212. char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  213. ATTR_MALLOC;
  214. char *parse_line_from_str(char *line, char **key_out, char **value_out);
  215. char *expand_filename(const char *filename);
  216. struct smartlist_t *tor_listdir(const char *dirname);
  217. int path_is_relative(const char *filename) ATTR_PURE;
  218. /* Net helpers */
  219. int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
  220. int parse_addr_port(int severity, const char *addrport, char **address,
  221. uint32_t *addr, uint16_t *port_out);
  222. int parse_port_range(const char *port, uint16_t *port_min_out,
  223. uint16_t *port_max_out);
  224. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  225. maskbits_t *maskbits_out, uint16_t *port_min_out,
  226. uint16_t *port_max_out);
  227. int addr_mask_get_bits(uint32_t mask);
  228. int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
  229. int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
  230. char *tor_dup_addr(uint32_t addr) ATTR_MALLOC;
  231. int get_interface_address(int severity, uint32_t *addr);
  232. int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
  233. int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2);
  234. int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  235. maskbits_t mask);
  236. int tor_addr_is_v4(const tor_addr_t *addr);
  237. int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
  238. int tor_addr_parse_mask_ports(const char *s,
  239. tor_addr_t *addr_out, maskbits_t *mask_out,
  240. uint16_t *port_min_out, uint16_t *port_max_out);
  241. const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len);
  242. int tor_addr_from_str(tor_addr_t *addr, const char *src);
  243. void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
  244. void tor_addr_from_ipv4(tor_addr_t *dest, uint32_t v4addr);
  245. int tor_addr_is_null(const tor_addr_t *addr);
  246. /* Process helpers */
  247. void start_daemon(void);
  248. void finish_daemon(const char *desired_cwd);
  249. void write_pidfile(char *filename);
  250. #endif