2003-04-16 19:04:58 +02:00
|
|
|
/* Copyright 2003 Roger Dingledine */
|
|
|
|
/* 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-04-03 06:05:12 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
|
|
|
#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
|
|
|
|
2003-08-14 19:13:52 +02:00
|
|
|
#if _MSC_VER > 1300
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
2004-08-19 19:21:17 +02:00
|
|
|
#if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
|
2004-07-20 23:23:50 +02:00
|
|
|
struct timeval {
|
|
|
|
time_t tv_sec;
|
|
|
|
unsigned int tv_usec;
|
|
|
|
};
|
2003-08-12 05:08:41 +02:00
|
|
|
#endif
|
|
|
|
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Windows names string functions differently from most other platforms. */
|
2003-08-12 05:08:41 +02:00
|
|
|
#define strncasecmp strnicmp
|
|
|
|
#define strcasecmp stricmp
|
2004-05-01 22:46:28 +02:00
|
|
|
/* "inline" is __inline on windows. " */
|
2003-08-12 05:08:41 +02:00
|
|
|
#define INLINE __inline
|
2004-04-28 22:13:21 +02:00
|
|
|
/* Windows compilers before VC7 don't have __FUNCTION__. */
|
|
|
|
#if _MSC_VER < 1300
|
|
|
|
#define __FUNCTION__ "???"
|
|
|
|
#endif
|
2003-08-12 05:08:41 +02:00
|
|
|
#else
|
|
|
|
#define INLINE inline
|
|
|
|
#endif
|
2003-04-16 19:04:58 +02:00
|
|
|
|
2004-05-07 19:04:12 +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
|
|
|
|
#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-05-07 19:04:12 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/** On windows, you have to call close() on fds returned by open(),
|
|
|
|
* and closesocket() on fds returned by socket(). On Unix, everything
|
|
|
|
* gets close()'d. We abstract this difference by always using
|
|
|
|
* tor_close_socket to close sockets, and always using close() on
|
2004-05-01 22:46:28 +02:00
|
|
|
* files.
|
|
|
|
*/
|
2004-04-28 23:17:42 +02:00
|
|
|
#define tor_close_socket(s) closesocket(s)
|
2004-04-28 23:14:56 +02:00
|
|
|
#else
|
|
|
|
#define tor_close_socket(s) close(s)
|
|
|
|
#endif
|
|
|
|
|
2004-08-18 06:44:24 +02:00
|
|
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
|
|
|
|
2004-03-17 08:28:09 +01:00
|
|
|
size_t strlcat(char *dst, const char *src, size_t siz);
|
|
|
|
size_t strlcpy(char *dst, const char *src, size_t siz);
|
|
|
|
|
2003-05-20 08:37:34 +02:00
|
|
|
void *tor_malloc(size_t size);
|
2003-11-18 09:20:09 +01:00
|
|
|
void *tor_malloc_zero(size_t size);
|
2003-10-14 03:11:42 +02:00
|
|
|
void *tor_realloc(void *ptr, size_t size);
|
2003-10-04 05:29:09 +02:00
|
|
|
char *tor_strdup(const char *s);
|
2003-12-09 00:45:37 +01:00
|
|
|
char *tor_strndup(const char *s, size_t n);
|
2003-10-21 11:48:58 +02:00
|
|
|
#define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
|
2004-03-20 02:48:05 +01:00
|
|
|
void tor_strlower(char *s);
|
2004-09-02 20:25:50 +02:00
|
|
|
int strcmpstart(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-07 22:58:53 +02:00
|
|
|
|
2003-05-20 08:37:34 +02:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Some platforms segfault when you try to access a multi-byte type
|
|
|
|
* that isn't aligned to a word boundary. The macros and/or functions
|
|
|
|
* below can be used to access unaligned data on any platform.
|
|
|
|
*/
|
2004-03-21 05:14:06 +01:00
|
|
|
#ifdef UNALIGNED_INT_ACCESS_OK
|
2004-03-21 03:01:17 +01:00
|
|
|
#define get_uint16(cp) (*(uint16_t*)(cp))
|
|
|
|
#define get_uint32(cp) (*(uint32_t*)(cp))
|
2004-03-31 04:07:38 +02:00
|
|
|
#define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
|
|
|
|
#define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
|
2004-03-21 03:01:17 +01:00
|
|
|
#else
|
2004-03-31 04:07:38 +02:00
|
|
|
#if 1
|
2004-04-28 22:13:21 +02:00
|
|
|
uint16_t get_uint16(const char *cp);
|
|
|
|
uint32_t get_uint32(const char *cp);
|
2004-03-21 03:01:17 +01:00
|
|
|
void set_uint16(char *cp, uint16_t v);
|
|
|
|
void set_uint32(char *cp, uint32_t v);
|
2004-03-31 04:07:38 +02:00
|
|
|
#else
|
2004-04-06 05:46:37 +02:00
|
|
|
#define get_uint16(cp) \
|
|
|
|
( ((*(((uint8_t*)(cp))+0))<<8) + \
|
2004-03-31 04:07:38 +02:00
|
|
|
((*(((uint8_t*)(cp))+1)) ) )
|
2004-04-06 05:46:37 +02:00
|
|
|
#define get_uint32(cp) \
|
|
|
|
( ((*(((uint8_t*)(cp))+0))<<24) + \
|
|
|
|
((*(((uint8_t*)(cp))+1))<<16) + \
|
|
|
|
((*(((uint8_t*)(cp))+2))<<8 ) + \
|
2004-03-31 04:07:38 +02:00
|
|
|
((*(((uint8_t*)(cp))+3)) ) )
|
2004-04-06 05:46:37 +02:00
|
|
|
#define set_uint16(cp,v) \
|
|
|
|
do { \
|
|
|
|
uint16_t u16v = (v); \
|
|
|
|
*(((uint8_t*)(cp))+0) = (v >> 8)&0xff; \
|
|
|
|
*(((uint8_t*)(cp))+1) = (v >> 0)&0xff; \
|
2004-03-31 04:07:38 +02:00
|
|
|
} while (0)
|
2004-04-06 05:46:37 +02:00
|
|
|
#define set_uint32(cp,val) \
|
|
|
|
do { \
|
|
|
|
uint32_t u32v = (v); \
|
|
|
|
*(((uint8_t*)(cp))+0) = s32 >> 24)&0xff; \
|
|
|
|
*(((uint8_t*)(cp))+1) = s32 >> 16)&0xff; \
|
|
|
|
*(((uint8_t*)(cp))+2) = s32 >> 8)&0xff; \
|
|
|
|
*(((uint8_t*)(cp))+3) = s32 >> 0)&0xff; \
|
2004-03-31 04:07:38 +02:00
|
|
|
} while (0)
|
|
|
|
#endif
|
2004-03-21 03:01:17 +01:00
|
|
|
#endif
|
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
const char *hex_str(const char *from, int fromlen);
|
2004-04-03 01:30:54 +02:00
|
|
|
|
2004-05-07 19:04:12 +02:00
|
|
|
/** Generic resizeable array. */
|
2004-04-03 02:58:54 +02:00
|
|
|
typedef struct smartlist_t smartlist_t;
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2004-03-31 00:59:00 +02:00
|
|
|
smartlist_t *smartlist_create();
|
2003-12-13 02:42:44 +01:00
|
|
|
void smartlist_free(smartlist_t *sl);
|
2004-03-31 00:59:00 +02:00
|
|
|
void smartlist_set_capacity(smartlist_t *sl, int n);
|
2004-04-06 23:19:59 +02:00
|
|
|
void smartlist_clear(smartlist_t *sl);
|
2004-04-07 23:36:03 +02:00
|
|
|
void smartlist_truncate(smartlist_t *sl, int n);
|
2003-12-13 02:42:44 +01:00
|
|
|
void smartlist_add(smartlist_t *sl, void *element);
|
2004-04-07 23:36:03 +02:00
|
|
|
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
|
2003-12-14 05:57:47 +01:00
|
|
|
void smartlist_remove(smartlist_t *sl, void *element);
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_isin(const smartlist_t *sl, void *element);
|
2004-08-16 22:46:00 +02:00
|
|
|
int smartlist_string_isin(const smartlist_t *sl, const char *element);
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
|
|
|
|
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
|
|
|
|
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
|
|
|
|
void *smartlist_choose(const smartlist_t *sl);
|
|
|
|
void *smartlist_get(const smartlist_t *sl, int idx);
|
2004-04-03 02:58:54 +02:00
|
|
|
void *smartlist_set(smartlist_t *sl, int idx, void *val);
|
|
|
|
void *smartlist_del(smartlist_t *sl, int idx);
|
2004-04-07 21:46:27 +02:00
|
|
|
void *smartlist_del_keeporder(smartlist_t *sl, int idx);
|
|
|
|
void smartlist_insert(smartlist_t *sl, int idx, void *val);
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_len(const smartlist_t *sl);
|
2004-09-02 20:25:50 +02:00
|
|
|
#define SPLIT_SKIP_SPACE 0x01
|
|
|
|
#define SPLIT_IGNORE_BLANK 0x02
|
2004-08-17 23:06:36 +02:00
|
|
|
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
|
2004-09-02 20:25:50 +02:00
|
|
|
int flags, int max);
|
2004-08-17 23:06:36 +02:00
|
|
|
|
2004-04-06 05:46:37 +02:00
|
|
|
#define SMARTLIST_FOREACH(sl, type, var, cmd) \
|
|
|
|
do { \
|
|
|
|
int sl_idx, sl_len=smartlist_len(sl); \
|
|
|
|
type var; \
|
|
|
|
for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
|
|
|
|
var = smartlist_get((sl),sl_idx); \
|
|
|
|
do {cmd;} while(0); \
|
2004-04-03 02:58:54 +02:00
|
|
|
} } while (0)
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2004-03-19 23:07:24 +01:00
|
|
|
/* Map from const char * to void*. Implemented with a splay tree. */
|
|
|
|
typedef struct strmap_t strmap_t;
|
|
|
|
typedef struct strmap_entry_t strmap_entry_t;
|
|
|
|
typedef struct strmap_entry_t strmap_iter_t;
|
|
|
|
strmap_t* strmap_new(void);
|
|
|
|
void* strmap_set(strmap_t *map, const char *key, void *val);
|
|
|
|
void* strmap_get(strmap_t *map, const char *key);
|
|
|
|
void* strmap_remove(strmap_t *map, const char *key);
|
2004-03-20 02:48:05 +01:00
|
|
|
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
|
|
|
|
void* strmap_get_lc(strmap_t *map, const char *key);
|
|
|
|
void* strmap_remove_lc(strmap_t *map, const char *key);
|
|
|
|
typedef void* (*strmap_foreach_fn)(const char *key, void *val, void *data);
|
|
|
|
void strmap_foreach(strmap_t *map, strmap_foreach_fn fn, void *data);
|
2004-03-19 23:07:24 +01:00
|
|
|
void strmap_free(strmap_t *map, void (*free_val)(void*));
|
2004-08-17 08:28:34 +02:00
|
|
|
int strmap_isempty(strmap_t *map);
|
2004-03-19 23:07:24 +01:00
|
|
|
|
|
|
|
strmap_iter_t *strmap_iter_init(strmap_t *map);
|
|
|
|
strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter);
|
|
|
|
strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter);
|
|
|
|
void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp);
|
|
|
|
|
|
|
|
int strmap_iter_done(strmap_iter_t *iter);
|
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* String manipulation */
|
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-05-01 22:46:28 +02:00
|
|
|
/* Time helpers */
|
2003-10-21 11:48:58 +02:00
|
|
|
void tor_gettimeofday(struct timeval *timeval);
|
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
|
|
|
|
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-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-08-12 05:08:41 +02:00
|
|
|
void set_socket_nonblocking(int socket);
|
|
|
|
|
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);
|
|
|
|
int check_private_dir(const char *dirname, int create);
|
2004-09-08 09:16:34 +02:00
|
|
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
|
|
|
char *read_file_to_str(const char *filename, int bin);
|
2003-09-29 09:50:08 +02:00
|
|
|
int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
|
2004-07-15 04:00:43 +02:00
|
|
|
char *expand_filename(const char *filename);
|
2004-08-09 06:28:25 +02:00
|
|
|
int replace_file(const char *from, const char *to);
|
2003-09-26 20:27:35 +02:00
|
|
|
|
2003-08-12 09:01:20 +02:00
|
|
|
int spawn_func(int (*func)(void *), void *data);
|
|
|
|
void spawn_exit();
|
|
|
|
|
2004-06-05 03:50:35 +02:00
|
|
|
/* Because we use threads instead of processes on Windows, we need locking on Windows.
|
|
|
|
* On Unixy platforms, these functions are no-ops. */
|
|
|
|
typedef struct tor_mutex_t tor_mutex_t;
|
|
|
|
tor_mutex_t *tor_mutex_new(void);
|
|
|
|
void tor_mutex_acquire(tor_mutex_t *m);
|
|
|
|
void tor_mutex_release(tor_mutex_t *m);
|
|
|
|
void tor_mutex_free(tor_mutex_t *m);
|
|
|
|
|
2003-08-12 09:43:15 +02:00
|
|
|
int tor_socketpair(int family, int type, int protocol, int fd[2]);
|
|
|
|
|
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-03-14 19:07:46 +01:00
|
|
|
|
2003-10-02 00:31:13 +02:00
|
|
|
const char *get_uname(void);
|
|
|
|
|
2004-06-30 18:37:08 +02:00
|
|
|
void start_daemon(const char *desired_cwd);
|
2004-01-03 23:40:49 +01:00
|
|
|
void finish_daemon(void);
|
|
|
|
|
2003-10-22 13:21:29 +02:00
|
|
|
void write_pidfile(char *filename);
|
|
|
|
int switch_id(char *user, char *group);
|
|
|
|
|
2004-03-09 23:01:17 +01:00
|
|
|
struct in_addr;
|
|
|
|
int tor_inet_aton(const char *cp, struct in_addr *addr);
|
2004-04-28 21:35:12 +02:00
|
|
|
int tor_lookup_hostname(const char *name, uint32_t *addr);
|
2004-10-12 17:48:30 +02:00
|
|
|
int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
|
|
|
|
uint16_t *port);
|
2004-03-09 23:01:17 +01:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* For stupid historical reasons, windows sockets have an independent
|
|
|
|
* set of errnos, and an independent way to get them. Also, you can't
|
|
|
|
* always believe WSAEWOULDBLOCK. Use the macros below to compare
|
|
|
|
* errnos against expected values, and use tor_socket_errno to find
|
|
|
|
* the actual errno after a socket operation fails.
|
2003-08-14 19:13:52 +02:00
|
|
|
*/
|
|
|
|
#ifdef MS_WINDOWS
|
2004-05-07 19:04:12 +02:00
|
|
|
/** Return true if e is EAGAIN or the local equivalent. */
|
2004-05-01 22:46:28 +02:00
|
|
|
#define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
|
2004-05-07 19:04:12 +02:00
|
|
|
/** Return true if e is EINPROGRESS or the local equivalent. */
|
2004-05-01 22:46:28 +02:00
|
|
|
#define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
|
2004-05-07 19:04:12 +02:00
|
|
|
/** Return true if e is EINPROGRESS or the local equivalent as returned by
|
|
|
|
* a call to connect(). */
|
2004-05-01 22:46:28 +02:00
|
|
|
#define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
|
|
|
|
int tor_socket_errno(int sock);
|
2004-05-02 22:18:21 +02:00
|
|
|
const char *tor_socket_strerror(int e);
|
2003-08-14 19:13:52 +02:00
|
|
|
#else
|
2004-05-01 22:46:28 +02:00
|
|
|
#define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
|
|
|
|
#define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
|
|
|
|
#define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
|
|
|
|
#define tor_socket_errno(sock) (errno)
|
2004-05-02 22:27:48 +02:00
|
|
|
#define tor_socket_strerror(e) strerror(e)
|
2003-08-14 19:13:52 +02:00
|
|
|
#endif
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#endif
|
2004-04-03 05:37:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|