util.h 11 KB

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