if we rotate our onion key, publish a new descriptor, and
[tor.git] / src / common / util.h
blob13d51b4413c533dae3841dcfb4e0c200d9174639
1 /* Copyright 2003-2004 Roger Dingledine
2 * Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
6 /**
7 * \file util.h
8 * \brief Headers for util.c
9 **/
11 #ifndef __UTIL_H
12 #define __UTIL_H
13 #define UTIL_H_ID "$Id$"
15 #include "orconfig.h"
16 #include "torint.h"
17 #include "compat.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #ifdef HAVE_SYS_TIME_H
21 #include <sys/time.h>
22 #endif
23 #ifdef HAVE_TIME_H
24 #include <time.h>
25 #endif
27 /* Replace assert() with a variant that sends failures to the log before
28 * calling assert() normally.
30 #ifdef NDEBUG
31 /* Nobody should ever want to build with NDEBUG set. 99% of our 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."
40 #else
41 #ifdef __GNUC__
42 #define PREDICT_FALSE(x) PREDICT((x) == ((typeof(x)) 0), 0)
43 #else
44 #define PREDICT_FALSE(x) !(x)
45 #endif
46 #define tor_assert(expr) do { \
47 if (PREDICT_FALSE(expr)) { \
48 log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
49 _SHORT_FILE_, __LINE__, __func__, #expr); \
50 fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
51 _SHORT_FILE_, __LINE__, __func__, #expr); \
52 abort(); \
53 } } while (0)
54 #endif
56 #ifdef USE_DMALLOC
57 #define DMALLOC_PARAMS , const char *file, const int line
58 #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
59 #else
60 #define DMALLOC_PARAMS
61 #define DMALLOC_ARGS
62 #endif
64 /** Define this if you want Tor to crash when any problem comes up,
65 * so you can get a coredump and track things down. */
66 // #define tor_fragile_assert() tor_assert(0)
67 #define tor_fragile_assert()
69 /* Memory management */
70 void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
71 void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
72 void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
73 char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
74 char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
75 ATTR_MALLOC ATTR_NONNULL((1));
76 void *_tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
77 ATTR_MALLOC ATTR_NONNULL((1));
78 void _tor_free(void *mem);
79 #ifdef USE_DMALLOC
80 extern int dmalloc_free(const char *file, const int line, void *pnt,
81 const int func_id);
82 #define tor_free(p) do { \
83 if (PREDICT((p)!=NULL, 1)) { \
84 dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
85 (p)=NULL; \
86 } \
87 } while (0)
88 #else
89 #define tor_free(p) do { if (PREDICT((p)!=NULL,1)) { free(p); (p)=NULL;} } \
90 while (0)
91 #endif
93 #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
94 #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
95 #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
96 #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
97 #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
98 #define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
100 /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
101 #if defined(__GNUC__) && __GNUC__ > 3
102 #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
103 #else
104 #define STRUCT_OFFSET(tp, member) \
105 ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
106 #endif
108 /* String manipulation */
109 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
110 void tor_strlower(char *s) ATTR_NONNULL((1));
111 void tor_strupper(char *s) ATTR_NONNULL((1));
112 int tor_strisprint(const char *s) ATTR_PURE ATTR_NONNULL((1));
113 int tor_strisnonupper(const char *s) ATTR_PURE ATTR_NONNULL((1));
114 int strcmpstart(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
115 int strcasecmpstart(const char *s1, const char *s2)
116 ATTR_PURE ATTR_NONNULL((1,2));
117 int strcmpend(const char *s1, const char *s2) ATTR_PURE ATTR_NONNULL((1,2));
118 int strcasecmpend(const char *s1, const char *s2)
119 ATTR_PURE ATTR_NONNULL((1,2));
120 int tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
121 typedef enum {
122 ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
123 } part_finish_rule_t;
124 int tor_strpartition(char *dest, size_t dest_len,
125 const char *s, const char *insert, size_t n,
126 part_finish_rule_t rule);
127 long tor_parse_long(const char *s, int base, long min,
128 long max, int *ok, char **next);
129 unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
130 unsigned long max, int *ok, char **next);
131 uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
132 uint64_t max, int *ok, char **next);
133 const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
134 const char *eat_whitespace(const char *s) ATTR_PURE;
135 const char *eat_whitespace_no_nl(const char *s) ATTR_PURE;
136 const char *find_whitespace(const char *s) ATTR_PURE;
137 int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE;
138 int tor_digest_is_zero(const char *digest) ATTR_PURE;
139 char *esc_for_log(const char *string) ATTR_MALLOC;
140 const char *escaped(const char *string);
142 void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
143 int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
145 /* Time helpers */
146 long tv_udiff(struct timeval *start, struct timeval *end);
147 void tv_addms(struct timeval *a, long ms);
148 void tv_add(struct timeval *a, struct timeval *b);
149 int tv_cmp(struct timeval *a, struct timeval *b);
150 time_t tor_timegm(struct tm *tm);
151 #define RFC1123_TIME_LEN 29
152 void format_rfc1123_time(char *buf, time_t t);
153 int parse_rfc1123_time(const char *buf, time_t *t);
154 #define ISO_TIME_LEN 19
155 void format_local_iso_time(char *buf, time_t t);
156 void format_iso_time(char *buf, time_t t);
157 int parse_iso_time(const char *buf, time_t *t);
159 /* File helpers */
160 int write_all(int fd, const char *buf, size_t count, int isSocket);
161 int read_all(int fd, char *buf, size_t count, int isSocket);
163 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
164 file_status_t file_status(const char *filename);
166 typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
167 int check_private_dir(const char *dirname, cpd_check_t check);
168 int write_str_to_file(const char *fname, const char *str, int bin);
169 int write_bytes_to_file(const char *fname, const char *str, size_t len,
170 int bin);
171 /** An ad-hoc type to hold a string of characters and a count; used by
172 * write_chunks_to_file. */
173 typedef struct sized_chunk_t {
174 const char *bytes;
175 size_t len;
176 } sized_chunk_t;
177 struct smartlist_t;
178 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
179 int bin);
180 int append_bytes_to_file(const char *fname, const char *str, size_t len,
181 int bin);
183 char *read_file_to_str(const char *filename, int bin, size_t *size_out)
184 ATTR_MALLOC;
185 char *parse_line_from_str(char *line, char **key_out, char **value_out);
186 char *expand_filename(const char *filename);
187 struct smartlist_t *tor_listdir(const char *dirname);
188 int path_is_relative(const char *filename) ATTR_PURE;
190 /* Net helpers */
191 int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
192 int parse_addr_port(int severity, const char *addrport, char **address,
193 uint32_t *addr, uint16_t *port_out);
194 int parse_port_range(const char *port, uint16_t *port_min_out,
195 uint16_t *port_max_out);
196 int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
197 uint32_t *mask_out, uint16_t *port_min_out,
198 uint16_t *port_max_out);
199 int addr_mask_get_bits(uint32_t mask);
200 #define INET_NTOA_BUF_LEN 16
201 int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
202 char *tor_dup_addr(uint32_t addr) ATTR_MALLOC;
203 int is_plausible_address(const char *name);
204 int get_interface_address(int severity, uint32_t *addr);
206 /* Process helpers */
207 void start_daemon(void);
208 void finish_daemon(const char *desired_cwd);
209 void write_pidfile(char *filename);
211 #endif