123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #ifndef __UTIL_H
- #define __UTIL_H
- #define UTIL_H_ID "$Id$"
- #include "orconfig.h"
- #include "torint.h"
- #include "compat.h"
- #include <stdio.h>
- #include <stdlib.h>
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
- #ifndef O_TEXT
- #define O_TEXT 0
- #endif
- #ifdef NDEBUG
- #error "Sorry; we don't support building with NDEBUG."
- #else
- #ifdef __GNUC__
- #define IS_FALSE_AS_INT(x) ((x) == ((typeof(x)) 0))
- #else
- #define IS_FALSE_AS_INT(x) !(x)
- #endif
- #define tor_assert(expr) STMT_BEGIN \
- if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \
- log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
- _SHORT_FILE_, __LINE__, __func__, #expr); \
- fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
- _SHORT_FILE_, __LINE__, __func__, #expr); \
- abort(); \
- } STMT_END
- #endif
- #ifdef USE_DMALLOC
- #define DMALLOC_PARAMS , const char *file, const int line
- #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
- #else
- #define DMALLOC_PARAMS
- #define DMALLOC_ARGS
- #endif
- #define tor_fragile_assert()
- void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
- void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
- void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
- char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
- char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
- ATTR_MALLOC ATTR_NONNULL((1));
- void *_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
- ATTR_MALLOC ATTR_NONNULL((1));
- void _tor_free(void *mem);
- #ifdef USE_DMALLOC
- extern int dmalloc_free(const char *file, const int line, void *pnt,
- const int func_id);
- #define tor_free(p) STMT_BEGIN \
- if (PREDICT_LIKELY((p)!=NULL)) { \
- dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
- (p)=NULL; \
- } \
- STMT_END
- #else
- #define tor_free(p) STMT_BEGIN \
- if (PREDICT_LIKELY((p)!=NULL)) { \
- free(p); \
- (p)=NULL; \
- } \
- STMT_END
- #endif
- #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
- #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
- #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
- #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
- #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
- #define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
- void tor_log_mallinfo(int severity);
- #if defined(__GNUC__) && __GNUC__ > 3
- #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
- #else
- #define STRUCT_OFFSET(tp, member) \
- ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
- #endif
- #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
- #define SUBTYPE_P(p, subtype, basemember) \
- ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
- #define bool_eq(a,b) (!(a)==!(b))
- #define bool_neq(a,b) (!(a)!=!(b))
- int tor_log2(uint64_t u64) ATTR_CONST;
- uint64_t round_to_power_of_2(uint64_t u64);
- #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
- void tor_strlower(char *s) ATTR_NONNULL((1));
- void tor_strupper(char *s) ATTR_NONNULL((1));
- int tor_strisprint(const char *s) ATTR_PURE ATTR_NONNULL((1));
- int tor_strisnonupper(const char *s) ATTR_PURE ATTR_NONNULL((1));
- int strcmpstart(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
- int strcmp_len(const char *s1, const char *s2, size_t len)
- ATTR_PURE ATTR_NONNULL((1,2));
- int strcasecmpstart(const char *s1, const char *s2)
- ATTR_PURE ATTR_NONNULL((1,2));
- int strcmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
- int strcasecmpend(const char *s1, const char *s2)
- ATTR_PURE ATTR_NONNULL((1,2));
- int tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
- int tor_strpartition(char *dest, size_t dest_len,
- const char *s, const char *insert, size_t n);
- 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);
- 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));
- const char *eat_whitespace(const char *s) ATTR_PURE;
- const char *eat_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
- const char *eat_whitespace_no_nl(const char *s) ATTR_PURE;
- const char *eat_whitespace_eos_no_nl(const char *s, const char *eos) ATTR_PURE;
- const char *find_whitespace(const char *s) ATTR_PURE;
- const char *find_whitespace_eos(const char *s, const char *eos) ATTR_PURE;
- int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
- int tor_digest_is_zero(const char *digest) ATTR_PURE;
- char *esc_for_log(const char *string) ATTR_MALLOC;
- const char *escaped(const char *string);
- struct smartlist_t;
- void wrap_string(struct smartlist_t *out, const char *string, size_t width,
- const char *prefix0, const char *prefixRest);
- void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
- int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
- long tv_udiff(const struct timeval *start, const struct timeval *end);
- void tv_addms(struct timeval *a, long ms);
- void tv_add(struct timeval *a, const struct timeval *b);
- int tv_cmp(const struct timeval *a, const struct timeval *b);
- time_t tor_timegm(struct tm *tm);
- #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
- void format_local_iso_time(char *buf, time_t t);
- void format_iso_time(char *buf, time_t t);
- int parse_iso_time(const char *buf, 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);
- void ftime_set_maximum_sloppiness(int seconds);
- void ftime_set_estimated_skew(int seconds);
- int ftime_maybe_after(time_t now, time_t when);
- int ftime_maybe_before(time_t now, time_t when);
- int ftime_definitely_after(time_t now, time_t when);
- int ftime_definitely_before(time_t now, time_t when);
- int write_all(int fd, const char *buf, size_t count, int isSocket);
- int read_all(int fd, char *buf, size_t count, int isSocket);
- typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR } file_status_t;
- file_status_t file_status(const char *filename);
- typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
- int check_private_dir(const char *dirname, cpd_check_t check);
- #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
- #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
- 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);
- int write_str_to_file(const char *fname, const char *str, int bin);
- 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;
- int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
- int bin);
- int append_bytes_to_file(const char *fname, const char *str, size_t len,
- int bin);
- #define RFTS_BIN 1
- #define RFTS_IGNORE_MISSING 2
- struct stat;
- char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
- ATTR_MALLOC;
- char *parse_line_from_str(char *line, char **key_out, char **value_out);
- char *expand_filename(const char *filename);
- struct smartlist_t *tor_listdir(const char *dirname);
- int path_is_relative(const char *filename) ATTR_PURE;
- int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
- int parse_addr_port(int severity, const char *addrport, char **address,
- uint32_t *addr, uint16_t *port_out);
- int parse_port_range(const char *port, uint16_t *port_min_out,
- uint16_t *port_max_out);
- int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
- maskbits_t *maskbits_out, uint16_t *port_min_out,
- uint16_t *port_max_out);
- int addr_mask_get_bits(uint32_t mask);
- int addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits);
- int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
- char *tor_dup_addr(uint32_t addr) ATTR_MALLOC;
- int get_interface_address(int severity, uint32_t *addr);
- int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
- int tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2);
- int tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
- maskbits_t mask);
- int tor_addr_is_v4(const tor_addr_t *addr);
- int tor_addr_is_internal(const tor_addr_t *ip, int for_listening) ATTR_PURE;
- int tor_addr_parse_mask_ports(const char *s,
- tor_addr_t *addr_out, maskbits_t *mask_out,
- uint16_t *port_min_out, uint16_t *port_max_out);
- const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, int len);
- int tor_addr_from_str(tor_addr_t *addr, const char *src);
- void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
- void tor_addr_from_ipv4(tor_addr_t *dest, uint32_t v4addr);
- int tor_addr_is_null(const tor_addr_t *addr);
- void start_daemon(void);
- void finish_daemon(const char *desired_cwd);
- void write_pidfile(char *filename);
- #endif
|