Use socketclose on windows as appropriate; end pid files with newline
[tor.git] / src / common / util.h
blob555aa6b050bb5f5251893201bdf10b7237710728
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 /* Windows compilers before VC7 don't have __FUNCTION__. */
40 #if _MSC_VER < 1300
41 #define __FUNCTION__ "???"
42 #endif
43 #else
44 #define INLINE inline
45 #endif
47 #ifdef NDEBUG
48 #define tor_assert(expr) do {} while(0)
49 #else
50 #define tor_assert(expr) do { \
51 if (!(expr)) { \
52 log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
53 __FILE__, __LINE__, __FUNCTION__, #expr); \
54 assert(expr); /* write to console too. */ \
55 abort(); /* unreached */ \
56 } } while (0)
57 #endif
59 #ifdef MS_WINDOWS
60 #define tor_close_socket(s) socketclose(s)
61 #else
62 #define tor_close_socket(s) close(s)
63 #endif
65 /* legal characters in a filename */
66 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
68 size_t strlcat(char *dst, const char *src, size_t siz);
69 size_t strlcpy(char *dst, const char *src, size_t siz);
71 void *tor_malloc(size_t size);
72 void *tor_malloc_zero(size_t size);
73 void *tor_realloc(void *ptr, size_t size);
74 char *tor_strdup(const char *s);
75 char *tor_strndup(const char *s, size_t n);
76 #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
77 void tor_strlower(char *s);
79 #ifdef UNALIGNED_INT_ACCESS_OK
80 /* XXX Not actually used yet, but would probably be faster on non-sun
81 * hardare.
83 #define get_uint16(cp) (*(uint16_t*)(cp))
84 #define get_uint32(cp) (*(uint32_t*)(cp))
85 #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
86 #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
87 #else
88 #if 1
89 uint16_t get_uint16(const char *cp);
90 uint32_t get_uint32(const char *cp);
91 void set_uint16(char *cp, uint16_t v);
92 void set_uint32(char *cp, uint32_t v);
93 #else
94 #define get_uint16(cp) \
95 ( ((*(((uint8_t*)(cp))+0))<<8) + \
96 ((*(((uint8_t*)(cp))+1)) ) )
97 #define get_uint32(cp) \
98 ( ((*(((uint8_t*)(cp))+0))<<24) + \
99 ((*(((uint8_t*)(cp))+1))<<16) + \
100 ((*(((uint8_t*)(cp))+2))<<8 ) + \
101 ((*(((uint8_t*)(cp))+3)) ) )
102 #define set_uint16(cp,v) \
103 do { \
104 uint16_t u16v = (v); \
105 *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
106 *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
107 } while (0)
108 #define set_uint32(cp,val) \
109 do { \
110 uint32_t u32v = (v); \
111 *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
112 *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
113 *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
114 *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
115 } while (0)
116 #endif
117 #endif
119 void hex_encode(const char *from, int fromlen, char *to);
120 const char *hex_str(const char *from, int fromlen);
122 typedef struct smartlist_t smartlist_t;
124 smartlist_t *smartlist_create();
125 void smartlist_free(smartlist_t *sl);
126 void smartlist_set_capacity(smartlist_t *sl, int n);
127 void smartlist_clear(smartlist_t *sl);
128 void smartlist_truncate(smartlist_t *sl, int n);
129 void smartlist_add(smartlist_t *sl, void *element);
130 void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
131 void smartlist_remove(smartlist_t *sl, void *element);
132 int smartlist_isin(const smartlist_t *sl, void *element);
133 int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
134 void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
135 void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
136 void *smartlist_choose(const smartlist_t *sl);
137 void *smartlist_get(const smartlist_t *sl, int idx);
138 void *smartlist_set(smartlist_t *sl, int idx, void *val);
139 void *smartlist_del(smartlist_t *sl, int idx);
140 void *smartlist_del_keeporder(smartlist_t *sl, int idx);
141 void smartlist_insert(smartlist_t *sl, int idx, void *val);
142 int smartlist_len(const smartlist_t *sl);
143 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
144 do { \
145 int sl_idx, sl_len=smartlist_len(sl); \
146 type var; \
147 for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
148 var = smartlist_get((sl),sl_idx); \
149 do {cmd;} while(0); \
150 } } while (0)
152 /* Map from const char * to void*. Implemented with a splay tree. */
153 typedef struct strmap_t strmap_t;
154 typedef struct strmap_entry_t strmap_entry_t;
155 typedef struct strmap_entry_t strmap_iter_t;
156 strmap_t* strmap_new(void);
157 void* strmap_set(strmap_t *map, const char *key, void *val);
158 void* strmap_get(strmap_t *map, const char *key);
159 void* strmap_remove(strmap_t *map, const char *key);
160 void* strmap_set_lc(strmap_t *map, const char *key, void *val);
161 void* strmap_get_lc(strmap_t *map, const char *key);
162 void* strmap_remove_lc(strmap_t *map, const char *key);
163 typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
164 void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
165 void strmap_free(strmap_t *map, void (*free_val)(void*));
167 strmap_iter_t *strmap_iter_init(strmap_t *map);
168 strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
169 strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
170 void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
172 int strmap_iter_done(strmap_iter_t *iter);
174 const char *eat_whitespace(const char *s);
175 const char *eat_whitespace_no_nl(const char *s);
176 const char *find_whitespace(const char *s);
178 void tor_gettimeofday(struct timeval *timeval);
179 long tv_udiff(struct timeval *start, struct timeval *end);
180 void tv_addms(struct timeval *a, long ms);
181 void tv_add(struct timeval *a, struct timeval *b);
182 int tv_cmp(struct timeval *a, struct timeval *b);
183 time_t tor_timegm(struct tm *tm);
185 int write_all(int fd, const char *buf, size_t count, int isSocket);
186 int read_all(int fd, char *buf, size_t count, int isSocket);
188 void set_socket_nonblocking(int socket);
190 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
192 file_status_t file_status(const char *filename);
193 int check_private_dir(const char *dirname, int create);
194 int write_str_to_file(const char *fname, const char *str);
195 char *read_file_to_str(const char *filename);
196 int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
198 int spawn_func(int (*func)(void *), void *data);
199 void spawn_exit();
201 int tor_socketpair(int family, int type, int protocol, int fd[2]);
203 int is_internal_IP(uint32_t ip);
205 const char *get_uname(void);
207 /* Start putting the process into daemon mode: fork and drop all resources
208 * except standard fds. The parent process never returns, but stays around
209 * until finish_daemon is called. (Note: it's safe to call this more
210 * than once: calls after the first are ignored.)
212 void start_daemon(char *desired_cwd);
213 /* Finish putting the process into daemon mode: drop standard fds, and tell
214 * the parent process to exit. (Note: it's safe to call this more than once:
215 * calls after the first are ignored. Calls start_daemon first if it hasn't
216 * been called already.)
218 void finish_daemon(void);
220 void write_pidfile(char *filename);
221 int switch_id(char *user, char *group);
223 struct in_addr;
224 int tor_inet_aton(const char *cp, struct in_addr *addr);
225 int tor_lookup_hostname(const char *name, uint32_t *addr);
227 /* For stupid historical reasons, windows sockets have an independent set of
228 * errnos which they use as the fancy strikes them.
230 #ifdef MS_WINDOWS
231 #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
232 #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
233 #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
234 int correct_socket_errno(int s);
235 #else
236 #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
237 #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
238 #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
239 #define correct_socket_errno(s) (errno)
240 #endif
242 #endif
245 Local Variables:
246 mode:c
247 indent-tabs-mode:nil
248 c-basic-offset:2
249 End: