util.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /* XXXX This isn't so on windows. */
  73. /** Legal characters in a filename */
  74. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  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. /* Some platforms segfault when you try to access a multi-byte type
  85. * that isn't aligned to a word boundary. The macros and/or functions
  86. * below can be used to access unaligned data on any platform.
  87. */
  88. #ifdef UNALIGNED_INT_ACCESS_OK
  89. #define get_uint16(cp) (*(uint16_t*)(cp))
  90. #define get_uint32(cp) (*(uint32_t*)(cp))
  91. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  92. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  93. #else
  94. #if 1
  95. uint16_t get_uint16(const char *cp);
  96. uint32_t get_uint32(const char *cp);
  97. void set_uint16(char *cp, uint16_t v);
  98. void set_uint32(char *cp, uint32_t v);
  99. #else
  100. #define get_uint16(cp) \
  101. ( ((*(((uint8_t*)(cp))+0))<<8) + \
  102. ((*(((uint8_t*)(cp))+1)) ) )
  103. #define get_uint32(cp) \
  104. ( ((*(((uint8_t*)(cp))+0))<<24) + \
  105. ((*(((uint8_t*)(cp))+1))<<16) + \
  106. ((*(((uint8_t*)(cp))+2))<<8 ) + \
  107. ((*(((uint8_t*)(cp))+3)) ) )
  108. #define set_uint16(cp,v) \
  109. do { \
  110. uint16_t u16v = (v); \
  111. *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
  112. *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
  113. } while (0)
  114. #define set_uint32(cp,val) \
  115. do { \
  116. uint32_t u32v = (v); \
  117. *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
  118. *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
  119. *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
  120. *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
  121. } while (0)
  122. #endif
  123. #endif
  124. void hex_encode(const char *from, int fromlen, char *to);
  125. const char *hex_str(const char *from, int fromlen);
  126. /** Generic resizeable array. */
  127. typedef struct smartlist_t smartlist_t;
  128. smartlist_t *smartlist_create();
  129. void smartlist_free(smartlist_t *sl);
  130. void smartlist_set_capacity(smartlist_t *sl, int n);
  131. void smartlist_clear(smartlist_t *sl);
  132. void smartlist_truncate(smartlist_t *sl, int n);
  133. void smartlist_add(smartlist_t *sl, void *element);
  134. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  135. void smartlist_remove(smartlist_t *sl, void *element);
  136. int smartlist_isin(const smartlist_t *sl, void *element);
  137. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  138. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  139. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  140. void *smartlist_choose(const smartlist_t *sl);
  141. void *smartlist_get(const smartlist_t *sl, int idx);
  142. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  143. void *smartlist_del(smartlist_t *sl, int idx);
  144. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  145. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  146. int smartlist_len(const smartlist_t *sl);
  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. strmap_iter_t *strmap_iter_init(strmap_t *map);
  170. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  171. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  172. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  173. int strmap_iter_done(strmap_iter_t *iter);
  174. /* String manipulation */
  175. const char *eat_whitespace(const char *s);
  176. const char *eat_whitespace_no_nl(const char *s);
  177. const char *find_whitespace(const char *s);
  178. /* Time helpers */
  179. void tor_gettimeofday(struct timeval *timeval);
  180. long tv_udiff(struct timeval *start, struct timeval *end);
  181. void tv_addms(struct timeval *a, long ms);
  182. void tv_add(struct timeval *a, struct timeval *b);
  183. int tv_cmp(struct timeval *a, struct timeval *b);
  184. time_t tor_timegm(struct tm *tm);
  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);
  192. char *read_file_to_str(const char *filename);
  193. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  194. int spawn_func(int (*func)(void *), void *data);
  195. void spawn_exit();
  196. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  197. int is_internal_IP(uint32_t ip);
  198. const char *get_uname(void);
  199. void start_daemon(char *desired_cwd);
  200. void finish_daemon(void);
  201. void write_pidfile(char *filename);
  202. int switch_id(char *user, char *group);
  203. struct in_addr;
  204. int tor_inet_aton(const char *cp, struct in_addr *addr);
  205. int tor_lookup_hostname(const char *name, uint32_t *addr);
  206. /* For stupid historical reasons, windows sockets have an independent
  207. * set of errnos, and an independent way to get them. Also, you can't
  208. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  209. * errnos against expected values, and use tor_socket_errno to find
  210. * the actual errno after a socket operation fails.
  211. */
  212. #ifdef MS_WINDOWS
  213. /** Return true if e is EAGAIN or the local equivalent. */
  214. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  215. /** Return true if e is EINPROGRESS or the local equivalent. */
  216. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  217. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  218. * a call to connect(). */
  219. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
  220. int tor_socket_errno(int sock);
  221. const char *tor_socket_strerror(int e);
  222. #else
  223. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
  224. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
  225. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  226. #define tor_socket_errno(sock) (errno)
  227. #define tor_socket_strerror(e) strerror(e)
  228. #endif
  229. #endif
  230. /*
  231. Local Variables:
  232. mode:c
  233. indent-tabs-mode:nil
  234. c-basic-offset:2
  235. End:
  236. */