util.h 6.68 KB
Newer Older
Nick Mathewson's avatar
Nick Mathewson committed
1
2
/* Copyright 2003-2004 Roger Dingledine
 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3
4
5
/* See LICENSE for licensing information */
/* $Id$ */

6
7
8
/**
 * \file util.h
 * \brief Headers for util.c
9
 **/
10

11
12
#ifndef __UTIL_H
#define __UTIL_H
13
#define UTIL_H_ID "$Id$"
14

15
16
#include "orconfig.h"
#include "torint.h"
17
#include "compat.h"
18
19
20
21
#include <stdio.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
Nick Mathewson's avatar
Nick Mathewson committed
22
23
24
#ifdef HAVE_TIME_H
#include <time.h>
#endif
25

26
/* Replace assert() with a variant that sends failures to the log before
27
28
 * calling assert() normally.
 */
29
#ifdef NDEBUG
30
31
32
33
34
35
36
37
38
/* Nobody should ever want to build with NDEBUG set.  99% of your asserts will
 * be outside the critical path anyway, so it's silly to disable bugchecking
 * throughout the entire program just because a few asserts are slowing you
 * down.  Profile, optimize the critical path, and keep debugging on.
 *
 * And I'm not just saying that because some of our asserts check
 * security-critical properties.
 */
#error "Sorry; we don't support building with NDEBUG."
39
40
41
42
#else
#define tor_assert(expr) do {                                 \
 if (!(expr)) {                                               \
   log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.",  \
43
       _SHORT_FILE_, __LINE__, __FUNCTION__, #expr);          \
44
45
   fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
       _SHORT_FILE_, __LINE__, __FUNCTION__, #expr);          \
46
47
48
49
   abort();  /* unreached */                                  \
 } } while (0)
#endif

50
#ifdef USE_DMALLOC
51
52
#define DMALLOC_PARAMS , const char *file, const int line
#define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
53
54
55
56
57
#else
#define DMALLOC_PARAMS
#define DMALLOC_ARGS
#endif

58
59
60
61
62
/** Define this if you want Tor to crash when any problem comes up,
 * so you can get a coredump and track things down. */
// #define tor_fragile_assert() tor_assert(0)
#define tor_fragile_assert()

63
/* Memory management */
64
65
66
67
68
void *_tor_malloc(size_t size DMALLOC_PARAMS);
void *_tor_malloc_zero(size_t size DMALLOC_PARAMS);
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
char *_tor_strdup(const char *s DMALLOC_PARAMS);
char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS);
69
70
71
72
73
74
75
76
77
78
#ifdef USE_DMALLOC
extern int dmalloc_free(const char *file, const int line, void *pnt,
                        const int func_id);
#define tor_free(p) do { \
    if (p) {                                        \
      dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
      (p)=NULL;                                     \
    }                                               \
  } while (0)
#else
79
#define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
80
#endif
81

82
83
84
85
86
#define tor_malloc(size)       _tor_malloc(size DMALLOC_ARGS)
#define tor_malloc_zero(size)  _tor_malloc_zero(size DMALLOC_ARGS)
#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
#define tor_strdup(s)          _tor_strdup(s DMALLOC_ARGS)
#define tor_strndup(s, n)      _tor_strndup(s, n DMALLOC_ARGS)
87

88
89
/* String manipulation */
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
90
void tor_strlower(char *s);
Nick Mathewson's avatar
Nick Mathewson committed
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
98
99
typedef enum {
  ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
} part_finish_rule_t;
100
int tor_strpartition(char *dest, size_t dest_len,
101
102
                     const char *s, const char *insert, size_t n,
                     part_finish_rule_t rule);
103
104
105
106
long tor_parse_long(const char *s, int base, long min,
                    long max, int *ok, char **next);
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
                              unsigned long max, int *ok, char **next);
107
108
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
                         uint64_t max, int *ok, char **next);
109
const char *hex_str(const char *from, size_t fromlen);
110
111
112
const char *eat_whitespace(const char *s);
const char *eat_whitespace_no_nl(const char *s);
const char *find_whitespace(const char *s);
113

114
115
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
116

117
/* Time helpers */
118
119
120
121
long tv_udiff(struct timeval *start, struct timeval *end);
void tv_addms(struct timeval *a, long ms);
void tv_add(struct timeval *a, struct timeval *b);
int tv_cmp(struct timeval *a, struct timeval *b);
Nick Mathewson's avatar
Nick Mathewson committed
122
time_t tor_timegm(struct tm *tm);
123
#define RFC1123_TIME_LEN 29
124
125
126
void format_rfc1123_time(char *buf, time_t t);
int parse_rfc1123_time(const char *buf, time_t *t);
#define ISO_TIME_LEN 19
127
void format_local_iso_time(char *buf, time_t t);
128
129
void format_iso_time(char *buf, time_t t);
int parse_iso_time(const char *buf, time_t *t);
130

131
/* File helpers */
132
133
int write_all(int fd, const char *buf, size_t count, int isSocket);
int read_all(int fd, char *buf, size_t count, int isSocket);
Roger Dingledine's avatar
Roger Dingledine committed
134

135
136
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
file_status_t file_status(const char *filename);
137

138
139
typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
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
142
int write_bytes_to_file(const char *fname, const char *str, size_t len,
                        int bin);
143
144
/** An ad-hoc type to hold a string of characters and a count; used by
 * write_chunks_to_file. */
145
146
147
148
149
150
151
152
153
154
typedef struct sized_chunk_t {
  const char *bytes;
  size_t len;
} sized_chunk_t;
struct smartlist_t;
int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
                         int bin);
int append_bytes_to_file(const char *fname, const char *str, size_t len,
                         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);
159

160
/* Net helpers */
161
int is_internal_IP(uint32_t ip);
162
int is_local_IP(uint32_t ip);
163
164
int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
                    uint16_t *port);
165
166
167
int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
                              uint32_t *mask_out, uint16_t *port_min_out,
                              uint16_t *port_max_out);
168
169
#define INET_NTOA_BUF_LEN 16
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);
173

174
/* Process helpers */
175
176
void start_daemon(void);
void finish_daemon(const char *desired_cwd);
177
void write_pidfile(char *filename);
178

179
#endif
180