util.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file util.h
  6. * \brief Headers for util.c
  7. **/
  8. #ifndef __UTIL_H
  9. #define __UTIL_H
  10. #include "orconfig.h"
  11. #include "torint.h"
  12. #include <stdio.h>
  13. #ifdef HAVE_SYS_TIME_H
  14. #include <sys/time.h>
  15. #endif
  16. #ifdef HAVE_TIME_H
  17. #include <time.h>
  18. #endif
  19. #if _MSC_VER > 1300
  20. #include <winsock2.h>
  21. #include <ws2tcpip.h>
  22. #elif defined(_MSC_VER)
  23. #include <winsock.h>
  24. #endif
  25. #ifndef HAVE_GETTIMEOFDAY
  26. #ifdef HAVE_FTIME
  27. #define USING_FAKE_TIMEVAL
  28. #include <sys/timeb.h>
  29. struct timeval {
  30. time_t tv_sec;
  31. unsigned int tv_usec;
  32. };
  33. #endif
  34. #endif
  35. #ifdef MS_WINDOWS
  36. /* Windows names string functions differently from most other platforms. */
  37. #define strncasecmp strnicmp
  38. #define strcasecmp stricmp
  39. /* "inline" is __inline on windows. " */
  40. #define INLINE __inline
  41. /* Windows compilers before VC7 don't have __FUNCTION__. */
  42. #if _MSC_VER < 1300
  43. #define __FUNCTION__ "???"
  44. #endif
  45. #else
  46. #define INLINE inline
  47. #endif
  48. /** Replace assert() with a variant that sends failures to the log before
  49. * calling assert() normally.
  50. */
  51. #ifdef NDEBUG
  52. #define tor_assert(expr) do {} while(0)
  53. #else
  54. #define tor_assert(expr) do { \
  55. if (!(expr)) { \
  56. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  57. __FILE__, __LINE__, __FUNCTION__, #expr); \
  58. assert(expr); /* write to console too. */ \
  59. abort(); /* unreached */ \
  60. } } while (0)
  61. #endif
  62. #ifdef MS_WINDOWS
  63. /** On windows, you have to call close() on fds returned by open(),
  64. * and closesocket() on fds returned by socket(). On Unix, everything
  65. * gets close()'d. We abstract this difference by always using
  66. * tor_close_socket to close sockets, and always using close() on
  67. * files.
  68. */
  69. #define tor_close_socket(s) closesocket(s)
  70. #else
  71. #define tor_close_socket(s) close(s)
  72. #endif
  73. /** Legal characters in a filename */
  74. #ifdef MS_WINDOWS
  75. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/\\ "
  76. #else
  77. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/ "
  78. #endif
  79. size_t strlcat(char *dst, const char *src, size_t siz);
  80. size_t strlcpy(char *dst, const char *src, size_t siz);
  81. void *tor_malloc(size_t size);
  82. void *tor_malloc_zero(size_t size);
  83. void *tor_realloc(void *ptr, size_t size);
  84. char *tor_strdup(const char *s);
  85. char *tor_strndup(const char *s, size_t n);
  86. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  87. void tor_strlower(char *s);
  88. /* Some platforms segfault when you try to access a multi-byte type
  89. * that isn't aligned to a word boundary. The macros and/or functions
  90. * below can be used to access unaligned data on any platform.
  91. */
  92. #ifdef UNALIGNED_INT_ACCESS_OK
  93. #define get_uint16(cp) (*(uint16_t*)(cp))
  94. #define get_uint32(cp) (*(uint32_t*)(cp))
  95. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  96. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  97. #else
  98. #if 1
  99. uint16_t get_uint16(const char *cp);
  100. uint32_t get_uint32(const char *cp);
  101. void set_uint16(char *cp, uint16_t v);
  102. void set_uint32(char *cp, uint32_t v);
  103. #else
  104. #define get_uint16(cp) \
  105. ( ((*(((uint8_t*)(cp))+0))<<8) + \
  106. ((*(((uint8_t*)(cp))+1)) ) )
  107. #define get_uint32(cp) \
  108. ( ((*(((uint8_t*)(cp))+0))<<24) + \
  109. ((*(((uint8_t*)(cp))+1))<<16) + \
  110. ((*(((uint8_t*)(cp))+2))<<8 ) + \
  111. ((*(((uint8_t*)(cp))+3)) ) )
  112. #define set_uint16(cp,v) \
  113. do { \
  114. uint16_t u16v = (v); \
  115. *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
  116. *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
  117. } while (0)
  118. #define set_uint32(cp,val) \
  119. do { \
  120. uint32_t u32v = (v); \
  121. *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
  122. *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
  123. *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
  124. *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
  125. } while (0)
  126. #endif
  127. #endif
  128. void hex_encode(const char *from, int fromlen, char *to);
  129. const char *hex_str(const char *from, int fromlen);
  130. /** Generic resizeable array. */
  131. typedef struct smartlist_t smartlist_t;
  132. smartlist_t *smartlist_create();
  133. void smartlist_free(smartlist_t *sl);
  134. void smartlist_set_capacity(smartlist_t *sl, int n);
  135. void smartlist_clear(smartlist_t *sl);
  136. void smartlist_truncate(smartlist_t *sl, int n);
  137. void smartlist_add(smartlist_t *sl, void *element);
  138. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  139. void smartlist_remove(smartlist_t *sl, void *element);
  140. int smartlist_isin(const smartlist_t *sl, void *element);
  141. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  142. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  143. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  144. void *smartlist_choose(const smartlist_t *sl);
  145. void *smartlist_get(const smartlist_t *sl, int idx);
  146. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  147. void *smartlist_del(smartlist_t *sl, int idx);
  148. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  149. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  150. int smartlist_len(const smartlist_t *sl);
  151. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  152. do { \
  153. int sl_idx, sl_len=smartlist_len(sl); \
  154. type var; \
  155. for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
  156. var = smartlist_get((sl),sl_idx); \
  157. do {cmd;} while(0); \
  158. } } while (0)
  159. /* Map from const char * to void*. Implemented with a splay tree. */
  160. typedef struct strmap_t strmap_t;
  161. typedef struct strmap_entry_t strmap_entry_t;
  162. typedef struct strmap_entry_t strmap_iter_t;
  163. strmap_t* strmap_new(void);
  164. void* strmap_set(strmap_t *map, const char *key, void *val);
  165. void* strmap_get(strmap_t *map, const char *key);
  166. void* strmap_remove(strmap_t *map, const char *key);
  167. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  168. void* strmap_get_lc(strmap_t *map, const char *key);
  169. void* strmap_remove_lc(strmap_t *map, const char *key);
  170. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  171. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  172. void strmap_free(strmap_t *map, void (*free_val)(void*));
  173. strmap_iter_t *strmap_iter_init(strmap_t *map);
  174. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  175. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  176. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  177. int strmap_iter_done(strmap_iter_t *iter);
  178. /* String manipulation */
  179. const char *eat_whitespace(const char *s);
  180. const char *eat_whitespace_no_nl(const char *s);
  181. const char *find_whitespace(const char *s);
  182. /* Time helpers */
  183. void tor_gettimeofday(struct timeval *timeval);
  184. long tv_udiff(struct timeval *start, struct timeval *end);
  185. void tv_addms(struct timeval *a, long ms);
  186. void tv_add(struct timeval *a, struct timeval *b);
  187. int tv_cmp(struct timeval *a, struct timeval *b);
  188. time_t tor_timegm(struct tm *tm);
  189. int write_all(int fd, const char *buf, size_t count, int isSocket);
  190. int read_all(int fd, char *buf, size_t count, int isSocket);
  191. void set_socket_nonblocking(int socket);
  192. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  193. file_status_t file_status(const char *filename);
  194. int check_private_dir(const char *dirname, int create);
  195. int write_str_to_file(const char *fname, const char *str);
  196. char *read_file_to_str(const char *filename);
  197. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  198. char *expand_filename(const char *filename);
  199. int spawn_func(int (*func)(void *), void *data);
  200. void spawn_exit();
  201. /* Because we use threads instead of processes on Windows, we need locking on Windows.
  202. * On Unixy platforms, these functions are no-ops. */
  203. typedef struct tor_mutex_t tor_mutex_t;
  204. tor_mutex_t *tor_mutex_new(void);
  205. void tor_mutex_acquire(tor_mutex_t *m);
  206. void tor_mutex_release(tor_mutex_t *m);
  207. void tor_mutex_free(tor_mutex_t *m);
  208. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  209. int is_internal_IP(uint32_t ip);
  210. int is_local_IP(uint32_t ip);
  211. const char *get_uname(void);
  212. void start_daemon(const char *desired_cwd);
  213. void finish_daemon(void);
  214. void write_pidfile(char *filename);
  215. int switch_id(char *user, char *group);
  216. struct in_addr;
  217. int tor_inet_aton(const char *cp, struct in_addr *addr);
  218. int tor_lookup_hostname(const char *name, uint32_t *addr);
  219. /* For stupid historical reasons, windows sockets have an independent
  220. * set of errnos, and an independent way to get them. Also, you can't
  221. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  222. * errnos against expected values, and use tor_socket_errno to find
  223. * the actual errno after a socket operation fails.
  224. */
  225. #ifdef MS_WINDOWS
  226. /** Return true if e is EAGAIN or the local equivalent. */
  227. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  228. /** Return true if e is EINPROGRESS or the local equivalent. */
  229. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  230. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  231. * a call to connect(). */
  232. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
  233. int tor_socket_errno(int sock);
  234. const char *tor_socket_strerror(int e);
  235. #else
  236. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  237. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  238. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  239. #define tor_socket_errno(sock) (errno)
  240. #define tor_socket_strerror(e) strerror(e)
  241. #endif
  242. #endif
  243. /*
  244. Local Variables:
  245. mode:c
  246. indent-tabs-mode:nil
  247. c-basic-offset:2
  248. End:
  249. */