1
/* Copyright 2003 Roger Dingledine */
2
/* See LICENSE for licensing information */
3
/* $Id: util.h,v 1.71 2004/06/05 01:56:54 nickm Exp $ */
7
* \brief Headers for util.c
16
#ifdef HAVE_SYS_TIME_H
26
#elif defined(_MSC_VER)
29
#ifndef HAVE_GETTIMEOFDAY
31
#define USING_FAKE_TIMEVAL
32
#include <sys/timeb.h>
35
#define tv_usec millitm
40
/* Windows names string functions differently from most other platforms. */
41
#define strncasecmp strnicmp
42
#define strcasecmp stricmp
43
/* "inline" is __inline on windows. " */
44
#define INLINE __inline
45
/* Windows compilers before VC7 don't have __FUNCTION__. */
47
#define __FUNCTION__ "???"
53
/** Replace assert() with a variant that sends failures to the log before
54
* calling assert() normally.
57
#define tor_assert(expr) do {} while(0)
59
#define tor_assert(expr) do { \
61
log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
62
__FILE__, __LINE__, __FUNCTION__, #expr); \
63
assert(expr); /* write to console too. */ \
64
abort(); /* unreached */ \
69
/** On windows, you have to call close() on fds returned by open(),
70
* and closesocket() on fds returned by socket(). On Unix, everything
71
* gets close()'d. We abstract this difference by always using
72
* tor_close_socket to close sockets, and always using close() on
75
#define tor_close_socket(s) closesocket(s)
77
#define tor_close_socket(s) close(s)
80
/** Legal characters in a filename */
82
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/\\ "
84
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/ "
87
size_t strlcat(char *dst, const char *src, size_t siz);
88
size_t strlcpy(char *dst, const char *src, size_t siz);
90
void *tor_malloc(size_t size);
91
void *tor_malloc_zero(size_t size);
92
void *tor_realloc(void *ptr, size_t size);
93
char *tor_strdup(const char *s);
94
char *tor_strndup(const char *s, size_t n);
95
#define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
96
void tor_strlower(char *s);
98
/* Some platforms segfault when you try to access a multi-byte type
99
* that isn't aligned to a word boundary. The macros and/or functions
100
* below can be used to access unaligned data on any platform.
102
#ifdef UNALIGNED_INT_ACCESS_OK
103
#define get_uint16(cp) (*(uint16_t*)(cp))
104
#define get_uint32(cp) (*(uint32_t*)(cp))
105
#define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
106
#define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
109
uint16_t get_uint16(const char *cp);
110
uint32_t get_uint32(const char *cp);
111
void set_uint16(char *cp, uint16_t v);
112
void set_uint32(char *cp, uint32_t v);
114
#define get_uint16(cp) \
115
( ((*(((uint8_t*)(cp))+0))<<8) + \
116
((*(((uint8_t*)(cp))+1)) ) )
117
#define get_uint32(cp) \
118
( ((*(((uint8_t*)(cp))+0))<<24) + \
119
((*(((uint8_t*)(cp))+1))<<16) + \
120
((*(((uint8_t*)(cp))+2))<<8 ) + \
121
((*(((uint8_t*)(cp))+3)) ) )
122
#define set_uint16(cp,v) \
124
uint16_t u16v = (v); \
125
*(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
126
*(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
128
#define set_uint32(cp,val) \
130
uint32_t u32v = (v); \
131
*(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
132
*(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
133
*(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
134
*(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
139
void hex_encode(const char *from, int fromlen, char *to);
140
const char *hex_str(const char *from, int fromlen);
142
/** Generic resizeable array. */
143
typedef struct smartlist_t smartlist_t;
145
smartlist_t *smartlist_create();
146
void smartlist_free(smartlist_t *sl);
147
void smartlist_set_capacity(smartlist_t *sl, int n);
148
void smartlist_clear(smartlist_t *sl);
149
void smartlist_truncate(smartlist_t *sl, int n);
150
void smartlist_add(smartlist_t *sl, void *element);
151
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
152
void smartlist_remove(smartlist_t *sl, void *element);
153
int smartlist_isin(const smartlist_t *sl, void *element);
154
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
155
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
156
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
157
void *smartlist_choose(const smartlist_t *sl);
158
void *smartlist_get(const smartlist_t *sl, int idx);
159
void *smartlist_set(smartlist_t *sl, int idx, void *val);
160
void *smartlist_del(smartlist_t *sl, int idx);
161
void *smartlist_del_keeporder(smartlist_t *sl, int idx);
162
void smartlist_insert(smartlist_t *sl, int idx, void *val);
163
int smartlist_len(const smartlist_t *sl);
164
#define SMARTLIST_FOREACH(sl, type, var, cmd) \
166
int sl_idx, sl_len=smartlist_len(sl); \
168
for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
169
var = smartlist_get((sl),sl_idx); \
170
do {cmd;} while(0); \
173
/* Map from const char * to void*. Implemented with a splay tree. */
174
typedef struct strmap_t strmap_t;
175
typedef struct strmap_entry_t strmap_entry_t;
176
typedef struct strmap_entry_t strmap_iter_t;
177
strmap_t* strmap_new(void);
178
void* strmap_set(strmap_t *map, const char *key, void *val);
179
void* strmap_get(strmap_t *map, const char *key);
180
void* strmap_remove(strmap_t *map, const char *key);
181
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
182
void* strmap_get_lc(strmap_t *map, const char *key);
183
void* strmap_remove_lc(strmap_t *map, const char *key);
184
typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
185
void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
186
void strmap_free(strmap_t *map, void (*free_val)(void*));
188
strmap_iter_t *strmap_iter_init(strmap_t *map);
189
strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
190
strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
191
void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
193
int strmap_iter_done(strmap_iter_t *iter);
195
/* String manipulation */
196
const char *eat_whitespace(const char *s);
197
const char *eat_whitespace_no_nl(const char *s);
198
const char *find_whitespace(const char *s);
201
void tor_gettimeofday(struct timeval *timeval);
202
long tv_udiff(struct timeval *start, struct timeval *end);
203
void tv_addms(struct timeval *a, long ms);
204
void tv_add(struct timeval *a, struct timeval *b);
205
int tv_cmp(struct timeval *a, struct timeval *b);
206
time_t tor_timegm(struct tm *tm);
208
int write_all(int fd, const char *buf, size_t count, int isSocket);
209
int read_all(int fd, char *buf, size_t count, int isSocket);
211
void set_socket_nonblocking(int socket);
213
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
215
file_status_t file_status(const char *filename);
216
int check_private_dir(const char *dirname, int create);
217
int write_str_to_file(const char *fname, const char *str);
218
char *read_file_to_str(const char *filename);
219
int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
221
int spawn_func(int (*func)(void *), void *data);
224
/* Because we use threads instead of processes on Windows, we need locking on Windows.
225
* On Unixy platforms, these functions are no-ops. */
226
typedef struct tor_mutex_t tor_mutex_t;
227
tor_mutex_t *tor_mutex_new(void);
228
void tor_mutex_acquire(tor_mutex_t *m);
229
void tor_mutex_release(tor_mutex_t *m);
230
void tor_mutex_free(tor_mutex_t *m);
232
int tor_socketpair(int family, int type, int protocol, int fd[2]);
234
int is_internal_IP(uint32_t ip);
236
const char *get_uname(void);
238
void start_daemon(char *desired_cwd);
239
void finish_daemon(void);
241
void write_pidfile(char *filename);
242
int switch_id(char *user, char *group);
245
int tor_inet_aton(const char *cp, struct in_addr *addr);
246
int tor_lookup_hostname(const char *name, uint32_t *addr);
248
/* For stupid historical reasons, windows sockets have an independent
249
* set of errnos, and an independent way to get them. Also, you can't
250
* always believe WSAEWOULDBLOCK. Use the macros below to compare
251
* errnos against expected values, and use tor_socket_errno to find
252
* the actual errno after a socket operation fails.
255
/** Return true if e is EAGAIN or the local equivalent. */
256
#define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
257
/** Return true if e is EINPROGRESS or the local equivalent. */
258
#define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
259
/** Return true if e is EINPROGRESS or the local equivalent as returned by
260
* a call to connect(). */
261
#define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
262
int tor_socket_errno(int sock);
263
const char *tor_socket_strerror(int e);
265
#define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
266
#define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
267
#define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
268
#define tor_socket_errno(sock) (errno)
269
#define tor_socket_strerror(e) strerror(e)