util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
  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. #define UTIL_H_ID "$Id$"
  11. #include "orconfig.h"
  12. #include "torint.h"
  13. #include <stdio.h>
  14. #ifdef HAVE_SYS_TIME_H
  15. #include <sys/time.h>
  16. #endif
  17. #ifdef HAVE_TIME_H
  18. #include <time.h>
  19. #endif
  20. /** Replace assert() with a variant that sends failures to the log before
  21. * calling assert() normally.
  22. */
  23. #ifdef NDEBUG
  24. /* Nobody should ever want to build with NDEBUG set. 99% of your asserts will
  25. * be outside the critical path anyway, so it's silly to disable bugchecking
  26. * throughout the entire program just because a few asserts are slowing you
  27. * down. Profile, optimize the critical path, and keep debugging on.
  28. *
  29. * And I'm not just saying that because some of our asserts check
  30. * security-critical properties.
  31. */
  32. #error "Sorry; we don't support building with NDEBUG."
  33. #else
  34. #define tor_assert(expr) do { \
  35. if (!(expr)) { \
  36. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  37. __FILE__, __LINE__, __FUNCTION__, #expr); \
  38. assert(expr); /* write to console too. */ \
  39. abort(); /* unreached */ \
  40. } } while (0)
  41. #endif
  42. /* Memory management */
  43. void *tor_malloc(size_t size);
  44. void *tor_malloc_zero(size_t size);
  45. void *tor_realloc(void *ptr, size_t size);
  46. char *tor_strdup(const char *s);
  47. char *tor_strndup(const char *s, size_t n);
  48. #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
  49. /* String manipulation */
  50. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  51. void tor_strlower(char *s);
  52. int strcmpstart(const char *s1, const char *s2);
  53. int strcmpend(const char *s1, const char *s2);
  54. int tor_strstrip(char *s, const char *strip);
  55. typedef enum {
  56. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  57. } part_finish_rule_t;
  58. int tor_strpartition(char *dest, size_t dest_len,
  59. const char *s, const char *insert, size_t n,
  60. part_finish_rule_t rule);
  61. long tor_parse_long(const char *s, int base, long min,
  62. long max, int *ok, char **next);
  63. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  64. unsigned long max, int *ok, char **next);
  65. uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
  66. uint64_t max, int *ok, char **next);
  67. const char *hex_str(const char *from, size_t fromlen);
  68. const char *eat_whitespace(const char *s);
  69. const char *eat_whitespace_no_nl(const char *s);
  70. const char *find_whitespace(const char *s);
  71. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  72. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  73. /* Time helpers */
  74. long tv_udiff(struct timeval *start, struct timeval *end);
  75. void tv_addms(struct timeval *a, long ms);
  76. void tv_add(struct timeval *a, struct timeval *b);
  77. int tv_cmp(struct timeval *a, struct timeval *b);
  78. time_t tor_timegm(struct tm *tm);
  79. #define RFC1123_TIME_LEN 29
  80. void format_rfc1123_time(char *buf, time_t t);
  81. int parse_rfc1123_time(const char *buf, time_t *t);
  82. #define ISO_TIME_LEN 19
  83. void format_local_iso_time(char *buf, time_t t);
  84. void format_iso_time(char *buf, time_t t);
  85. int parse_iso_time(const char *buf, time_t *t);
  86. /* File helpers */
  87. int write_all(int fd, const char *buf, size_t count, int isSocket);
  88. int read_all(int fd, char *buf, size_t count, int isSocket);
  89. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  90. file_status_t file_status(const char *filename);
  91. typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
  92. int check_private_dir(const char *dirname, cpd_check_t check);
  93. int write_str_to_file(const char *fname, const char *str, int bin);
  94. int write_bytes_to_file(const char *fname, const char *str, size_t len,
  95. int bin);
  96. char *read_file_to_str(const char *filename, int bin);
  97. char *parse_line_from_str(char *line, char **key_out, char **value_out);
  98. char *expand_filename(const char *filename);
  99. /* Net helpers */
  100. int is_internal_IP(uint32_t ip);
  101. int is_local_IP(uint32_t ip);
  102. int tor_lookup_hostname(const char *name, uint32_t *addr);
  103. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  104. uint16_t *port);
  105. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  106. uint32_t *mask_out, uint16_t *port_min_out,
  107. uint16_t *port_max_out);
  108. /* Process helpers */
  109. void start_daemon(const char *desired_cwd);
  110. void finish_daemon(void);
  111. void write_pidfile(char *filename);
  112. #endif