util.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /** Macro: evaluate the expression x, which we expect to be false.
  39. * Used to hint the compiler that a branch won't be taken. */
  40. #define PREDICT_FALSE(x) PREDICT((x) == ((typeof(x)) 0), 0)
  41. #else
  42. #define PREDICT_FALSE(x) !(x)
  43. #endif
  44. /** Like assert(3), but send assertion failures to the log as well as to
  45. * stderr. */
  46. #define tor_assert(expr) do { \
  47. if (PREDICT_FALSE(expr)) { \
  48. log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
  49. _SHORT_FILE_, __LINE__, __func__, #expr); \
  50. fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
  51. _SHORT_FILE_, __LINE__, __func__, #expr); \
  52. abort(); \
  53. } } while (0)
  54. #endif
  55. #ifdef USE_DMALLOC
  56. #define DMALLOC_PARAMS , const char *file, const int line
  57. #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
  58. #else
  59. #define DMALLOC_PARAMS
  60. #define DMALLOC_ARGS
  61. #endif
  62. /** Define this if you want Tor to crash when any problem comes up,
  63. * so you can get a coredump and track things down. */
  64. // #define tor_fragile_assert() tor_assert(0)
  65. #define tor_fragile_assert()
  66. /* Memory management */
  67. void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
  68. void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
  69. void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
  70. char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
  71. char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
  72. ATTR_MALLOC ATTR_NONNULL((1));
  73. void *_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
  74. ATTR_MALLOC ATTR_NONNULL((1));
  75. void _tor_free(void *mem);
  76. #ifdef USE_DMALLOC
  77. extern int dmalloc_free(const char *file, const int line, void *pnt,
  78. const int func_id);
  79. #define tor_free(p) do { \
  80. if (PREDICT((p)!=NULL, 1)) { \
  81. dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
  82. (p)=NULL; \
  83. } \
  84. } while (0)
  85. #else
  86. #define tor_free(p) do { if (PREDICT((p)!=NULL,1)) { free(p); (p)=NULL;} } \
  87. while (0)
  88. #endif
  89. #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
  90. #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
  91. #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
  92. #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
  93. #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
  94. #define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
  95. /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
  96. #if defined(__GNUC__) && __GNUC__ > 3
  97. #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
  98. #else
  99. #define STRUCT_OFFSET(tp, member) \
  100. ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  101. #endif
  102. /* String manipulation */
  103. /** Allowable characters in a hexadecimal string. */
  104. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  105. void tor_strlower(char *s) ATTR_NONNULL((1));
  106. void tor_strupper(char *s) ATTR_NONNULL((1));
  107. int tor_strisprint(const char *s) ATTR_PURE ATTR_NONNULL((1));
  108. int tor_strisnonupper(const char *s) ATTR_PURE ATTR_NONNULL((1));
  109. int strcmpstart(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
  110. int strcasecmpstart(const char *s1, const char *s2)
  111. ATTR_PURE ATTR_NONNULL((1,2));
  112. int strcmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
  113. int strcasecmpend(const char *s1, const char *s2)
  114. ATTR_PURE ATTR_NONNULL((1,2));
  115. int tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
  116. int tor_strpartition(char *dest, size_t dest_len,
  117. const char *s, const char *insert, size_t n);
  118. long tor_parse_long(const char *s, int base, long min,
  119. long max, int *ok, char **next);
  120. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  121. unsigned long max, int *ok, char **next);
  122. uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
  123. uint64_t max, int *ok, char **next);
  124. const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
  125. const char *eat_whitespace(const char *s) ATTR_PURE;
  126. const char *eat_whitespace_no_nl(const char *s) ATTR_PURE;
  127. const char *find_whitespace(const char *s) ATTR_PURE;
  128. int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
  129. int tor_digest_is_zero(const char *digest) ATTR_PURE;
  130. char *esc_for_log(const char *string) ATTR_MALLOC;
  131. const char *escaped(const char *string);
  132. struct smartlist_t;
  133. void wrap_string(struct smartlist_t *out, const char *string, size_t width,
  134. const char *prefix0, const char *prefixRest);
  135. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  136. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  137. /* Time helpers */
  138. long tv_udiff(struct timeval *start, struct timeval *end);
  139. void tv_addms(struct timeval *a, long ms);
  140. void tv_add(struct timeval *a, struct timeval *b);
  141. int tv_cmp(struct timeval *a, struct timeval *b);
  142. time_t tor_timegm(struct tm *tm);
  143. #define RFC1123_TIME_LEN 29
  144. void format_rfc1123_time(char *buf, time_t t);
  145. int parse_rfc1123_time(const char *buf, time_t *t);
  146. #define ISO_TIME_LEN 19
  147. void format_local_iso_time(char *buf, time_t t);
  148. void format_iso_time(char *buf, time_t t);
  149. int parse_iso_time(const char *buf, time_t *t);
  150. /* File helpers */
  151. int write_all(int fd, const char *buf, size_t count, int isSocket);
  152. int read_all(int fd, char *buf, size_t count, int isSocket);
  153. /** Return values from file_status(); see that function's documentation
  154. * for details. */
  155. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR } file_status_t;
  156. file_status_t file_status(const char *filename);
  157. /** Possible behaviors for check_private_dir() on encountering a nonexistent
  158. * directory; see that function's documentation for details. */
  159. typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
  160. int check_private_dir(const char *dirname, cpd_check_t check);
  161. int write_str_to_file(const char *fname, const char *str, int bin);
  162. int write_bytes_to_file(const char *fname, const char *str, size_t len,
  163. int bin);
  164. /** An ad-hoc type to hold a string of characters and a count; used by
  165. * write_chunks_to_file. */
  166. typedef struct sized_chunk_t {
  167. const char *bytes;
  168. size_t len;
  169. } sized_chunk_t;
  170. int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
  171. int bin);
  172. int append_bytes_to_file(const char *fname, const char *str, size_t len,
  173. int bin);
  174. /** Flag for read_file_to_str: open the file in binary mode. */
  175. #define RFTS_BIN 1
  176. /** Flag for read_file_to_str: it's okay if the file doesn't exist */
  177. #define RFTS_IGNORE_MISSING 2
  178. struct stat;
  179. char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  180. ATTR_MALLOC;
  181. char *parse_line_from_str(char *line, char **key_out, char **value_out);
  182. char *expand_filename(const char *filename);
  183. struct smartlist_t *tor_listdir(const char *dirname);
  184. int path_is_relative(const char *filename) ATTR_PURE;
  185. /* Net helpers */
  186. int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
  187. int parse_addr_port(int severity, const char *addrport, char **address,
  188. uint32_t *addr, uint16_t *port_out);
  189. int parse_port_range(const char *port, uint16_t *port_min_out,
  190. uint16_t *port_max_out);
  191. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  192. uint32_t *mask_out, uint16_t *port_min_out,
  193. uint16_t *port_max_out);
  194. int addr_mask_get_bits(uint32_t mask);
  195. #define INET_NTOA_BUF_LEN 16
  196. int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
  197. char *tor_dup_addr(uint32_t addr) ATTR_MALLOC;
  198. int get_interface_address(int severity, uint32_t *addr);
  199. /* Process helpers */
  200. void start_daemon(void);
  201. void finish_daemon(const char *desired_cwd);
  202. void write_pidfile(char *filename);
  203. #endif