123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- #ifndef TOR_UTIL_H
- #define TOR_UTIL_H
- #include "orconfig.h"
- #include "lib/cc/torint.h"
- #include "common/compat.h"
- #include "lib/ctime/di_ops.h"
- #include "lib/testsupport/testsupport.h"
- #include <stdio.h>
- #include <stdlib.h>
- #ifdef _WIN32
- #include <sys/stat.h>
- #endif
- #include "lib/err/torerr.h"
- #include "lib/malloc/util_malloc.h"
- #include "lib/wallclock/approx_time.h"
- #include "lib/string/util_string.h"
- #include "lib/string/scanf.h"
- #include "common/util_bug.h"
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
- #ifndef O_TEXT
- #define O_TEXT 0
- #endif
- #ifndef O_NOFOLLOW
- #define O_NOFOLLOW 0
- #endif
- uint64_t tor_htonll(uint64_t a);
- uint64_t tor_ntohll(uint64_t a);
- void tor_log_mallinfo(int severity);
- #define SUBTYPE_P(p, subtype, basemember) \
- ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
- #define bool_eq(a,b) (!(a)==!(b))
- #define bool_neq(a,b) (!(a)!=!(b))
- double tor_mathlog(double d) ATTR_CONST;
- long tor_lround(double d) ATTR_CONST;
- int64_t tor_llround(double d) ATTR_CONST;
- int tor_log2(uint64_t u64) ATTR_CONST;
- uint64_t round_to_power_of_2(uint64_t u64);
- unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
- uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor);
- uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor);
- int64_t sample_laplace_distribution(double mu, double b, double p);
- int64_t add_laplace_noise(int64_t signal, double random, double delta_f,
- double epsilon);
- int n_bits_set_u8(uint8_t v);
- int64_t clamp_double_to_int64(double number);
- void simplify_fraction64(uint64_t *numer, uint64_t *denom);
- uint32_t tor_add_u32_nowrap(uint32_t a, uint32_t b);
- #define CEIL_DIV(a,b) (((a)+((b)-1))/(b))
- #define CLAMP(min,v,max) \
- ( ((v) < (min)) ? (min) : \
- ((v) > (max)) ? (max) : \
- (v) )
- long tor_parse_long(const char *s, int base, long min,
- long max, int *ok, char **next);
- unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
- unsigned long max, int *ok, char **next);
- double tor_parse_double(const char *s, double min, double max, int *ok,
- char **next);
- uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
- uint64_t max, int *ok, char **next);
- const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
- int string_is_key_value(int severity, const char *string);
- int string_is_valid_dest(const char *string);
- int string_is_valid_nonrfc_hostname(const char *string);
- int string_is_valid_ipv4_address(const char *string);
- int string_is_valid_ipv6_address(const char *string);
- int tor_mem_is_zero(const char *mem, size_t len);
- int tor_digest_is_zero(const char *digest);
- int tor_digest256_is_zero(const char *digest);
- char *esc_for_log(const char *string) ATTR_MALLOC;
- char *esc_for_log_len(const char *chars, size_t n) ATTR_MALLOC;
- const char *escaped(const char *string);
- char *tor_escape_str_for_pt_args(const char *string,
- const char *chars_to_escape);
- long tv_udiff(const struct timeval *start, const struct timeval *end);
- long tv_mdiff(const struct timeval *start, const struct timeval *end);
- int64_t tv_to_msec(const struct timeval *tv);
- int tor_timegm(const struct tm *tm, time_t *time_out);
- #define RFC1123_TIME_LEN 29
- void format_rfc1123_time(char *buf, time_t t);
- int parse_rfc1123_time(const char *buf, time_t *t);
- #define ISO_TIME_LEN 19
- #define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
- void format_local_iso_time(char *buf, time_t t);
- void format_iso_time(char *buf, time_t t);
- void format_local_iso_time_nospace(char *buf, time_t t);
- void format_iso_time_nospace(char *buf, time_t t);
- void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
- int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace);
- int parse_iso_time(const char *buf, time_t *t);
- int parse_iso_time_nospace(const char *cp, time_t *t);
- int parse_http_time(const char *buf, struct tm *tm);
- int format_time_interval(char *out, size_t out_len, long interval);
- typedef struct ratelim_t {
- int rate;
- time_t last_allowed;
- int n_calls_since_last_time;
- } ratelim_t;
- #define RATELIM_INIT(r) { (r), 0, 0 }
- #define RATELIM_TOOMANY (16*1000*1000)
- char *rate_limit_log(ratelim_t *lim, time_t now);
- ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
- ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
- enum stream_status {
- IO_STREAM_OKAY,
- IO_STREAM_EAGAIN,
- IO_STREAM_TERM,
- IO_STREAM_CLOSED
- };
- const char *stream_status_to_string(enum stream_status stream_status);
- enum stream_status get_string_from_pipe(int fd, char *buf, size_t count);
- MOCK_DECL(int,tor_unlink,(const char *pathname));
- typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY } file_status_t;
- file_status_t file_status(const char *filename);
- typedef unsigned int cpd_check_t;
- #define CPD_NONE 0
- #define CPD_CREATE (1u << 0)
- #define CPD_CHECK (1u << 1)
- #define CPD_GROUP_OK (1u << 2)
- #define CPD_GROUP_READ (1u << 3)
- #define CPD_CHECK_MODE_ONLY (1u << 4)
- #define CPD_RELAX_DIRMODE_CHECK (1u << 5)
- MOCK_DECL(int, check_private_dir,
- (const char *dirname, cpd_check_t check,
- const char *effective_user));
- #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
- #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
- #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
- typedef struct open_file_t open_file_t;
- int start_writing_to_file(const char *fname, int open_flags, int mode,
- open_file_t **data_out);
- FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
- open_file_t **data_out);
- FILE *fdopen_file(open_file_t *file_data);
- int finish_writing_to_file(open_file_t *file_data);
- int abort_writing_to_file(open_file_t *file_data);
- MOCK_DECL(int,
- write_str_to_file,(const char *fname, const char *str, int bin));
- MOCK_DECL(int,
- write_bytes_to_file,(const char *fname, const char *str, size_t len,
- int bin));
- typedef struct sized_chunk_t {
- const char *bytes;
- size_t len;
- } sized_chunk_t;
- struct smartlist_t;
- int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
- int bin, int no_tempfile);
- int append_bytes_to_file(const char *fname, const char *str, size_t len,
- int bin);
- int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
- int bin);
- #define RFTS_BIN 1
- #define RFTS_IGNORE_MISSING 2
- #ifndef _WIN32
- struct stat;
- #endif
- MOCK_DECL_ATTR(char *, read_file_to_str,
- (const char *filename, int flags, struct stat *stat_out),
- ATTR_MALLOC);
- char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
- size_t *sz_out)
- ATTR_MALLOC;
- const char *unescape_string(const char *s, char **result, size_t *size_out);
- char *get_unquoted_path(const char *path);
- char *expand_filename(const char *filename);
- MOCK_DECL(struct smartlist_t *, tor_listdir, (const char *dirname));
- int path_is_relative(const char *filename);
- void start_daemon(void);
- void finish_daemon(const char *desired_cwd);
- int write_pidfile(const char *filename);
- void tor_disable_spawning_background_processes(void);
- typedef struct process_handle_t process_handle_t;
- typedef struct process_environment_t process_environment_t;
- int tor_spawn_background(const char *const filename, const char **argv,
- process_environment_t *env,
- process_handle_t **process_handle_out);
- #define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
- #ifdef _WIN32
- HANDLE load_windows_system_library(const TCHAR *library_name);
- #endif
- int environment_variable_names_equal(const char *s1, const char *s2);
- struct process_environment_t {
-
- char *windows_environment_block;
-
- char **unixoid_environment_block;
- };
- process_environment_t *process_environment_make(struct smartlist_t *env_vars);
- void process_environment_free_(process_environment_t *env);
- #define process_environment_free(env) \
- FREE_AND_NULL(process_environment_t, process_environment_free_, (env))
- struct smartlist_t *get_current_process_environment_variables(void);
- void set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
- const char *new_var,
- void (*free_old)(void*),
- int free_p);
- #define PROCESS_STATUS_NOTRUNNING 0
- #define PROCESS_STATUS_RUNNING 1
- #define PROCESS_STATUS_ERROR -1
- #ifdef UTIL_PRIVATE
- struct waitpid_callback_t;
- struct process_handle_t {
-
- int status;
- #ifdef _WIN32
- HANDLE stdin_pipe;
- HANDLE stdout_pipe;
- HANDLE stderr_pipe;
- PROCESS_INFORMATION pid;
- #else
- int stdin_pipe;
- int stdout_pipe;
- int stderr_pipe;
- pid_t pid;
-
- struct waitpid_callback_t *waitpid_cb;
-
- int waitpid_exit_status;
- #endif
- };
- #endif
- #define PROCESS_EXIT_RUNNING 1
- #define PROCESS_EXIT_EXITED 0
- #define PROCESS_EXIT_ERROR -1
- int tor_get_exit_code(process_handle_t *process_handle,
- int block, int *exit_code);
- int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
- #ifdef _WIN32
- ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
- const process_handle_t *process);
- #else
- ssize_t tor_read_all_handle(int fd, char *buf, size_t count,
- const process_handle_t *process,
- int *eof);
- #endif
- ssize_t tor_read_all_from_process_stdout(
- const process_handle_t *process_handle, char *buf, size_t count);
- ssize_t tor_read_all_from_process_stderr(
- const process_handle_t *process_handle, char *buf, size_t count);
- char *tor_join_win_cmdline(const char *argv[]);
- int tor_process_get_pid(process_handle_t *process_handle);
- #ifdef _WIN32
- HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
- #else
- int tor_process_get_stdout_pipe(process_handle_t *process_handle);
- #endif
- #ifdef _WIN32
- MOCK_DECL(struct smartlist_t *,
- tor_get_lines_from_handle,(HANDLE *handle,
- enum stream_status *stream_status));
- #else
- MOCK_DECL(struct smartlist_t *,
- tor_get_lines_from_handle,(int fd,
- enum stream_status *stream_status));
- #endif
- int
- tor_terminate_process(process_handle_t *process_handle);
- MOCK_DECL(void,
- tor_process_handle_destroy,(process_handle_t *process_handle,
- int also_terminate_process));
- typedef struct tor_weak_rng_t {
- uint32_t state;
- } tor_weak_rng_t;
- #define TOR_WEAK_RNG_INIT {383745623}
- #define TOR_WEAK_RANDOM_MAX (INT_MAX)
- void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed);
- int32_t tor_weak_random(tor_weak_rng_t *weak_rng);
- int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top);
- #define tor_weak_random_one_in_n(rng, n) (0==tor_weak_random_range((rng),(n)))
- #ifdef UTIL_PRIVATE
- #ifndef _WIN32
- STATIC int format_helper_exit_status(unsigned char child_state,
- int saved_errno, char *hex_errno);
- #define HEX_ERRNO_SIZE (sizeof(char) * 2 + 1 + \
- 1 + sizeof(int) * 2 + 1)
- #endif
- #endif
- #endif
|