tor  master
util.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #ifndef TOR_UTIL_H
12 #define TOR_UTIL_H
13 
14 #include "orconfig.h"
15 #include "torint.h"
16 #include "compat.h"
17 #include "di_ops.h"
18 #include "testsupport.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #ifdef _WIN32
22 /* for the correct alias to struct stat */
23 #include <sys/stat.h>
24 #endif
25 #include "util_bug.h"
26 
27 #ifndef O_BINARY
28 #define O_BINARY 0
29 #endif
30 #ifndef O_TEXT
31 #define O_TEXT 0
32 #endif
33 #ifndef O_NOFOLLOW
34 #define O_NOFOLLOW 0
35 #endif
36 
37 /* If we're building with dmalloc, we want all of our memory allocation
38  * functions to take an extra file/line pair of arguments. If not, not.
39  * We define DMALLOC_PARAMS to the extra parameters to insert in the
40  * function prototypes, and DMALLOC_ARGS to the extra arguments to add
41  * to calls. */
42 #ifdef USE_DMALLOC
43 #define DMALLOC_PARAMS , const char *file, const int line
44 #define DMALLOC_ARGS , SHORT_FILE__, __LINE__
45 #else
46 #define DMALLOC_PARAMS
47 #define DMALLOC_ARGS
48 #endif /* defined(USE_DMALLOC) */
49 
50 /* Memory management */
51 void *tor_malloc_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
52 void *tor_malloc_zero_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
53 void *tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC;
54 void *tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS);
55 void *tor_reallocarray_(void *ptr, size_t size1, size_t size2 DMALLOC_PARAMS);
56 char *tor_strdup_(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
57 char *tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
58  ATTR_MALLOC ATTR_NONNULL((1));
59 void *tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS)
60  ATTR_MALLOC ATTR_NONNULL((1));
61 void *tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS)
62  ATTR_MALLOC ATTR_NONNULL((1));
63 void tor_free_(void *mem);
64 uint64_t tor_htonll(uint64_t a);
65 uint64_t tor_ntohll(uint64_t a);
66 #ifdef USE_DMALLOC
67 extern int dmalloc_free(const char *file, const int line, void *pnt,
68  const int func_id);
69 #define tor_free(p) STMT_BEGIN \
70  if (PREDICT_LIKELY((p)!=NULL)) { \
71  dmalloc_free(SHORT_FILE__, __LINE__, (p), 0); \
72  (p)=NULL; \
73  } \
74  STMT_END
75 #else /* !(defined(USE_DMALLOC)) */
76 
88 #ifdef __GNUC__
89 #define tor_free(p) STMT_BEGIN \
90  typeof(&(p)) tor_free__tmpvar = &(p); \
91  raw_free(*tor_free__tmpvar); \
92  *tor_free__tmpvar=NULL; \
93  STMT_END
94 #else
95 #define tor_free(p) STMT_BEGIN \
96  raw_free(p); \
97  (p)=NULL; \
98  STMT_END
99 #endif
100 #endif /* defined(USE_DMALLOC) */
101 
102 #define tor_malloc(size) tor_malloc_(size DMALLOC_ARGS)
103 #define tor_malloc_zero(size) tor_malloc_zero_(size DMALLOC_ARGS)
104 #define tor_calloc(nmemb,size) tor_calloc_(nmemb, size DMALLOC_ARGS)
105 #define tor_realloc(ptr, size) tor_realloc_(ptr, size DMALLOC_ARGS)
106 #define tor_reallocarray(ptr, sz1, sz2) \
107  tor_reallocarray_((ptr), (sz1), (sz2) DMALLOC_ARGS)
108 #define tor_strdup(s) tor_strdup_(s DMALLOC_ARGS)
109 #define tor_strndup(s, n) tor_strndup_(s, n DMALLOC_ARGS)
110 #define tor_memdup(s, n) tor_memdup_(s, n DMALLOC_ARGS)
111 #define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n DMALLOC_ARGS)
112 
113 /* Aliases for the underlying system malloc/realloc/free. Only use
114  * them to indicate "I really want the underlying system function, I know
115  * what I'm doing." */
116 #define raw_malloc malloc
117 #define raw_realloc realloc
118 #define raw_free free
119 #define raw_strdup strdup
120 
121 void tor_log_mallinfo(int severity);
122 
123 /* Helper macro: free a variable of type 'typename' using freefn, and
124  * set the variable to NULL.
125  */
126 #define FREE_AND_NULL(typename, freefn, var) \
127  do { \
128  /* only evaluate (var) once. */ \
129  typename **tmp__free__ptr ## freefn = &(var); \
130  freefn(*tmp__free__ptr ## freefn); \
131  (*tmp__free__ptr ## freefn) = NULL; \
132  } while (0)
133 
143 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
144 
154 #define SUBTYPE_P(p, subtype, basemember) \
155  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
156 
157 /* Logic */
159 #define bool_eq(a,b) (!(a)==!(b))
160 
161 #define bool_neq(a,b) (!(a)!=!(b))
162 
163 /* Math functions */
164 double tor_mathlog(double d) ATTR_CONST;
165 long tor_lround(double d) ATTR_CONST;
166 int64_t tor_llround(double d) ATTR_CONST;
167 int tor_log2(uint64_t u64) ATTR_CONST;
168 uint64_t round_to_power_of_2(uint64_t u64);
169 unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
170 uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor);
171 uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor);
172 int64_t sample_laplace_distribution(double mu, double b, double p);
173 int64_t add_laplace_noise(int64_t signal, double random, double delta_f,
174  double epsilon);
175 int n_bits_set_u8(uint8_t v);
176 int64_t clamp_double_to_int64(double number);
177 void simplify_fraction64(uint64_t *numer, uint64_t *denom);
178 
179 uint32_t tor_add_u32_nowrap(uint32_t a, uint32_t b);
180 
181 /* Compute the CEIL of <b>a</b> divided by <b>b</b>, for nonnegative <b>a</b>
182  * and positive <b>b</b>. Works on integer types only. Not defined if a+(b-1)
183  * can overflow. */
184 #define CEIL_DIV(a,b) (((a)+((b)-1))/(b))
185 
186 /* Return <b>v</b> if it's between <b>min</b> and <b>max</b>. Otherwise
187  * return <b>min</b> if <b>v</b> is smaller than <b>min</b>, or <b>max</b> if
188  * <b>b</b> is larger than <b>max</b>.
189  *
190  * Requires that <b>min</b> is no more than <b>max</b>. May evaluate any of
191  * its arguments more than once! */
192 #define CLAMP(min,v,max) \
193  ( ((v) < (min)) ? (min) : \
194  ((v) > (max)) ? (max) : \
195  (v) )
196 
197 /* String manipulation */
198 
200 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
201 void tor_strlower(char *s) ATTR_NONNULL((1));
202 void tor_strupper(char *s) ATTR_NONNULL((1));
203 int tor_strisprint(const char *s) ATTR_NONNULL((1));
204 int tor_strisnonupper(const char *s) ATTR_NONNULL((1));
205 int tor_strisspace(const char *s);
206 int strcmp_opt(const char *s1, const char *s2);
207 int strcmpstart(const char *s1, const char *s2) ATTR_NONNULL((1,2));
208 int strcmp_len(const char *s1, const char *s2, size_t len) ATTR_NONNULL((1,2));
209 int strcasecmpstart(const char *s1, const char *s2) ATTR_NONNULL((1,2));
210 int strcmpend(const char *s1, const char *s2) ATTR_NONNULL((1,2));
211 int strcasecmpend(const char *s1, const char *s2) ATTR_NONNULL((1,2));
212 int fast_memcmpstart(const void *mem, size_t memlen, const char *prefix);
213 
214 void tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
215 long tor_parse_long(const char *s, int base, long min,
216  long max, int *ok, char **next);
217 unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
218  unsigned long max, int *ok, char **next);
219 double tor_parse_double(const char *s, double min, double max, int *ok,
220  char **next);
221 uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
222  uint64_t max, int *ok, char **next);
223 const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
224 const char *eat_whitespace(const char *s);
225 const char *eat_whitespace_eos(const char *s, const char *eos);
226 const char *eat_whitespace_no_nl(const char *s);
227 const char *eat_whitespace_eos_no_nl(const char *s, const char *eos);
228 const char *find_whitespace(const char *s);
229 const char *find_whitespace_eos(const char *s, const char *eos);
230 const char *find_str_at_start_of_line(const char *haystack,
231  const char *needle);
232 int string_is_C_identifier(const char *string);
233 int string_is_key_value(int severity, const char *string);
234 int string_is_valid_dest(const char *string);
235 int string_is_valid_nonrfc_hostname(const char *string);
236 int string_is_valid_ipv4_address(const char *string);
237 int string_is_valid_ipv6_address(const char *string);
238 
239 int tor_mem_is_zero(const char *mem, size_t len);
240 int tor_digest_is_zero(const char *digest);
241 int tor_digest256_is_zero(const char *digest);
242 char *esc_for_log(const char *string) ATTR_MALLOC;
243 char *esc_for_log_len(const char *chars, size_t n) ATTR_MALLOC;
244 const char *escaped(const char *string);
245 
246 char *tor_escape_str_for_pt_args(const char *string,
247  const char *chars_to_escape);
248 
249 struct smartlist_t;
250 int tor_vsscanf(const char *buf, const char *pattern, va_list ap) \
251  CHECK_SCANF(2, 0);
252 int tor_sscanf(const char *buf, const char *pattern, ...)
253  CHECK_SCANF(2, 3);
254 
255 void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
256  CHECK_PRINTF(2, 3);
257 void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
258  va_list args)
259  CHECK_PRINTF(2, 0);
260 void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
261 
262 /* Time helpers */
263 long tv_udiff(const struct timeval *start, const struct timeval *end);
264 long tv_mdiff(const struct timeval *start, const struct timeval *end);
265 int64_t tv_to_msec(const struct timeval *tv);
266 int tor_timegm(const struct tm *tm, time_t *time_out);
267 #define RFC1123_TIME_LEN 29
268 void format_rfc1123_time(char *buf, time_t t);
269 int parse_rfc1123_time(const char *buf, time_t *t);
270 #define ISO_TIME_LEN 19
271 #define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
272 void format_local_iso_time(char *buf, time_t t);
273 void format_iso_time(char *buf, time_t t);
274 void format_local_iso_time_nospace(char *buf, time_t t);
275 void format_iso_time_nospace(char *buf, time_t t);
276 void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
277 int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace);
278 int parse_iso_time(const char *buf, time_t *t);
279 int parse_iso_time_nospace(const char *cp, time_t *t);
280 int parse_http_time(const char *buf, struct tm *tm);
281 int format_time_interval(char *out, size_t out_len, long interval);
282 
283 /* Cached time */
284 #ifdef TIME_IS_FAST
285 #define approx_time() time(NULL)
286 #define update_approx_time(t) STMT_NIL
287 #else
288 time_t approx_time(void);
289 void update_approx_time(time_t now);
290 #endif /* defined(TIME_IS_FAST) */
291 
292 /* Rate-limiter */
293 
318 typedef struct ratelim_t {
319  int rate;
320  time_t last_allowed;
321  int n_calls_since_last_time;
322 } ratelim_t;
323 
324 #define RATELIM_INIT(r) { (r), 0, 0 }
325 #define RATELIM_TOOMANY (16*1000*1000)
326 
327 char *rate_limit_log(ratelim_t *lim, time_t now);
328 
329 /* File helpers */
330 ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
331 ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
332 
335  IO_STREAM_OKAY,
336  IO_STREAM_EAGAIN,
337  IO_STREAM_TERM,
338  IO_STREAM_CLOSED
339 };
340 
342 
343 enum stream_status get_string_from_pipe(int fd, char *buf, size_t count);
344 
345 MOCK_DECL(int,tor_unlink,(const char *pathname));
346 
349 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
350 file_status_t file_status(const char *filename);
351 
354 typedef unsigned int cpd_check_t;
355 #define CPD_NONE 0
356 #define CPD_CREATE (1u << 0)
357 #define CPD_CHECK (1u << 1)
358 #define CPD_GROUP_OK (1u << 2)
359 #define CPD_GROUP_READ (1u << 3)
360 #define CPD_CHECK_MODE_ONLY (1u << 4)
361 #define CPD_RELAX_DIRMODE_CHECK (1u << 5)
362 MOCK_DECL(int, check_private_dir,
363  (const char *dirname, cpd_check_t check,
364  const char *effective_user));
365 
366 #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
367 #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
368 #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
369 typedef struct open_file_t open_file_t;
370 int start_writing_to_file(const char *fname, int open_flags, int mode,
371  open_file_t **data_out);
372 FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
373  open_file_t **data_out);
374 FILE *fdopen_file(open_file_t *file_data);
375 int finish_writing_to_file(open_file_t *file_data);
376 int abort_writing_to_file(open_file_t *file_data);
377 MOCK_DECL(int,
378 write_str_to_file,(const char *fname, const char *str, int bin));
379 MOCK_DECL(int,
380 write_bytes_to_file,(const char *fname, const char *str, size_t len,
381  int bin));
384 typedef struct sized_chunk_t {
385  const char *bytes;
386  size_t len;
387 } sized_chunk_t;
388 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
389  int bin, int no_tempfile);
390 int append_bytes_to_file(const char *fname, const char *str, size_t len,
391  int bin);
392 int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
393  int bin);
394 
396 #define RFTS_BIN 1
397 
398 #define RFTS_IGNORE_MISSING 2
399 
400 #ifndef _WIN32
401 struct stat;
402 #endif
403 MOCK_DECL_ATTR(char *, read_file_to_str,
404  (const char *filename, int flags, struct stat *stat_out),
405  ATTR_MALLOC);
406 char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
407  size_t *sz_out)
408  ATTR_MALLOC;
409 const char *unescape_string(const char *s, char **result, size_t *size_out);
410 char *get_unquoted_path(const char *path);
411 char *expand_filename(const char *filename);
412 MOCK_DECL(struct smartlist_t *, tor_listdir, (const char *dirname));
413 int path_is_relative(const char *filename);
414 
415 /* Process helpers */
416 void start_daemon(void);
417 void finish_daemon(const char *desired_cwd);
418 int write_pidfile(const char *filename);
419 
421 
422 typedef struct process_handle_t process_handle_t;
424 int tor_spawn_background(const char *const filename, const char **argv,
426  process_handle_t **process_handle_out);
427 
428 #define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
429 
430 #ifdef _WIN32
431 HANDLE load_windows_system_library(const TCHAR *library_name);
432 #endif
433 
434 int environment_variable_names_equal(const char *s1, const char *s2);
435 
436 /* DOCDOC process_environment_t */
444 };
445 
448 #define process_environment_free(env) \
449  FREE_AND_NULL(process_environment_t, process_environment_free_, (env))
450 
452 
454  const char *new_var,
455  void (*free_old)(void*),
456  int free_p);
457 
458 /* Values of process_handle_t.status. */
459 #define PROCESS_STATUS_NOTRUNNING 0
460 #define PROCESS_STATUS_RUNNING 1
461 #define PROCESS_STATUS_ERROR -1
462 
463 #ifdef UTIL_PRIVATE
464 struct waitpid_callback_t;
467 struct process_handle_t {
469  int status;
470 #ifdef _WIN32
471  HANDLE stdin_pipe;
472  HANDLE stdout_pipe;
473  HANDLE stderr_pipe;
474  PROCESS_INFORMATION pid;
475 #else /* !(defined(_WIN32)) */
476  int stdin_pipe;
477  int stdout_pipe;
478  int stderr_pipe;
479  pid_t pid;
483  struct waitpid_callback_t *waitpid_cb;
485  int waitpid_exit_status;
486 #endif /* defined(_WIN32) */
487 };
488 #endif /* defined(UTIL_PRIVATE) */
489 
490 /* Return values of tor_get_exit_code() */
491 #define PROCESS_EXIT_RUNNING 1
492 #define PROCESS_EXIT_EXITED 0
493 #define PROCESS_EXIT_ERROR -1
494 int tor_get_exit_code(process_handle_t *process_handle,
495  int block, int *exit_code);
496 int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
497 #ifdef _WIN32
498 ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
499  const process_handle_t *process);
500 #else
501 ssize_t tor_read_all_handle(int fd, char *buf, size_t count,
502  const process_handle_t *process,
503  int *eof);
504 #endif /* defined(_WIN32) */
506  const process_handle_t *process_handle, char *buf, size_t count);
508  const process_handle_t *process_handle, char *buf, size_t count);
509 char *tor_join_win_cmdline(const char *argv[]);
510 
511 int tor_process_get_pid(process_handle_t *process_handle);
512 #ifdef _WIN32
513 HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
514 #else
515 int tor_process_get_stdout_pipe(process_handle_t *process_handle);
516 #endif
517 
518 #ifdef _WIN32
519 MOCK_DECL(struct smartlist_t *,
520 tor_get_lines_from_handle,(HANDLE *handle,
521  enum stream_status *stream_status));
522 #else
523 MOCK_DECL(struct smartlist_t *,
524 tor_get_lines_from_handle,(int fd,
525  enum stream_status *stream_status));
526 #endif /* defined(_WIN32) */
527 
528 int
529 tor_terminate_process(process_handle_t *process_handle);
530 
531 MOCK_DECL(void,
532 tor_process_handle_destroy,(process_handle_t *process_handle,
533  int also_terminate_process));
534 
535 /* ===== Insecure rng */
536 typedef struct tor_weak_rng_t {
537  uint32_t state;
539 
540 #define TOR_WEAK_RNG_INIT {383745623}
541 #define TOR_WEAK_RANDOM_MAX (INT_MAX)
542 void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed);
543 int32_t tor_weak_random(tor_weak_rng_t *weak_rng);
544 int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top);
547 #define tor_weak_random_one_in_n(rng, n) (0==tor_weak_random_range((rng),(n)))
548 
549 int format_hex_number_sigsafe(unsigned long x, char *buf, int max_len);
550 int format_dec_number_sigsafe(unsigned long x, char *buf, int max_len);
551 
552 #ifdef UTIL_PRIVATE
553 /* Prototypes for private functions only used by util.c (and unit tests) */
554 
555 #ifndef _WIN32
556 STATIC int format_helper_exit_status(unsigned char child_state,
557  int saved_errno, char *hex_errno);
558 
559 /* Space for hex values of child state, a slash, saved_errno (with
560  leading minus) and newline (no null) */
561 #define HEX_ERRNO_SIZE (sizeof(char) * 2 + 1 + \
562  1 + sizeof(int) * 2 + 1)
563 #endif /* !defined(_WIN32) */
564 
565 #endif /* defined(UTIL_PRIVATE) */
566 
567 int size_mul_check(const size_t x, const size_t y);
568 
569 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
570 
571 #endif /* !defined(TOR_UTIL_H) */
572 
uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
Definition: util.c:511
int write_bytes_to_new_file(const char *fname, const char *str, size_t len, int bin)
Definition: util.c:2824
int parse_iso_time(const char *buf, time_t *t)
Definition: util.c:1928
void set_environment_variable_in_smartlist(struct smartlist_t *env_vars, const char *new_var, void(*free_old)(void *), int free_p)
Definition: util.c:4906
int format_time_interval(char *out, size_t out_len, long interval)
Definition: util.c:2017
int tor_strisnonupper(const char *s) ATTR_NONNULL((1))
Definition: util.c:710
char * windows_environment_block
Definition: util.h:440
int strcmp_opt(const char *s1, const char *s2)
Definition: util.c:736
char ** unixoid_environment_block
Definition: util.h:443
Definition: util_process.c:38
int64_t sample_laplace_distribution(double mu, double b, double p)
Definition: util.c:526
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: util.c:2115
int start_writing_to_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: util.c:2577
void tor_free_(void *mem)
Definition: util.c:343
Headers for di_ops.c.
double tor_parse_double(const char *s, double min, double max, int *ok, char **next)
Definition: util.c:1264
const char * find_str_at_start_of_line(const char *haystack, const char *needle)
Definition: util.c:954
int64_t tor_llround(double d) ATTR_CONST
Definition: util.c:411
int write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin, int no_tempfile)
Definition: util.c:2774
uint64_t round_to_power_of_2(uint64_t u64)
Definition: util.c:457
int tor_strisprint(const char *s) ATTR_NONNULL((1))
Definition: util.c:697
void tor_strstrip(char *s, const char *strip)
Definition: util.c:644
int format_hex_number_sigsafe(unsigned long x, char *buf, int max_len)
Definition: util.c:3976
const char * unescape_string(const char *s, char **result, size_t *size_out)
Definition: util.c:3004
int parse_http_time(const char *buf, struct tm *tm)
Definition: util.c:1945
long tv_udiff(const struct timeval *start, const struct timeval *end)
Definition: util.c:1497
void update_approx_time(time_t now)
Definition: util.c:2079
void finish_daemon(const char *desired_cwd)
Definition: util.c:3709
void * tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:329
enum stream_status get_string_from_pipe(int fd, char *buf, size_t count)
Definition: util.c:5228
int string_is_valid_nonrfc_hostname(const char *string)
Definition: util.c:1124
stream_status
Definition: util.h:334
int n_bits_set_u8(uint8_t v)
Definition: util.c:613
const char * hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1))
Definition: util.c:663
const char * eat_whitespace_eos(const char *s, const char *eos)
Definition: util.c:858
void format_local_iso_time_nospace(char *buf, time_t t)
Definition: util.c:1840
void tor_strlower(char *s) ATTR_NONNULL((1))
Definition: util.c:675
MOCK_DECL(int, router_have_minimum_dir_info,(void))
process_environment_t * process_environment_make(struct smartlist_t *env_vars)
Definition: util.c:4801
Definition: util.h:437
uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
Definition: util.c:496
int tor_digest_is_zero(const char *digest)
Definition: util.c:1024
Definition: container.h:18
int strcmpstart(const char *s1, const char *s2)
Definition: util.c:754
Header file to define uint32_t and friends.
Definition: d.py:1
int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace)
Definition: util.c:1872
int string_is_C_identifier(const char *string)
Definition: util.c:977
void format_local_iso_time(char *buf, time_t t)
Definition: util.c:1821
long tv_mdiff(const struct timeval *start, const struct timeval *end)
Definition: util.c:1546
char * tor_strdup_(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:272
int tor_process_get_pid(process_handle_t *process_handle)
Definition: util.c:4138
ssize_t tor_read_all_from_process_stdout(const process_handle_t *process_handle, char *buf, size_t count)
Definition: util.c:5038
int32_t tor_weak_random(tor_weak_rng_t *weak_rng)
Definition: util.c:5264
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
Definition: util.c:1859
int write_pidfile(const char *filename)
Definition: util.c:3771
int int void void void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
Definition: util.c:3537
Definition: util.c:2549
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: util.c:1245
unsigned int cpd_check_t
Definition: util.h:354
uint64_t tor_htonll(uint64_t a)
Definition: util.c:5360
void tor_disable_spawning_background_processes(void)
Definition: util.c:4228
int tor_digest256_is_zero(const char *digest)
Definition: util.c:1182
unsigned round_to_next_multiple_of(unsigned number, unsigned divisor)
Definition: util.c:482
void * tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:316
void * tor_malloc_zero_(size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:170
char * tor_strndup_(const char *s, size_t n DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:298
void * tor_malloc_(size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:134
struct sized_chunk_t sized_chunk_t
int tor_spawn_background(const char *const filename, const char **argv, process_environment_t *env, process_handle_t **process_handle_out)
Definition: util.c:4252
int int int int int int fast_memcmpstart(const void *mem, size_t memlen, const char *prefix)
Definition: util.c:818
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: util.c:2814
file_status_t file_status(const char *filename)
Definition: util.c:2237
const char * stream_status_to_string(enum stream_status stream_status)
Definition: util.c:5119
const char * find_whitespace_eos(const char *s, const char *eos)
Definition: util.c:929
int string_is_valid_ipv6_address(const char *string)
Definition: util.c:1080
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
Definition: util.c:2145
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min, uint64_t max, int *ok, char **next)
Definition: util.c:1277
int tor_sscanf(const char *buf, const char *pattern,...)
Definition: util.c:3501
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: util.c:2658
ssize_t tor_read_all_from_process_stderr(const process_handle_t *process_handle, char *buf, size_t count)
Definition: util.c:5052
int strcasecmpend(const char *s1, const char *s2)
Definition: util.c:802
struct smartlist_t * get_current_process_environment_variables(void)
Definition: util.c:4889
const char * eat_whitespace(const char *s)
Definition: util.c:831
int tor_timegm(const struct tm *tm, time_t *time_out)
Definition: util.c:1632
long tor_lround(double d) ATTR_CONST
Definition: util.c:396
char * esc_for_log_len(const char *chars, size_t n) ATTR_MALLOC
Definition: util.c:1396
const char * eat_whitespace_no_nl(const char *s)
Definition: util.c:886
const char * find_whitespace(const char *s)
Definition: util.c:907
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2 DMALLOC_PARAMS)
Definition: util.c:258
void start_daemon(void)
Definition: util.c:3648
Definition: compat_time.h:33
int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
Definition: util.c:5280
void tor_log_mallinfo(int severity)
Definition: util.c:352
int64_t add_laplace_noise(int64_t signal, double random, double delta_f, double epsilon)
Definition: util.c:551
int parse_iso_time_nospace(const char *cp, time_t *t)
Definition: util.c:1937
struct ratelim_t ratelim_t
void * tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:214
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: util.c:3514
FILE * fdopen_file(open_file_t *file_data)
Definition: util.c:2641
int tor_log2(uint64_t u64) ATTR_CONST
Definition: util.c:424
int tor_vsscanf(const char *buf, const char *pattern, va_list ap)
Definition: util.c:3371
int strcmpend(const char *s1, const char *s2)
Definition: util.c:789
int tor_strisspace(const char *s)
Definition: util.c:723
void long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: util.c:1226
Definition: base.py:1
int string_is_valid_dest(const char *string)
Definition: util.c:1091
const char * eat_whitespace_eos_no_nl(const char *s, const char *eos)
Definition: util.c:896
STATIC int format_helper_exit_status(unsigned char child_state, int saved_errno, char *hex_errno)
Definition: util.c:4005
int string_is_valid_ipv4_address(const char *string)
Definition: util.c:1069
char * tor_join_win_cmdline(const char *argv[])
Definition: util.c:3884
const char * escaped(const char *string)
Definition: util.c:1412
void tor_strupper(char *s) ATTR_NONNULL((1))
Definition: util.c:686
int string_is_key_value(int severity, const char *string)
Definition: util.c:1036
int strcasecmpstart(const char *s1, const char *s2)
Definition: util.c:779
int finish_writing_to_file(open_file_t *file_data)
Definition: util.c:2725
int environment_variable_names_equal(const char *s1, const char *s2)
Definition: util.c:4770
ssize_t tor_read_all_handle(int fd, char *buf, size_t count, const process_handle_t *process, int *eof)
Definition: util.c:4997
uint64_t tor_ntohll(uint64_t a)
Definition: util.c:5374
char * expand_filename(const char *filename)
Definition: util.c:3152
int parse_rfc1123_time(const char *buf, time_t *t)
Definition: util.c:1742
Definition: util.h:318
int tor_split_lines(struct smartlist_t *sl, char *buf, int len)
Definition: util.c:5070
void process_environment_free_(process_environment_t *env)
Definition: util.c:4782
Definition: util.h:536
void format_iso_time_nospace(char *buf, time_t t)
Definition: util.c:1849
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util.c:1002
char * read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC
Definition: util.c:2840
char * tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
Definition: util.c:1428
void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern, va_list args)
Definition: util.c:3524
int64_t tv_to_msec(const struct timeval *tv)
Definition: util.c:1604
int format_dec_number_sigsafe(unsigned long x, char *buf, int max_len)
Definition: util.c:3983
int strcmp_len(const char *s1, const char *s2, size_t s1_len)
Definition: util.c:765
time_t approx_time(void)
Definition: util.c:2070
int path_is_relative(const char *filename)
Definition: util.c:3614
int abort_writing_to_file(open_file_t *file_data)
Definition: util.c:2733
void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed)
Definition: util.c:5254
char * esc_for_log(const char *string) ATTR_MALLOC
Definition: util.c:1316
Definition: util.h:384
int tor_get_exit_code(process_handle_t *process_handle, int block, int *exit_code)
Definition: util.c:4678
void format_iso_time(char *buf, time_t t)
Definition: util.c:1831
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
Definition: util.c:2169
Macros to manage assertions, fatal and non-fatal.
char * get_unquoted_path(const char *path)
Definition: util.c:3118
file_status_t
Definition: util.h:349
int size_mul_check(const size_t x, const size_t y)
Definition: util.c:193
double tor_mathlog(double d) ATTR_CONST
Definition: util.c:387
int64_t clamp_double_to_int64(double number)
Definition: util.c:5298
void format_rfc1123_time(char *buf, time_t t)
Definition: util.c:1718
void * tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
Definition: util.c:225
int tor_terminate_process(process_handle_t *process_handle)
Definition: util.c:4115