1 /* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
2 /* See LICENSE for licensing information */
7 * \brief Headers for util.c
12 #define UTIL_H_ID "$Id$"
18 #ifdef HAVE_SYS_TIME_H
25 /** Replace assert() with a variant that sends failures to the log before
26 * calling assert() normally.
29 /* Nobody should ever want to build with NDEBUG set. 99% of your asserts will
30 * be outside the critical path anyway, so it's silly to disable bugchecking
31 * throughout the entire program just because a few asserts are slowing you
32 * down. Profile, optimize the critical path, and keep debugging on.
34 * And I'm not just saying that because some of our asserts check
35 * security-critical properties.
37 #error "Sorry; we don't support building with NDEBUG."
39 #define tor_assert(expr) do { \
41 log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
42 _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
43 fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
44 _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
45 abort(); /* unreached */ \
49 /* Memory management */
50 void *_tor_malloc(const char *file
, const int line
, size_t size
);
51 void *_tor_malloc_zero(const char *file
, const int line
, size_t size
);
52 void *_tor_realloc(const char *file
, const int line
, void *ptr
, size_t size
);
53 char *_tor_strdup(const char *file
, const int line
, const char *s
);
54 char *_tor_strndup(const char *file
, const int line
, const char *s
, size_t n
);
55 #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
57 #define tor_malloc(size) _tor_malloc(_SHORT_FILE_, __LINE__, size)
58 #define tor_malloc_zero(size) _tor_malloc_zero(_SHORT_FILE_, __LINE__, size)
59 #define tor_realloc(ptr, size) _tor_realloc(_SHORT_FILE_, __LINE__, ptr, size)
60 #define tor_strdup(s) _tor_strdup(_SHORT_FILE_, __LINE__, s)
61 #define tor_strndup(s, n) _tor_strndup(_SHORT_FILE_, __LINE__, s, n)
63 /* String manipulation */
64 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
65 void tor_strlower(char *s
);
66 int strcmpstart(const char *s1
, const char *s2
);
67 int strcasecmpstart(const char *s1
, const char *s2
);
68 int strcmpend(const char *s1
, const char *s2
);
69 int strcasecmpend(const char *s1
, const char *s2
);
70 int tor_strstrip(char *s
, const char *strip
);
72 ALWAYS_TERMINATE
, NEVER_TERMINATE
, TERMINATE_IF_EVEN
74 int tor_strpartition(char *dest
, size_t dest_len
,
75 const char *s
, const char *insert
, size_t n
,
76 part_finish_rule_t rule
);
77 long tor_parse_long(const char *s
, int base
, long min
,
78 long max
, int *ok
, char **next
);
79 unsigned long tor_parse_ulong(const char *s
, int base
, unsigned long min
,
80 unsigned long max
, int *ok
, char **next
);
81 uint64_t tor_parse_uint64(const char *s
, int base
, uint64_t min
,
82 uint64_t max
, int *ok
, char **next
);
83 const char *hex_str(const char *from
, size_t fromlen
);
84 const char *eat_whitespace(const char *s
);
85 const char *eat_whitespace_no_nl(const char *s
);
86 const char *find_whitespace(const char *s
);
88 void base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
89 int base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
92 long tv_udiff(struct timeval
*start
, struct timeval
*end
);
93 void tv_addms(struct timeval
*a
, long ms
);
94 void tv_add(struct timeval
*a
, struct timeval
*b
);
95 int tv_cmp(struct timeval
*a
, struct timeval
*b
);
96 time_t tor_timegm(struct tm
*tm
);
97 #define RFC1123_TIME_LEN 29
98 void format_rfc1123_time(char *buf
, time_t t
);
99 int parse_rfc1123_time(const char *buf
, time_t *t
);
100 #define ISO_TIME_LEN 19
101 void format_local_iso_time(char *buf
, time_t t
);
102 void format_iso_time(char *buf
, time_t t
);
103 int parse_iso_time(const char *buf
, time_t *t
);
106 int write_all(int fd
, const char *buf
, size_t count
, int isSocket
);
107 int read_all(int fd
, char *buf
, size_t count
, int isSocket
);
109 typedef enum { FN_ERROR
, FN_NOENT
, FN_FILE
, FN_DIR
} file_status_t
;
110 file_status_t
file_status(const char *filename
);
112 typedef enum { CPD_NONE
, CPD_CREATE
, CPD_CHECK
} cpd_check_t
;
113 int check_private_dir(const char *dirname
, cpd_check_t check
);
114 int write_str_to_file(const char *fname
, const char *str
, int bin
);
115 int write_bytes_to_file(const char *fname
, const char *str
, size_t len
,
117 char *read_file_to_str(const char *filename
, int bin
);
118 char *parse_line_from_str(char *line
, char **key_out
, char **value_out
);
119 char *expand_filename(const char *filename
);
122 int is_internal_IP(uint32_t ip
);
123 int is_local_IP(uint32_t ip
);
124 int parse_addr_port(const char *addrport
, char **address
, uint32_t *addr
,
126 int parse_addr_and_port_range(const char *s
, uint32_t *addr_out
,
127 uint32_t *mask_out
, uint16_t *port_min_out
,
128 uint16_t *port_max_out
);
130 /* Process helpers */
131 void start_daemon(const char *desired_cwd
);
132 void finish_daemon(void);
133 void write_pidfile(char *filename
);