1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Headers for util.c
27 /* Replace assert() with a variant that sends failures to the log before
28 * calling assert() normally.
31 /* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
32 * be outside the critical path anyway, so it's silly to disable bug-checking
33 * throughout the entire program just because a few asserts are slowing you
34 * down. Profile, optimize the critical path, and keep debugging on.
36 * And I'm not just saying that because some of our asserts check
37 * security-critical properties.
39 #error "Sorry; we don't support building with NDEBUG."
42 /** Like assert(3), but send assertion failures to the log as well as to
44 #define tor_assert(expr) STMT_BEGIN \
45 if (PREDICT_UNLIKELY(!(expr))) { \
46 log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
47 _SHORT_FILE_, __LINE__, __func__, #expr); \
48 fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
49 _SHORT_FILE_, __LINE__, __func__, #expr); \
53 /* If we're building with dmalloc, we want all of our memory allocation
54 * functions to take an extra file/line pair of arguments. If not, not.
55 * We define DMALLOC_PARAMS to the extra parameters to insert in the
56 * function prototypes, and DMALLOC_ARGS to the extra arguments to add
59 #define DMALLOC_PARAMS , const char *file, const int line
60 #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
62 #define DMALLOC_PARAMS
66 /** Define this if you want Tor to crash when any problem comes up,
67 * so you can get a coredump and track things down. */
68 // #define tor_fragile_assert() tor_assert(0)
69 #define tor_fragile_assert()
71 /* Memory management */
72 void *_tor_malloc(size_t size DMALLOC_PARAMS
) ATTR_MALLOC
;
73 void *_tor_malloc_zero(size_t size DMALLOC_PARAMS
) ATTR_MALLOC
;
74 void *_tor_malloc_roundup(size_t *size DMALLOC_PARAMS
) ATTR_MALLOC
;
75 void *_tor_realloc(void *ptr
, size_t size DMALLOC_PARAMS
);
76 char *_tor_strdup(const char *s DMALLOC_PARAMS
) ATTR_MALLOC
ATTR_NONNULL((1));
77 char *_tor_strndup(const char *s
, size_t n DMALLOC_PARAMS
)
78 ATTR_MALLOC
ATTR_NONNULL((1));
79 void *_tor_memdup(const void *mem
, size_t len DMALLOC_PARAMS
)
80 ATTR_MALLOC
ATTR_NONNULL((1));
81 void _tor_free(void *mem
);
83 extern int dmalloc_free(const char *file
, const int line
, void *pnt
,
85 #define tor_free(p) STMT_BEGIN \
86 if (PREDICT_LIKELY((p)!=NULL)) { \
87 dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
92 /** Release memory allocated by tor_malloc, tor_realloc, tor_strdup, etc.
93 * Unlike the free() function, tor_free() will still work on NULL pointers,
94 * and it sets the pointer value to NULL after freeing it.
96 * This is a macro. If you need a function pointer to release memory from
97 * tor_malloc(), use _tor_free().
99 #define tor_free(p) STMT_BEGIN \
100 if (PREDICT_LIKELY((p)!=NULL)) { \
107 #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
108 #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
109 #define tor_malloc_roundup(szp) _tor_malloc_roundup(szp DMALLOC_ARGS)
110 #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
111 #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
112 #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
113 #define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
115 void tor_log_mallinfo(int severity
);
117 /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
118 #if defined(__GNUC__) && __GNUC__ > 3
119 #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
121 #define STRUCT_OFFSET(tp, member) \
122 ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
125 /** Macro: yield a pointer to the field at position <b>off</b> within the
126 * structure <b>st</b>. Example:
128 * struct a { int foo; int bar; } x;
129 * off_t bar_offset = STRUCT_OFFSET(struct a, bar);
130 * int *bar_p = STRUCT_VAR_P(&x, bar_offset);
134 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
136 /** Macro: yield a pointer to an enclosing structure given a pointer to
137 * a substructure at offset <b>off</b>. Example:
139 * struct base { ... };
140 * struct subtype { int x; struct base b; } x;
141 * struct base *bp = &x.base;
142 * struct *sp = SUBTYPE_P(bp, struct subtype, b);
145 #define SUBTYPE_P(p, subtype, basemember) \
146 ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
149 /** Macro: true if two values have the same boolean value. */
150 #define bool_eq(a,b) (!(a)==!(b))
151 /** Macro: true if two values have different boolean values. */
152 #define bool_neq(a,b) (!(a)!=!(b))
155 int tor_log2(uint64_t u64
) ATTR_CONST
;
156 uint64_t round_to_power_of_2(uint64_t u64
);
158 /* String manipulation */
160 /** Allowable characters in a hexadecimal string. */
161 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
162 void tor_strlower(char *s
) ATTR_NONNULL((1));
163 void tor_strupper(char *s
) ATTR_NONNULL((1));
164 int tor_strisprint(const char *s
) ATTR_PURE
ATTR_NONNULL((1));
165 int tor_strisnonupper(const char *s
) ATTR_PURE
ATTR_NONNULL((1));
166 int strcmpstart(const char *s1
, const char *s2
) ATTR_PURE
ATTR_NONNULL((1,2));
167 int strcmp_len(const char *s1
, const char *s2
, size_t len
)
168 ATTR_PURE
ATTR_NONNULL((1,2));
169 int strcasecmpstart(const char *s1
, const char *s2
)
170 ATTR_PURE
ATTR_NONNULL((1,2));
171 int strcmpend(const char *s1
, const char *s2
) ATTR_PURE
ATTR_NONNULL((1,2));
172 int strcasecmpend(const char *s1
, const char *s2
)
173 ATTR_PURE
ATTR_NONNULL((1,2));
174 int memcmpstart(const void *mem
, size_t memlen
,
175 const char *prefix
) ATTR_PURE
;
177 void tor_strstrip(char *s
, const char *strip
) ATTR_NONNULL((1,2));
178 long tor_parse_long(const char *s
, int base
, long min
,
179 long max
, int *ok
, char **next
);
180 unsigned long tor_parse_ulong(const char *s
, int base
, unsigned long min
,
181 unsigned long max
, int *ok
, char **next
);
182 uint64_t tor_parse_uint64(const char *s
, int base
, uint64_t min
,
183 uint64_t max
, int *ok
, char **next
);
184 const char *hex_str(const char *from
, size_t fromlen
) ATTR_NONNULL((1));
185 const char *eat_whitespace(const char *s
) ATTR_PURE
;
186 const char *eat_whitespace_eos(const char *s
, const char *eos
) ATTR_PURE
;
187 const char *eat_whitespace_no_nl(const char *s
) ATTR_PURE
;
188 const char *eat_whitespace_eos_no_nl(const char *s
, const char *eos
) ATTR_PURE
;
189 const char *find_whitespace(const char *s
) ATTR_PURE
;
190 const char *find_whitespace_eos(const char *s
, const char *eos
) ATTR_PURE
;
191 int tor_mem_is_zero(const char *mem
, size_t len
) ATTR_PURE
;
192 int tor_digest_is_zero(const char *digest
) ATTR_PURE
;
193 char *esc_for_log(const char *string
) ATTR_MALLOC
;
194 const char *escaped(const char *string
);
196 void wrap_string(struct smartlist_t
*out
, const char *string
, size_t width
,
197 const char *prefix0
, const char *prefixRest
);
198 int tor_vsscanf(const char *buf
, const char *pattern
, va_list ap
);
199 int tor_sscanf(const char *buf
, const char *pattern
, ...)
201 __attribute__((format(scanf
, 2, 3)))
205 int hex_decode_digit(char c
);
206 void base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
207 int base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
210 long tv_udiff(const struct timeval
*start
, const struct timeval
*end
);
211 time_t tor_timegm(struct tm
*tm
);
212 #define RFC1123_TIME_LEN 29
213 void format_rfc1123_time(char *buf
, time_t t
);
214 int parse_rfc1123_time(const char *buf
, time_t *t
);
215 #define ISO_TIME_LEN 19
216 void format_local_iso_time(char *buf
, time_t t
);
217 void format_iso_time(char *buf
, time_t t
);
218 int parse_iso_time(const char *buf
, time_t *t
);
219 int parse_http_time(const char *buf
, struct tm
*tm
);
220 int format_time_interval(char *out
, size_t out_len
, long interval
);
224 #define approx_time() time(NULL)
225 #define update_approx_time(t) STMT_NIL
227 time_t approx_time(void);
228 void update_approx_time(time_t now
);
232 void ftime_set_maximum_sloppiness(int seconds
);
233 void ftime_set_estimated_skew(int seconds
);
234 /* typedef struct ftime_t { time_t earliest; time_t latest; } ftime_t; */
235 /* void ftime_get_window(time_t now, ftime_t *ft_out); */
236 int ftime_maybe_after(time_t now
, time_t when
);
237 int ftime_maybe_before(time_t now
, time_t when
);
238 int ftime_definitely_after(time_t now
, time_t when
);
239 int ftime_definitely_before(time_t now
, time_t when
);
242 ssize_t
write_all(int fd
, const char *buf
, size_t count
, int isSocket
);
243 ssize_t
read_all(int fd
, char *buf
, size_t count
, int isSocket
);
245 /** Return values from file_status(); see that function's documentation
247 typedef enum { FN_ERROR
, FN_NOENT
, FN_FILE
, FN_DIR
} file_status_t
;
248 file_status_t
file_status(const char *filename
);
250 /** Possible behaviors for check_private_dir() on encountering a nonexistent
251 * directory; see that function's documentation for details. */
252 typedef enum { CPD_NONE
, CPD_CREATE
, CPD_CHECK
} cpd_check_t
;
253 int check_private_dir(const char *dirname
, cpd_check_t check
);
254 #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
255 #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
256 typedef struct open_file_t open_file_t
;
257 int start_writing_to_file(const char *fname
, int open_flags
, int mode
,
258 open_file_t
**data_out
);
259 FILE *start_writing_to_stdio_file(const char *fname
, int open_flags
, int mode
,
260 open_file_t
**data_out
);
261 FILE *fdopen_file(open_file_t
*file_data
);
262 int finish_writing_to_file(open_file_t
*file_data
);
263 int abort_writing_to_file(open_file_t
*file_data
);
264 int write_str_to_file(const char *fname
, const char *str
, int bin
);
265 int write_bytes_to_file(const char *fname
, const char *str
, size_t len
,
267 /** An ad-hoc type to hold a string of characters and a count; used by
268 * write_chunks_to_file. */
269 typedef struct sized_chunk_t
{
273 int write_chunks_to_file(const char *fname
, const struct smartlist_t
*chunks
,
275 int append_bytes_to_file(const char *fname
, const char *str
, size_t len
,
278 /** Flag for read_file_to_str: open the file in binary mode. */
280 /** Flag for read_file_to_str: it's okay if the file doesn't exist. */
281 #define RFTS_IGNORE_MISSING 2
284 char *read_file_to_str(const char *filename
, int flags
, struct stat
*stat_out
)
286 const char *parse_config_line_from_str(const char *line
,
287 char **key_out
, char **value_out
);
288 char *expand_filename(const char *filename
);
289 struct smartlist_t
*tor_listdir(const char *dirname
);
290 int path_is_relative(const char *filename
) ATTR_PURE
;
292 /* Process helpers */
293 void start_daemon(void);
294 void finish_daemon(const char *desired_cwd
);
295 void write_pidfile(char *filename
);