2003-04-16 19:04:58 +02:00
|
|
|
/* Copyright 2003 Roger Dingledine */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#include "orconfig.h"
|
2004-04-28 22:13:21 +02:00
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#define WIN32_WINNT 0x400
|
|
|
|
#define _WIN32_WINNT 0x400
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#if _MSC_VER > 1300
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
#include <io.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "crypto.h"
|
2004-03-19 23:07:24 +01:00
|
|
|
#include "../or/tree.h"
|
2003-08-12 10:18:13 +02:00
|
|
|
|
2003-10-02 00:31:13 +02:00
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
2004-04-03 06:05:12 +02:00
|
|
|
#ifdef HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2004-04-28 22:13:21 +02:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2004-04-17 08:35:03 +02:00
|
|
|
#ifdef HAVE_SYS_LIMITS_H
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#endif
|
2004-04-26 01:47:26 +02:00
|
|
|
#ifdef HAVE_MACHINE_LIMITS_H
|
|
|
|
#include <machine/limits.h>
|
|
|
|
#endif
|
2004-04-03 06:05:12 +02:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2004-04-28 21:35:12 +02:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2004-04-03 06:05:12 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FCNTL_H
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GRP_H
|
|
|
|
#include <grp.h>
|
|
|
|
#endif
|
2004-04-28 22:13:21 +02:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
2004-04-28 21:35:12 +02:00
|
|
|
#endif
|
|
|
|
|
2004-03-18 20:59:39 +01:00
|
|
|
/* used by inet_addr, not defined on solaris anywhere!? */
|
|
|
|
#ifndef INADDR_NONE
|
|
|
|
#define INADDR_NONE ((unsigned long) -1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* in-line the strl functions */
|
2004-03-17 08:28:09 +01:00
|
|
|
#ifndef HAVE_STRLCPY
|
|
|
|
#include "strlcpy.c"
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_STRLCAT
|
|
|
|
#include "strlcat.c"
|
|
|
|
#endif
|
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
/*
|
2003-10-04 05:29:09 +02:00
|
|
|
* Memory wrappers
|
2003-09-26 20:27:35 +02:00
|
|
|
*/
|
|
|
|
|
2003-05-20 08:37:34 +02:00
|
|
|
void *tor_malloc(size_t size) {
|
|
|
|
void *result;
|
|
|
|
|
2004-04-08 05:05:08 +02:00
|
|
|
/* Some libcs don't do the right thing on size==0. Override them. */
|
|
|
|
if (size==0) {
|
|
|
|
size=1;
|
|
|
|
}
|
2003-05-20 08:37:34 +02:00
|
|
|
result = malloc(size);
|
|
|
|
|
|
|
|
if(!result) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_ERR, "Out of memory. Dying.");
|
2003-05-20 08:37:34 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-18 02:07:58 +02:00
|
|
|
// memset(result,'X',size); /* deadbeef to encourage bugs */
|
2003-05-20 08:37:34 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-11-18 09:20:09 +01:00
|
|
|
void *tor_malloc_zero(size_t size) {
|
|
|
|
void *result = tor_malloc(size);
|
|
|
|
memset(result, 0, size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-10-14 03:11:42 +02:00
|
|
|
void *tor_realloc(void *ptr, size_t size) {
|
|
|
|
void *result;
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2003-10-14 03:11:42 +02:00
|
|
|
result = realloc(ptr, size);
|
|
|
|
if (!result) {
|
|
|
|
log_fn(LOG_ERR, "Out of memory. Dying.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
char *tor_strdup(const char *s) {
|
|
|
|
char *dup;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(s);
|
2003-10-04 05:29:09 +02:00
|
|
|
|
|
|
|
dup = strdup(s);
|
|
|
|
if(!dup) {
|
|
|
|
log_fn(LOG_ERR,"Out of memory. Dying.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2003-12-09 00:45:37 +01:00
|
|
|
char *tor_strndup(const char *s, size_t n) {
|
|
|
|
char *dup;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(s);
|
2003-12-09 00:45:37 +01:00
|
|
|
dup = tor_malloc(n+1);
|
|
|
|
strncpy(dup, s, n);
|
|
|
|
dup[n] = 0;
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2004-03-20 02:21:19 +01:00
|
|
|
/* Convert s to lowercase. */
|
|
|
|
void tor_strlower(char *s)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
*s = tolower(*s);
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-21 05:14:06 +01:00
|
|
|
#ifndef UNALIGNED_INT_ACCESS_OK
|
2004-04-28 22:13:21 +02:00
|
|
|
uint16_t get_uint16(const char *cp)
|
2004-03-21 03:01:17 +01:00
|
|
|
{
|
|
|
|
uint16_t v;
|
|
|
|
memcpy(&v,cp,2);
|
|
|
|
return v;
|
|
|
|
}
|
2004-04-28 22:13:21 +02:00
|
|
|
uint32_t get_uint32(const char *cp)
|
2004-03-21 03:01:17 +01:00
|
|
|
{
|
|
|
|
uint32_t v;
|
|
|
|
memcpy(&v,cp,4);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
void set_uint16(char *cp, uint16_t v)
|
|
|
|
{
|
|
|
|
memcpy(cp,&v,2);
|
|
|
|
}
|
|
|
|
void set_uint32(char *cp, uint32_t v)
|
|
|
|
{
|
|
|
|
memcpy(cp,&v,4);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-03 01:30:54 +02:00
|
|
|
void hex_encode(const char *from, int fromlen, char *to)
|
|
|
|
{
|
|
|
|
const unsigned char *fp = from;
|
|
|
|
static const char TABLE[] = "0123456789abcdef";
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(from && fromlen>=0 && to);
|
2004-04-03 04:55:42 +02:00
|
|
|
while (fromlen--) {
|
2004-04-03 01:30:54 +02:00
|
|
|
*to++ = TABLE[*fp >> 4];
|
|
|
|
*to++ = TABLE[*fp & 7];
|
|
|
|
++fp;
|
|
|
|
}
|
|
|
|
*to = '\0';
|
|
|
|
}
|
2004-03-20 02:21:19 +01:00
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
const char *hex_str(const char *from, int fromlen)
|
|
|
|
{
|
|
|
|
static char buf[65];
|
|
|
|
if (fromlen>(sizeof(buf)-1)/2)
|
|
|
|
fromlen = (sizeof(buf)-1)/2;
|
|
|
|
hex_encode(from,fromlen,buf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-12-13 02:42:44 +01:00
|
|
|
/*
|
|
|
|
* A simple smartlist interface to make an unordered list of acceptable
|
|
|
|
* nodes and then choose a random one.
|
|
|
|
* smartlist_create() mallocs the list, _free() frees the list,
|
|
|
|
* _add() adds an element, _remove() removes an element if it's there,
|
|
|
|
* _choose() returns a random element.
|
|
|
|
*/
|
2004-03-31 00:59:00 +02:00
|
|
|
#define SMARTLIST_DEFAULT_CAPACITY 32
|
2004-04-03 02:58:54 +02:00
|
|
|
|
|
|
|
struct smartlist_t {
|
|
|
|
void **list;
|
|
|
|
int num_used;
|
|
|
|
int capacity;
|
|
|
|
};
|
|
|
|
|
2004-03-31 00:59:00 +02:00
|
|
|
smartlist_t *smartlist_create() {
|
2003-12-13 02:42:44 +01:00
|
|
|
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
|
|
|
|
sl->num_used = 0;
|
2004-03-31 00:59:00 +02:00
|
|
|
sl->capacity = SMARTLIST_DEFAULT_CAPACITY;
|
2004-03-30 21:25:43 +02:00
|
|
|
sl->list = tor_malloc(sizeof(void *) * sl->capacity);
|
2003-12-13 02:42:44 +01:00
|
|
|
return sl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void smartlist_free(smartlist_t *sl) {
|
|
|
|
free(sl->list);
|
|
|
|
free(sl);
|
|
|
|
}
|
|
|
|
|
2004-03-31 00:59:00 +02:00
|
|
|
void smartlist_set_capacity(smartlist_t *sl, int n) {
|
2004-04-03 02:58:54 +02:00
|
|
|
if (n<0)
|
|
|
|
n = sl->num_used;
|
2004-03-31 00:59:00 +02:00
|
|
|
if (sl->capacity != n && sl->num_used < n) {
|
2004-03-30 21:25:43 +02:00
|
|
|
sl->capacity = n;
|
|
|
|
sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-06 23:19:59 +02:00
|
|
|
/* Remove all elements from the list. */
|
|
|
|
void smartlist_clear(smartlist_t *sl) {
|
|
|
|
sl->num_used = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
void smartlist_truncate(smartlist_t *sl, int len)
|
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(len <= sl->num_used);
|
2004-04-07 23:36:03 +02:00
|
|
|
sl->num_used = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add element to the list */
|
2003-12-13 02:42:44 +01:00
|
|
|
void smartlist_add(smartlist_t *sl, void *element) {
|
2004-03-30 22:05:52 +02:00
|
|
|
if (sl->num_used >= sl->capacity) {
|
|
|
|
sl->capacity *= 2;
|
|
|
|
sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
|
|
|
|
}
|
|
|
|
sl->list[sl->num_used++] = element;
|
2003-12-13 02:42:44 +01:00
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
/* Add all elements from S2 to S1. */
|
|
|
|
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(s2, void *, element, smartlist_add(sl, element));
|
|
|
|
}
|
|
|
|
|
2003-12-14 05:57:47 +01:00
|
|
|
void smartlist_remove(smartlist_t *sl, void *element) {
|
|
|
|
int i;
|
|
|
|
if(element == NULL)
|
|
|
|
return;
|
|
|
|
for(i=0; i < sl->num_used; i++)
|
|
|
|
if(sl->list[i] == element) {
|
|
|
|
sl->list[i] = sl->list[--sl->num_used]; /* swap with the end */
|
|
|
|
i--; /* so we process the new i'th element */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_isin(const smartlist_t *sl, void *element) {
|
2003-12-14 00:32:03 +01:00
|
|
|
int i;
|
|
|
|
for(i=0; i < sl->num_used; i++)
|
|
|
|
if(sl->list[i] == element)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
|
2003-12-14 00:32:03 +01:00
|
|
|
int i;
|
|
|
|
for(i=0; i < sl2->num_used; i++)
|
|
|
|
if(smartlist_isin(sl1, sl2->list[i]))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove elements of sl1 that aren't in sl2 */
|
2004-04-07 23:36:03 +02:00
|
|
|
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
|
2003-12-14 00:32:03 +01:00
|
|
|
int i;
|
|
|
|
for(i=0; i < sl1->num_used; i++)
|
|
|
|
if(!smartlist_isin(sl2, sl1->list[i])) {
|
|
|
|
sl1->list[i] = sl1->list[--sl1->num_used]; /* swap with the end */
|
|
|
|
i--; /* so we process the new i'th element */
|
|
|
|
}
|
|
|
|
}
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2003-12-14 05:57:47 +01:00
|
|
|
/* remove all elements of sl2 from sl1 */
|
2004-04-07 23:36:03 +02:00
|
|
|
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
|
2003-12-14 05:57:47 +01:00
|
|
|
int i;
|
|
|
|
for(i=0; i < sl2->num_used; i++)
|
|
|
|
smartlist_remove(sl1, sl2->list[i]);
|
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
void *smartlist_choose(const smartlist_t *sl) {
|
2003-12-13 02:42:44 +01:00
|
|
|
if(sl->num_used)
|
|
|
|
return sl->list[crypto_pseudo_rand_int(sl->num_used)];
|
|
|
|
return NULL; /* no elements to choose from */
|
|
|
|
}
|
|
|
|
|
2004-04-07 23:36:03 +02:00
|
|
|
void *smartlist_get(const smartlist_t *sl, int idx)
|
2004-04-03 02:58:54 +02:00
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(sl && idx>=0 && idx < sl->num_used);
|
2004-04-03 02:58:54 +02:00
|
|
|
return sl->list[idx];
|
|
|
|
}
|
|
|
|
void *smartlist_set(smartlist_t *sl, int idx, void *val)
|
|
|
|
{
|
|
|
|
void *old;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(sl && idx>=0 && idx < sl->num_used);
|
2004-04-03 02:58:54 +02:00
|
|
|
old = sl->list[idx];
|
|
|
|
sl->list[idx] = val;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
void *smartlist_del(smartlist_t *sl, int idx)
|
|
|
|
{
|
|
|
|
void *old;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(sl && idx>=0 && idx < sl->num_used);
|
2004-04-03 02:58:54 +02:00
|
|
|
old = sl->list[idx];
|
|
|
|
sl->list[idx] = sl->list[--sl->num_used];
|
|
|
|
return old;
|
|
|
|
}
|
2004-04-07 21:46:27 +02:00
|
|
|
void *smartlist_del_keeporder(smartlist_t *sl, int idx)
|
|
|
|
{
|
|
|
|
void *old;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(sl && idx>=0 && idx < sl->num_used);
|
2004-04-07 21:46:27 +02:00
|
|
|
old = sl->list[idx];
|
|
|
|
--sl->num_used;
|
|
|
|
if (idx < sl->num_used)
|
|
|
|
memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
|
|
|
|
return old;
|
|
|
|
}
|
2004-04-07 23:36:03 +02:00
|
|
|
int smartlist_len(const smartlist_t *sl)
|
2004-04-03 02:58:54 +02:00
|
|
|
{
|
|
|
|
return sl->num_used;
|
|
|
|
}
|
2004-04-07 21:46:27 +02:00
|
|
|
void smartlist_insert(smartlist_t *sl, int idx, void *val)
|
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(sl && idx >= 0 && idx <= sl->num_used);
|
2004-04-07 21:46:27 +02:00
|
|
|
if (idx == sl->num_used) {
|
|
|
|
smartlist_add(sl, val);
|
|
|
|
} else {
|
|
|
|
/* Ensure sufficient capacity */
|
|
|
|
if (sl->num_used >= sl->capacity) {
|
|
|
|
sl->capacity *= 2;
|
|
|
|
sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
|
|
|
|
}
|
|
|
|
/* Move other elements away */
|
|
|
|
if (idx < sl->num_used)
|
|
|
|
memmove(sl->list + idx + 1, sl->list + idx,
|
|
|
|
sizeof(void*)*(sl->num_used-idx));
|
|
|
|
sl->num_used++;
|
|
|
|
sl->list[idx] = val;
|
|
|
|
}
|
|
|
|
}
|
2004-04-03 02:58:54 +02:00
|
|
|
|
2004-03-19 23:07:24 +01:00
|
|
|
/*
|
|
|
|
* Splay-tree implementation of string-to-void* map
|
|
|
|
*/
|
|
|
|
struct strmap_entry_t {
|
|
|
|
SPLAY_ENTRY(strmap_entry_t) node;
|
|
|
|
char *key;
|
|
|
|
void *val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct strmap_t {
|
|
|
|
SPLAY_HEAD(strmap_tree, strmap_entry_t) head;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int compare_strmap_entries(struct strmap_entry_t *a,
|
2004-04-06 05:44:36 +02:00
|
|
|
struct strmap_entry_t *b)
|
2004-03-19 23:07:24 +01:00
|
|
|
{
|
|
|
|
return strcmp(a->key, b->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
|
|
|
SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
|
|
|
|
|
|
|
/* Create a new empty map from strings to void*'s.
|
|
|
|
*/
|
|
|
|
strmap_t* strmap_new(void)
|
|
|
|
{
|
|
|
|
strmap_t *result;
|
|
|
|
result = tor_malloc(sizeof(strmap_t));
|
|
|
|
SPLAY_INIT(&result->head);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the current value for <key> with <val>. Returns the previous
|
|
|
|
* value for <key> if one was set, or NULL if one was not.
|
|
|
|
*
|
|
|
|
* This function makes a copy of 'key' if necessary, but not of 'val'.
|
|
|
|
*/
|
|
|
|
void* strmap_set(strmap_t *map, const char *key, void *val)
|
|
|
|
{
|
|
|
|
strmap_entry_t *resolve;
|
|
|
|
strmap_entry_t search;
|
|
|
|
void *oldval;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && key && val);
|
2004-03-19 23:07:24 +01:00
|
|
|
search.key = (char*)key;
|
|
|
|
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
|
|
|
if (resolve) {
|
|
|
|
oldval = resolve->val;
|
|
|
|
resolve->val = val;
|
|
|
|
return oldval;
|
|
|
|
} else {
|
|
|
|
resolve = tor_malloc_zero(sizeof(strmap_entry_t));
|
|
|
|
resolve->key = tor_strdup(key);
|
|
|
|
resolve->val = val;
|
|
|
|
SPLAY_INSERT(strmap_tree, &map->head, resolve);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the current value associated with <key>, or NULL if no
|
|
|
|
* value is set.
|
|
|
|
*/
|
|
|
|
void* strmap_get(strmap_t *map, const char *key)
|
|
|
|
{
|
|
|
|
strmap_entry_t *resolve;
|
|
|
|
strmap_entry_t search;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && key);
|
2004-03-19 23:07:24 +01:00
|
|
|
search.key = (char*)key;
|
|
|
|
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
|
|
|
if (resolve) {
|
|
|
|
return resolve->val;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the value currently associated with <key> from the map.
|
|
|
|
* Return the value if one was set, or NULL if there was no entry for
|
|
|
|
* <key>.
|
|
|
|
*
|
|
|
|
* Note: you must free any storage associated with the returned value.
|
|
|
|
*/
|
|
|
|
void* strmap_remove(strmap_t *map, const char *key)
|
|
|
|
{
|
|
|
|
strmap_entry_t *resolve;
|
|
|
|
strmap_entry_t search;
|
|
|
|
void *oldval;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && key);
|
2004-03-19 23:07:24 +01:00
|
|
|
search.key = (char*)key;
|
|
|
|
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
|
|
|
if (resolve) {
|
|
|
|
oldval = resolve->val;
|
|
|
|
SPLAY_REMOVE(strmap_tree, &map->head, resolve);
|
|
|
|
tor_free(resolve->key);
|
|
|
|
tor_free(resolve);
|
|
|
|
return oldval;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-20 02:21:19 +01:00
|
|
|
/* Same as strmap_set, but first converts <key> to lowercase. */
|
|
|
|
void* strmap_set_lc(strmap_t *map, const char *key, void *val)
|
|
|
|
{
|
|
|
|
/* We could be a little faster by using strcasecmp instead, and a separate
|
|
|
|
* type, but I don't think it matters. */
|
|
|
|
void *v;
|
|
|
|
char *lc_key = tor_strdup(key);
|
|
|
|
tor_strlower(lc_key);
|
|
|
|
v = strmap_set(map,lc_key,val);
|
|
|
|
tor_free(lc_key);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
/* Same as strmap_get, but first converts <key> to lowercase. */
|
|
|
|
void* strmap_get_lc(strmap_t *map, const char *key)
|
|
|
|
{
|
|
|
|
void *v;
|
|
|
|
char *lc_key = tor_strdup(key);
|
|
|
|
tor_strlower(lc_key);
|
|
|
|
v = strmap_get(map,lc_key);
|
|
|
|
tor_free(lc_key);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
/* Same as strmap_remove, but first converts <key> to lowercase */
|
|
|
|
void* strmap_remove_lc(strmap_t *map, const char *key)
|
|
|
|
{
|
|
|
|
void *v;
|
|
|
|
char *lc_key = tor_strdup(key);
|
|
|
|
tor_strlower(lc_key);
|
|
|
|
v = strmap_remove(map,lc_key);
|
|
|
|
tor_free(lc_key);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-19 23:07:24 +01:00
|
|
|
/* Invoke fn() on every entry of the map, in order. For every entry,
|
|
|
|
* fn() is invoked with that entry's key, that entry's value, and the
|
|
|
|
* value of <data> supplied to strmap_foreach. fn() must return a new
|
|
|
|
* (possibly unmodified) value for each entry: if fn() returns NULL, the
|
|
|
|
* entry is removed.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* static void* upcase_and_remove_empty_vals(const char *key, void *val,
|
|
|
|
* void* data) {
|
|
|
|
* char *cp = (char*)val;
|
|
|
|
* if (!*cp) { // val is an empty string.
|
|
|
|
* free(val);
|
|
|
|
* return NULL;
|
|
|
|
* } else {
|
|
|
|
* for (; *cp; cp++)
|
|
|
|
* *cp = toupper(*cp);
|
|
|
|
* }
|
|
|
|
* return val;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
|
|
|
|
*/
|
|
|
|
void strmap_foreach(strmap_t *map,
|
2004-04-06 05:44:36 +02:00
|
|
|
void* (*fn)(const char *key, void *val, void *data),
|
|
|
|
void *data)
|
2004-03-19 23:07:24 +01:00
|
|
|
{
|
|
|
|
strmap_entry_t *ptr, *next;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && fn);
|
2004-03-19 23:07:24 +01:00
|
|
|
for (ptr = SPLAY_MIN(strmap_tree, &map->head); ptr != NULL; ptr = next) {
|
|
|
|
/* This remove-in-place usage is specifically blessed in tree(3). */
|
|
|
|
next = SPLAY_NEXT(strmap_tree, &map->head, ptr);
|
|
|
|
ptr->val = fn(ptr->key, ptr->val, data);
|
|
|
|
if (!ptr->val) {
|
|
|
|
SPLAY_REMOVE(strmap_tree, &map->head, ptr);
|
|
|
|
tor_free(ptr->key);
|
|
|
|
tor_free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return an 'iterator' pointer to the front of a map.
|
|
|
|
*
|
|
|
|
* Iterator example:
|
|
|
|
*
|
|
|
|
* // uppercase values in "map", removing empty values.
|
|
|
|
*
|
2004-03-31 05:42:56 +02:00
|
|
|
* strmap_iter_t *iter;
|
2004-03-19 23:07:24 +01:00
|
|
|
* const char *key;
|
|
|
|
* void *val;
|
|
|
|
* char *cp;
|
|
|
|
*
|
|
|
|
* for (iter = strmap_iter_init(map); !strmap_iter_done(iter); ) {
|
|
|
|
* strmap_iter_get(iter, &key, &val);
|
|
|
|
* cp = (char*)val;
|
|
|
|
* if (!*cp) {
|
|
|
|
* iter = strmap_iter_next_rmv(iter);
|
|
|
|
* free(val);
|
|
|
|
* } else {
|
|
|
|
* for(;*cp;cp++) *cp = toupper(*cp);
|
|
|
|
* iter = strmap_iter_next(iter);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
strmap_iter_t *strmap_iter_init(strmap_t *map)
|
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map);
|
2004-03-19 23:07:24 +01:00
|
|
|
return SPLAY_MIN(strmap_tree, &map->head);
|
|
|
|
}
|
|
|
|
/* Advance the iterator 'iter' for map a single step to the next entry.
|
|
|
|
*/
|
|
|
|
strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
|
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && iter);
|
2004-03-19 23:07:24 +01:00
|
|
|
return SPLAY_NEXT(strmap_tree, &map->head, iter);
|
|
|
|
}
|
|
|
|
/* Advance the iterator 'iter' a single step to the next entry, removing
|
|
|
|
* the current entry.
|
|
|
|
*/
|
|
|
|
strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
|
|
|
|
{
|
|
|
|
strmap_iter_t *next;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(map && iter);
|
2004-03-19 23:07:24 +01:00
|
|
|
next = SPLAY_NEXT(strmap_tree, &map->head, iter);
|
|
|
|
SPLAY_REMOVE(strmap_tree, &map->head, iter);
|
|
|
|
tor_free(iter->key);
|
|
|
|
tor_free(iter);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
/* Set *keyp and *valp to the current entry pointed to by iter.
|
|
|
|
*/
|
|
|
|
void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
|
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(iter && keyp && valp);
|
2004-03-19 23:07:24 +01:00
|
|
|
*keyp = iter->key;
|
|
|
|
*valp = iter->val;
|
|
|
|
}
|
|
|
|
/* Return true iff iter has advanced past the last entry of map.
|
|
|
|
*/
|
|
|
|
int strmap_iter_done(strmap_iter_t *iter)
|
|
|
|
{
|
|
|
|
return iter == NULL;
|
|
|
|
}
|
|
|
|
/* Remove all entries from <map>, and deallocate storage for those entries.
|
|
|
|
* If free_val is provided, it is invoked on every value in <map>.
|
|
|
|
*/
|
|
|
|
void strmap_free(strmap_t *map, void (*free_val)(void*))
|
|
|
|
{
|
|
|
|
strmap_entry_t *ent, *next;
|
|
|
|
for (ent = SPLAY_MIN(strmap_tree, &map->head); ent != NULL; ent = next) {
|
|
|
|
next = SPLAY_NEXT(strmap_tree, &map->head, ent);
|
|
|
|
SPLAY_REMOVE(strmap_tree, &map->head, ent);
|
|
|
|
tor_free(ent->key);
|
|
|
|
if (free_val)
|
|
|
|
tor_free(ent->val);
|
|
|
|
}
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(SPLAY_EMPTY(&map->head));
|
2004-03-19 23:07:24 +01:00
|
|
|
tor_free(map);
|
|
|
|
}
|
|
|
|
|
2003-11-12 20:34:19 +01:00
|
|
|
/*
|
|
|
|
* String manipulation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* return the first char of s that is not whitespace and not a comment */
|
2003-12-09 00:45:37 +01:00
|
|
|
const char *eat_whitespace(const char *s) {
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(s);
|
2003-11-12 20:34:19 +01:00
|
|
|
|
2004-03-19 21:50:12 +01:00
|
|
|
while(isspace((int)*s) || *s == '#') {
|
|
|
|
while(isspace((int)*s))
|
2003-11-12 20:34:19 +01:00
|
|
|
s++;
|
|
|
|
if(*s == '#') { /* read to a \n or \0 */
|
|
|
|
while(*s && *s != '\n')
|
|
|
|
s++;
|
|
|
|
if(!*s)
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-12-09 00:45:37 +01:00
|
|
|
const char *eat_whitespace_no_nl(const char *s) {
|
2003-12-17 22:14:13 +01:00
|
|
|
while(*s == ' ' || *s == '\t')
|
2003-11-12 20:34:19 +01:00
|
|
|
++s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the first char of s that is whitespace or '#' or '\0 */
|
2003-12-09 00:45:37 +01:00
|
|
|
const char *find_whitespace(const char *s) {
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(s);
|
2003-11-12 20:34:19 +01:00
|
|
|
|
2004-03-19 21:50:12 +01:00
|
|
|
while(*s && !isspace((int)*s) && *s != '#')
|
2003-11-12 20:34:19 +01:00
|
|
|
s++;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
/*
|
|
|
|
* Time
|
|
|
|
*/
|
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
void tor_gettimeofday(struct timeval *timeval) {
|
2003-08-12 05:08:41 +02:00
|
|
|
#ifdef HAVE_GETTIMEOFDAY
|
2003-04-16 19:04:58 +02:00
|
|
|
if (gettimeofday(timeval, NULL)) {
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_ERR, "gettimeofday failed.");
|
2003-04-16 19:04:58 +02:00
|
|
|
/* If gettimeofday dies, we have either given a bad timezone (we didn't),
|
|
|
|
or segfaulted.*/
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-08-12 05:08:41 +02:00
|
|
|
#elif defined(HAVE_FTIME)
|
|
|
|
ftime(timeval);
|
|
|
|
#else
|
|
|
|
#error "No way to get time."
|
|
|
|
#endif
|
2003-04-16 19:04:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
tv_udiff(struct timeval *start, struct timeval *end)
|
|
|
|
{
|
2003-04-17 01:22:05 +02:00
|
|
|
long udiff;
|
2003-04-16 19:04:58 +02:00
|
|
|
long secdiff = end->tv_sec - start->tv_sec;
|
2003-04-17 03:59:41 +02:00
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
if (secdiff+1 > LONG_MAX/1000000) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log_fn(LOG_WARN, "comparing times too far apart.");
|
2003-04-16 19:04:58 +02:00
|
|
|
return LONG_MAX;
|
|
|
|
}
|
2003-04-17 04:03:55 +02:00
|
|
|
|
2003-10-07 18:30:05 +02:00
|
|
|
udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
|
2003-04-17 01:22:05 +02:00
|
|
|
if(udiff < 0) {
|
2003-10-07 18:30:05 +02:00
|
|
|
log_fn(LOG_INFO, "start (%ld.%ld) is after end (%ld.%ld). Returning 0.",
|
2003-10-15 20:48:48 +02:00
|
|
|
(long)start->tv_sec, (long)start->tv_usec, (long)end->tv_sec, (long)end->tv_usec);
|
2003-04-17 01:22:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return udiff;
|
2003-04-16 19:04:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int tv_cmp(struct timeval *a, struct timeval *b) {
|
|
|
|
if (a->tv_sec > b->tv_sec)
|
|
|
|
return 1;
|
|
|
|
if (a->tv_sec < b->tv_sec)
|
|
|
|
return -1;
|
|
|
|
if (a->tv_usec > b->tv_usec)
|
|
|
|
return 1;
|
|
|
|
if (a->tv_usec < b->tv_usec)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tv_add(struct timeval *a, struct timeval *b) {
|
|
|
|
a->tv_usec += b->tv_usec;
|
|
|
|
a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
|
|
|
|
a->tv_usec %= 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tv_addms(struct timeval *a, long ms) {
|
|
|
|
a->tv_usec += (ms * 1000) % 1000000;
|
|
|
|
a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
|
|
|
|
a->tv_usec %= 1000000;
|
|
|
|
}
|
2003-08-12 05:08:41 +02:00
|
|
|
|
2003-11-10 07:28:53 +01:00
|
|
|
|
|
|
|
#define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
|
|
|
|
static int n_leapdays(int y1, int y2) {
|
|
|
|
--y1;
|
|
|
|
--y2;
|
|
|
|
return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
|
|
|
|
}
|
2003-12-17 22:14:13 +01:00
|
|
|
static const int days_per_month[] =
|
2003-11-10 07:28:53 +01:00
|
|
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
|
2003-10-20 22:19:59 +02:00
|
|
|
time_t tor_timegm (struct tm *tm) {
|
2003-12-17 22:14:13 +01:00
|
|
|
/* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
|
2003-11-10 07:28:53 +01:00
|
|
|
* It's way more brute-force than fiddling with tzset().
|
|
|
|
*/
|
2003-10-20 22:19:59 +02:00
|
|
|
time_t ret;
|
2003-11-10 07:28:53 +01:00
|
|
|
unsigned long year, days, hours, minutes;
|
|
|
|
int i;
|
|
|
|
year = tm->tm_year + 1900;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(year >= 1970);
|
|
|
|
tor_assert(tm->tm_mon >= 0 && tm->tm_mon <= 11);
|
2003-11-10 07:28:53 +01:00
|
|
|
days = 365 * (year-1970) + n_leapdays(1970,year);
|
|
|
|
for (i = 0; i < tm->tm_mon; ++i)
|
|
|
|
days += days_per_month[i];
|
|
|
|
if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
|
|
|
|
++days;
|
|
|
|
days += tm->tm_mday - 1;
|
|
|
|
hours = days*24 + tm->tm_hour;
|
2003-12-17 22:14:13 +01:00
|
|
|
|
2003-11-10 07:28:53 +01:00
|
|
|
minutes = hours*60 + tm->tm_min;
|
|
|
|
ret = minutes*60 + tm->tm_sec;
|
2003-10-20 22:19:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
/*
|
|
|
|
* Low-level I/O.
|
|
|
|
*/
|
|
|
|
|
2003-08-21 01:05:22 +02:00
|
|
|
/* a wrapper for write(2) that makes sure to write all count bytes.
|
2003-09-28 08:47:29 +02:00
|
|
|
* Only use if fd is a blocking fd. */
|
2004-03-11 07:35:03 +01:00
|
|
|
int write_all(int fd, const char *buf, size_t count, int isSocket) {
|
2004-03-09 23:01:17 +01:00
|
|
|
size_t written = 0;
|
2003-08-21 01:05:22 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
while(written != count) {
|
2004-03-11 07:35:03 +01:00
|
|
|
if (isSocket)
|
|
|
|
result = send(fd, buf+written, count-written, 0);
|
|
|
|
else
|
|
|
|
result = write(fd, buf+written, count-written);
|
2003-08-21 01:05:22 +02:00
|
|
|
if(result<0)
|
|
|
|
return -1;
|
|
|
|
written += result;
|
2003-09-14 04:58:50 +02:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a wrapper for read(2) that makes sure to read all count bytes.
|
2003-09-28 08:47:29 +02:00
|
|
|
* Only use if fd is a blocking fd. */
|
2004-03-11 07:35:03 +01:00
|
|
|
int read_all(int fd, char *buf, size_t count, int isSocket) {
|
2004-03-09 23:01:17 +01:00
|
|
|
size_t numread = 0;
|
2003-09-14 04:58:50 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
while(numread != count) {
|
2004-04-07 21:57:40 +02:00
|
|
|
if (isSocket)
|
2004-03-11 07:35:03 +01:00
|
|
|
result = recv(fd, buf+numread, count-numread, 0);
|
|
|
|
else
|
|
|
|
result = read(fd, buf+numread, count-numread);
|
2003-09-14 04:58:50 +02:00
|
|
|
if(result<=0)
|
|
|
|
return -1;
|
|
|
|
numread += result;
|
2003-08-21 01:05:22 +02:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-08-12 05:08:41 +02:00
|
|
|
void set_socket_nonblocking(int socket)
|
|
|
|
{
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2003-12-13 02:42:44 +01:00
|
|
|
/* Yes means no and no means yes. Do you not want to be nonblocking? */
|
|
|
|
int nonblocking = 0;
|
|
|
|
ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
|
2003-08-12 05:08:41 +02:00
|
|
|
#else
|
2003-12-13 02:42:44 +01:00
|
|
|
fcntl(socket, F_SETFL, O_NONBLOCK);
|
2003-08-12 05:08:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
2003-08-12 09:01:20 +02:00
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
/*
|
|
|
|
* Process control
|
|
|
|
*/
|
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
/* Minimalist interface to run a void function in the background. On
|
|
|
|
* unix calls fork, on win32 calls beginthread. Returns -1 on failure.
|
|
|
|
* func should not return, but rather should call spawn_exit.
|
|
|
|
*/
|
2003-08-12 09:01:20 +02:00
|
|
|
int spawn_func(int (*func)(void *), void *data)
|
|
|
|
{
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2003-08-12 09:01:20 +02:00
|
|
|
int rv;
|
|
|
|
rv = _beginthread(func, 0, data);
|
|
|
|
if (rv == (unsigned long) -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
|
|
|
if (pid<0)
|
|
|
|
return -1;
|
|
|
|
if (pid==0) {
|
|
|
|
/* Child */
|
|
|
|
func(data);
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(0); /* Should never reach here. */
|
2003-08-14 19:51:36 +02:00
|
|
|
return 0; /* suppress "control-reaches-end-of-non-void" warning. */
|
2003-08-12 09:01:20 +02:00
|
|
|
} else {
|
|
|
|
/* Parent */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void spawn_exit()
|
|
|
|
{
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2003-08-12 09:01:20 +02:00
|
|
|
_endthread();
|
|
|
|
#else
|
|
|
|
exit(0);
|
|
|
|
#endif
|
|
|
|
}
|
2003-08-12 09:43:15 +02:00
|
|
|
|
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
/*
|
|
|
|
* Windows compatibility.
|
|
|
|
*/
|
2003-08-12 09:43:15 +02:00
|
|
|
int
|
|
|
|
tor_socketpair(int family, int type, int protocol, int fd[2])
|
|
|
|
{
|
2004-04-19 23:31:32 +02:00
|
|
|
#ifdef HAVE_SOCKETPAIR
|
2003-08-12 09:43:15 +02:00
|
|
|
return socketpair(family, type, protocol, fd);
|
|
|
|
#else
|
2004-04-19 23:31:32 +02:00
|
|
|
/* This socketpair does not work when localhost is down. So
|
|
|
|
* it's really not the same thing at all. But it's close enough
|
|
|
|
* for now, and really, when localhost is down sometimes, we
|
|
|
|
* have other problems too.
|
|
|
|
*/
|
2003-08-12 09:43:15 +02:00
|
|
|
int listener = -1;
|
|
|
|
int connector = -1;
|
|
|
|
int acceptor = -1;
|
|
|
|
struct sockaddr_in listen_addr;
|
|
|
|
struct sockaddr_in connect_addr;
|
2003-10-15 20:48:48 +02:00
|
|
|
int size;
|
2003-12-17 22:14:13 +01:00
|
|
|
|
2003-08-12 09:43:15 +02:00
|
|
|
if (protocol
|
|
|
|
#ifdef AF_UNIX
|
|
|
|
|| family != AF_UNIX
|
|
|
|
#endif
|
|
|
|
) {
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
errno = WSAEAFNOSUPPORT;
|
2003-08-12 10:18:13 +02:00
|
|
|
#else
|
2003-08-12 09:43:15 +02:00
|
|
|
errno = EAFNOSUPPORT;
|
2003-08-12 10:18:13 +02:00
|
|
|
#endif
|
2003-08-12 09:43:15 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!fd) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = socket(AF_INET, type, 0);
|
|
|
|
if (listener == -1)
|
|
|
|
return -1;
|
|
|
|
memset (&listen_addr, 0, sizeof (listen_addr));
|
|
|
|
listen_addr.sin_family = AF_INET;
|
|
|
|
listen_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
|
|
|
listen_addr.sin_port = 0; /* kernel choses port. */
|
|
|
|
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
|
|
|
|
== -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (listen(listener, 1) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
|
|
|
|
connector = socket(AF_INET, type, 0);
|
|
|
|
if (connector == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
/* We want to find out the port number to connect to. */
|
|
|
|
size = sizeof (connect_addr);
|
|
|
|
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof (connect_addr))
|
|
|
|
goto abort_tidy_up_and_fail;
|
|
|
|
if (connect(connector, (struct sockaddr *) &connect_addr,
|
|
|
|
sizeof (connect_addr)) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
|
|
|
|
size = sizeof (listen_addr);
|
|
|
|
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
|
|
|
|
if (acceptor == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof(listen_addr))
|
|
|
|
goto abort_tidy_up_and_fail;
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(listener);
|
2003-08-12 09:43:15 +02:00
|
|
|
/* Now check we are talking to ourself by matching port and host on the
|
|
|
|
two sockets. */
|
|
|
|
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof (connect_addr)
|
|
|
|
|| listen_addr.sin_family != connect_addr.sin_family
|
|
|
|
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|
|
|
|
|| listen_addr.sin_port != connect_addr.sin_port) {
|
|
|
|
goto abort_tidy_up_and_fail;
|
|
|
|
}
|
|
|
|
fd[0] = connector;
|
|
|
|
fd[1] = acceptor;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
abort_tidy_up_and_fail:
|
2003-08-14 19:13:52 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2003-08-12 10:18:13 +02:00
|
|
|
errno = WSAECONNABORTED;
|
|
|
|
#else
|
2003-08-12 09:43:15 +02:00
|
|
|
errno = ECONNABORTED; /* I hope this is portable and appropriate. */
|
2003-08-12 10:18:13 +02:00
|
|
|
#endif
|
2003-08-12 09:43:15 +02:00
|
|
|
tidy_up_and_fail:
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
if (listener != -1)
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(listener);
|
2003-08-12 09:43:15 +02:00
|
|
|
if (connector != -1)
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(connector);
|
2003-08-12 09:43:15 +02:00
|
|
|
if (acceptor != -1)
|
2004-04-28 23:14:56 +02:00
|
|
|
tor_close_socket(acceptor);
|
2003-08-12 09:43:15 +02:00
|
|
|
errno = save_errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2003-08-14 19:13:52 +02:00
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int correct_socket_errno(int s)
|
|
|
|
{
|
2003-08-14 19:51:36 +02:00
|
|
|
int optval, optvallen=sizeof(optval);
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(errno == WSAEWOULDBLOCK);
|
2003-08-14 19:13:52 +02:00
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
|
|
|
|
return errno;
|
|
|
|
if (optval)
|
|
|
|
return optval;
|
|
|
|
return WSAEWOULDBLOCK;
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-26 20:27:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Filesystem operations.
|
|
|
|
*/
|
2003-10-04 05:29:09 +02:00
|
|
|
|
|
|
|
/* Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
|
|
|
|
* exist, FN_FILE if it is a regular file, or FN_DIR if it's a
|
|
|
|
* directory. */
|
2003-09-26 20:27:35 +02:00
|
|
|
file_status_t file_status(const char *fname)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(fname, &st)) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
return FN_NOENT;
|
|
|
|
}
|
|
|
|
return FN_ERROR;
|
|
|
|
}
|
2003-12-17 22:14:13 +01:00
|
|
|
if (st.st_mode & S_IFDIR)
|
2003-09-26 20:27:35 +02:00
|
|
|
return FN_DIR;
|
|
|
|
else if (st.st_mode & S_IFREG)
|
|
|
|
return FN_FILE;
|
|
|
|
else
|
|
|
|
return FN_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
/* Check whether dirname exists and is private. If yes returns
|
|
|
|
0. Else returns -1. */
|
2003-09-26 20:27:35 +02:00
|
|
|
int check_private_dir(const char *dirname, int create)
|
|
|
|
{
|
2004-03-09 23:01:17 +01:00
|
|
|
int r;
|
2003-09-26 20:27:35 +02:00
|
|
|
struct stat st;
|
|
|
|
if (stat(dirname, &st)) {
|
|
|
|
if (errno != ENOENT) {
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_WARN, "Directory %s cannot be read: %s", dirname,
|
2003-09-26 20:27:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
2003-12-17 22:14:13 +01:00
|
|
|
}
|
2003-09-26 20:27:35 +02:00
|
|
|
if (!create) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log(LOG_WARN, "Directory %s does not exist.", dirname);
|
2003-09-26 20:27:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_INFO, "Creating directory %s", dirname);
|
2004-03-09 23:01:17 +01:00
|
|
|
#ifdef MS_WINDOWS
|
2004-03-12 14:02:16 +01:00
|
|
|
r = mkdir(dirname);
|
2004-03-09 23:01:17 +01:00
|
|
|
#else
|
2004-03-12 14:02:16 +01:00
|
|
|
r = mkdir(dirname, 0700);
|
2004-03-09 23:01:17 +01:00
|
|
|
#endif
|
|
|
|
if (r) {
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_WARN, "Error creating directory %s: %s", dirname,
|
2003-09-26 20:27:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(st.st_mode & S_IFDIR)) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log(LOG_WARN, "%s is not a directory", dirname);
|
2003-09-26 20:27:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-03-09 23:01:17 +01:00
|
|
|
#ifndef MS_WINDOWS
|
2003-09-26 20:27:35 +02:00
|
|
|
if (st.st_uid != getuid()) {
|
2003-12-15 22:35:52 +01:00
|
|
|
log(LOG_WARN, "%s is not owned by this UID (%d)", dirname, (int)getuid());
|
2003-09-26 20:27:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (st.st_mode & 0077) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log(LOG_WARN, "Fixing permissions on directory %s", dirname);
|
2003-09-26 20:27:35 +02:00
|
|
|
if (chmod(dirname, 0700)) {
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_WARN, "Could not chmod directory %s: %s", dirname,
|
2003-09-26 20:27:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-03-09 23:01:17 +01:00
|
|
|
#endif
|
2003-09-26 20:27:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_str_to_file(const char *fname, const char *str)
|
|
|
|
{
|
|
|
|
char tempname[1024];
|
|
|
|
int fd;
|
|
|
|
FILE *file;
|
2004-03-17 08:28:09 +01:00
|
|
|
if ((strlcpy(tempname,fname,1024) >= 1024) ||
|
|
|
|
(strlcat(tempname,".tmp",1024) >= 1024)) {
|
|
|
|
log(LOG_WARN, "Filename %s.tmp too long (>1024 chars)", fname);
|
2003-09-26 20:27:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_WARN, "Couldn't open %s for writing: %s", tempname,
|
2003-09-26 20:27:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(file = fdopen(fd, "w"))) {
|
2003-12-17 22:14:13 +01:00
|
|
|
log(LOG_WARN, "Couldn't fdopen %s for writing: %s", tempname,
|
2003-09-26 20:27:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
close(fd); return -1;
|
|
|
|
}
|
2003-09-26 23:26:25 +02:00
|
|
|
if (fputs(str,file) == EOF) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log(LOG_WARN, "Error writing to %s: %s", tempname, strerror(errno));
|
2003-09-26 20:27:35 +02:00
|
|
|
fclose(file); return -1;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
if (rename(tempname, fname)) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log(LOG_WARN, "Error replacing %s: %s", fname, strerror(errno));
|
2003-09-26 20:27:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-28 08:47:29 +02:00
|
|
|
|
|
|
|
char *read_file_to_str(const char *filename) {
|
|
|
|
int fd; /* router file */
|
|
|
|
struct stat statbuf;
|
|
|
|
char *string;
|
|
|
|
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(filename);
|
2003-09-28 08:47:29 +02:00
|
|
|
|
|
|
|
if(strcspn(filename,CONFIG_LEGAL_FILENAME_CHARACTERS) != 0) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log_fn(LOG_WARN,"Filename %s contains illegal characters.",filename);
|
2003-09-28 08:47:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2003-09-28 08:47:29 +02:00
|
|
|
if(stat(filename, &statbuf) < 0) {
|
2003-09-30 01:14:49 +02:00
|
|
|
log_fn(LOG_INFO,"Could not stat %s.",filename);
|
2003-09-28 08:47:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(filename,O_RDONLY,0);
|
|
|
|
if (fd<0) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log_fn(LOG_WARN,"Could not open %s.",filename);
|
2003-09-28 08:47:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
string = tor_malloc(statbuf.st_size+1);
|
|
|
|
|
2004-03-11 07:35:03 +01:00
|
|
|
if(read_all(fd,string,statbuf.st_size,0) != statbuf.st_size) {
|
2003-10-10 03:48:03 +02:00
|
|
|
log_fn(LOG_WARN,"Couldn't read all %ld bytes of file '%s'.",
|
2003-09-28 08:47:29 +02:00
|
|
|
(long)statbuf.st_size,filename);
|
|
|
|
free(string);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
2003-12-17 22:14:13 +01:00
|
|
|
|
2003-09-28 08:47:29 +02:00
|
|
|
string[statbuf.st_size] = 0; /* null terminate it */
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2003-09-29 09:50:08 +02:00
|
|
|
/* read lines from f (no more than maxlen-1 bytes each) until we
|
2004-03-02 05:59:52 +01:00
|
|
|
* get a non-whitespace line. If it isn't of the form "key value"
|
|
|
|
* (value can have spaces), return -1.
|
|
|
|
* Point *key to the first word in line, point *value * to the second.
|
2003-09-29 09:50:08 +02:00
|
|
|
* Put a \0 at the end of key, remove everything at the end of value
|
|
|
|
* that is whitespace or comment.
|
|
|
|
* Return 1 if success, 0 if no more lines, -1 if error.
|
|
|
|
*/
|
|
|
|
int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out) {
|
|
|
|
char *s, *key, *end, *value;
|
|
|
|
|
|
|
|
try_next_line:
|
|
|
|
if(!fgets(line, maxlen, f)) {
|
|
|
|
if(feof(f))
|
|
|
|
return 0;
|
|
|
|
return -1; /* real error */
|
|
|
|
}
|
|
|
|
|
|
|
|
if((s = strchr(line,'#'))) /* strip comments */
|
|
|
|
*s = 0; /* stop the line there */
|
|
|
|
|
|
|
|
/* remove end whitespace */
|
|
|
|
s = strchr(line, 0); /* now we're at the null */
|
|
|
|
do {
|
|
|
|
*s = 0;
|
|
|
|
s--;
|
2004-03-19 21:50:12 +01:00
|
|
|
} while (s >= line && isspace((int)*s));
|
2003-09-29 09:50:08 +02:00
|
|
|
|
|
|
|
key = line;
|
2004-03-19 21:50:12 +01:00
|
|
|
while(isspace((int)*key))
|
2003-09-29 09:50:08 +02:00
|
|
|
key++;
|
|
|
|
if(*key == 0)
|
|
|
|
goto try_next_line; /* this line has nothing on it */
|
|
|
|
end = key;
|
2004-03-19 21:50:12 +01:00
|
|
|
while(*end && !isspace((int)*end))
|
2003-09-29 09:50:08 +02:00
|
|
|
end++;
|
|
|
|
value = end;
|
2004-03-19 21:50:12 +01:00
|
|
|
while(*value && isspace((int)*value))
|
2003-09-29 09:50:08 +02:00
|
|
|
value++;
|
|
|
|
|
|
|
|
if(!*end || !*value) { /* only a key on this line. no value. */
|
2003-11-21 06:55:03 +01:00
|
|
|
*end = 0;
|
2004-03-02 05:59:52 +01:00
|
|
|
log_fn(LOG_WARN,"Line has keyword '%s' but no value. Failing.",key);
|
|
|
|
return -1;
|
2003-09-29 09:50:08 +02:00
|
|
|
}
|
|
|
|
*end = 0; /* null it out */
|
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"got keyword '%s', value '%s'", key, value);
|
|
|
|
*key_out = key, *value_out = value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-14 19:07:46 +01:00
|
|
|
int is_internal_IP(uint32_t ip) {
|
|
|
|
|
2004-03-15 05:00:11 +01:00
|
|
|
if (((ip & 0xff000000) == 0x0a000000) || /* 10/8 */
|
2004-03-14 19:07:46 +01:00
|
|
|
((ip & 0xff000000) == 0x00000000) || /* 0/8 */
|
|
|
|
((ip & 0xff000000) == 0x7f000000) || /* 127/8 */
|
|
|
|
((ip & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
|
|
|
|
((ip & 0xfff00000) == 0xac100000) || /* 172.16/12 */
|
|
|
|
((ip & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-02 00:31:13 +02:00
|
|
|
static char uname_result[256];
|
|
|
|
static int uname_result_is_set = 0;
|
|
|
|
|
|
|
|
const char *
|
|
|
|
get_uname(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
struct utsname u;
|
|
|
|
#endif
|
|
|
|
if (!uname_result_is_set) {
|
|
|
|
#ifdef HAVE_UNAME
|
2004-03-20 22:22:16 +01:00
|
|
|
if (uname(&u) != -1) {
|
|
|
|
/* (linux says 0 is success, solaris says 1 is success) */
|
2004-02-18 22:23:50 +01:00
|
|
|
snprintf(uname_result, 255, "%s %s %s",
|
|
|
|
u.sysname, u.nodename, u.machine);
|
2003-10-02 00:31:13 +02:00
|
|
|
uname_result[255] = '\0';
|
2003-12-17 22:14:13 +01:00
|
|
|
} else
|
2003-10-02 00:31:13 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
strcpy(uname_result, "Unknown platform");
|
|
|
|
}
|
|
|
|
uname_result_is_set = 1;
|
|
|
|
}
|
|
|
|
return uname_result;
|
|
|
|
}
|
2003-12-13 02:42:44 +01:00
|
|
|
|
2004-01-03 23:40:49 +01:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
/* Based on code contributed by christian grothoff */
|
|
|
|
static int start_daemon_called = 0;
|
|
|
|
static int finish_daemon_called = 0;
|
|
|
|
static int daemon_filedes[2];
|
2004-02-29 00:21:29 +01:00
|
|
|
void start_daemon(char *desired_cwd)
|
2004-01-03 23:40:49 +01:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (start_daemon_called)
|
|
|
|
return;
|
|
|
|
start_daemon_called = 1;
|
|
|
|
|
2004-02-29 00:21:29 +01:00
|
|
|
if(!desired_cwd)
|
|
|
|
desired_cwd = "/";
|
2004-01-03 23:40:49 +01:00
|
|
|
/* Don't hold the wrong FS mounted */
|
2004-02-29 00:21:29 +01:00
|
|
|
if (chdir(desired_cwd) < 0) {
|
|
|
|
log_fn(LOG_ERR,"chdir to %s failed. Exiting.",desired_cwd);
|
2003-10-23 16:28:44 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-22 13:21:29 +02:00
|
|
|
|
2004-01-03 23:40:49 +01:00
|
|
|
pipe(daemon_filedes);
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
2004-02-29 00:21:29 +01:00
|
|
|
log_fn(LOG_ERR,"fork failed. Exiting.");
|
2004-01-03 23:40:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid) { /* Parent */
|
|
|
|
int ok;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
close(daemon_filedes[1]); /* we only read */
|
|
|
|
ok = -1;
|
|
|
|
while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
|
|
|
|
if (c == '.')
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
if (ok == 1)
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
exit(1); /* child reported error */
|
|
|
|
} else { /* Child */
|
|
|
|
close(daemon_filedes[0]); /* we only write */
|
|
|
|
|
|
|
|
pid = setsid(); /* Detach from controlling terminal */
|
|
|
|
/*
|
2004-04-07 21:57:40 +02:00
|
|
|
* Fork one more time, so the parent (the session group leader) can exit.
|
2004-01-03 23:40:49 +01:00
|
|
|
* This means that we, as a non-session group leader, can never regain a
|
|
|
|
* controlling terminal. This part is recommended by Stevens's
|
|
|
|
* _Advanced Programming in the Unix Environment_.
|
2004-02-29 00:21:29 +01:00
|
|
|
*/
|
2004-01-03 23:40:49 +01:00
|
|
|
if (fork() != 0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2003-10-22 13:21:29 +02:00
|
|
|
|
2004-01-03 23:40:49 +01:00
|
|
|
void finish_daemon(void)
|
|
|
|
{
|
|
|
|
int nullfd;
|
|
|
|
char c = '.';
|
|
|
|
if (finish_daemon_called)
|
|
|
|
return;
|
|
|
|
if (!start_daemon_called)
|
2004-02-29 00:21:29 +01:00
|
|
|
start_daemon(NULL);
|
2004-01-03 23:40:49 +01:00
|
|
|
finish_daemon_called = 1;
|
|
|
|
|
|
|
|
nullfd = open("/dev/null",
|
2004-02-29 00:21:29 +01:00
|
|
|
O_CREAT | O_RDWR | O_APPEND);
|
2004-01-03 23:40:49 +01:00
|
|
|
if (nullfd < 0) {
|
2004-02-29 00:21:29 +01:00
|
|
|
log_fn(LOG_ERR,"/dev/null can't be opened. Exiting.");
|
2004-01-03 23:40:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* close fds linking to invoking terminal, but
|
|
|
|
* close usual incoming fds, but redirect them somewhere
|
|
|
|
* useful so the fds don't get reallocated elsewhere.
|
|
|
|
*/
|
|
|
|
if (dup2(nullfd,0) < 0 ||
|
|
|
|
dup2(nullfd,1) < 0 ||
|
|
|
|
dup2(nullfd,2) < 0) {
|
2004-02-29 00:21:29 +01:00
|
|
|
log_fn(LOG_ERR,"dup2 failed. Exiting.");
|
2004-01-03 23:40:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
write(daemon_filedes[1], &c, sizeof(char)); /* signal success */
|
|
|
|
close(daemon_filedes[1]);
|
2003-10-22 13:21:29 +02:00
|
|
|
}
|
2004-01-03 23:40:49 +01:00
|
|
|
#else
|
|
|
|
/* defined(MS_WINDOWS) */
|
2004-03-09 23:01:17 +01:00
|
|
|
void start_daemon(char *cp) {}
|
2004-01-03 23:40:49 +01:00
|
|
|
void finish_daemon(void) {}
|
|
|
|
#endif
|
2003-10-22 13:21:29 +02:00
|
|
|
|
|
|
|
void write_pidfile(char *filename) {
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
FILE *pidfile;
|
|
|
|
|
|
|
|
if ((pidfile = fopen(filename, "w")) == NULL) {
|
|
|
|
log_fn(LOG_WARN, "unable to open %s for writing: %s", filename,
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
2004-04-28 23:14:56 +02:00
|
|
|
fprintf(pidfile, "%d\n", (int)getpid());
|
2003-10-22 13:21:29 +02:00
|
|
|
fclose(pidfile);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int switch_id(char *user, char *group) {
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
struct group *gr = NULL;
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
pw = getpwnam(user);
|
|
|
|
if (pw == NULL) {
|
|
|
|
log_fn(LOG_ERR,"User '%s' not found.", user);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-13 07:45:59 +01:00
|
|
|
/* switch the group first, while we still have the privileges to do so */
|
2003-10-22 13:21:29 +02:00
|
|
|
if (group) {
|
|
|
|
gr = getgrnam(group);
|
|
|
|
if (gr == NULL) {
|
|
|
|
log_fn(LOG_ERR,"Group '%s' not found.", group);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-10-27 11:26:44 +01:00
|
|
|
if (setgid(gr->gr_gid) != 0) {
|
2003-10-22 13:21:29 +02:00
|
|
|
log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (user) {
|
2003-10-27 11:26:44 +01:00
|
|
|
if (setgid(pw->pw_gid) != 0) {
|
2003-10-22 13:21:29 +02:00
|
|
|
log_fn(LOG_ERR,"Error setting GID: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now that the group is switched, we can switch users and lose
|
2003-10-27 11:26:44 +01:00
|
|
|
privileges */
|
2003-10-22 13:21:29 +02:00
|
|
|
if (user) {
|
2003-10-27 11:26:44 +01:00
|
|
|
if (setuid(pw->pw_uid) != 0) {
|
2003-10-22 13:21:29 +02:00
|
|
|
log_fn(LOG_ERR,"Error setting UID: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2003-12-17 22:14:13 +01:00
|
|
|
log_fn(LOG_ERR,
|
2003-10-22 19:25:58 +02:00
|
|
|
"User or group specified, but switching users is not supported.");
|
2003-10-22 13:21:29 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2003-11-12 20:34:19 +01:00
|
|
|
|
2004-03-09 23:01:17 +01:00
|
|
|
int tor_inet_aton(const char *c, struct in_addr* addr)
|
|
|
|
{
|
2004-03-09 23:09:13 +01:00
|
|
|
#ifdef HAVE_INET_ATON
|
2004-03-09 23:01:17 +01:00
|
|
|
return inet_aton(c, addr);
|
|
|
|
#else
|
|
|
|
uint32_t r;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(c && addr);
|
2004-03-09 23:01:17 +01:00
|
|
|
if (strcmp(c, "255.255.255.255") == 0) {
|
2004-03-12 14:02:16 +01:00
|
|
|
addr->s_addr = 0xFFFFFFFFu;
|
|
|
|
return 1;
|
2004-03-09 23:01:17 +01:00
|
|
|
}
|
|
|
|
r = inet_addr(c);
|
|
|
|
if (r == INADDR_NONE)
|
2004-03-12 14:02:16 +01:00
|
|
|
return 0;
|
2004-03-09 23:01:17 +01:00
|
|
|
addr->s_addr = r;
|
|
|
|
return 1;
|
|
|
|
#endif
|
2004-03-09 23:09:13 +01:00
|
|
|
}
|
2004-04-06 05:44:36 +02:00
|
|
|
|
2004-04-28 21:35:12 +02:00
|
|
|
/* Similar behavior to Unix gethostbyname: resolve 'name', and set
|
|
|
|
* *addr to the proper IP address, in network byte order. Returns 0
|
|
|
|
* on success, -1 on failure; 1 on transient failure.
|
|
|
|
*
|
|
|
|
* (This function exists because standard windows gethostbyname
|
|
|
|
* doesn't treat raw IP addresses properly.)
|
|
|
|
*/
|
|
|
|
/* Perhaps eventually this should be replaced by a tor_getaddrinfo or
|
|
|
|
* something.
|
|
|
|
*/
|
|
|
|
int tor_lookup_hostname(const char *name, uint32_t *addr)
|
|
|
|
{
|
|
|
|
struct in_addr iaddr;
|
|
|
|
struct hostent *ent;
|
|
|
|
tor_assert(addr);
|
|
|
|
if (tor_inet_aton(name, &iaddr)) {
|
|
|
|
/* It's an IP. */
|
|
|
|
memcpy(addr, &iaddr.s_addr, 4);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
ent = gethostbyname(name);
|
|
|
|
if (ent) {
|
|
|
|
/* break to remind us if we move away from IPv4 */
|
|
|
|
tor_assert(ent->h_length == 4);
|
|
|
|
memcpy(addr, ent->h_addr, 4);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(addr, 0, 4);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
return (WSAGetLastError() == WSATRY_AGAIN) ? 1 : -1;
|
|
|
|
#else
|
|
|
|
return (h_errno == TRY_AGAIN) ? 1 : -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|