~ubuntu-branches/ubuntu/hoary/tor/hoary

« back to all changes in this revision

Viewing changes to src/common/util.h

  • Committer: Bazaar Package Importer
  • Author(s): Peter Palfrader
  • Date: 2004-06-07 21:46:08 UTC
  • Revision ID: james.westby@ubuntu.com-20040607214608-do51p01di99xdmjr
Tags: upstream-0.0.7
ImportĀ upstreamĀ versionĀ 0.0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $ */
 
4
 
 
5
/**
 
6
 * \file util.h
 
7
 * \brief Headers for util.c
 
8
 **/
 
9
 
 
10
#ifndef __UTIL_H
 
11
#define __UTIL_H
 
12
 
 
13
#include "orconfig.h"
 
14
#include "torint.h"
 
15
#include <stdio.h>
 
16
#ifdef HAVE_SYS_TIME_H
 
17
#include <sys/time.h>
 
18
#endif
 
19
#ifdef HAVE_TIME_H
 
20
#include <time.h>
 
21
#endif
 
22
 
 
23
#if _MSC_VER > 1300
 
24
#include <winsock2.h>
 
25
#include <ws2tcpip.h>
 
26
#elif defined(_MSC_VER)
 
27
#include <winsock.h>
 
28
#endif
 
29
#ifndef HAVE_GETTIMEOFDAY
 
30
#ifdef HAVE_FTIME
 
31
#define USING_FAKE_TIMEVAL
 
32
#include <sys/timeb.h>
 
33
#define timeval timeb
 
34
#define tv_sec time
 
35
#define tv_usec millitm
 
36
#endif
 
37
#endif
 
38
 
 
39
#ifdef MS_WINDOWS
 
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__. */
 
46
#if _MSC_VER < 1300
 
47
#define __FUNCTION__ "???"
 
48
#endif
 
49
#else
 
50
#define INLINE inline
 
51
#endif
 
52
 
 
53
/** Replace assert() with a variant that sends failures to the log before
 
54
 * calling assert() normally.
 
55
 */
 
56
#ifdef NDEBUG
 
57
#define tor_assert(expr) do {} while(0)
 
58
#else
 
59
#define tor_assert(expr) do {                                 \
 
60
 if (!(expr)) {                                               \
 
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 */                                  \
 
65
 } } while (0)
 
66
#endif
 
67
 
 
68
#ifdef MS_WINDOWS
 
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
 
73
 * files.
 
74
 */
 
75
#define tor_close_socket(s) closesocket(s)
 
76
#else
 
77
#define tor_close_socket(s) close(s)
 
78
#endif
 
79
 
 
80
/** Legal characters in a filename */
 
81
#ifdef MS_WINDOWS
 
82
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/\\ "
 
83
#else
 
84
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/ "
 
85
#endif
 
86
 
 
87
size_t strlcat(char *dst, const char *src, size_t siz);
 
88
size_t strlcpy(char *dst, const char *src, size_t siz);
 
89
 
 
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);
 
97
 
 
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.
 
101
 */
 
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)
 
107
#else
 
108
#if 1
 
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);
 
113
#else
 
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)                        \
 
123
  do {                                          \
 
124
    uint16_t u16v = (v);                        \
 
125
    *(((uint8_t*)(cp))+0) = (v >> 8)&0xff;      \
 
126
    *(((uint8_t*)(cp))+1) = (v >> 0)&0xff;      \
 
127
  } while (0)
 
128
#define set_uint32(cp,val)                      \
 
129
  do {                                          \
 
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;     \
 
135
  } while (0)
 
136
#endif
 
137
#endif
 
138
 
 
139
void hex_encode(const char *from, int fromlen, char *to);
 
140
const char *hex_str(const char *from, int fromlen);
 
141
 
 
142
/** Generic resizeable array. */
 
143
typedef struct smartlist_t smartlist_t;
 
144
 
 
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)                   \
 
165
  do {                                                          \
 
166
    int sl_idx, sl_len=smartlist_len(sl);                       \
 
167
    type var;                                                   \
 
168
    for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) {                \
 
169
      var = smartlist_get((sl),sl_idx);                         \
 
170
      do {cmd;} while(0);                                       \
 
171
    } } while (0)
 
172
 
 
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*));
 
187
 
 
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);
 
192
 
 
193
int strmap_iter_done(strmap_iter_t *iter);
 
194
 
 
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);
 
199
 
 
200
/* Time helpers */
 
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);
 
207
 
 
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);
 
210
 
 
211
void set_socket_nonblocking(int socket);
 
212
 
 
213
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
 
214
 
 
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);
 
220
 
 
221
int spawn_func(int (*func)(void *), void *data);
 
222
void spawn_exit();
 
223
 
 
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);
 
231
 
 
232
int tor_socketpair(int family, int type, int protocol, int fd[2]);
 
233
 
 
234
int is_internal_IP(uint32_t ip);
 
235
 
 
236
const char *get_uname(void);
 
237
 
 
238
void start_daemon(char *desired_cwd);
 
239
void finish_daemon(void);
 
240
 
 
241
void write_pidfile(char *filename);
 
242
int switch_id(char *user, char *group);
 
243
 
 
244
struct in_addr;
 
245
int tor_inet_aton(const char *cp, struct in_addr *addr);
 
246
int tor_lookup_hostname(const char *name, uint32_t *addr);
 
247
 
 
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.
 
253
 */
 
254
#ifdef MS_WINDOWS
 
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);
 
264
#else
 
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)
 
270
#endif
 
271
 
 
272
#endif
 
273
 
 
274
/*
 
275
  Local Variables:
 
276
  mode:c
 
277
  indent-tabs-mode:nil
 
278
  c-basic-offset:2
 
279
  End:
 
280
*/