util.h 10 KB

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