util.h 6.2 KB

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