2004-11-01 20:41:47 +00:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
|
2003-04-16 17:04:58 +00:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-05-07 17:04:12 +00:00
|
|
|
/**
|
|
|
|
* \file util.h
|
|
|
|
* \brief Headers for util.c
|
2004-05-10 07:54:13 +00:00
|
|
|
**/
|
2004-05-07 17:04:12 +00:00
|
|
|
|
2003-04-16 17:04:58 +00:00
|
|
|
#ifndef __UTIL_H
|
|
|
|
#define __UTIL_H
|
|
|
|
|
2004-04-03 04:05:12 +00:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2004-04-03 04:08:54 +00:00
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2003-08-12 03:08:41 +00:00
|
|
|
|
2004-05-07 17:04:12 +00:00
|
|
|
/** Replace assert() with a variant that sends failures to the log before
|
2004-05-01 20:46:28 +00:00
|
|
|
* calling assert() normally.
|
|
|
|
*/
|
2004-04-25 19:37:39 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#define tor_assert(expr) do {} while(0)
|
|
|
|
#else
|
|
|
|
#define tor_assert(expr) do { \
|
|
|
|
if (!(expr)) { \
|
|
|
|
log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.", \
|
|
|
|
__FILE__, __LINE__, __FUNCTION__, #expr); \
|
|
|
|
assert(expr); /* write to console too. */ \
|
|
|
|
abort(); /* unreached */ \
|
|
|
|
} } while (0)
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 20:41:47 +00:00
|
|
|
/* Memory management */
|
2003-05-20 06:37:34 +00:00
|
|
|
void *tor_malloc(size_t size);
|
2003-11-18 08:20:09 +00:00
|
|
|
void *tor_malloc_zero(size_t size);
|
2003-10-14 01:11:42 +00:00
|
|
|
void *tor_realloc(void *ptr, size_t size);
|
2003-10-04 03:29:09 +00:00
|
|
|
char *tor_strdup(const char *s);
|
2003-12-08 23:45:37 +00:00
|
|
|
char *tor_strndup(const char *s, size_t n);
|
2003-10-21 09:48:58 +00:00
|
|
|
#define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
|
2004-11-01 20:41:47 +00:00
|
|
|
|
|
|
|
/* String manipulation */
|
|
|
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
2004-03-20 01:48:05 +00:00
|
|
|
void tor_strlower(char *s);
|
2004-09-02 18:25:50 +00:00
|
|
|
int strcmpstart(const char *s1, const char *s2);
|
2004-11-09 18:22:17 +00:00
|
|
|
int strcmpend(const char *s1, const char *s2);
|
2004-10-07 03:11:42 +00:00
|
|
|
int tor_strstrip(char *s, const char *strip);
|
2004-10-07 21:37:06 +00:00
|
|
|
typedef enum {
|
|
|
|
ALWAYS_TERMINATE, NEVER_TERMINATE, TERMINATE_IF_EVEN
|
|
|
|
} part_finish_rule_t;
|
2004-10-07 20:58:53 +00:00
|
|
|
int tor_strpartition(char *dest, size_t dest_len,
|
2004-10-07 21:37:06 +00:00
|
|
|
const char *s, const char *insert, size_t n,
|
|
|
|
part_finish_rule_t rule);
|
2004-10-12 19:32:41 +00: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 17:54:50 +00:00
|
|
|
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
|
|
|
|
uint64_t max, int *ok, char **next);
|
2004-10-12 20:20:19 +00:00
|
|
|
const char *hex_str(const char *from, size_t fromlen);
|
2003-12-08 23:45:37 +00: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 19:34:19 +00:00
|
|
|
|
2004-11-02 02:28:51 +00: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 20:41:47 +00:00
|
|
|
|
2004-05-01 20:46:28 +00:00
|
|
|
/* Time helpers */
|
2003-04-16 17:04:58 +00: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 04:08:54 +00:00
|
|
|
time_t tor_timegm(struct tm *tm);
|
2004-08-04 01:11:15 +00:00
|
|
|
#define RFC1123_TIME_LEN 29
|
2004-08-07 02:46:16 +00: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
|
|
|
|
void format_iso_time(char *buf, time_t t);
|
|
|
|
int parse_iso_time(const char *buf, time_t *t);
|
2003-10-20 20:19:59 +00:00
|
|
|
|
2004-11-01 20:41:47 +00:00
|
|
|
/* File helpers */
|
2004-03-11 06:35:03 +00: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-20 23:05:22 +00:00
|
|
|
|
2003-09-26 18:27:35 +00: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 20:41:47 +00:00
|
|
|
|
2004-11-09 07:12:31 +00: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 07:16:34 +00:00
|
|
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
2004-11-03 19:02:48 +00:00
|
|
|
int write_bytes_to_file(const char *fname, const char *str, size_t len,
|
|
|
|
int bin);
|
2004-09-08 07:16:34 +00:00
|
|
|
char *read_file_to_str(const char *filename, int bin);
|
2004-11-04 22:29:45 +00:00
|
|
|
char *parse_line_from_str(char *line, char **key_out, char **value_out);
|
2004-07-15 02:00:43 +00:00
|
|
|
char *expand_filename(const char *filename);
|
2003-08-12 07:43:15 +00:00
|
|
|
|
2004-11-01 20:41:47 +00:00
|
|
|
/* Net helpers */
|
2004-03-14 18:07:46 +00:00
|
|
|
int is_internal_IP(uint32_t ip);
|
2004-07-13 16:57:37 +00:00
|
|
|
int is_local_IP(uint32_t ip);
|
2004-04-28 19:35:12 +00:00
|
|
|
int tor_lookup_hostname(const char *name, uint32_t *addr);
|
2004-10-12 15:48:30 +00:00
|
|
|
int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
|
|
|
|
uint16_t *port);
|
2004-10-16 21:41:12 +00: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);
|
|
|
|
|
2004-11-01 20:41:47 +00:00
|
|
|
/* Process helpers */
|
|
|
|
void start_daemon(const char *desired_cwd);
|
|
|
|
void finish_daemon(void);
|
|
|
|
void write_pidfile(char *filename);
|
2003-08-14 17:13:52 +00:00
|
|
|
|
2003-04-16 17:04:58 +00:00
|
|
|
#endif
|
2004-04-03 03:37:11 +00:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|