util.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __UTIL_H
  5. #define __UTIL_H
  6. #include "orconfig.h"
  7. #include "torint.h"
  8. #include <stdio.h>
  9. #ifdef HAVE_SYS_TIME_H
  10. #include <sys/time.h>
  11. #endif
  12. #ifdef HAVE_TIME_H
  13. #include <time.h>
  14. #endif
  15. #if _MSC_VER > 1300
  16. #include <winsock2.h>
  17. #include <ws2tcpip.h>
  18. #elif defined(_MSC_VER)
  19. #include <winsock.h>
  20. #endif
  21. #ifndef HAVE_GETTIMEOFDAY
  22. #ifdef HAVE_FTIME
  23. #define USING_FAKE_TIMEVAL
  24. #include <sys/timeb.h>
  25. #define timeval timeb
  26. #define tv_sec time
  27. #define tv_usec millitm
  28. #endif
  29. #endif
  30. #ifdef MS_WINDOWS
  31. /* Windows names string functions funnily. */
  32. #define strncasecmp strnicmp
  33. #define strcasecmp stricmp
  34. #define INLINE __inline
  35. #define _ARRAYSIZE(x) (((x)==0)?1:0)
  36. /* Windows compilers before VC7 don't have __FUNCTION__. */
  37. #if _MSC_VER < 1300
  38. #define __FUNCTION__ "???"
  39. #endif
  40. #else
  41. #define INLINE inline
  42. #define _ARRAYSIZE(x) (x)
  43. #endif
  44. #ifdef NDEBUG
  45. #define tor_assert(expr) do {} while(0)
  46. #else
  47. #define tor_assert(expr) do { \
  48. if (!(expr)) { \
  49. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  50. __FILE__, __LINE__, __FUNCTION__, #expr); \
  51. assert(expr); /* write to console too. */ \
  52. abort(); /* unreached */ \
  53. } } while (0)
  54. #endif
  55. /* legal characters in a filename */
  56. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  57. size_t strlcat(char *dst, const char *src, size_t siz);
  58. size_t strlcpy(char *dst, const char *src, size_t siz);
  59. void *tor_malloc(size_t size);
  60. void *tor_malloc_zero(size_t size);
  61. void *tor_realloc(void *ptr, size_t size);
  62. char *tor_strdup(const char *s);
  63. char *tor_strndup(const char *s, size_t n);
  64. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  65. void tor_strlower(char *s);
  66. #ifdef UNALIGNED_INT_ACCESS_OK
  67. /* XXX Not actually used yet, but would probably be faster on non-sun
  68. * hardare.
  69. */
  70. #define get_uint16(cp) (*(uint16_t*)(cp))
  71. #define get_uint32(cp) (*(uint32_t*)(cp))
  72. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  73. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  74. #else
  75. #if 1
  76. uint16_t get_uint16(const char *cp);
  77. uint32_t get_uint32(const char *cp);
  78. void set_uint16(char *cp, uint16_t v);
  79. void set_uint32(char *cp, uint32_t v);
  80. #else
  81. #define get_uint16(cp) \
  82. ( ((*(((uint8_t*)(cp))+0))<<8) + \
  83. ((*(((uint8_t*)(cp))+1)) ) )
  84. #define get_uint32(cp) \
  85. ( ((*(((uint8_t*)(cp))+0))<<24) + \
  86. ((*(((uint8_t*)(cp))+1))<<16) + \
  87. ((*(((uint8_t*)(cp))+2))<<8 ) + \
  88. ((*(((uint8_t*)(cp))+3)) ) )
  89. #define set_uint16(cp,v) \
  90. do { \
  91. uint16_t u16v = (v); \
  92. *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
  93. *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
  94. } while (0)
  95. #define set_uint32(cp,val) \
  96. do { \
  97. uint32_t u32v = (v); \
  98. *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
  99. *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
  100. *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
  101. *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
  102. } while (0)
  103. #endif
  104. #endif
  105. void hex_encode(const char *from, int fromlen, char *to);
  106. const char *hex_str(const char *from, int fromlen);
  107. typedef struct smartlist_t smartlist_t;
  108. smartlist_t *smartlist_create();
  109. void smartlist_free(smartlist_t *sl);
  110. void smartlist_set_capacity(smartlist_t *sl, int n);
  111. void smartlist_clear(smartlist_t *sl);
  112. void smartlist_truncate(smartlist_t *sl, int n);
  113. void smartlist_add(smartlist_t *sl, void *element);
  114. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  115. void smartlist_remove(smartlist_t *sl, void *element);
  116. int smartlist_isin(const smartlist_t *sl, void *element);
  117. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  118. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  119. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  120. void *smartlist_choose(const smartlist_t *sl);
  121. void *smartlist_get(const smartlist_t *sl, int idx);
  122. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  123. void *smartlist_del(smartlist_t *sl, int idx);
  124. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  125. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  126. int smartlist_len(const smartlist_t *sl);
  127. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  128. do { \
  129. int sl_idx, sl_len=smartlist_len(sl); \
  130. type var; \
  131. for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
  132. var = smartlist_get((sl),sl_idx); \
  133. do {cmd;} while(0); \
  134. } } while (0)
  135. /* Map from const char * to void*. Implemented with a splay tree. */
  136. typedef struct strmap_t strmap_t;
  137. typedef struct strmap_entry_t strmap_entry_t;
  138. typedef struct strmap_entry_t strmap_iter_t;
  139. strmap_t* strmap_new(void);
  140. void* strmap_set(strmap_t *map, const char *key, void *val);
  141. void* strmap_get(strmap_t *map, const char *key);
  142. void* strmap_remove(strmap_t *map, const char *key);
  143. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  144. void* strmap_get_lc(strmap_t *map, const char *key);
  145. void* strmap_remove_lc(strmap_t *map, const char *key);
  146. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  147. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  148. void strmap_free(strmap_t *map, void (*free_val)(void*));
  149. strmap_iter_t *strmap_iter_init(strmap_t *map);
  150. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  151. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  152. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  153. int strmap_iter_done(strmap_iter_t *iter);
  154. const char *eat_whitespace(const char *s);
  155. const char *eat_whitespace_no_nl(const char *s);
  156. const char *find_whitespace(const char *s);
  157. void tor_gettimeofday(struct timeval *timeval);
  158. long tv_udiff(struct timeval *start, struct timeval *end);
  159. void tv_addms(struct timeval *a, long ms);
  160. void tv_add(struct timeval *a, struct timeval *b);
  161. int tv_cmp(struct timeval *a, struct timeval *b);
  162. time_t tor_timegm(struct tm *tm);
  163. int write_all(int fd, const char *buf, size_t count, int isSocket);
  164. int read_all(int fd, char *buf, size_t count, int isSocket);
  165. void set_socket_nonblocking(int socket);
  166. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  167. file_status_t file_status(const char *filename);
  168. int check_private_dir(const char *dirname, int create);
  169. int write_str_to_file(const char *fname, const char *str);
  170. char *read_file_to_str(const char *filename);
  171. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  172. int spawn_func(int (*func)(void *), void *data);
  173. void spawn_exit();
  174. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  175. int is_internal_IP(uint32_t ip);
  176. const char *get_uname(void);
  177. /* Start putting the process into daemon mode: fork and drop all resources
  178. * except standard fds. The parent process never returns, but stays around
  179. * until finish_daemon is called. (Note: it's safe to call this more
  180. * than once: calls after the first are ignored.)
  181. */
  182. void start_daemon(char *desired_cwd);
  183. /* Finish putting the process into daemon mode: drop standard fds, and tell
  184. * the parent process to exit. (Note: it's safe to call this more than once:
  185. * calls after the first are ignored. Calls start_daemon first if it hasn't
  186. * been called already.)
  187. */
  188. void finish_daemon(void);
  189. void write_pidfile(char *filename);
  190. int switch_id(char *user, char *group);
  191. struct in_addr;
  192. int tor_inet_aton(const char *cp, struct in_addr *addr);
  193. int tor_lookup_hostname(const char *name, uint32_t *addr);
  194. /* For stupid historical reasons, windows sockets have an independent set of
  195. * errnos which they use as the fancy strikes them.
  196. */
  197. #ifdef MS_WINDOWS
  198. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  199. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  200. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  201. int correct_socket_errno(int s);
  202. #else
  203. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  204. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  205. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  206. #define correct_socket_errno(s) (errno)
  207. #endif
  208. #endif
  209. /*
  210. Local Variables:
  211. mode:c
  212. indent-tabs-mode:nil
  213. c-basic-offset:2
  214. End:
  215. */