util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifdef HAVE_SYS_TIME_H
  14. #include <sys/time.h>
  15. #endif
  16. #ifdef HAVE_TIME_H
  17. #include <time.h>
  18. #endif
  19. /** Replace assert() with a variant that sends failures to the log before
  20. * calling assert() normally.
  21. */
  22. #ifdef NDEBUG
  23. #define tor_assert(expr) do {} while(0)
  24. #else
  25. #define tor_assert(expr) do { \
  26. if (!(expr)) { \
  27. log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
  28. __FILE__, __LINE__, __FUNCTION__, #expr); \
  29. assert(expr); /* write to console too. */ \
  30. abort(); /* unreached */ \
  31. } } while (0)
  32. #endif
  33. /* Memory management */
  34. void *tor_malloc(size_t size);
  35. void *tor_malloc_zero(size_t size);
  36. void *tor_realloc(void *ptr, size_t size);
  37. char *tor_strdup(const char *s);
  38. char *tor_strndup(const char *s, size_t n);
  39. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  40. /* String manipulation */
  41. #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
  42. void tor_strlower(char *s);
  43. int strcmpstart(const char *s1, const char *s2);
  44. int strcmpend(const char *s1, const char *s2);
  45. int tor_strstrip(char *s, const char *strip);
  46. typedef enum {
  47. ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
  48. } part_finish_rule_t;
  49. int tor_strpartition(char *dest, size_t dest_len,
  50. const char *s, const char *insert, size_t n,
  51. part_finish_rule_t rule);
  52. long tor_parse_long(const char *s, int base, long min,
  53. long max, int *ok, char **next);
  54. unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
  55. unsigned long max, int *ok, char **next);
  56. uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
  57. uint64_t 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_local_iso_time(char *buf, time_t t);
  75. void format_iso_time(char *buf, time_t t);
  76. int parse_iso_time(const char *buf, time_t *t);
  77. /* File helpers */
  78. int write_all(int fd, const char *buf, size_t count, int isSocket);
  79. int read_all(int fd, char *buf, size_t count, int isSocket);
  80. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  81. file_status_t file_status(const char *filename);
  82. typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
  83. int check_private_dir(const char *dirname, cpd_check_t check);
  84. int write_str_to_file(const char *fname, const char *str, int bin);
  85. int write_bytes_to_file(const char *fname, const char *str, size_t len,
  86. int bin);
  87. char *read_file_to_str(const char *filename, int bin);
  88. char *parse_line_from_str(char *line, char **key_out, char **value_out);
  89. char *expand_filename(const char *filename);
  90. /* Net helpers */
  91. int is_internal_IP(uint32_t ip);
  92. int is_local_IP(uint32_t ip);
  93. int tor_lookup_hostname(const char *name, uint32_t *addr);
  94. int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
  95. uint16_t *port);
  96. int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  97. uint32_t *mask_out, uint16_t *port_min_out,
  98. uint16_t *port_max_out);
  99. /* Process helpers */
  100. void start_daemon(const char *desired_cwd);
  101. void finish_daemon(void);
  102. void write_pidfile(char *filename);
  103. #endif