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