2005-04-01 22:15:56 +02:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine
|
|
|
|
* Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
|
2003-04-16 19:04:58 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-05-07 19:04:12 +02:00
|
|
|
/**
|
|
|
|
* \file util.h
|
|
|
|
* \brief Headers for util.c
|
2004-05-10 09:54:13 +02:00
|
|
|
**/
|
2004-05-07 19:04:12 +02:00
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#ifndef __UTIL_H
|
|
|
|
#define __UTIL_H
|
2004-11-29 23:25:31 +01:00
|
|
|
#define UTIL_H_ID "$Id$"
|
2003-04-16 19:04:58 +02:00
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
2004-12-22 03:32:26 +01:00
|
|
|
#include "compat.h"
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2004-04-03 06:08:54 +02:00
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2003-08-12 05:08:41 +02:00
|
|
|
|
2005-04-26 20:52:16 +02:00
|
|
|
/* Replace assert() with a variant that sends failures to the log before
|
2004-05-01 22:46:28 +02:00
|
|
|
* calling assert() normally.
|
|
|
|
*/
|
2004-04-25 21:37:39 +02:00
|
|
|
#ifdef NDEBUG
|
2004-11-29 08:22:57 +01:00
|
|
|
/* 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."
|
2004-04-25 21:37:39 +02:00
|
|
|
#else
|
|
|
|
#define tor_assert(expr) do { \
|
|
|
|
if (!(expr)) { \
|
|
|
|
log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
|
2004-12-22 03:46:28 +01:00
|
|
|
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
|
|
|
|
_SHORT_FILE_, __LINE__, __FUNCTION__, #expr); \
|
2004-04-25 21:37:39 +02:00
|
|
|
abort(); /* unreached */ \
|
|
|
|
} } while (0)
|
|
|
|
#endif
|
|
|
|
|
2005-04-26 20:52:16 +02:00
|
|
|
/** 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()
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Memory management */
|
2005-02-03 08:25:10 +01:00
|
|
|
void *_tor_malloc(const char *file, const int line, size_t size);
|
|
|
|
void *_tor_malloc_zero(const char *file, const int line, size_t size);
|
|
|
|
void *_tor_realloc(const char *file, const int line, void *ptr, size_t size);
|
|
|
|
char *_tor_strdup(const char *file, const int line, const char *s);
|
|
|
|
char *_tor_strndup(const char *file, const int line, const char *s, size_t n);
|
2004-11-28 10:05:49 +01:00
|
|
|
#define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-02-03 08:25:10 +01:00
|
|
|
#define tor_malloc(size) _tor_malloc(_SHORT_FILE_, __LINE__, size)
|
|
|
|
#define tor_malloc_zero(size) _tor_malloc_zero(_SHORT_FILE_, __LINE__, size)
|
|
|
|
#define tor_realloc(ptr, size) _tor_realloc(_SHORT_FILE_, __LINE__, ptr, size)
|
|
|
|
#define tor_strdup(s) _tor_strdup(_SHORT_FILE_, __LINE__, s)
|
|
|
|
#define tor_strndup(s, n) _tor_strndup(_SHORT_FILE_, __LINE__, s, n)
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* String manipulation */
|
|
|
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
2004-03-20 02:48:05 +01:00
|
|
|
void tor_strlower(char *s);
|
2005-09-08 20:33:51 +02:00
|
|
|
void tor_strupper(char *s);
|
2004-09-02 20:25:50 +02:00
|
|
|
int strcmpstart(const char *s1, const char *s2);
|
2004-11-30 04:10:56 +01:00
|
|
|
int strcasecmpstart(const char *s1, const char *s2);
|
2004-11-09 19:22:17 +01:00
|
|
|
int strcmpend(const char *s1, const char *s2);
|
2004-11-30 04:10:56 +01:00
|
|
|
int strcasecmpend(const char *s1, const char *s2);
|
2004-10-07 05:11:42 +02:00
|
|
|
int tor_strstrip(char *s, const char *strip);
|
2004-10-07 23:37:06 +02:00
|
|
|
typedef enum {
|
|
|
|
ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
|
|
|
|
} part_finish_rule_t;
|
2004-10-07 22:58:53 +02:00
|
|
|
int tor_strpartition(char *dest, size_t dest_len,
|
2004-10-07 23:37:06 +02:00
|
|
|
const char *s, const char *insert, size_t n,
|
|
|
|
part_finish_rule_t rule);
|
2004-10-12 21:32:41 +02:00
|
|
|
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);
|
2004-11-05 18:54:50 +01:00
|
|
|
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
|
|
|
|
uint64_t max, int *ok, char **next);
|
2004-10-12 22:20:19 +02:00
|
|
|
const char *hex_str(const char *from, size_t fromlen);
|
2003-12-09 00:45:37 +01:00
|
|
|
const char *eat_whitespace(const char *s);
|
|
|
|
const char *eat_whitespace_no_nl(const char *s);
|
|
|
|
const char *find_whitespace(const char *s);
|
2003-11-12 20:34:19 +01:00
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
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);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Time helpers */
|
2003-04-16 19:04:58 +02:00
|
|
|
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);
|
2004-04-03 06:08:54 +02:00
|
|
|
time_t tor_timegm(struct tm *tm);
|
2004-08-04 03:11:15 +02:00
|
|
|
#define RFC1123_TIME_LEN 29
|
2004-08-07 04:46:16 +02:00
|
|
|
void format_rfc1123_time(char *buf, time_t t);
|
|
|
|
int parse_rfc1123_time(const char *buf, time_t *t);
|
|
|
|
#define ISO_TIME_LEN 19
|
2004-11-22 22:38:44 +01:00
|
|
|
void format_local_iso_time(char *buf, time_t t);
|
2004-08-07 04:46:16 +02:00
|
|
|
void format_iso_time(char *buf, time_t t);
|
|
|
|
int parse_iso_time(const char *buf, time_t *t);
|
2003-10-20 22:19:59 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* File helpers */
|
2004-03-11 07:35:03 +01:00
|
|
|
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);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
|
|
|
|
file_status_t file_status(const char *filename);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2004-11-09 08:12:31 +01:00
|
|
|
typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
|
|
|
|
int check_private_dir(const char *dirname, cpd_check_t check);
|
2004-09-08 09:16:34 +02:00
|
|
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
2004-11-03 20:02:48 +01:00
|
|
|
int write_bytes_to_file(const char *fname, const char *str, size_t len,
|
|
|
|
int bin);
|
2004-09-08 09:16:34 +02:00
|
|
|
char *read_file_to_str(const char *filename, int bin);
|
2004-11-04 23:29:45 +01:00
|
|
|
char *parse_line_from_str(char *line, char **key_out, char **value_out);
|
2004-07-15 04:00:43 +02:00
|
|
|
char *expand_filename(const char *filename);
|
2005-09-03 06:40:05 +02:00
|
|
|
struct smartlist_t *tor_listdir(const char *dirname);
|
2003-08-12 09:43:15 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Net helpers */
|
2004-03-14 19:07:46 +01:00
|
|
|
int is_internal_IP(uint32_t ip);
|
2004-07-13 18:57:37 +02:00
|
|
|
int is_local_IP(uint32_t ip);
|
2004-10-12 17:48:30 +02:00
|
|
|
int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
|
|
|
|
uint16_t *port);
|
2004-10-16 23:41:12 +02:00
|
|
|
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);
|
2005-02-22 09:18:36 +01:00
|
|
|
#define INET_NTOA_BUF_LEN 16
|
|
|
|
int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
|
2005-08-26 09:37:07 +02:00
|
|
|
char *tor_dup_addr(uint32_t addr);
|
2005-02-25 21:46:13 +01:00
|
|
|
int is_plausible_address(const char *name);
|
2005-08-03 22:42:17 +02:00
|
|
|
int get_interface_address(uint32_t *addr);
|
2004-10-16 23:41:12 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Process helpers */
|
2005-04-06 21:07:38 +02:00
|
|
|
void start_daemon(void);
|
|
|
|
void finish_daemon(const char *desired_cwd);
|
2004-11-01 21:41:47 +01:00
|
|
|
void write_pidfile(char *filename);
|
2003-08-14 19:13:52 +02:00
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#endif
|
2005-06-09 21:03:31 +02:00
|
|
|
|