Make Tor build on win32 with VC6 without warnings.
[tor.git] / src / common / util.h
blobdcfe7a94dc5d1400102675ad27f1386a3baafad8
1 /* Copyright 2003 Roger Dingledine */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __UTIL_H
6 #define __UTIL_H
8 #include "orconfig.h"
9 #include "torint.h"
10 #include <stdio.h>
11 #ifdef HAVE_SYS_TIME_H
12 #include <sys/time.h>
13 #endif
14 #ifdef HAVE_TIME_H
15 #include <time.h>
16 #endif
18 #if _MSC_VER > 1300
19 #include <winsock2.h>
20 #include <ws2tcpip.h>
21 #elif defined(_MSC_VER)
22 #include <winsock.h>
23 #endif
24 #ifndef HAVE_GETTIMEOFDAY
25 #ifdef HAVE_FTIME
26 #define USING_FAKE_TIMEVAL
27 #include <sys/timeb.h>
28 #define timeval timeb
29 #define tv_sec time
30 #define tv_usec millitm
31 #endif
32 #endif
34 #ifdef MS_WINDOWS
35 /* Windows names string functions funnily. */
36 #define strncasecmp strnicmp
37 #define strcasecmp stricmp
38 #define INLINE __inline
39 #define _ARRAYSIZE(x) (((x)==0)?1:0)
40 /* Windows compilers before VC7 don't have __FUNCTION__. */
41 #if _MSC_VER < 1300
42 #define __FUNCTION__ "???"
43 #endif
44 #else
45 #define INLINE inline
46 #define _ARRAYSIZE(x) (x)
47 #endif
49 #ifdef NDEBUG
50 #define tor_assert(expr) do {} while(0)
51 #else
52 #define tor_assert(expr) do { \
53 if (!(expr)) { \
54 log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
55 __FILE__, __LINE__, __FUNCTION__, #expr); \
56 assert(expr); /* write to console too. */ \
57 abort(); /* unreached */ \
58 } } while (0)
59 #endif
61 /* legal characters in a filename */
62 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
64 size_t strlcat(char *dst, const char *src, size_t siz);
65 size_t strlcpy(char *dst, const char *src, size_t siz);
67 void *tor_malloc(size_t size);
68 void *tor_malloc_zero(size_t size);
69 void *tor_realloc(void *ptr, size_t size);
70 char *tor_strdup(const char *s);
71 char *tor_strndup(const char *s, size_t n);
72 #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
73 void tor_strlower(char *s);
75 #ifdef UNALIGNED_INT_ACCESS_OK
76 /* XXX Not actually used yet, but would probably be faster on non-sun
77 * hardare.
79 #define get_uint16(cp) (*(uint16_t*)(cp))
80 #define get_uint32(cp) (*(uint32_t*)(cp))
81 #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
82 #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
83 #else
84 #if 1
85 uint16_t get_uint16(const char *cp);
86 uint32_t get_uint32(const char *cp);
87 void set_uint16(char *cp, uint16_t v);
88 void set_uint32(char *cp, uint32_t v);
89 #else
90 #define get_uint16(cp) \
91 ( ((*(((uint8_t*)(cp))+0))<<8) + \
92 ((*(((uint8_t*)(cp))+1)) ) )
93 #define get_uint32(cp) \
94 ( ((*(((uint8_t*)(cp))+0))<<24) + \
95 ((*(((uint8_t*)(cp))+1))<<16) + \
96 ((*(((uint8_t*)(cp))+2))<<8 ) + \
97 ((*(((uint8_t*)(cp))+3)) ) )
98 #define set_uint16(cp,v) \
99 do { \
100 uint16_t u16v = (v); \
101 *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
102 *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
103 } while (0)
104 #define set_uint32(cp,val) \
105 do { \
106 uint32_t u32v = (v); \
107 *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
108 *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
109 *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
110 *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
111 } while (0)
112 #endif
113 #endif
115 void hex_encode(const char *from, int fromlen, char *to);
116 const char *hex_str(const char *from, int fromlen);
118 typedef struct smartlist_t smartlist_t;
120 smartlist_t *smartlist_create();
121 void smartlist_free(smartlist_t *sl);
122 void smartlist_set_capacity(smartlist_t *sl, int n);
123 void smartlist_clear(smartlist_t *sl);
124 void smartlist_truncate(smartlist_t *sl, int n);
125 void smartlist_add(smartlist_t *sl, void *element);
126 void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
127 void smartlist_remove(smartlist_t *sl, void *element);
128 int smartlist_isin(const smartlist_t *sl, void *element);
129 int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
130 void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
131 void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
132 void *smartlist_choose(const smartlist_t *sl);
133 void *smartlist_get(const smartlist_t *sl, int idx);
134 void *smartlist_set(smartlist_t *sl, int idx, void *val);
135 void *smartlist_del(smartlist_t *sl, int idx);
136 void *smartlist_del_keeporder(smartlist_t *sl, int idx);
137 void smartlist_insert(smartlist_t *sl, int idx, void *val);
138 int smartlist_len(const smartlist_t *sl);
139 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
140 do { \
141 int sl_idx, sl_len=smartlist_len(sl); \
142 type var; \
143 for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
144 var = smartlist_get((sl),sl_idx); \
145 do {cmd;} while(0); \
146 } } while (0)
148 /* Map from const char * to void*. Implemented with a splay tree. */
149 typedef struct strmap_t strmap_t;
150 typedef struct strmap_entry_t strmap_entry_t;
151 typedef struct strmap_entry_t strmap_iter_t;
152 strmap_t* strmap_new(void);
153 void* strmap_set(strmap_t *map, const char *key, void *val);
154 void* strmap_get(strmap_t *map, const char *key);
155 void* strmap_remove(strmap_t *map, const char *key);
156 void* strmap_set_lc(strmap_t *map, const char *key, void *val);
157 void* strmap_get_lc(strmap_t *map, const char *key);
158 void* strmap_remove_lc(strmap_t *map, const char *key);
159 typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
160 void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
161 void strmap_free(strmap_t *map, void (*free_val)(void*));
163 strmap_iter_t *strmap_iter_init(strmap_t *map);
164 strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
165 strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
166 void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
168 int strmap_iter_done(strmap_iter_t *iter);
170 const char *eat_whitespace(const char *s);
171 const char *eat_whitespace_no_nl(const char *s);
172 const char *find_whitespace(const char *s);
174 void tor_gettimeofday(struct timeval *timeval);
175 long tv_udiff(struct timeval *start, struct timeval *end);
176 void tv_addms(struct timeval *a, long ms);
177 void tv_add(struct timeval *a, struct timeval *b);
178 int tv_cmp(struct timeval *a, struct timeval *b);
179 time_t tor_timegm(struct tm *tm);
181 int write_all(int fd, const char *buf, size_t count, int isSocket);
182 int read_all(int fd, char *buf, size_t count, int isSocket);
184 void set_socket_nonblocking(int socket);
186 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
188 file_status_t file_status(const char *filename);
189 int check_private_dir(const char *dirname, int create);
190 int write_str_to_file(const char *fname, const char *str);
191 char *read_file_to_str(const char *filename);
192 int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
194 int spawn_func(int (*func)(void *), void *data);
195 void spawn_exit();
197 int tor_socketpair(int family, int type, int protocol, int fd[2]);
199 int is_internal_IP(uint32_t ip);
201 const char *get_uname(void);
203 /* Start putting the process into daemon mode: fork and drop all resources
204 * except standard fds. The parent process never returns, but stays around
205 * until finish_daemon is called. (Note: it's safe to call this more
206 * than once: calls after the first are ignored.)
208 void start_daemon(char *desired_cwd);
209 /* Finish putting the process into daemon mode: drop standard fds, and tell
210 * the parent process to exit. (Note: it's safe to call this more than once:
211 * calls after the first are ignored. Calls start_daemon first if it hasn't
212 * been called already.)
214 void finish_daemon(void);
216 void write_pidfile(char *filename);
217 int switch_id(char *user, char *group);
219 struct in_addr;
220 int tor_inet_aton(const char *cp, struct in_addr *addr);
221 int tor_lookup_hostname(const char *name, uint32_t *addr);
223 /* For stupid historical reasons, windows sockets have an independent set of
224 * errnos which they use as the fancy strikes them.
226 #ifdef MS_WINDOWS
227 #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
228 #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
229 #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
230 int correct_socket_errno(int s);
231 #else
232 #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
233 #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
234 #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
235 #define correct_socket_errno(s) (errno)
236 #endif
238 #endif
241 Local Variables:
242 mode:c
243 indent-tabs-mode:nil
244 c-basic-offset:2
245 End: