Reorganized sources slightly.

This commit is contained in:
Jan Vidar Krey
2009-07-25 20:05:27 +02:00
parent e281f61472
commit 36a07e3f7e
52 changed files with 72 additions and 69 deletions

425
src/util/ipcalc.c Normal file
View File

@@ -0,0 +1,425 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
int ip_is_valid_ipv4(const char* address)
{
int i = 0; /* address index */
int o = 0; /* octet number */
int n = 0; /* numbers after each dot */
int d = 0; /* dots */
if (!address || strlen(address) > 15 || strlen(address) < 7)
return 0;
for (; i < strlen(address); i++)
{
if (is_num(address[i]))
{
n++;
o *= 10;
o += (address[i] - '0');
}
else if (address[i] == '.')
{
if (n == 0 || n > 3 || o > 255) return 0;
n = 0;
o = 0;
d++;
}
else
{
return 0;
}
}
if (n == 0 || n > 3 || o > 255 || d != 3) return 0;
return 1;
}
int ip_is_valid_ipv6(const char* address)
{
unsigned char buf[16];
int ret = net_string_to_address(AF_INET6, address, buf);
if (ret <= 0) return 0;
return 1;
}
int ip_convert_to_binary(const char* taddr, struct ip_addr_encap* raw)
{
if (ip_is_valid_ipv6(taddr))
{
if (net_string_to_address(AF_INET6, taddr, &raw->internal_ip_data.in6) <= 0)
{
return -1;
}
raw->af = AF_INET6;
return AF_INET6;
}
else if (ip_is_valid_ipv4(taddr))
{
if (net_string_to_address(AF_INET, taddr, &raw->internal_ip_data.in) <= 0)
{
return -1;
}
raw->af = AF_INET;
return AF_INET;
}
return -1;
}
const char* ip_convert_to_string(struct ip_addr_encap* raw)
{
static char address[INET6_ADDRSTRLEN+1];
memset(address, 0, INET6_ADDRSTRLEN);
net_address_to_string(raw->af, (void*) &raw->internal_ip_data, address, INET6_ADDRSTRLEN+1);
if (strncmp(address, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
{
return &address[7];
}
return address;
}
int ip_convert_address(const char* text_address, int port, struct sockaddr* addr, socklen_t* addr_len)
{
struct sockaddr_in6 addr6;
struct sockaddr_in addr4;
size_t sockaddr_size;
const char* taddr = 0;
int ipv6sup = net_is_ipv6_supported();
if (strcmp(text_address, "any") == 0)
{
if (ipv6sup)
{
taddr = "::";
}
else
{
taddr = "0.0.0.0";
}
}
else if (strcmp(text_address, "loopback") == 0)
{
if (ipv6sup)
{
taddr = "::1";
}
else
{
taddr = "127.0.0.1";
}
}
else
{
taddr = text_address;
}
if (ip_is_valid_ipv6(taddr) && ipv6sup)
{
sockaddr_size = sizeof(struct sockaddr_in6);
memset(&addr6, 0, sockaddr_size);
addr6.sin6_family = AF_INET6;
addr6.sin6_port = htons(port);
if (net_string_to_address(AF_INET6, taddr, &addr6.sin6_addr) <= 0)
{
hub_log(log_fatal, "Unable to convert socket address (ipv6)");
return 0;
}
memcpy(addr, &addr6, sockaddr_size);
*addr_len = sockaddr_size;
}
else if (ip_is_valid_ipv4(taddr))
{
sockaddr_size = sizeof(struct sockaddr_in);
memset(&addr4, 0, sockaddr_size);
addr4.sin_family = AF_INET;
addr4.sin_port = htons(port);
if (net_string_to_address(AF_INET, taddr, &addr4.sin_addr) <= 0)
{
hub_log(log_fatal, "Unable to convert socket address (ipv4)");
return 0;
}
memcpy(addr, &addr4, sockaddr_size);
*addr_len = sockaddr_size;
}
else
{
addr = 0;
*addr_len = 0;
return -1;
}
return 0;
}
int ip_mask_create_left(int af, int bits, struct ip_addr_encap* result)
{
uint32_t mask;
int fill, remain_bits, n;
memset(result, 0, sizeof(struct ip_addr_encap));
result->af = af;
if (bits < 0) bits = 0;
if (af == AF_INET)
{
if (bits > 32) bits = 32;
mask = (0xffffffff << (32 - bits));
if (bits == 0) mask = 0;
result->internal_ip_data.in.s_addr = (((uint8_t*) &mask)[0] << 24) | (((uint8_t*) &mask)[1] << 16) | (((uint8_t*) &mask)[2] << 8) | (((uint8_t*) &mask)[3] << 0);
}
else if (af == AF_INET6)
{
if (bits > 128) bits = 128;
fill = (128-bits) / 8;
remain_bits = (128-bits) % 8;
mask = (0xff << (8 - remain_bits));
n = 0;
for (n = 0; n < fill; n++)
((uint8_t*) &result->internal_ip_data.in6)[n] = (uint8_t) 0xff;
if (fill < 16)
((uint8_t*) &result->internal_ip_data.in6)[fill] = (uint8_t) mask;
}
else
{
return -1;
}
#ifdef IP_CALC_DEBUG
char* r_str = hub_strdup(ip_convert_to_string(result));
hub_log(log_debug, "Created left mask: %s", r_str);
hub_free(r_str);
#endif
return 0;
}
int ip_mask_create_right(int af, int bits, struct ip_addr_encap* result)
{
uint32_t mask;
int fill, remain_bits, n, start;
uint8_t mask8;
memset(result, 0, sizeof(struct ip_addr_encap));
result->af = af;
if (bits < 0) bits = 0;
if (af == AF_INET)
{
if (bits > 32) bits = 32;
mask = (0xffffffff >> (32-bits));
if (bits == 0) mask = 0;
result->internal_ip_data.in.s_addr = (((uint8_t*) &mask)[0] << 24) | (((uint8_t*) &mask)[1] << 16) | (((uint8_t*) &mask)[2] << 8) | (((uint8_t*) &mask)[3] << 0);
}
else if (af == AF_INET6)
{
if (bits > 128) bits = 128;
fill = (128-bits) / 8;
remain_bits = (128-bits) % 8;
mask8 = (0xff >> (8 - remain_bits));
n = 0;
start = 16-fill;
for (n = 0; n < start; n++)
((uint8_t*) &result->internal_ip_data.in6)[n] = (uint8_t) 0x00;
for (n = start; n < 16; n++)
((uint8_t*) &result->internal_ip_data.in6)[n] = (uint8_t) 0xff;
if (start > 0)
((uint8_t*) &result->internal_ip_data.in6)[start-1] = (uint8_t) mask8;
}
else
{
return -1;
}
#ifdef IP_CALC_DEBUG
char* r_str = hub_strdup(ip_convert_to_string(result));
hub_log(log_debug, "Created right mask: %s", r_str);
hub_free(r_str);
#endif
return 0;
}
void ip_mask_apply_AND(struct ip_addr_encap* addr, struct ip_addr_encap* mask, struct ip_addr_encap* result)
{
memset(result, 0, sizeof(struct ip_addr_encap));
result->af = addr->af;
if (addr->af == AF_INET)
{
result->internal_ip_data.in.s_addr = addr->internal_ip_data.in.s_addr & mask->internal_ip_data.in.s_addr;
}
else if (addr->af == AF_INET6)
{
uint32_t A, B, C, D;
int n = 0;
int offset = 0;
for (n = 0; n < 4; n++)
{
offset = n * 4;
A = (((uint8_t*) &addr->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+3] << 0);
B = (((uint8_t*) &mask->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+3] << 0);
C = A & B;
D = (((uint8_t*) &C)[0] << 24) |
(((uint8_t*) &C)[1] << 16) |
(((uint8_t*) &C)[2] << 8) |
(((uint8_t*) &C)[3] << 0);
((uint32_t*) &result->internal_ip_data.in6)[n] = D;
}
}
}
void ip_mask_apply_OR(struct ip_addr_encap* addr, struct ip_addr_encap* mask, struct ip_addr_encap* result)
{
memset(result, 0, sizeof(struct ip_addr_encap));
result->af = addr->af;
if (addr->af == AF_INET)
{
result->internal_ip_data.in.s_addr = addr->internal_ip_data.in.s_addr | mask->internal_ip_data.in.s_addr;
}
else if (addr->af == AF_INET6)
{
uint32_t A, B, C, D;
int n = 0;
int offset = 0;
for (n = 0; n < 4; n++)
{
offset = n * 4;
A = (((uint8_t*) &addr->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &addr->internal_ip_data.in6)[offset+3] << 0);
B = (((uint8_t*) &mask->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &mask->internal_ip_data.in6)[offset+3] << 0);
C = A | B;
D = (((uint8_t*) &C)[0] << 24) |
(((uint8_t*) &C)[1] << 16) |
(((uint8_t*) &C)[2] << 8) |
(((uint8_t*) &C)[3] << 0);
((uint32_t*) &result->internal_ip_data.in6)[n] = D;
}
}
}
int ip_compare(struct ip_addr_encap* a, struct ip_addr_encap* b)
{
int ret = 0;
uint32_t A, B;
if (a->af == AF_INET)
{
A = (((uint8_t*) &a->internal_ip_data.in.s_addr)[0] << 24) |
(((uint8_t*) &a->internal_ip_data.in.s_addr)[1] << 16) |
(((uint8_t*) &a->internal_ip_data.in.s_addr)[2] << 8) |
(((uint8_t*) &a->internal_ip_data.in.s_addr)[3] << 0);
B = (((uint8_t*) &b->internal_ip_data.in.s_addr)[0] << 24) |
(((uint8_t*) &b->internal_ip_data.in.s_addr)[1] << 16) |
(((uint8_t*) &b->internal_ip_data.in.s_addr)[2] << 8) |
(((uint8_t*) &b->internal_ip_data.in.s_addr)[3] << 0);
ret = A - B;
}
else if (a->af == AF_INET6)
{
int n = 0;
int offset = 0;
for (n = 0; n < 4; n++)
{
offset = n * 4;
A = (((uint8_t*) &a->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &a->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &a->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &a->internal_ip_data.in6)[offset+3] << 0);
B = (((uint8_t*) &b->internal_ip_data.in6)[offset+0] << 24) |
(((uint8_t*) &b->internal_ip_data.in6)[offset+1] << 16) |
(((uint8_t*) &b->internal_ip_data.in6)[offset+2] << 8) |
(((uint8_t*) &b->internal_ip_data.in6)[offset+3] << 0);
if (A == B) continue;
return A - B;
}
return 0;
}
#ifdef IP_CALC_DEBUG
char* a_str = hub_strdup(ip_convert_to_string(a));
char* b_str = hub_strdup(ip_convert_to_string(b));
hub_log(log_debug, "Comparing IPs '%s' AND '%s' => %d", a_str, b_str, ret);
hub_free(a_str);
hub_free(b_str);
#endif
return ret;
}

84
src/util/ipcalc.h Normal file
View File

@@ -0,0 +1,84 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is used for fiddling with IP-addresses,
* primarily used for IP-banning in uhub.
*/
#ifndef HAVE_UHUB_IPCALC_H
#define HAVE_UHUB_IPCALC_H
struct ip_addr_encap {
int af;
union {
struct in_addr in;
struct in6_addr in6;
} internal_ip_data;
};
extern int ip_convert_to_binary(const char* text_addr, struct ip_addr_encap* raw);
extern const char* ip_convert_to_string(struct ip_addr_encap* raw);
/*
* @return 1 if address is a valid IPv4 address in text notation
* 0 if invalid
*/
extern int ip_is_valid_ipv4(const char* address);
/*
* @return 1 if address is a valid IPv6 address in text notation
* 0 if invalid
*/
extern int ip_is_valid_ipv6(const char* address);
/*
* This function converts an IP address in text_address to a binary
* struct sockaddr.
* This will auto-detect if the IP-address is IPv6 (and that is supported),
* or if IPv4 should be used.
* NOTE: Use sockaddr_storage to allocate enough memory for IPv6.
*
* @param text_addr is an ipaddress either ipv6 or ipv4.
* Special magic addresses called "any" and "loopback" exist,
* and will work accross IPv6/IPv4.
* @param port Fill the struct sockaddr* with the given port, can safely be ignored.
*/
extern int ip_convert_address(const char* text_address, int port, struct sockaddr* addr, socklen_t* addr_len);
extern int ip_mask_create_left(int af, int bits, struct ip_addr_encap* result);
extern int ip_mask_create_right(int af, int bits, struct ip_addr_encap* result);
extern void ip_mask_apply_AND(struct ip_addr_encap* address, struct ip_addr_encap* mask, struct ip_addr_encap* result);
extern void ip_mask_apply_OR(struct ip_addr_encap* address, struct ip_addr_encap* mask, struct ip_addr_encap* result);
/**
* @return <0 if a is less than b
* @return >0 if a is greater than b
* @return 0 if they are equal
*/
extern int ip_compare(struct ip_addr_encap* a, struct ip_addr_encap* b);
#endif /* HAVE_UHUB_IPCALC_H */

192
src/util/list.c Normal file
View File

@@ -0,0 +1,192 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
struct linked_list* list_create()
{
struct linked_list* list = NULL;
list = (struct linked_list*) hub_malloc_zero(sizeof(struct linked_list));
if (list == NULL)
return NULL;
return list;
}
void list_destroy(struct linked_list* list)
{
if (list)
hub_free(list);
}
void list_clear(struct linked_list* list, void (*free_handle)(void* ptr))
{
struct node* node = list->first;
struct node* tmp = NULL;
while (node)
{
tmp = node->next;
free_handle(node->ptr);
hub_free(node);
node = tmp;
}
memset(list, 0, sizeof(struct linked_list));
}
void list_append(struct linked_list* list, void* data_ptr)
{
struct node* new_node = (struct node*) hub_malloc_zero(sizeof(struct node));
new_node->ptr = data_ptr;
if (list->last)
{
list->last->next = new_node;
new_node->prev = list->last;
}
else
{
list->first = new_node;
}
list->last = new_node;
list->size++;
}
void list_remove(struct linked_list* list, void* data_ptr)
{
struct node* node = list->first;
assert(data_ptr);
list->iterator = NULL;
while (node)
{
if (node->ptr == data_ptr)
{
if (node->next)
node->next->prev = node->prev;
if (node->prev)
node->prev->next = node->next;
if (node == list->last)
list->last = node->prev;
if (node == list->first)
list->first = node->next;
hub_free(node);
list->size--;
break;
}
node = node->next;
}
}
size_t list_size(struct linked_list* list)
{
return list->size;
}
void* list_get_index(struct linked_list* list, size_t offset)
{
struct node* node = list->first;
size_t n = 0;
for (n = 0; n < list->size; n++)
{
if (n == offset)
{
return node->ptr;
}
node = node->next;
}
return NULL;
}
void* list_get_first(struct linked_list* list)
{
list->iterator = list->first;
if (list->iterator == NULL)
return NULL;
return list->iterator->ptr;
}
struct node* list_get_first_node(struct linked_list* list)
{
list->iterator = list->first;
if (list->iterator == NULL)
return NULL;
return list->iterator;
}
void* list_get_last(struct linked_list* list)
{
list->iterator = list->last;
if (list->iterator == NULL)
return NULL;
return list->iterator->ptr;
}
struct node* list_get_last_node(struct linked_list* list)
{
list->iterator = list->last;
if (list->iterator == NULL)
return NULL;
return list->iterator;
}
void* list_get_next(struct linked_list* list)
{
if (list->iterator == NULL)
list->iterator = list->first;
else
list->iterator = list->iterator->next;
if (list->iterator == NULL)
return NULL;
return list->iterator->ptr;
}
void* list_get_prev(struct linked_list* list)
{
if (list->iterator == NULL)
return NULL;
list->iterator = list->iterator->prev;
if (list->iterator == NULL)
return NULL;
return list->iterator->ptr;
}

61
src/util/list.h Normal file
View File

@@ -0,0 +1,61 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_LINKED_LIST_H
#define HAVE_UHUB_LINKED_LIST_H
struct linked_list
{
size_t size;
struct node* first;
struct node* last;
struct node* iterator;
};
struct node
{
void* ptr;
struct node* next;
struct node* prev;
};
extern struct linked_list* list_create();
extern void list_destroy(struct linked_list*);
extern void list_clear(struct linked_list*, void (*free_handle)(void* ptr) );
extern void list_append(struct linked_list* list, void* data_ptr);
/**
* Remove data_ptr from the list. If multiple versions occur, only the first one is removed.
*/
extern void list_remove(struct linked_list* list, void* data_ptr);
extern size_t list_size(struct linked_list* list);
extern void* list_get_index(struct linked_list*, size_t offset);
extern void* list_get_first(struct linked_list*);
extern void* list_get_last(struct linked_list*);
extern void* list_get_next(struct linked_list*);
extern void* list_get_prev(struct linked_list*);
extern struct node* list_get_first_node(struct linked_list*);
extern struct node* list_get_last_node(struct linked_list*);
#endif /* HAVE_UHUB_LINKED_LIST_H */

231
src/util/log.c Normal file
View File

@@ -0,0 +1,231 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
#include <locale.h>
#ifndef WIN32
#include <syslog.h>
static int use_syslog = 0;
#endif
static int verbosity = 4;
static FILE* logfile = NULL;
#ifdef MEMORY_DEBUG
static FILE* memfile = NULL;
#define MEMORY_DEBUG_FILE "memlog.txt"
#endif
#ifdef NETWORK_DUMP_DEBUG
#define NETWORK_DUMP_FILE "netdump.log"
static FILE* netdump = NULL;
#endif
static const char* prefixes[] =
{
"FATAL",
"ERROR",
"WARN",
"USER",
"INFO",
"DEBUG",
"TRACE",
"DUMP",
"MEM",
"PROTO",
0
};
void hub_log_initialize(const char* file, int syslog)
{
setlocale(LC_ALL, "C");
#ifdef MEMORY_DEBUG
memfile = fopen(MEMORY_DEBUG_FILE, "w");
if (!memfile)
{
fprintf(stderr, "Unable to create " MEMORY_DEBUG_FILE " for logging memory allocations\n");
return;
}
#endif
#ifdef NETWORK_DUMP_DEBUG
netdump = fopen(NETWORK_DUMP_FILE, "w");
if (!netdump)
{
fprintf(stderr, "Unable to create " NETWORK_DUMP_FILE " for logging network traffic\n");
return;
}
#endif
#ifndef WIN32
if (syslog)
{
use_syslog = 1;
openlog("uhub", LOG_PID, LOG_USER);
}
#endif
if (!file)
{
logfile = stderr;
return;
}
logfile = fopen(file, "a");
if (!logfile)
{
logfile = stderr;
return;
}
}
void hub_log_shutdown()
{
if (logfile && logfile != stderr)
{
fclose(logfile);
logfile = NULL;
}
#ifdef MEMORY_DEBUG
if (memfile)
{
fclose(memfile);
memfile = NULL;
}
#endif
#ifdef NETWORK_DUMP_DEBUG
if (netdump)
{
fclose(netdump);
netdump = NULL;
}
#endif
#ifndef WIN32
if (use_syslog)
{
use_syslog = 0;
closelog();
}
#endif
}
void hub_set_log_verbosity(int verb)
{
verbosity = verb;
}
void hub_log(int log_verbosity, const char *format, ...)
{
static char logmsg[1024];
static char timestamp[32];
struct tm *tmp;
time_t t;
va_list args;
#ifdef MEMORY_DEBUG
if (memfile && log_verbosity == log_memory)
{
va_start(args, format);
vsnprintf(logmsg, 1024, format, args);
va_end(args);
fprintf(memfile, "%s\n", logmsg);
fflush(memfile);
return;
}
#endif
#ifdef NETWORK_DUMP_DEBUG
if (netdump && log_verbosity == log_protocol)
{
va_start(args, format);
vsnprintf(logmsg, 1024, format, args);
va_end(args);
fprintf(netdump, "%s\n", logmsg);
fflush(netdump);
return;
}
#endif
if (log_verbosity < verbosity)
{
t = time(NULL);
tmp = localtime(&t);
strftime(timestamp, 32, "%Y-%m-%d %H:%M:%S", tmp);
va_start(args, format);
vsnprintf(logmsg, 1024, format, args);
va_end(args);
if (logfile)
{
fprintf(logfile, "%s %5s: %s\n", timestamp, prefixes[log_verbosity], logmsg);
fflush(logfile);
}
else
{
fprintf(stderr, "%s %5s: %s\n", timestamp, prefixes[log_verbosity], logmsg);
}
}
#ifndef WIN32
if (use_syslog)
{
int level = 0;
if (verbosity < log_info)
return;
va_start(args, format);
vsnprintf(logmsg, 1024, format, args);
va_end(args);
switch (log_verbosity)
{
case log_fatal: level = LOG_CRIT; break;
case log_error: level = LOG_ERR; break;
case log_warning: level = LOG_WARNING; break;
case log_user: level = LOG_INFO | LOG_AUTH; break;
case log_info: level = LOG_INFO; break;
case log_debug: level = LOG_DEBUG; break;
default:
level = 0;
break;
}
if (level == 0)
return;
level |= (LOG_USER | LOG_DAEMON);
syslog(level, "%s", logmsg);
}
#endif
}

59
src/util/log.h Normal file
View File

@@ -0,0 +1,59 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_LOG_H
#define HAVE_UHUB_LOG_H
enum log_verbosity {
log_fatal = 0,
log_error = 1,
log_warning = 2,
log_user = 3,
log_info = 4,
log_debug = 5,
log_trace = 6,
log_dump = 7,
log_memory = 8,
log_protocol = 9,
};
/**
* Specify a minimum log verbosity for what messages should
* be printed in the log.
*/
extern void hub_set_log_verbosity(int log_verbosity);
/**
* Print a message in the log.
*/
extern void hub_log(int log_verbosity, const char *format, ...);
/**
* Initialize the log subsystem, if no output file is given (file is null)
* stderr is assumed by default.
* If the file cannot be opened for writing, stdout is also used.
*/
extern void hub_log_initialize(const char* file, int syslog);
/**
* Shut down and close the logfile.
*/
extern void hub_log_shutdown();
#endif /* HAVE_UHUB_LOG_H */

248
src/util/memory.c Normal file
View File

@@ -0,0 +1,248 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
#ifdef MEMORY_DEBUG
#define REALTIME_MALLOC_TRACKING
#ifdef REALTIME_MALLOC_TRACKING
#define UHUB_MAX_ALLOCS 50000
struct malloc_info
{
void* ptr;
size_t size;
void* stack1;
void* stack2;
};
static int hub_alloc_count = 0;
static size_t hub_alloc_size = 0;
static int hub_alloc_peak_count = 0;
static size_t hub_alloc_peak_size = 0;
static size_t hub_alloc_oom = 0;
static struct malloc_info hub_allocs[UHUB_MAX_ALLOCS] = { { 0, }, };
static int malloc_slot = -1; /* free slot (-1, no slot) */
void internal_debug_print_leaks()
{
size_t n = 0;
size_t leak = 0;
size_t count = 0;
hub_log(log_memory, "--- exit (allocs: %d, size: %zu) ---", hub_alloc_count, hub_alloc_size);
for (; n < UHUB_MAX_ALLOCS; n++)
{
if (hub_allocs[n].ptr)
{
leak += hub_allocs[n].size;
count++;
hub_log(log_memory, "leak %p size: %zu (bt: %p %p)", hub_allocs[n].ptr, hub_allocs[n].size, hub_allocs[n].stack1, hub_allocs[n].stack2);
}
}
hub_log(log_memory, "--- done (allocs: %d, size: %zu, peak: %d/%zu, oom: %zu) ---", count, leak, hub_alloc_peak_count, hub_alloc_peak_size, hub_alloc_oom);
}
#endif /* REALTIME_MALLOC_TRACKING */
void* internal_debug_mem_malloc(size_t size, const char* where)
{
size_t n = 0;
char* ptr = malloc(size);
#ifdef REALTIME_MALLOC_TRACKING
/* Make sure the malloc info struct is initialized */
if (!hub_alloc_count)
{
hub_log(log_memory, "--- start ---");
for (n = 0; n < UHUB_MAX_ALLOCS; n++)
{
hub_allocs[n].ptr = 0;
hub_allocs[n].size = 0;
hub_allocs[n].stack1 = 0;
hub_allocs[n].stack2 = 0;
}
atexit(&internal_debug_print_leaks);
}
if (ptr)
{
if (malloc_slot != -1)
n = (size_t) malloc_slot;
else
n = 0;
for (; n < UHUB_MAX_ALLOCS; n++)
{
if (!hub_allocs[n].ptr)
{
hub_allocs[n].ptr = ptr;
hub_allocs[n].size = size;
hub_allocs[n].stack1 = __builtin_return_address(1);
hub_allocs[n].stack2 = __builtin_return_address(2);
hub_alloc_size += size;
hub_alloc_count++;
hub_alloc_peak_count = MAX(hub_alloc_count, hub_alloc_peak_count);
hub_alloc_peak_size = MAX(hub_alloc_size, hub_alloc_peak_size);
hub_log(log_memory, "%s %p (%d bytes) (bt: %p %p) {allocs: %d, size: %zu}", where, ptr, (int) size, hub_allocs[n].stack1, hub_allocs[n].stack2, hub_alloc_count, hub_alloc_size);
break;
}
}
}
else
{
hub_log(log_memory, "%s *** OOM for %d bytes", where, size);
hub_alloc_oom++;
return 0;
}
#endif /* REALTIME_MALLOC_TRACKING */
return ptr;
}
void internal_debug_mem_free(void* ptr)
{
#ifdef REALTIME_MALLOC_TRACKING
size_t n = 0;
void* stack1 = __builtin_return_address(1);
void* stack2 = __builtin_return_address(2);
if (!ptr) return;
for (; n < UHUB_MAX_ALLOCS; n++)
{
if (hub_allocs[n].ptr == ptr)
{
hub_alloc_size -= hub_allocs[n].size;
hub_alloc_count--;
hub_allocs[n].ptr = 0;
hub_allocs[n].size = 0;
hub_allocs[n].stack1 = 0;
hub_allocs[n].stack2 = 0;
hub_log(log_memory, "free %p (bt: %p %p) {allocs: %d, size: %zu}", ptr, stack1, stack2, hub_alloc_count, hub_alloc_size);
malloc_slot = n;
free(ptr);
return;
}
}
malloc_slot = -1;
abort();
hub_log(log_memory, "free %p *** NOT ALLOCATED *** (bt: %p %p)", ptr, stack1, stack2);
#else
free(ptr);
#endif /* REALTIME_MALLOC_TRACKING */
}
char* debug_mem_strdup(const char* s)
{
size_t size = strlen(s);
char* ptr = internal_debug_mem_malloc(size+1, "strdup");
if (ptr)
{
memcpy(ptr, s, size);
ptr[size] = 0;
}
return ptr;
}
char* debug_mem_strndup(const char* s, size_t n)
{
size_t size = MIN(strlen(s), n);
char* ptr = internal_debug_mem_malloc(size+1, "strndup");
if (ptr)
{
memcpy(ptr, s, size);
ptr[size] = 0;
}
return ptr;
}
void* debug_mem_malloc(size_t size)
{
void* ptr = internal_debug_mem_malloc(size, "malloc");
return ptr;
}
void debug_mem_free(void *ptr)
{
internal_debug_mem_free(ptr);
}
#endif
void* hub_malloc_zero(size_t size)
{
void* data = hub_malloc(size);
if (data)
{
memset(data, 0, size);
}
return data;
}
#ifdef DEBUG_FUNCTION_TRACE
#define FTRACE_LOG "ftrace.log"
static FILE* functrace = 0;
void main_constructor() __attribute__ ((no_instrument_function, constructor));
void main_deconstructor() __attribute__ ((no_instrument_function, destructor));
void __cyg_profile_func_enter(void* frame, void* callsite) __attribute__ ((no_instrument_function));
void __cyg_profile_func_exit(void* frame, void* callsite) __attribute__ ((no_instrument_function));
void main_constructor()
{
functrace = fopen(FTRACE_LOG, "w");
if (functrace == NULL)
{
fprintf(stderr, "Cannot create function trace file: " FTRACE_LOG "\n");
exit(-1);
}
}
void main_deconstructor()
{
fclose(functrace);
}
void __cyg_profile_func_enter(void* frame, void* callsite)
{
if (functrace)
fprintf(functrace, "E%p\n", frame);
}
void __cyg_profile_func_exit(void* frame, void* callsite)
{
if (functrace)
fprintf(functrace, "X%p\n", frame);
}
#endif /* DEBUG_FUNCTION_TRACE */

45
src/util/memory.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_MEMORY_HANDLER_H
#define HAVE_UHUB_MEMORY_HANDLER_H
#ifdef MEMORY_DEBUG
#define hub_malloc debug_mem_malloc
#define hub_free debug_mem_free
#define hub_strdup debug_mem_strdup
#define hub_strndup debug_mem_strndup
extern void* debug_mem_malloc(size_t size);
extern void debug_mem_free(void* ptr);
extern char* debug_mem_strdup(const char* s);
extern char* debug_mem_strndup(const char* s, size_t n);
#else
#define hub_malloc malloc
#define hub_free free
#define hub_strdup strdup
#define hub_strndup strndup
#endif
extern void* hub_malloc_zero(size_t size);
#endif /* HAVE_UHUB_MEMORY_HANDLER_H */

363
src/util/misc.c Normal file
View File

@@ -0,0 +1,363 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
int is_space(char c)
{
if (c == ' ') return 1;
return 0;
}
int is_white_space(char c)
{
if (c == ' ' || c == '\t' || c == '\r') return 1;
return 0;
}
static int is_printable(unsigned char c)
{
if (c >= 32)
return 1;
if (c == '\t' || c == '\r' || c == '\n')
return 1;
return 0;
}
char* strip_white_space(char* string)
{
char* pos;
while (string[0] && is_white_space(string[0])) string++;
if (!*string)
return 0;
/* Strip appending whitespace */
pos = &string[strlen(string)-1];
while (&string[0] < &pos[0] && is_white_space(pos[0])) { pos[0] = 0; pos--; }
return string;
}
static int is_valid_utf8_str(const char* string, size_t length)
{
int expect = 0;
char div = 0;
size_t pos = 0;
if (length == 0) return 1;
for (pos = 0; pos < length; pos++)
{
if (expect)
{
if ((string[pos] & 0xC0) == 0x80) expect--;
else return 0;
}
else
{
if (string[pos] & 0x80)
{
for (div = 0x40; div > 0x10; div /= 2)
{
if (string[pos] & div) expect++;
else break;
}
if ((string[pos] & div) || (pos+expect >= length)) return 0;
}
}
}
return 1;
}
int is_valid_utf8(const char* string)
{
return is_valid_utf8_str(string, strlen(string));
}
int is_printable_utf8(const char* string, size_t length)
{
size_t pos = 0;
for (pos = 0; pos < length; pos++)
{
if (!is_printable(string[pos]))
return 0;
}
return is_valid_utf8_str(string, length);
}
int is_valid_base32_char(char c)
{
if ((c >= 'A' && c <= 'Z') || (c >= '2' && c <= '7')) return 1;
return 0;
}
int is_num(char c)
{
if (c >= '0' && c <= '9') return 1;
return 0;
}
void base32_encode(const unsigned char* buffer, size_t len, char* result) {
unsigned char word = 0;
size_t n = 0;
size_t i = 0;
size_t index = 0;
for (; i < len;) {
if (index > 3) {
word = (buffer[i] & (0xFF >> index));
index = (index + 5) % 8;
word <<= index;
if (i < len - 1)
word |= buffer[i + 1] >> (8 - index);
i++;
} else {
word = (buffer[i] >> (8 - (index + 5))) & 0x1F;
index = (index + 5) % 8;
if (index == 0) i++;
}
result[n++] = BASE32_ALPHABET[word];
}
result[n] = '\0';
}
void base32_decode(const char* src, unsigned char* dst, size_t len) {
size_t index = 0;
size_t offset = 0;
size_t i = 0;
memset(dst, 0, len);
for (i = 0; src[i]; i++) {
unsigned char n = 0;
for (; n < 32; n++) if (src[i] == BASE32_ALPHABET[n]) break;
if (n == 32) continue;
if (index <= 3) {
index = (index + 5) % 8;
if (index == 0) {
dst[offset++] |= n;
if (offset == len) break;
} else {
dst[offset] |= n << (8 - index);
}
} else {
index = (index + 5) % 8;
dst[offset++] |= (n >> index);
if (offset == len) break;
dst[offset] |= n << (8 - index);
}
}
}
int file_read_lines(const char* file, void* data, file_line_handler_t handler)
{
int fd;
ssize_t ret;
char buf[MAX_RECV_BUF];
char *pos, *start;
size_t line_count = 0;
memset(buf, 0, MAX_RECV_BUF);
hub_log(log_trace, "Opening file %s for line reading.", file);
fd = open(file, 0);
if (fd == -1)
{
hub_log(log_error, "Unable to open file %s: %s", file, strerror(errno));
return -2;
}
ret = read(fd, buf, MAX_RECV_BUF);
if (ret < 0)
{
hub_log(log_error, "Unable to read from file %s: %s", file, strerror(errno));
close(fd);
return -1;
}
else if (ret == 0)
{
close(fd);
hub_log(log_warning, "File is empty.");
return 0;
}
else
{
close(fd);
/* Parse configuaration */
start = buf;
while ((pos = strchr(start, '\n')))
{
pos[0] = '\0';
if (*start)
{
hub_log(log_dump, "Line: %s", start);
if (handler(start, line_count+1, data) < 0)
return -1;
}
start = &pos[1];
line_count++;
}
if (*start)
{
buf[strlen(start)] = 0;
hub_log(log_dump, "Line: %s", start);
if (handler(start, line_count+1, data) < 0)
return -1;
}
}
return line_count+1;
}
int uhub_atoi(const char* value) {
int len = strlen(value);
int offset = 0;
int val = 0;
int i = 0;
for (; i < len; i++)
if (value[i] > '9' || value[i] < '0')
offset++;
for (i = offset; i< len; i++)
val = val*10 + (value[i] - '0');
return value[0] == '-' ? -val : val;
}
/*
* FIXME: -INTMIN is wrong!
*/
const char* uhub_itoa(int val)
{
size_t i;
int value;
static char buf[22];
memset(buf, 0, sizeof(buf));
if (!val)
{
strcat(buf, "0");
return buf;
}
i = sizeof(buf) - 1;
for (value = abs(val); value && i > 0; value /= 10)
buf[--i] = "0123456789"[value % 10];
if (val < 0 && i > 0)
buf[--i] = '-';
return buf+i;
}
const char* uhub_ulltoa(uint64_t val)
{
size_t i;
static char buf[22] = { 0, };
memset(buf, 0, sizeof(buf));
if (!val)
{
strcat(buf, "0");
return buf;
}
i = sizeof(buf) - 1;
for (; val && i > 0; val /= 10)
buf[--i] = "0123456789"[val % 10];
return buf+i;
}
#ifndef HAVE_STRNDUP
char* strndup(const char* string, size_t n)
{
size_t max = MIN(strlen(string), n);
char* tmp = hub_malloc(max+1);
memcpy(tmp, string, max);
tmp[max] = 0;
return tmp;
}
#endif
#ifndef HAVE_MEMMEM
void* memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
{
char* c_buf = (char*) haystack;
char* c_pat = (char*) needle;
char* ptr = memchr(c_buf, c_pat[0], haystacklen);
while (ptr && (&ptr[0] - &c_buf[0] < haystacklen))
{
if (!memcmp(ptr, c_pat, needlelen))
return ptr;
ptr = memchr(&ptr[1], c_pat[0], &c_buf[haystacklen] - &ptr[0]);
}
return 0;
}
#endif
int split_string(const char* string, const char* split, struct linked_list* list, int allow_empty)
{
char* tmp1, *tmp2;
int n = 0;
if (!string || !*string || !split || !*split || !list)
return -1;
for (;;)
{
tmp1 = strstr(string, split);
if (tmp1) tmp2 = hub_strndup(string, tmp1 - string);
else tmp2 = hub_strdup(string);
if (!tmp2)
{
list_clear(list, &hub_free);
return -1;
}
if (*tmp2 || allow_empty)
{
/* store in list */
list_append(list, tmp2);
n++;
}
else
{
/* ignore element */
hub_free(tmp2);
}
if (!tmp1) break; /* last element found */
string = tmp1;
string += strlen(split);
}
return n;
}

65
src/util/misc.h Normal file
View File

@@ -0,0 +1,65 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_MISC_H
#define HAVE_UHUB_MISC_H
typedef int (*file_line_handler_t)(char* line, int line_number, void* data);
extern int is_num(char c);
extern int is_space(char c);
extern int is_white_space(char c);
extern int is_valid_utf8(const char* string);
extern int is_printable_utf8(const char* string, size_t length);
extern int is_valid_base32_char(char c);
extern void base32_encode(const unsigned char* buffer, size_t len, char* result);
extern void base32_decode(const char* src, unsigned char* dst, size_t len);
extern char* strip_white_space(char* string);
extern int file_read_lines(const char* file, void* data, file_line_handler_t handler);
extern const char* uhub_itoa(int val);
extern const char* uhub_ulltoa(uint64_t val);
extern int uhub_atoi(const char* value);
#ifdef NEED_ATOLL
extern int atoll(const char* value);
#endif
#ifndef HAVE_STRNDUP
extern char* strndup(const char* string, size_t n);
#endif
#ifndef HAVE_MEMMEM
void* memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
#endif
/**
* Split the string based on split, and place the different parts into list.
* @return the number of items in the list after split, or -1 if an error occured.
*/
struct linked_list;
extern int split_string(const char* string, const char* split, struct linked_list* list, int allow_empty);
#endif /* HAVE_UHUB_MISC_H */

137
src/util/rbtree.c Normal file
View File

@@ -0,0 +1,137 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#if 0
#include <sys/types.h>
#include "rbtree.h"
#define RED 0
#define BLACK 1
struct rb_node
{
const void* key;
const void* value; /* data */
int color;
struct rb_node* parent;
struct rb_node* left;
struct rb_node* right;
};
struct rb_tree
{
struct rb_node* root;
size_t elements;
rb_tree_alloc alloc;
rb_tree_free free;
rb_tree_compare compare;
};
/* returns the grandparent of a node, if it exits */
static inline struct rb_node* get_grandparent(struct rb_node* n)
{
if (n->parent)
return n->parent->parent;
return 0;
}
static struct rb_node* get_uncle(struct rb_node* n)
{
struct rb_node* gparent = n->parent ? n->parent->parent : 0;
if (gparent)
return (n->parent == gparent->left) ? gparent->right : gparent->left;
return 0;
}
static struct rb_node* tree_search(struct rb_tree* tree, const void* key)
{
struct rb_node* node = tree->root;
while (node)
{
int res = tree->compare(key, node->key);
if (res < 0) node = node->left;
else if (res > 0) node = node->right;
else return node;
}
return 0;
}
static struct rb_node* tree_insert(struct rb_tree* tree, const void* key, const void* value)
{
struct rb_node* node = tree->root;
struct rb_node* newnode = tree->alloc(sizeof(struct rb_node));
newnode->key = key;
newnode->value = value;
newnode->color = RED;
while (node)
{
int res = tree->compare(key, node->key);
if (res < 0) node = node->left;
else if (res > 0) node = node->right;
else
{
/* key already exists in tree */
return node;
}
}
return newnode;
}
struct rb_tree* rb_tree_create(rb_tree_compare compare, rb_tree_alloc a, rb_tree_free f)
{
struct rb_tree* tree = a(sizeof(struct rb_tree));
tree->compare = compare;
tree->alloc = a;
tree->free = f;
return tree;
}
void rb_tree_destroy(struct rb_tree* tree)
{
rb_tree_free f = tree->free;
f(tree);
}
void* rb_tree_insert(struct rb_tree* tree, const void* key, const void* value)
{
struct rb_node* node = tree_insert(tree, key, value);
if (node)
return (void*) node->value;
return 0;
}
void* rb_tree_remove(struct rb_tree* tree, const void* key)
{
}
void* rb_tree_get(struct rb_tree* tree, const void* key)
{
struct rb_node* node = tree_search(tree, key);
if (node)
return node->value;
return 0;
}
#endif

38
src/util/rbtree.h Normal file
View File

@@ -0,0 +1,38 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_RED_BLACK_TREE_H
#define HAVE_UHUB_RED_BLACK_TREE_H
struct rb_tree;
typedef int (*rb_tree_compare)(const void* a, const void* b);
typedef void* (*rb_tree_alloc)(size_t);
typedef void (*rb_tree_free)(void*);
extern struct rb_tree* rb_tree_create(rb_tree_compare, rb_tree_alloc, rb_tree_free);
extern void rb_tree_destroy(struct rb_tree*);
extern void* rb_tree_insert(struct rb_tree* tree, const void* key, const void* data);
extern void* rb_tree_remove(struct rb_tree* tree, const void* key);
extern void* rb_tree_get(struct rb_tree* tree, const void* key);
#endif /* HAVE_UHUB_RED_BLACK_TREE_H */

1241
src/util/tiger.c Normal file

File diff suppressed because it is too large Load Diff

26
src/util/tiger.h Normal file
View File

@@ -0,0 +1,26 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_HASH_TIGER_H
#define HAVE_UHUB_HASH_TIGER_H
extern void tiger(uint64_t* str, uint64_t length, uint64_t* res);
#endif /* HAVE_UHUB_HASH_TIGER_H */