util.h 10 KB

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