1 /* Copyright 2003-2004 Roger Dingledine
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
8 * \brief Headers for util.c
13 #define UTIL_H_ID "$Id$"
20 #ifdef HAVE_SYS_TIME_H
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 your asserts will
32 * be outside the critical path anyway, so it's silly to disable bugchecking
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."
41 #define tor_assert(expr) do { \
43 log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
44 _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
45 fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
46 _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
52 #define DMALLOC_PARAMS , const char *file, const int line
53 #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
55 #define DMALLOC_PARAMS
59 /** Define this if you want Tor to crash when any problem comes up,
60 * so you can get a coredump and track things down. */
61 // #define tor_fragile_assert() tor_assert(0)
62 #define tor_fragile_assert()
64 /* Memory management */
65 void *_tor_malloc(size_t size DMALLOC_PARAMS
);
66 void *_tor_malloc_zero(size_t size DMALLOC_PARAMS
);
67 void *_tor_realloc(void *ptr
, size_t size DMALLOC_PARAMS
);
68 char *_tor_strdup(const char *s DMALLOC_PARAMS
);
69 char *_tor_strndup(const char *s
, size_t n DMALLOC_PARAMS
);
71 extern int dmalloc_free(const char *file
, const int line
, void *pnt
,
73 #define tor_free(p) do { \
75 dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
80 #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
83 #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
84 #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
85 #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
86 #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
87 #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
89 /* String manipulation */
90 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
91 void tor_strlower(char *s
);
92 void tor_strupper(char *s
);
93 int strcmpstart(const char *s1
, const char *s2
);
94 int strcasecmpstart(const char *s1
, const char *s2
);
95 int strcmpend(const char *s1
, const char *s2
);
96 int strcasecmpend(const char *s1
, const char *s2
);
97 int tor_strstrip(char *s
, const char *strip
);
99 ALWAYS_TERMINATE
, NEVER_TERMINATE
, TERMINATE_IF_EVEN
100 } part_finish_rule_t
;
101 int tor_strpartition(char *dest
, size_t dest_len
,
102 const char *s
, const char *insert
, size_t n
,
103 part_finish_rule_t rule
);
104 long tor_parse_long(const char *s
, int base
, long min
,
105 long max
, int *ok
, char **next
);
106 unsigned long tor_parse_ulong(const char *s
, int base
, unsigned long min
,
107 unsigned long max
, int *ok
, char **next
);
108 uint64_t tor_parse_uint64(const char *s
, int base
, uint64_t min
,
109 uint64_t max
, int *ok
, char **next
);
110 const char *hex_str(const char *from
, size_t fromlen
);
111 const char *eat_whitespace(const char *s
);
112 const char *eat_whitespace_no_nl(const char *s
);
113 const char *find_whitespace(const char *s
);
114 int tor_digest_is_zero(const char *digest
);
116 void base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
117 int base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
120 long tv_udiff(struct timeval
*start
, struct timeval
*end
);
121 void tv_addms(struct timeval
*a
, long ms
);
122 void tv_add(struct timeval
*a
, struct timeval
*b
);
123 int tv_cmp(struct timeval
*a
, struct timeval
*b
);
124 time_t tor_timegm(struct tm
*tm
);
125 #define RFC1123_TIME_LEN 29
126 void format_rfc1123_time(char *buf
, time_t t
);
127 int parse_rfc1123_time(const char *buf
, time_t *t
);
128 #define ISO_TIME_LEN 19
129 void format_local_iso_time(char *buf
, time_t t
);
130 void format_iso_time(char *buf
, time_t t
);
131 int parse_iso_time(const char *buf
, time_t *t
);
134 int write_all(int fd
, const char *buf
, size_t count
, int isSocket
);
135 int read_all(int fd
, char *buf
, size_t count
, int isSocket
);
137 typedef enum { FN_ERROR
, FN_NOENT
, FN_FILE
, FN_DIR
} file_status_t
;
138 file_status_t
file_status(const char *filename
);
140 typedef enum { CPD_NONE
, CPD_CREATE
, CPD_CHECK
} cpd_check_t
;
141 int check_private_dir(const char *dirname
, cpd_check_t check
);
142 int write_str_to_file(const char *fname
, const char *str
, int bin
);
143 int write_bytes_to_file(const char *fname
, const char *str
, size_t len
,
145 /** An ad-hoc type to hold a string of characters and a count; used by
146 * write_chunks_to_file. */
147 typedef struct sized_chunk_t
{
152 int write_chunks_to_file(const char *fname
, const struct smartlist_t
*chunks
,
154 int append_bytes_to_file(const char *fname
, const char *str
, size_t len
,
157 char *read_file_to_str(const char *filename
, int bin
);
158 char *parse_line_from_str(char *line
, char **key_out
, char **value_out
);
159 char *expand_filename(const char *filename
);
160 struct smartlist_t
*tor_listdir(const char *dirname
);
163 int is_internal_IP(uint32_t ip
);
164 int is_local_IP(uint32_t ip
);
165 int parse_addr_port(const char *addrport
, char **address
, uint32_t *addr
,
167 int parse_addr_and_port_range(const char *s
, uint32_t *addr_out
,
168 uint32_t *mask_out
, uint16_t *port_min_out
,
169 uint16_t *port_max_out
);
170 #define INET_NTOA_BUF_LEN 16
171 int tor_inet_ntoa(struct in_addr
*in
, char *buf
, size_t buf_len
);
172 char *tor_dup_addr(uint32_t addr
);
173 int is_plausible_address(const char *name
);
174 int get_interface_address(uint32_t *addr
);
176 /* Process helpers */
177 void start_daemon(void);
178 void finish_daemon(const char *desired_cwd
);
179 void write_pidfile(char *filename
);