util.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "orconfig.h"
  11. #include "torint.h"
  12. #include <stdio.h>
  13. #include <assert.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. #define tor_assert(expr) do {} while(0)
  25. #else
  26. #define tor_assert(expr) do { \
  27. if (!(expr)) { \
  28. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  29. __FILE__, __LINE__, __FUNCTION__, #expr); \
  30. assert(expr); /* write to console too. */ \
  31. abort(); /* unreached */ \
  32. } } while (0)
  33. #endif
  34. /* Memory management */
  35. void *tor_malloc(size_t size);
  36. void *tor_malloc_zero(size_t size);
  37. void *tor_realloc(void *ptr, size_t size);
  38. char *tor_strdup(const char *s);
  39. char *tor_strndup(const char *s, size_t n);
  40. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  41. /* String manipulation */
  42. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  43. size_t strlcat(char *dst, const char *src, size_t siz);
  44. size_t strlcpy(char *dst, const char *src, size_t siz);
  45. void tor_strlower(char *s);
  46. int strcmpstart(const char *s1, const char *s2);
  47. int tor_strstrip(char *s, const char *strip);
  48. typedef enum {
  49. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  50. } part_finish_rule_t;
  51. int tor_strpartition(char *dest, size_t dest_len,
  52. const char *s, const char *insert, size_t n,
  53. part_finish_rule_t rule);
  54. long tor_parse_long(const char *s, int base, long min,
  55. long max, int *ok, char **next);
  56. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  57. unsigned long max, int *ok, char **next);
  58. const char *hex_str(const char *from, size_t fromlen);
  59. const char *eat_whitespace(const char *s);
  60. const char *eat_whitespace_no_nl(const char *s);
  61. const char *find_whitespace(const char *s);
  62. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  63. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  64. /* Time helpers */
  65. long tv_udiff(struct timeval *start, struct timeval *end);
  66. void tv_addms(struct timeval *a, long ms);
  67. void tv_add(struct timeval *a, struct timeval *b);
  68. int tv_cmp(struct timeval *a, struct timeval *b);
  69. time_t tor_timegm(struct tm *tm);
  70. #define RFC1123_TIME_LEN 29
  71. void format_rfc1123_time(char *buf, time_t t);
  72. int parse_rfc1123_time(const char *buf, time_t *t);
  73. #define ISO_TIME_LEN 19
  74. void format_iso_time(char *buf, time_t t);
  75. int parse_iso_time(const char *buf, time_t *t);
  76. /* File helpers */
  77. int write_all(int fd, const char *buf, size_t count, int isSocket);
  78. int read_all(int fd, char *buf, size_t count, int isSocket);
  79. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  80. file_status_t file_status(const char *filename);
  81. int check_private_dir(const char *dirname, int create);
  82. int write_str_to_file(const char *fname, const char *str, int bin);
  83. char *read_file_to_str(const char *filename, int bin);
  84. int parse_line_from_file(char *line, size_t maxlen, FILE *f, char **key_out, char **value_out);
  85. char *expand_filename(const char *filename);
  86. /* Net helpers */
  87. int is_internal_IP(uint32_t ip);
  88. int is_local_IP(uint32_t ip);
  89. int tor_lookup_hostname(const char *name, uint32_t *addr);
  90. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  91. uint16_t *port);
  92. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  93. uint32_t *mask_out, uint16_t *port_min_out,
  94. uint16_t *port_max_out);
  95. /* Process helpers */
  96. void start_daemon(const char *desired_cwd);
  97. void finish_daemon(void);
  98. void write_pidfile(char *filename);
  99. #endif
  100. /*
  101. Local Variables:
  102. mode:c
  103. indent-tabs-mode:nil
  104. c-basic-offset:2
  105. End:
  106. */