util.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright 2003-2004 Roger Dingledine
  2. * Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. /**
  6. * \file util.h
  7. * \brief Headers for util.c
  8. **/
  9. #ifndef __UTIL_H
  10. #define __UTIL_H
  11. #define UTIL_H_ID "$Id$"
  12. #include "orconfig.h"
  13. #include "torint.h"
  14. #include "compat.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #ifdef HAVE_SYS_TIME_H
  18. #include <sys/time.h>
  19. #endif
  20. #ifdef HAVE_TIME_H
  21. #include <time.h>
  22. #endif
  23. /* Replace assert() with a variant that sends failures to the log before
  24. * calling assert() normally.
  25. */
  26. #ifdef NDEBUG
  27. /* Nobody should ever want to build with NDEBUG set. 99% of your asserts will
  28. * be outside the critical path anyway, so it's silly to disable bugchecking
  29. * throughout the entire program just because a few asserts are slowing you
  30. * down. Profile, optimize the critical path, and keep debugging on.
  31. *
  32. * And I'm not just saying that because some of our asserts check
  33. * security-critical properties.
  34. */
  35. #error "Sorry; we don't support building with NDEBUG."
  36. #else
  37. #define tor_assert(expr) do { \
  38. if (!(expr)) { \
  39. log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
  40. _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
  41. fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
  42. _SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
  43. abort(); \
  44. } } while (0)
  45. #endif
  46. #ifdef USE_DMALLOC
  47. #define DMALLOC_PARAMS , const char *file, const int line
  48. #define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
  49. #else
  50. #define DMALLOC_PARAMS
  51. #define DMALLOC_ARGS
  52. #endif
  53. /** Define this if you want Tor to crash when any problem comes up,
  54. * so you can get a coredump and track things down. */
  55. // #define tor_fragile_assert() tor_assert(0)
  56. #define tor_fragile_assert()
  57. /* Memory management */
  58. void *_tor_malloc(size_t size DMALLOC_PARAMS);
  59. void *_tor_malloc_zero(size_t size DMALLOC_PARAMS);
  60. void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
  61. char *_tor_strdup(const char *s DMALLOC_PARAMS);
  62. char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS);
  63. #ifdef USE_DMALLOC
  64. extern int dmalloc_free(const char *file, const int line, void *pnt,
  65. const int func_id);
  66. #define tor_free(p) do { \
  67. if (p) { \
  68. dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
  69. (p)=NULL; \
  70. } \
  71. } while (0)
  72. #else
  73. #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
  74. #endif
  75. #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
  76. #define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
  77. #define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
  78. #define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
  79. #define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
  80. /* String manipulation */
  81. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  82. void tor_strlower(char *s);
  83. void tor_strupper(char *s);
  84. int strcmpstart(const char *s1, const char *s2);
  85. int strcasecmpstart(const char *s1, const char *s2);
  86. int strcmpend(const char *s1, const char *s2);
  87. int strcasecmpend(const char *s1, const char *s2);
  88. int tor_strstrip(char *s, const char *strip);
  89. typedef enum {
  90. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  91. } part_finish_rule_t;
  92. int tor_strpartition(char *dest, size_t dest_len,
  93. const char *s, const char *insert, size_t n,
  94. part_finish_rule_t rule);
  95. long tor_parse_long(const char *s, int base, long min,
  96. long max, int *ok, char **next);
  97. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  98. unsigned long max, int *ok, char **next);
  99. uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
  100. uint64_t max, int *ok, char **next);
  101. const char *hex_str(const char *from, size_t fromlen);
  102. const char *eat_whitespace(const char *s);
  103. const char *eat_whitespace_no_nl(const char *s);
  104. const char *find_whitespace(const char *s);
  105. int tor_digest_is_zero(const char *digest);
  106. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  107. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  108. /* Time helpers */
  109. long tv_udiff(struct timeval *start, struct timeval *end);
  110. void tv_addms(struct timeval *a, long ms);
  111. void tv_add(struct timeval *a, struct timeval *b);
  112. int tv_cmp(struct timeval *a, struct timeval *b);
  113. time_t tor_timegm(struct tm *tm);
  114. #define RFC1123_TIME_LEN 29
  115. void format_rfc1123_time(char *buf, time_t t);
  116. int parse_rfc1123_time(const char *buf, time_t *t);
  117. #define ISO_TIME_LEN 19
  118. void format_local_iso_time(char *buf, time_t t);
  119. void format_iso_time(char *buf, time_t t);
  120. int parse_iso_time(const char *buf, time_t *t);
  121. /* File helpers */
  122. int write_all(int fd, const char *buf, size_t count, int isSocket);
  123. int read_all(int fd, char *buf, size_t count, int isSocket);
  124. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  125. file_status_t file_status(const char *filename);
  126. typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
  127. int check_private_dir(const char *dirname, cpd_check_t check);
  128. int write_str_to_file(const char *fname, const char *str, int bin);
  129. int write_bytes_to_file(const char *fname, const char *str, size_t len,
  130. int bin);
  131. /** An ad-hoc type to hold a string of characters and a count; used by
  132. * write_chunks_to_file. */
  133. typedef struct sized_chunk_t {
  134. const char *bytes;
  135. size_t len;
  136. } sized_chunk_t;
  137. struct smartlist_t;
  138. int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
  139. int bin);
  140. int append_bytes_to_file(const char *fname, const char *str, size_t len,
  141. int bin);
  142. char *read_file_to_str(const char *filename, int bin);
  143. char *parse_line_from_str(char *line, char **key_out, char **value_out);
  144. char *expand_filename(const char *filename);
  145. struct smartlist_t *tor_listdir(const char *dirname);
  146. int path_is_relative(const char *filename);
  147. /* Net helpers */
  148. int is_internal_IP(uint32_t ip, int for_listening);
  149. int is_local_IP(uint32_t ip);
  150. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  151. uint16_t *port);
  152. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  153. uint32_t *mask_out, uint16_t *port_min_out,
  154. uint16_t *port_max_out);
  155. #define INET_NTOA_BUF_LEN 16
  156. int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
  157. char *tor_dup_addr(uint32_t addr);
  158. int is_plausible_address(const char *name);
  159. int get_interface_address(uint32_t *addr);
  160. /* Process helpers */
  161. void start_daemon(void);
  162. void finish_daemon(const char *desired_cwd);
  163. void write_pidfile(char *filename);
  164. #endif