util.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #else
  36. #define INLINE inline
  37. #endif
  38. /* legal characters in a filename */
  39. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  40. size_t strlcat(char *dst, const char *src, size_t siz);
  41. size_t strlcpy(char *dst, const char *src, size_t siz);
  42. void *tor_malloc(size_t size);
  43. void *tor_malloc_zero(size_t size);
  44. void *tor_realloc(void *ptr, size_t size);
  45. char *tor_strdup(const char *s);
  46. char *tor_strndup(const char *s, size_t n);
  47. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  48. void tor_strlower(char *s);
  49. #ifdef UNALIGNED_INT_ACCESS_OK
  50. /* XXX Not actually used yet, but would probably be faster on non-sun
  51. * hardare.
  52. */
  53. #define get_uint16(cp) (*(uint16_t*)(cp))
  54. #define get_uint32(cp) (*(uint32_t*)(cp))
  55. #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
  56. #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
  57. #else
  58. #if 1
  59. uint16_t get_uint16(char *cp);
  60. uint32_t get_uint32(char *cp);
  61. void set_uint16(char *cp, uint16_t v);
  62. void set_uint32(char *cp, uint32_t v);
  63. #else
  64. #define get_uint16(cp) \
  65. ( ((*(((uint8_t*)(cp))+0))<<8) + \
  66. ((*(((uint8_t*)(cp))+1)) ) )
  67. #define get_uint32(cp) \
  68. ( ((*(((uint8_t*)(cp))+0))<<24) + \
  69. ((*(((uint8_t*)(cp))+1))<<16) + \
  70. ((*(((uint8_t*)(cp))+2))<<8 ) + \
  71. ((*(((uint8_t*)(cp))+3)) ) )
  72. #define set_uint16(cp,v) \
  73. do { \
  74. uint16_t u16v = (v); \
  75. *(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
  76. *(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
  77. } while (0)
  78. #define set_uint32(cp,val) \
  79. do { \
  80. uint32_t u32v = (v); \
  81. *(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
  82. *(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
  83. *(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
  84. *(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
  85. } while (0)
  86. #endif
  87. #endif
  88. void hex_encode(const char *from, int fromlen, char *to);
  89. const char *hex_str(const char *from, int fromlen);
  90. typedef struct smartlist_t smartlist_t;
  91. smartlist_t *smartlist_create();
  92. void smartlist_free(smartlist_t *sl);
  93. void smartlist_set_capacity(smartlist_t *sl, int n);
  94. void smartlist_clear(smartlist_t *sl);
  95. void smartlist_truncate(smartlist_t *sl, int n);
  96. void smartlist_add(smartlist_t *sl, void *element);
  97. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  98. void smartlist_remove(smartlist_t *sl, void *element);
  99. int smartlist_isin(const smartlist_t *sl, void *element);
  100. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  101. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  102. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  103. void *smartlist_choose(const smartlist_t *sl);
  104. void *smartlist_get(const smartlist_t *sl, int idx);
  105. void *smartlist_set(smartlist_t *sl, int idx, void *val);
  106. void *smartlist_del(smartlist_t *sl, int idx);
  107. void *smartlist_del_keeporder(smartlist_t *sl, int idx);
  108. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  109. int smartlist_len(const smartlist_t *sl);
  110. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  111. do { \
  112. int sl_idx, sl_len=smartlist_len(sl); \
  113. type var; \
  114. for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
  115. var = smartlist_get((sl),sl_idx); \
  116. do {cmd;} while(0); \
  117. } } while (0)
  118. /* Map from const char * to void*. Implemented with a splay tree. */
  119. typedef struct strmap_t strmap_t;
  120. typedef struct strmap_entry_t strmap_entry_t;
  121. typedef struct strmap_entry_t strmap_iter_t;
  122. strmap_t* strmap_new(void);
  123. void* strmap_set(strmap_t *map, const char *key, void *val);
  124. void* strmap_get(strmap_t *map, const char *key);
  125. void* strmap_remove(strmap_t *map, const char *key);
  126. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  127. void* strmap_get_lc(strmap_t *map, const char *key);
  128. void* strmap_remove_lc(strmap_t *map, const char *key);
  129. typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
  130. void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
  131. void strmap_free(strmap_t *map, void (*free_val)(void*));
  132. strmap_iter_t *strmap_iter_init(strmap_t *map);
  133. strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
  134. strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
  135. void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
  136. int strmap_iter_done(strmap_iter_t *iter);
  137. const char *eat_whitespace(const char *s);
  138. const char *eat_whitespace_no_nl(const char *s);
  139. const char *find_whitespace(const char *s);
  140. void tor_gettimeofday(struct timeval *timeval);
  141. long tv_udiff(struct timeval *start, struct timeval *end);
  142. void tv_addms(struct timeval *a, long ms);
  143. void tv_add(struct timeval *a, struct timeval *b);
  144. int tv_cmp(struct timeval *a, struct timeval *b);
  145. time_t tor_timegm(struct tm *tm);
  146. int write_all(int fd, const char *buf, size_t count, int isSocket);
  147. int read_all(int fd, char *buf, size_t count, int isSocket);
  148. void set_socket_nonblocking(int socket);
  149. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  150. file_status_t file_status(const char *filename);
  151. int check_private_dir(const char *dirname, int create);
  152. int write_str_to_file(const char *fname, const char *str);
  153. char *read_file_to_str(const char *filename);
  154. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  155. int spawn_func(int (*func)(void *), void *data);
  156. void spawn_exit();
  157. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  158. int is_internal_IP(uint32_t ip);
  159. const char *get_uname(void);
  160. /* Start putting the process into daemon mode: fork and drop all resources
  161. * except standard fds. The parent process never returns, but stays around
  162. * until finish_daemon is called. (Note: it's safe to call this more
  163. * than once: calls after the first are ignored.)
  164. */
  165. void start_daemon(char *desired_cwd);
  166. /* Finish putting the process into daemon mode: drop standard fds, and tell
  167. * the parent process to exit. (Note: it's safe to call this more than once:
  168. * calls after the first are ignored. Calls start_daemon first if it hasn't
  169. * been called already.)
  170. */
  171. void finish_daemon(void);
  172. void write_pidfile(char *filename);
  173. int switch_id(char *user, char *group);
  174. struct in_addr;
  175. int tor_inet_aton(const char *cp, struct in_addr *addr);
  176. /* For stupid historical reasons, windows sockets have an independent set of
  177. * errnos which they use as the fancy strikes them.
  178. */
  179. #ifdef MS_WINDOWS
  180. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  181. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  182. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  183. int correct_socket_errno(int s);
  184. #else
  185. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  186. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  187. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  188. #define correct_socket_errno(s) (errno)
  189. #endif
  190. #endif
  191. /*
  192. Local Variables:
  193. mode:c
  194. indent-tabs-mode:nil
  195. c-basic-offset:2
  196. End:
  197. */