util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <stdarg.h>
  14. #ifdef HAVE_SYS_TIME_H
  15. #include <sys/time.h>
  16. #endif
  17. #ifdef HAVE_TIME_H
  18. #include <time.h>
  19. #endif
  20. #ifndef NULL_REP_IS_ZERO_BYTES
  21. #error "It seems your platform does not represent NULL as zero. We can't cope."
  22. #endif
  23. #ifdef MS_WINDOWS
  24. #if (_MSC_VER <= 1300)
  25. #include <winsock.h>
  26. #else
  27. #include <winsock2.h>
  28. #include <ws2tcpip.h>
  29. #endif
  30. #endif
  31. #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  32. struct timeval {
  33. time_t tv_sec;
  34. unsigned int tv_usec;
  35. };
  36. #endif
  37. #ifdef MS_WINDOWS
  38. /* Windows names string functions differently from most other platforms. */
  39. #define strncasecmp strnicmp
  40. #define strcasecmp stricmp
  41. /* "inline" is __inline on windows. " */
  42. #define INLINE __inline
  43. /* Windows compilers before VC7 don't have __FUNCTION__. */
  44. #if _MSC_VER < 1300
  45. #define __FUNCTION__ "???"
  46. #endif
  47. #else
  48. #define INLINE inline
  49. #endif
  50. /** Replace assert() with a variant that sends failures to the log before
  51. * calling assert() normally.
  52. */
  53. #ifdef NDEBUG
  54. #define tor_assert(expr) do {} while(0)
  55. #else
  56. #define tor_assert(expr) do { \
  57. if (!(expr)) { \
  58. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  59. __FILE__, __LINE__, __FUNCTION__, #expr); \
  60. assert(expr); /* write to console too. */ \
  61. abort(); /* unreached */ \
  62. } } while (0)
  63. #endif
  64. #ifdef MS_WINDOWS
  65. /** On windows, you have to call close() on fds returned by open(),
  66. * and closesocket() on fds returned by socket(). On Unix, everything
  67. * gets close()'d. We abstract this difference by always using
  68. * tor_close_socket to close sockets, and always using close() on
  69. * files.
  70. */
  71. #define tor_close_socket(s) closesocket(s)
  72. #else
  73. #define tor_close_socket(s) close(s)
  74. #endif
  75. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  76. size_t strlcat(char *dst, const char *src, size_t siz);
  77. size_t strlcpy(char *dst, const char *src, size_t siz);
  78. void *tor_malloc(size_t size);
  79. void *tor_malloc_zero(size_t size);
  80. void *tor_realloc(void *ptr, size_t size);
  81. char *tor_strdup(const char *s);
  82. char *tor_strndup(const char *s, size_t n);
  83. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  84. void tor_strlower(char *s);
  85. int strcmpstart(const char *s1, const char *s2);
  86. int tor_strstrip(char *s, const char *strip);
  87. typedef enum {
  88. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  89. } part_finish_rule_t;
  90. int tor_strpartition(char *dest, size_t dest_len,
  91. const char *s, const char *insert, size_t n,
  92. part_finish_rule_t rule);
  93. long tor_parse_long(const char *s, int base, long min,
  94. long max, int *ok, char **next);
  95. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  96. unsigned long max, int *ok, char **next);
  97. /* XXXX duplicated from log.h */
  98. #ifdef __GNUC__
  99. #define CHECK_PRINTF(formatIdx, firstArg) \
  100. __attribute__ ((format (printf, formatIdx, firstArg)))
  101. #else
  102. #define CHECK_PRINTF(formatIdx, firstArg)
  103. #endif
  104. int tor_snprintf(char *str, size_t size, const char *format, ...)
  105. CHECK_PRINTF(3,4);
  106. int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);
  107. /* Some platforms segfault when you try to access a multi-byte type
  108. * that isn't aligned to a word boundary. The macros and/or functions
  109. * below can be used to access unaligned data on any platform.
  110. */
  111. #ifdef UNALIGNED_INT_ACCESS_OK
  112. #define get_uint16(cp) (*(uint16_t*)(cp))
  113. #define get_uint32(cp) (*(uint32_t*)(cp))
  114. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  115. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  116. #else
  117. uint16_t get_uint16(const char *cp);
  118. uint32_t get_uint32(const char *cp);
  119. void set_uint16(char *cp, uint16_t v);
  120. void set_uint32(char *cp, uint32_t v);
  121. #endif
  122. const char *hex_str(const char *from, size_t fromlen);
  123. /** Generic resizeable array. */
  124. typedef struct smartlist_t smartlist_t;
  125. smartlist_t *smartlist_create(void);
  126. void smartlist_free(smartlist_t *sl);
  127. void smartlist_set_capacity(smartlist_t *sl, int n);
  128. void smartlist_clear(smartlist_t *sl);
  129. void smartlist_truncate(smartlist_t *sl, int n);
  130. void smartlist_add(smartlist_t *sl, void *element);
  131. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  132. void smartlist_remove(smartlist_t *sl, void *element);
  133. int smartlist_isin(const smartlist_t *sl, void *element);
  134. int smartlist_string_isin(const smartlist_t *sl, const char *element);
  135. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  136. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  137. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  138. void *smartlist_choose(const smartlist_t *sl);
  139. void *smartlist_get(const smartlist_t *sl, int idx);
  140. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  141. void *smartlist_del(smartlist_t *sl, int idx);
  142. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  143. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  144. int smartlist_len(const smartlist_t *sl);
  145. #define SPLIT_SKIP_SPACE 0x01
  146. #define SPLIT_IGNORE_BLANK 0x02
  147. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  148. int flags, int max);
  149. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate);
  150. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  151. do { \
  152. int var ## _sl_idx, var ## _sl_len=smartlist_len(sl); \
  153. type var; \
  154. for(var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  155. ++var ## _sl_idx) { \
  156. var = smartlist_get((sl),var ## _sl_idx); \
  157. cmd; \
  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. int strmap_isempty(strmap_t *map);
  174. strmap_iter_t *strmap_iter_init(strmap_t *map);
  175. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  176. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  177. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  178. int strmap_iter_done(strmap_iter_t *iter);
  179. /* String manipulation */
  180. const char *eat_whitespace(const char *s);
  181. const char *eat_whitespace_no_nl(const char *s);
  182. const char *find_whitespace(const char *s);
  183. /* Time helpers */
  184. void tor_gettimeofday(struct timeval *timeval);
  185. long tv_udiff(struct timeval *start, struct timeval *end);
  186. void tv_addms(struct timeval *a, long ms);
  187. void tv_add(struct timeval *a, struct timeval *b);
  188. int tv_cmp(struct timeval *a, struct timeval *b);
  189. time_t tor_timegm(struct tm *tm);
  190. #define RFC1123_TIME_LEN 29
  191. void format_rfc1123_time(char *buf, time_t t);
  192. int parse_rfc1123_time(const char *buf, time_t *t);
  193. #define ISO_TIME_LEN 19
  194. void format_iso_time(char *buf, time_t t);
  195. int parse_iso_time(const char *buf, time_t *t);
  196. int write_all(int fd, const char *buf, size_t count, int isSocket);
  197. int read_all(int fd, char *buf, size_t count, int isSocket);
  198. void set_socket_nonblocking(int socket);
  199. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  200. file_status_t file_status(const char *filename);
  201. int check_private_dir(const char *dirname, int create);
  202. int write_str_to_file(const char *fname, const char *str, int bin);
  203. char *read_file_to_str(const char *filename, int bin);
  204. int parse_line_from_file(char *line, size_t maxlen, FILE *f, char **key_out, char **value_out);
  205. char *expand_filename(const char *filename);
  206. int replace_file(const char *from, const char *to);
  207. int spawn_func(int (*func)(void *), void *data);
  208. void spawn_exit(void);
  209. /* Because we use threads instead of processes on Windows, we need locking on Windows.
  210. * On Unixy platforms, these functions are no-ops. */
  211. typedef struct tor_mutex_t tor_mutex_t;
  212. tor_mutex_t *tor_mutex_new(void);
  213. void tor_mutex_acquire(tor_mutex_t *m);
  214. void tor_mutex_release(tor_mutex_t *m);
  215. void tor_mutex_free(tor_mutex_t *m);
  216. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  217. int is_internal_IP(uint32_t ip);
  218. int is_local_IP(uint32_t ip);
  219. const char *get_uname(void);
  220. void start_daemon(const char *desired_cwd);
  221. void finish_daemon(void);
  222. void write_pidfile(char *filename);
  223. int set_max_file_descriptors(int required_min);
  224. int switch_id(char *user, char *group);
  225. struct in_addr;
  226. int tor_inet_aton(const char *cp, struct in_addr *addr);
  227. int tor_lookup_hostname(const char *name, uint32_t *addr);
  228. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  229. uint16_t *port);
  230. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  231. uint32_t *mask_out, uint16_t *port_min_out,
  232. uint16_t *port_max_out);
  233. /* For stupid historical reasons, windows sockets have an independent
  234. * set of errnos, and an independent way to get them. Also, you can't
  235. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  236. * errnos against expected values, and use tor_socket_errno to find
  237. * the actual errno after a socket operation fails.
  238. */
  239. #ifdef MS_WINDOWS
  240. /** Return true if e is EAGAIN or the local equivalent. */
  241. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  242. /** Return true if e is EINPROGRESS or the local equivalent. */
  243. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  244. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  245. * a call to connect(). */
  246. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
  247. /** Return true if e is EAGAIN or another error indicating that a call to
  248. * accept() has no pending connections to return. */
  249. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  250. /** Return true if e is EMFILE or another error indicating that a call to
  251. * accept() has failed because we're out of fds or something. */
  252. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  253. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  254. int tor_socket_errno(int sock);
  255. const char *tor_socket_strerror(int e);
  256. #else
  257. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  258. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  259. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  260. #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
  261. #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
  262. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  263. #define tor_socket_errno(sock) (errno)
  264. #define tor_socket_strerror(e) strerror(e)
  265. #endif
  266. #endif
  267. /*
  268. Local Variables:
  269. mode:c
  270. indent-tabs-mode:nil
  271. c-basic-offset:2
  272. End:
  273. */