util.h 9.9 KB

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