mirror of
https://gitlab.xiph.org/xiph/icecast-common.git
synced 2024-12-04 14:46:31 -05:00
Update: Added "igloo_" to all experted symbols
This commit is contained in:
parent
f2efba644c
commit
5cc52a7b3d
146
avl/avl.c
146
avl/avl.c
@ -40,7 +40,7 @@
|
||||
#include "avl.h"
|
||||
|
||||
avl_node *
|
||||
avl_node_new (void * key,
|
||||
igloo_avl_node_new (void * key,
|
||||
avl_node * parent)
|
||||
{
|
||||
avl_node * node = (avl_node *) malloc (sizeof (avl_node));
|
||||
@ -63,7 +63,7 @@ avl_node_new (void * key,
|
||||
}
|
||||
|
||||
avl_tree *
|
||||
avl_tree_new (avl_key_compare_fun_type compare_fun,
|
||||
igloo_avl_tree_new (avl_key_compare_fun_type compare_fun,
|
||||
void * compare_arg)
|
||||
{
|
||||
avl_tree * t = (avl_tree *) malloc (sizeof (avl_tree));
|
||||
@ -71,7 +71,7 @@ avl_tree_new (avl_key_compare_fun_type compare_fun,
|
||||
if (!t) {
|
||||
return NULL;
|
||||
} else {
|
||||
avl_node * root = avl_node_new((void *)NULL, (avl_node *) NULL);
|
||||
avl_node * root = igloo_avl_node_new((void *)NULL, (avl_node *) NULL);
|
||||
if (!root) {
|
||||
free (t);
|
||||
return NULL;
|
||||
@ -88,44 +88,44 @@ avl_tree_new (avl_key_compare_fun_type compare_fun,
|
||||
}
|
||||
|
||||
static void
|
||||
avl_tree_free_helper (avl_node * node, avl_free_key_fun_type free_key_fun)
|
||||
igloo_avl_tree_free_helper (avl_node * node, avl_free_key_fun_type free_key_fun)
|
||||
{
|
||||
if (node->left) {
|
||||
avl_tree_free_helper (node->left, free_key_fun);
|
||||
igloo_avl_tree_free_helper (node->left, free_key_fun);
|
||||
}
|
||||
if (free_key_fun)
|
||||
free_key_fun (node->key);
|
||||
if (node->right) {
|
||||
avl_tree_free_helper (node->right, free_key_fun);
|
||||
igloo_avl_tree_free_helper (node->right, free_key_fun);
|
||||
}
|
||||
#ifdef HAVE_AVL_NODE_LOCK
|
||||
thread_rwlock_destroy (&node->rwlock);
|
||||
igloo_thread_rwlock_destroy (&node->rwlock);
|
||||
#endif
|
||||
free (node);
|
||||
}
|
||||
|
||||
void
|
||||
avl_tree_free (avl_tree * tree, avl_free_key_fun_type free_key_fun)
|
||||
igloo_avl_tree_free (avl_tree * tree, avl_free_key_fun_type free_key_fun)
|
||||
{
|
||||
if (tree->length) {
|
||||
avl_tree_free_helper (tree->root->right, free_key_fun);
|
||||
igloo_avl_tree_free_helper (tree->root->right, free_key_fun);
|
||||
}
|
||||
if (tree->root) {
|
||||
#ifdef HAVE_AVL_NODE_LOCK
|
||||
thread_rwlock_destroy(&tree->root->rwlock);
|
||||
igloo_thread_rwlock_destroy(&tree->root->rwlock);
|
||||
#endif
|
||||
free (tree->root);
|
||||
}
|
||||
thread_rwlock_destroy(&tree->rwlock);
|
||||
igloo_thread_rwlock_destroy(&tree->rwlock);
|
||||
free (tree);
|
||||
}
|
||||
|
||||
int
|
||||
avl_insert (avl_tree * ob,
|
||||
igloo_avl_insert (avl_tree * ob,
|
||||
void * key)
|
||||
{
|
||||
if (!(ob->root->right)) {
|
||||
avl_node * node = avl_node_new (key, ob->root);
|
||||
avl_node * node = igloo_avl_node_new (key, ob->root);
|
||||
if (!node) {
|
||||
return -1;
|
||||
} else {
|
||||
@ -147,7 +147,7 @@ avl_insert (avl_tree * ob,
|
||||
q = p->left;
|
||||
if (!q) {
|
||||
/* insert */
|
||||
avl_node * q_node = avl_node_new (key, p);
|
||||
avl_node * q_node = igloo_avl_node_new (key, p);
|
||||
if (!q_node) {
|
||||
return (-1);
|
||||
} else {
|
||||
@ -165,7 +165,7 @@ avl_insert (avl_tree * ob,
|
||||
q = p->right;
|
||||
if (!q) {
|
||||
/* insert */
|
||||
avl_node * q_node = avl_node_new (key, p);
|
||||
avl_node * q_node = igloo_avl_node_new (key, p);
|
||||
if (!q_node) {
|
||||
return -1;
|
||||
} else {
|
||||
@ -297,7 +297,7 @@ avl_insert (avl_tree * ob,
|
||||
}
|
||||
|
||||
int
|
||||
avl_get_by_index (avl_tree * tree,
|
||||
igloo_avl_get_by_index (avl_tree * tree,
|
||||
unsigned long index,
|
||||
void ** value_address)
|
||||
{
|
||||
@ -320,7 +320,7 @@ avl_get_by_index (avl_tree * tree,
|
||||
}
|
||||
|
||||
int
|
||||
avl_get_by_key (avl_tree * tree,
|
||||
igloo_avl_get_by_key (avl_tree * tree,
|
||||
void * key,
|
||||
void **value_address)
|
||||
{
|
||||
@ -349,7 +349,7 @@ avl_get_by_key (avl_tree * tree,
|
||||
}
|
||||
}
|
||||
|
||||
int avl_delete(avl_tree *tree, void *key, avl_free_key_fun_type free_key_fun)
|
||||
int igloo_avl_delete(avl_tree *tree, void *key, avl_free_key_fun_type free_key_fun)
|
||||
{
|
||||
avl_node *x, *y, *p, *q, *r, *top, *x_child;
|
||||
int shortened_side, shorter;
|
||||
@ -458,7 +458,7 @@ int avl_delete(avl_tree *tree, void *key, avl_free_key_fun_type free_key_fun)
|
||||
if (free_key_fun)
|
||||
free_key_fun (x->key);
|
||||
#ifdef HAVE_AVL_NODE_LOCK
|
||||
thread_rwlock_destroy (&x->rwlock);
|
||||
igloo_thread_rwlock_destroy (&x->rwlock);
|
||||
#endif
|
||||
free (x);
|
||||
|
||||
@ -625,13 +625,13 @@ int avl_delete(avl_tree *tree, void *key, avl_free_key_fun_type free_key_fun)
|
||||
}
|
||||
|
||||
static int
|
||||
avl_iterate_inorder_helper (avl_node * node,
|
||||
igloo_avl_iterate_inorder_helper (avl_node * node,
|
||||
avl_iter_fun_type iter_fun,
|
||||
void * iter_arg)
|
||||
{
|
||||
int result;
|
||||
if (node->left) {
|
||||
result = avl_iterate_inorder_helper (node->left, iter_fun, iter_arg);
|
||||
result = igloo_avl_iterate_inorder_helper (node->left, iter_fun, iter_arg);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
@ -641,7 +641,7 @@ avl_iterate_inorder_helper (avl_node * node,
|
||||
return result;
|
||||
}
|
||||
if (node->right) {
|
||||
result = avl_iterate_inorder_helper (node->right, iter_fun, iter_arg);
|
||||
result = igloo_avl_iterate_inorder_helper (node->right, iter_fun, iter_arg);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
@ -650,21 +650,21 @@ avl_iterate_inorder_helper (avl_node * node,
|
||||
}
|
||||
|
||||
int
|
||||
avl_iterate_inorder (avl_tree * tree,
|
||||
igloo_avl_iterate_inorder (avl_tree * tree,
|
||||
avl_iter_fun_type iter_fun,
|
||||
void * iter_arg)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (tree->length) {
|
||||
result = avl_iterate_inorder_helper (tree->root->right, iter_fun, iter_arg);
|
||||
result = igloo_avl_iterate_inorder_helper (tree->root->right, iter_fun, iter_arg);
|
||||
return (result);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
avl_node *avl_get_first(avl_tree *tree)
|
||||
avl_node *igloo_avl_get_first(avl_tree *tree)
|
||||
{
|
||||
avl_node *node;
|
||||
|
||||
@ -677,7 +677,7 @@ avl_node *avl_get_first(avl_tree *tree)
|
||||
return node;
|
||||
}
|
||||
|
||||
avl_node *avl_get_prev(avl_node *node)
|
||||
avl_node *igloo_avl_get_prev(avl_node *node)
|
||||
{
|
||||
if (node->left) {
|
||||
node = node->left;
|
||||
@ -700,7 +700,7 @@ avl_node *avl_get_prev(avl_node *node)
|
||||
}
|
||||
}
|
||||
|
||||
avl_node *avl_get_next(avl_node *node)
|
||||
avl_node *igloo_avl_get_next(avl_node *node)
|
||||
{
|
||||
if (node->right) {
|
||||
node = node->right;
|
||||
@ -726,7 +726,7 @@ avl_node *avl_get_next(avl_node *node)
|
||||
/* iterate a function over a range of indices, using get_predecessor */
|
||||
|
||||
int
|
||||
avl_iterate_index_range (avl_tree * tree,
|
||||
igloo_avl_iterate_index_range (avl_tree * tree,
|
||||
avl_iter_index_fun_type iter_fun,
|
||||
unsigned long low,
|
||||
unsigned long high,
|
||||
@ -759,7 +759,7 @@ avl_iterate_index_range (avl_tree * tree,
|
||||
if (iter_fun (num_left, node->key, iter_arg) != 0) {
|
||||
return -1;
|
||||
}
|
||||
node = avl_get_prev (node);
|
||||
node = igloo_avl_get_prev (node);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -770,7 +770,7 @@ avl_iterate_index_range (avl_tree * tree,
|
||||
*/
|
||||
|
||||
static avl_node *
|
||||
avl_get_index_by_key (avl_tree * tree,
|
||||
igloo_avl_get_index_by_key (avl_tree * tree,
|
||||
void * key,
|
||||
unsigned long * index)
|
||||
{
|
||||
@ -811,7 +811,7 @@ avl_get_index_by_key (avl_tree * tree,
|
||||
/* return the (low index, high index) pair that spans the given key */
|
||||
|
||||
int
|
||||
avl_get_span_by_key (avl_tree * tree,
|
||||
igloo_avl_get_span_by_key (avl_tree * tree,
|
||||
void * key,
|
||||
unsigned long * low,
|
||||
unsigned long * high)
|
||||
@ -819,7 +819,7 @@ avl_get_span_by_key (avl_tree * tree,
|
||||
unsigned long m, i, j;
|
||||
avl_node * node;
|
||||
|
||||
node = avl_get_index_by_key (tree, key, &m);
|
||||
node = igloo_avl_get_index_by_key (tree, key, &m);
|
||||
|
||||
/* did we find an exact match?
|
||||
* if so, we have to search left and right
|
||||
@ -829,17 +829,17 @@ avl_get_span_by_key (avl_tree * tree,
|
||||
if (node) {
|
||||
avl_node * left, * right;
|
||||
/* search left */
|
||||
left = avl_get_prev (node);
|
||||
left = igloo_avl_get_prev (node);
|
||||
i = m;
|
||||
while (left && (i > 0) && (tree->compare_fun (tree->compare_arg, key, left->key) == 0)) {
|
||||
left = avl_get_prev (left);
|
||||
left = igloo_avl_get_prev (left);
|
||||
i = i - 1;
|
||||
}
|
||||
/* search right */
|
||||
right = avl_get_next (node);
|
||||
right = igloo_avl_get_next (node);
|
||||
j = m;
|
||||
while (right && (j <= tree->length) && (tree->compare_fun (tree->compare_arg, key, right->key) == 0)) {
|
||||
right = avl_get_next (right);
|
||||
right = igloo_avl_get_next (right);
|
||||
j = j + 1;
|
||||
}
|
||||
*low = i;
|
||||
@ -854,7 +854,7 @@ avl_get_span_by_key (avl_tree * tree,
|
||||
/* return the (low index, high index) pair that spans the given key */
|
||||
|
||||
int
|
||||
avl_get_span_by_two_keys (avl_tree * tree,
|
||||
igloo_avl_get_span_by_two_keys (avl_tree * tree,
|
||||
void * low_key,
|
||||
void * high_key,
|
||||
unsigned long * low,
|
||||
@ -872,15 +872,15 @@ avl_get_span_by_two_keys (avl_tree * tree,
|
||||
high_key = temp;
|
||||
}
|
||||
|
||||
low_node = avl_get_index_by_key (tree, low_key, &i);
|
||||
high_node = avl_get_index_by_key (tree, high_key, &j);
|
||||
low_node = igloo_avl_get_index_by_key (tree, low_key, &i);
|
||||
high_node = igloo_avl_get_index_by_key (tree, high_key, &j);
|
||||
|
||||
if (low_node) {
|
||||
avl_node * left;
|
||||
/* search left */
|
||||
left = avl_get_prev (low_node);
|
||||
left = igloo_avl_get_prev (low_node);
|
||||
while (left && (i > 0) && (tree->compare_fun (tree->compare_arg, low_key, left->key) == 0)) {
|
||||
left = avl_get_prev (left);
|
||||
left = igloo_avl_get_prev (left);
|
||||
i = i - 1;
|
||||
}
|
||||
} else {
|
||||
@ -889,9 +889,9 @@ avl_get_span_by_two_keys (avl_tree * tree,
|
||||
if (high_node) {
|
||||
avl_node * right;
|
||||
/* search right */
|
||||
right = avl_get_next (high_node);
|
||||
right = igloo_avl_get_next (high_node);
|
||||
while (right && (j <= tree->length) && (tree->compare_fun (tree->compare_arg, high_key, right->key) == 0)) {
|
||||
right = avl_get_next (right);
|
||||
right = igloo_avl_get_next (right);
|
||||
j = j + 1;
|
||||
}
|
||||
} else {
|
||||
@ -905,7 +905,7 @@ avl_get_span_by_two_keys (avl_tree * tree,
|
||||
|
||||
|
||||
int
|
||||
avl_get_item_by_key_most (avl_tree * tree,
|
||||
igloo_avl_get_item_by_key_most (avl_tree * tree,
|
||||
void * key,
|
||||
void **value_address)
|
||||
{
|
||||
@ -949,7 +949,7 @@ avl_get_item_by_key_most (avl_tree * tree,
|
||||
}
|
||||
|
||||
int
|
||||
avl_get_item_by_key_least (avl_tree * tree,
|
||||
igloo_avl_get_item_by_key_least (avl_tree * tree,
|
||||
void * key,
|
||||
void **value_address)
|
||||
{
|
||||
@ -993,13 +993,13 @@ avl_get_item_by_key_least (avl_tree * tree,
|
||||
#define AVL_MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||
|
||||
static long
|
||||
avl_verify_balance (avl_node * node)
|
||||
igloo_avl_verify_balance (avl_node * node)
|
||||
{
|
||||
if (!node) {
|
||||
return 0;
|
||||
} else {
|
||||
long lh = avl_verify_balance (node->left);
|
||||
long rh = avl_verify_balance (node->right);
|
||||
long lh = igloo_avl_verify_balance (node->left);
|
||||
long rh = igloo_avl_verify_balance (node->right);
|
||||
if ((rh - lh) != AVL_GET_BALANCE(node)) {
|
||||
return 0;
|
||||
}
|
||||
@ -1011,31 +1011,31 @@ avl_verify_balance (avl_node * node)
|
||||
}
|
||||
|
||||
static void
|
||||
avl_verify_parent (avl_node * node, avl_node * parent)
|
||||
igloo_avl_verify_parent (avl_node * node, avl_node * parent)
|
||||
{
|
||||
if (node->parent != parent) {
|
||||
return;
|
||||
}
|
||||
if (node->left) {
|
||||
avl_verify_parent (node->left, node);
|
||||
igloo_avl_verify_parent (node->left, node);
|
||||
}
|
||||
if (node->right) {
|
||||
avl_verify_parent (node->right, node);
|
||||
igloo_avl_verify_parent (node->right, node);
|
||||
}
|
||||
}
|
||||
|
||||
static long
|
||||
avl_verify_rank (avl_node * node)
|
||||
igloo_avl_verify_rank (avl_node * node)
|
||||
{
|
||||
if (!node) {
|
||||
return 0;
|
||||
} else {
|
||||
unsigned long num_left=0, num_right=0;
|
||||
if (node->left) {
|
||||
num_left = avl_verify_rank (node->left);
|
||||
num_left = igloo_avl_verify_rank (node->left);
|
||||
}
|
||||
if (node->right) {
|
||||
num_right = avl_verify_rank (node->right);
|
||||
num_right = igloo_avl_verify_rank (node->right);
|
||||
}
|
||||
if (AVL_GET_RANK (node) != num_left + 1) {
|
||||
fprintf (stderr, "invalid rank at node %ld\n", (long) node->key);
|
||||
@ -1048,12 +1048,12 @@ avl_verify_rank (avl_node * node)
|
||||
/* sanity-check the tree */
|
||||
|
||||
int
|
||||
avl_verify (avl_tree * tree)
|
||||
igloo_avl_verify (avl_tree * tree)
|
||||
{
|
||||
if (tree->length) {
|
||||
avl_verify_balance (tree->root->right);
|
||||
avl_verify_parent (tree->root->right, tree->root);
|
||||
avl_verify_rank (tree->root->right);
|
||||
igloo_avl_verify_balance (tree->root->right);
|
||||
igloo_avl_verify_parent (tree->root->right, tree->root);
|
||||
igloo_avl_verify_rank (tree->root->right);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@ -1070,10 +1070,10 @@ typedef struct _link_node {
|
||||
int width;
|
||||
} link_node;
|
||||
|
||||
static char balance_chars[3] = {'\\', '-', '/'};
|
||||
static char igloo_balance_chars[3] = {'\\', '-', '/'};
|
||||
|
||||
static int
|
||||
default_key_printer (char * buffer, void * key)
|
||||
igloo_default_key_printer (char * buffer, void * key)
|
||||
{
|
||||
return snprintf (buffer, AVL_KEY_PRINTER_BUFLEN, "%p", key);
|
||||
}
|
||||
@ -1086,10 +1086,10 @@ default_key_printer (char * buffer, void * key)
|
||||
*/
|
||||
|
||||
static void
|
||||
print_connectors (link_node * link)
|
||||
igloo_print_connectors (link_node * link)
|
||||
{
|
||||
if (link->parent) {
|
||||
print_connectors (link->parent);
|
||||
igloo_print_connectors (link->parent);
|
||||
}
|
||||
if (link->parent && (link->parent->direction != link->direction) && (link->parent->parent)) {
|
||||
int i;
|
||||
@ -1113,7 +1113,7 @@ print_connectors (link_node * link)
|
||||
*/
|
||||
|
||||
static void
|
||||
print_node (avl_key_printer_fun_type key_printer,
|
||||
igloo_print_node (avl_key_printer_fun_type key_printer,
|
||||
avl_node * node,
|
||||
link_node * link)
|
||||
{
|
||||
@ -1126,11 +1126,11 @@ print_node (avl_key_printer_fun_type key_printer,
|
||||
here.parent = link;
|
||||
here.direction = 1;
|
||||
here.width = width + 11;
|
||||
print_node (key_printer, node->right, &here);
|
||||
igloo_print_node (key_printer, node->right, &here);
|
||||
}
|
||||
print_connectors (link);
|
||||
igloo_print_connectors (link);
|
||||
fprintf (stdout, "+-[%c %s %03d]",
|
||||
balance_chars[AVL_GET_BALANCE(node)+1],
|
||||
igloo_balance_chars[AVL_GET_BALANCE(node)+1],
|
||||
buffer,
|
||||
(int)AVL_GET_RANK(node));
|
||||
if (node->left || node->right) {
|
||||
@ -1143,36 +1143,36 @@ print_node (avl_key_printer_fun_type key_printer,
|
||||
here.parent = link;
|
||||
here.direction = -1;
|
||||
here.width = width + 11;
|
||||
print_node (key_printer, node->left, &here);
|
||||
igloo_print_node (key_printer, node->left, &here);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
avl_print_tree (avl_tree * tree, avl_key_printer_fun_type key_printer)
|
||||
igloo_avl_print_tree (avl_tree * tree, avl_key_printer_fun_type key_printer)
|
||||
{
|
||||
link_node top = {NULL, 0, 0};
|
||||
if (!key_printer) {
|
||||
key_printer = default_key_printer;
|
||||
key_printer = igloo_default_key_printer;
|
||||
}
|
||||
if (tree->length) {
|
||||
print_node (key_printer, tree->root->right, &top);
|
||||
igloo_print_node (key_printer, tree->root->right, &top);
|
||||
} else {
|
||||
fprintf (stdout, "<empty tree>\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void avl_tree_rlock(avl_tree *tree)
|
||||
void igloo_avl_tree_rlock(avl_tree *tree)
|
||||
{
|
||||
thread_rwlock_rlock(&tree->rwlock);
|
||||
}
|
||||
|
||||
void avl_tree_wlock(avl_tree *tree)
|
||||
void igloo_avl_tree_wlock(avl_tree *tree)
|
||||
{
|
||||
thread_rwlock_wlock(&tree->rwlock);
|
||||
}
|
||||
|
||||
void avl_tree_unlock(avl_tree *tree)
|
||||
void igloo_avl_tree_unlock(avl_tree *tree)
|
||||
{
|
||||
thread_rwlock_unlock(&tree->rwlock);
|
||||
}
|
||||
|
44
avl/avl.h
44
avl/avl.h
@ -18,7 +18,7 @@ extern "C" {
|
||||
#include "thread/thread.h"
|
||||
#else
|
||||
#define thread_rwlock_create(x) do{}while(0)
|
||||
#define thread_rwlock_destroy(x) do{}while(0)
|
||||
#define igloo_thread_rwlock_destroy(x) do{}while(0)
|
||||
#define thread_rwlock_rlock(x) do{}while(0)
|
||||
#define thread_rwlock_wlock(x) do{}while(0)
|
||||
#define thread_rwlock_unlock(x) do{}while(0)
|
||||
@ -76,44 +76,44 @@ typedef struct _avl_tree {
|
||||
#endif
|
||||
} avl_tree;
|
||||
|
||||
avl_tree * avl_tree_new (avl_key_compare_fun_type compare_fun, void * compare_arg);
|
||||
avl_node * avl_node_new (void * key, avl_node * parent);
|
||||
avl_tree * igloo_avl_tree_new (avl_key_compare_fun_type compare_fun, void * compare_arg);
|
||||
avl_node * igloo_avl_node_new (void * key, avl_node * parent);
|
||||
|
||||
void avl_tree_free (
|
||||
void igloo_avl_tree_free (
|
||||
avl_tree * tree,
|
||||
avl_free_key_fun_type free_key_fun
|
||||
);
|
||||
|
||||
int avl_insert (
|
||||
int igloo_avl_insert (
|
||||
avl_tree * ob,
|
||||
void * key
|
||||
);
|
||||
|
||||
int avl_delete (
|
||||
int igloo_avl_delete (
|
||||
avl_tree * tree,
|
||||
void * key,
|
||||
avl_free_key_fun_type free_key_fun
|
||||
);
|
||||
|
||||
int avl_get_by_index (
|
||||
int igloo_avl_get_by_index (
|
||||
avl_tree * tree,
|
||||
unsigned long index,
|
||||
void ** value_address
|
||||
);
|
||||
|
||||
int avl_get_by_key (
|
||||
int igloo_avl_get_by_key (
|
||||
avl_tree * tree,
|
||||
void * key,
|
||||
void ** value_address
|
||||
);
|
||||
|
||||
int avl_iterate_inorder (
|
||||
int igloo_avl_iterate_inorder (
|
||||
avl_tree * tree,
|
||||
avl_iter_fun_type iter_fun,
|
||||
void * iter_arg
|
||||
);
|
||||
|
||||
int avl_iterate_index_range (
|
||||
int igloo_avl_iterate_index_range (
|
||||
avl_tree * tree,
|
||||
avl_iter_index_fun_type iter_fun,
|
||||
unsigned long low,
|
||||
@ -121,14 +121,14 @@ int avl_iterate_index_range (
|
||||
void * iter_arg
|
||||
);
|
||||
|
||||
int avl_get_span_by_key (
|
||||
int igloo_avl_get_span_by_key (
|
||||
avl_tree * tree,
|
||||
void * key,
|
||||
unsigned long * low,
|
||||
unsigned long * high
|
||||
);
|
||||
|
||||
int avl_get_span_by_two_keys (
|
||||
int igloo_avl_get_span_by_two_keys (
|
||||
avl_tree * tree,
|
||||
void * key_a,
|
||||
void * key_b,
|
||||
@ -136,37 +136,37 @@ int avl_get_span_by_two_keys (
|
||||
unsigned long * high
|
||||
);
|
||||
|
||||
int avl_verify (avl_tree * tree);
|
||||
int igloo_avl_verify (avl_tree * tree);
|
||||
|
||||
void avl_print_tree (
|
||||
void igloo_avl_print_tree (
|
||||
avl_tree * tree,
|
||||
avl_key_printer_fun_type key_printer
|
||||
);
|
||||
|
||||
avl_node *avl_get_first(avl_tree *tree);
|
||||
avl_node *igloo_avl_get_first(avl_tree *tree);
|
||||
|
||||
avl_node *avl_get_prev(avl_node * node);
|
||||
avl_node *igloo_avl_get_prev(avl_node * node);
|
||||
|
||||
avl_node *avl_get_next(avl_node * node);
|
||||
avl_node *igloo_avl_get_next(avl_node * node);
|
||||
|
||||
/* These two are from David Ascher <david_ascher@brown.edu> */
|
||||
|
||||
int avl_get_item_by_key_most (
|
||||
int igloo_avl_get_item_by_key_most (
|
||||
avl_tree * tree,
|
||||
void * key,
|
||||
void ** value_address
|
||||
);
|
||||
|
||||
int avl_get_item_by_key_least (
|
||||
int igloo_avl_get_item_by_key_least (
|
||||
avl_tree * tree,
|
||||
void * key,
|
||||
void ** value_address
|
||||
);
|
||||
|
||||
/* optional locking stuff */
|
||||
void avl_tree_rlock(avl_tree *tree);
|
||||
void avl_tree_wlock(avl_tree *tree);
|
||||
void avl_tree_unlock(avl_tree *tree);
|
||||
void igloo_avl_tree_rlock(avl_tree *tree);
|
||||
void igloo_avl_tree_wlock(avl_tree *tree);
|
||||
void igloo_avl_tree_unlock(avl_tree *tree);
|
||||
void avl_node_rlock(avl_node *node);
|
||||
void avl_node_wlock(avl_node *node);
|
||||
void avl_node_unlock(avl_node *node);
|
||||
|
14
avl/test.c
14
avl/test.c
@ -25,21 +25,21 @@ int main(int argc, char **argv)
|
||||
|
||||
printf("avl test... max_nodes = %d...\n", max_nodes);
|
||||
|
||||
tree = avl_tree_new(_compare, NULL);
|
||||
tree = igloo_avl_tree_new(_compare, NULL);
|
||||
|
||||
printf("Filling tree...\n");
|
||||
for (i = 0; i < max_nodes; i++) {
|
||||
avl_insert(tree, (void *)rand());
|
||||
igloo_avl_insert(tree, (void *)rand());
|
||||
}
|
||||
|
||||
printf("Traversing tree...\n");
|
||||
node = avl_get_first(tree);
|
||||
node = igloo_avl_get_first(tree);
|
||||
while (node) {
|
||||
i = (int)node->key;
|
||||
|
||||
printf("...%5d\n", i);
|
||||
|
||||
node = avl_get_next(node);
|
||||
node = igloo_avl_get_next(node);
|
||||
}
|
||||
|
||||
printf("Trying to go backwards...\n");
|
||||
@ -47,13 +47,13 @@ int main(int argc, char **argv)
|
||||
while (node) {
|
||||
i = (int)node->key;
|
||||
printf("...%5d\n", i);
|
||||
node = avl_get_prev(node);
|
||||
node = igloo_avl_get_prev(node);
|
||||
}
|
||||
|
||||
printf("Printing tree...\n");
|
||||
avl_print_tree(tree, _printer);
|
||||
igloo_avl_print_tree(tree, _printer);
|
||||
|
||||
avl_tree_free(tree, _free);
|
||||
igloo_avl_tree_free(tree, _free);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -62,13 +62,13 @@ struct httpp_encoding_tag {
|
||||
|
||||
|
||||
/* Handlers, at end of file */
|
||||
static ssize_t __enc_identity_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
static ssize_t __enc_identity_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
static ssize_t __enc_chunked_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
static ssize_t __enc_chunked_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
static ssize_t igloo___enc_identity_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
static ssize_t igloo___enc_identity_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
static ssize_t igloo___enc_chunked_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
static ssize_t igloo___enc_chunked_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
|
||||
/* function to move some data out of our buffers */
|
||||
ssize_t __copy_buffer(void *dst, void **src, size_t *boffset, size_t *blen, size_t len)
|
||||
ssize_t igloo___copy_buffer(void *dst, void **src, size_t *boffset, size_t *blen, size_t len)
|
||||
{
|
||||
void *p;
|
||||
size_t have_len;
|
||||
@ -120,7 +120,7 @@ static inline void __flush_output(httpp_encoding_t *self, ssize_t (*cb)(void*, c
|
||||
|
||||
/* meta data functions */
|
||||
/* meta data is to be used in a encoding-specific way */
|
||||
httpp_meta_t *httpp_encoding_meta_new(const char *key, const char *value)
|
||||
httpp_meta_t *igloo_httpp_encoding_meta_new(const char *key, const char *value)
|
||||
{
|
||||
httpp_meta_t *ret = calloc(1, sizeof(httpp_meta_t));
|
||||
if (!ret)
|
||||
@ -138,11 +138,11 @@ httpp_meta_t *httpp_encoding_meta_new(const char *key, const char *value)
|
||||
|
||||
return ret;
|
||||
fail:
|
||||
httpp_encoding_meta_free(ret);
|
||||
igloo_httpp_encoding_meta_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int httpp_encoding_meta_free(httpp_meta_t *self)
|
||||
int igloo_httpp_encoding_meta_free(httpp_meta_t *self)
|
||||
{
|
||||
while (self) {
|
||||
httpp_meta_t *cur = self;
|
||||
@ -158,7 +158,7 @@ int httpp_encoding_meta_free(httpp_meta_t *self)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int httpp_encoding_meta_append(httpp_meta_t **dst, httpp_meta_t *next)
|
||||
int igloo_httpp_encoding_meta_append(httpp_meta_t **dst, httpp_meta_t *next)
|
||||
{
|
||||
httpp_meta_t **cur;
|
||||
|
||||
@ -177,7 +177,7 @@ int httpp_encoding_meta_append(httpp_meta_t **dst, httpp_meta_t *n
|
||||
}
|
||||
|
||||
/* General setup */
|
||||
httpp_encoding_t *httpp_encoding_new(const char *encoding) {
|
||||
httpp_encoding_t *igloo_httpp_encoding_new(const char *encoding) {
|
||||
httpp_encoding_t *ret = calloc(1, sizeof(httpp_encoding_t));
|
||||
if (!ret)
|
||||
return NULL;
|
||||
@ -186,11 +186,11 @@ httpp_encoding_t *httpp_encoding_new(const char *encoding) {
|
||||
ret->bytes_till_eof = -1;
|
||||
|
||||
if (strcasecmp(encoding, HTTPP_ENCODING_IDENTITY) == 0) {
|
||||
ret->process_read = __enc_identity_read;
|
||||
ret->process_write = __enc_identity_write;
|
||||
ret->process_read = igloo___enc_identity_read;
|
||||
ret->process_write = igloo___enc_identity_write;
|
||||
} else if (strcasecmp(encoding, HTTPP_ENCODING_CHUNKED) == 0) {
|
||||
ret->process_read = __enc_chunked_read;
|
||||
ret->process_write = __enc_chunked_write;
|
||||
ret->process_read = igloo___enc_chunked_read;
|
||||
ret->process_write = igloo___enc_chunked_write;
|
||||
} else {
|
||||
goto fail;
|
||||
}
|
||||
@ -198,11 +198,11 @@ httpp_encoding_t *httpp_encoding_new(const char *encoding) {
|
||||
return ret;
|
||||
|
||||
fail:
|
||||
httpp_encoding_release(ret);
|
||||
igloo_httpp_encoding_release(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int httpp_encoding_addref(httpp_encoding_t *self)
|
||||
int igloo_httpp_encoding_addref(httpp_encoding_t *self)
|
||||
{
|
||||
if (!self)
|
||||
return -1;
|
||||
@ -210,7 +210,7 @@ int httpp_encoding_addref(httpp_encoding_t *self)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int httpp_encoding_release(httpp_encoding_t *self)
|
||||
int igloo_httpp_encoding_release(httpp_encoding_t *self)
|
||||
{
|
||||
if (!self)
|
||||
return -1;
|
||||
@ -219,8 +219,8 @@ int httpp_encoding_release(httpp_encoding_t *self)
|
||||
if (self->refc)
|
||||
return 0;
|
||||
|
||||
httpp_encoding_meta_free(self->meta_read);
|
||||
httpp_encoding_meta_free(self->meta_write);
|
||||
igloo_httpp_encoding_meta_free(self->meta_read);
|
||||
igloo_httpp_encoding_meta_free(self->meta_write);
|
||||
|
||||
if (self->buf_read_raw)
|
||||
free(self->buf_read_raw);
|
||||
@ -237,7 +237,7 @@ int httpp_encoding_release(httpp_encoding_t *self)
|
||||
/* Read data from backend.
|
||||
* if cb is NULL this will read from the internal buffer.
|
||||
*/
|
||||
ssize_t httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
ssize_t igloo_httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
{
|
||||
ssize_t done = 0;
|
||||
ssize_t ret;
|
||||
@ -248,7 +248,7 @@ ssize_t httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
ret = __copy_buffer(buf, &(self->buf_read_decoded), &(self->buf_read_decoded_offset), &(self->buf_read_decoded_len), len);
|
||||
ret = igloo___copy_buffer(buf, &(self->buf_read_decoded), &(self->buf_read_decoded_offset), &(self->buf_read_decoded_len), len);
|
||||
|
||||
if (ret == (ssize_t)len)
|
||||
return ret;
|
||||
@ -270,7 +270,7 @@ ssize_t httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t
|
||||
len -= ret;
|
||||
|
||||
if (len) {
|
||||
ret = __copy_buffer(buf, &(self->buf_read_decoded), &(self->buf_read_decoded_offset), &(self->buf_read_decoded_len), len);
|
||||
ret = igloo___copy_buffer(buf, &(self->buf_read_decoded), &(self->buf_read_decoded_offset), &(self->buf_read_decoded_len), len);
|
||||
if (ret > 0) {
|
||||
done += ret;
|
||||
buf += ret;
|
||||
@ -281,7 +281,7 @@ ssize_t httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t
|
||||
return done;
|
||||
}
|
||||
|
||||
int httpp_encoding_eof(httpp_encoding_t *self, int (*cb)(void*), void *userdata)
|
||||
int igloo_httpp_encoding_eof(httpp_encoding_t *self, int (*cb)(void*), void *userdata)
|
||||
{
|
||||
if (!self)
|
||||
return -1;
|
||||
@ -302,7 +302,7 @@ int httpp_encoding_eof(httpp_encoding_t *self, int (*cb)(void*), v
|
||||
* After a call to this function the meta data is released from the
|
||||
* encoding object and the caller is responsible to free it.
|
||||
*/
|
||||
httpp_meta_t *httpp_encoding_get_meta(httpp_encoding_t *self)
|
||||
httpp_meta_t *igloo_httpp_encoding_get_meta(httpp_encoding_t *self)
|
||||
{
|
||||
httpp_meta_t *ret;
|
||||
|
||||
@ -319,7 +319,7 @@ httpp_meta_t *httpp_encoding_get_meta(httpp_encoding_t *self)
|
||||
* Depending on encoding flushing buffers may not be safe if not
|
||||
* at end of stream.
|
||||
*/
|
||||
ssize_t httpp_encoding_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
ssize_t igloo_httpp_encoding_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
@ -339,7 +339,7 @@ ssize_t httpp_encoding_write(httpp_encoding_t *self, const void *buf,
|
||||
}
|
||||
|
||||
/* Check if we have something to flush. */
|
||||
ssize_t httpp_encoding_pending(httpp_encoding_t *self)
|
||||
ssize_t igloo_httpp_encoding_pending(httpp_encoding_t *self)
|
||||
{
|
||||
if (!self)
|
||||
return -1;
|
||||
@ -351,15 +351,15 @@ ssize_t httpp_encoding_pending(httpp_encoding_t *self)
|
||||
/* Attach meta data to the stream.
|
||||
* this is to be written out as soon as the encoding supports.
|
||||
*/
|
||||
int httpp_encoding_append_meta(httpp_encoding_t *self, httpp_meta_t *meta)
|
||||
int igloo_httpp_encoding_append_meta(httpp_encoding_t *self, httpp_meta_t *meta)
|
||||
{
|
||||
if (!self)
|
||||
return -1;
|
||||
return httpp_encoding_meta_append(&(self->meta_write), meta);
|
||||
return igloo_httpp_encoding_meta_append(&(self->meta_write), meta);
|
||||
}
|
||||
|
||||
/* handlers for encodings */
|
||||
static ssize_t __enc_identity_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
static ssize_t igloo___enc_identity_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
{
|
||||
(void)self;
|
||||
if (!cb)
|
||||
@ -367,7 +367,7 @@ static ssize_t __enc_identity_read(httpp_encoding_t *self, void *buf, size_t len
|
||||
return cb(userdata, buf, len);
|
||||
}
|
||||
|
||||
static ssize_t __enc_identity_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
static ssize_t igloo___enc_identity_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
{
|
||||
(void)self;
|
||||
if (!cb)
|
||||
@ -414,7 +414,7 @@ static void __enc_chunked_read_extentions(httpp_encoding_t *self, char *p, size_
|
||||
p++;
|
||||
len--;
|
||||
|
||||
*parent = meta = httpp_encoding_meta_new(NULL, NULL);
|
||||
*parent = meta = igloo_httpp_encoding_meta_new(NULL, NULL);
|
||||
parent = &(meta->next);
|
||||
|
||||
for (key_len = 0; key_len < len && p[key_len] != '='; key_len++);
|
||||
@ -482,7 +482,7 @@ static void __enc_chunked_read_extentions(httpp_encoding_t *self, char *p, size_
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t __enc_chunked_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
static ssize_t igloo___enc_chunked_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata)
|
||||
{
|
||||
ssize_t ret;
|
||||
size_t buflen;
|
||||
@ -780,12 +780,12 @@ static char *__enc_chunked_write_extensions(httpp_encoding_t *self)
|
||||
|
||||
*p = 0; /* terminate the string */
|
||||
|
||||
httpp_encoding_meta_free(self->meta_write);
|
||||
igloo_httpp_encoding_meta_free(self->meta_write);
|
||||
self->meta_write = NULL;
|
||||
|
||||
return buf;
|
||||
}
|
||||
static ssize_t __enc_chunked_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
static ssize_t igloo___enc_chunked_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata)
|
||||
{
|
||||
char encoded_length[32];
|
||||
char *extensions = NULL;
|
||||
@ -798,7 +798,7 @@ static ssize_t __enc_chunked_write(httpp_encoding_t *self, const void *buf, size
|
||||
len = 0;
|
||||
|
||||
/* refuse to write if we still have stuff to flush. */
|
||||
if (httpp_encoding_pending(self) > 0)
|
||||
if (igloo_httpp_encoding_pending(self) > 0)
|
||||
return 0;
|
||||
|
||||
/* limit length to a bit more sane value */
|
||||
|
@ -46,44 +46,44 @@ struct httpp_meta_tag {
|
||||
|
||||
/* meta data functions */
|
||||
/* meta data is to be used in a encoding-specific way */
|
||||
httpp_meta_t *httpp_encoding_meta_new(const char *key, const char *value);
|
||||
int httpp_encoding_meta_free(httpp_meta_t *self);
|
||||
int httpp_encoding_meta_append(httpp_meta_t **dst, httpp_meta_t *next);
|
||||
httpp_meta_t *igloo_httpp_encoding_meta_new(const char *key, const char *value);
|
||||
int igloo_httpp_encoding_meta_free(httpp_meta_t *self);
|
||||
int igloo_httpp_encoding_meta_append(httpp_meta_t **dst, httpp_meta_t *next);
|
||||
|
||||
/* General setup */
|
||||
httpp_encoding_t *httpp_encoding_new(const char *encoding);
|
||||
int httpp_encoding_addref(httpp_encoding_t *self);
|
||||
int httpp_encoding_release(httpp_encoding_t *self);
|
||||
httpp_encoding_t *igloo_httpp_encoding_new(const char *encoding);
|
||||
int igloo_httpp_encoding_addref(httpp_encoding_t *self);
|
||||
int igloo_httpp_encoding_release(httpp_encoding_t *self);
|
||||
|
||||
/* Read data from backend.
|
||||
* if cb is NULL this will read from the internal buffer.
|
||||
*/
|
||||
ssize_t httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
ssize_t igloo_httpp_encoding_read(httpp_encoding_t *self, void *buf, size_t len, ssize_t (*cb)(void*, void*, size_t), void *userdata);
|
||||
|
||||
/* Check if EOF is reached.
|
||||
* If cb is not NULL this also considers backend state.
|
||||
*/
|
||||
int httpp_encoding_eof(httpp_encoding_t *self, int (*cb)(void*), void *userdata);
|
||||
int igloo_httpp_encoding_eof(httpp_encoding_t *self, int (*cb)(void*), void *userdata);
|
||||
|
||||
/* Read any meta data that is in buffer.
|
||||
* After a call to this function the meta data is released from the
|
||||
* encoding object and the caller is responsible to free it.
|
||||
*/
|
||||
httpp_meta_t *httpp_encoding_get_meta(httpp_encoding_t *self);
|
||||
httpp_meta_t *igloo_httpp_encoding_get_meta(httpp_encoding_t *self);
|
||||
|
||||
/* Write data to backend.
|
||||
* If buf is NULL this will flush buffers.
|
||||
* Depending on encoding flushing buffers may not be safe if not
|
||||
* at end of stream.
|
||||
*/
|
||||
ssize_t httpp_encoding_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
ssize_t igloo_httpp_encoding_write(httpp_encoding_t *self, const void *buf, size_t len, ssize_t (*cb)(void*, const void*, size_t), void *userdata);
|
||||
|
||||
/* Check if we have something to flush. */
|
||||
ssize_t httpp_encoding_pending(httpp_encoding_t *self);
|
||||
ssize_t igloo_httpp_encoding_pending(httpp_encoding_t *self);
|
||||
|
||||
/* Attach meta data to the stream.
|
||||
* this is to be written out as soon as the encoding supports.
|
||||
*/
|
||||
int httpp_encoding_append_meta(httpp_encoding_t *self, httpp_meta_t *meta);
|
||||
int igloo_httpp_encoding_append_meta(httpp_encoding_t *self, httpp_meta_t *meta);
|
||||
|
||||
#endif
|
||||
|
168
httpp/httpp.c
168
httpp/httpp.c
@ -49,17 +49,17 @@
|
||||
static char *_lowercase(char *str);
|
||||
|
||||
/* for avl trees */
|
||||
static int _compare_vars(void *compare_arg, void *a, void *b);
|
||||
static int _free_vars(void *key);
|
||||
static int igloo__compare_vars(void *compare_arg, void *a, void *b);
|
||||
static int igloo__free_vars(void *key);
|
||||
|
||||
/* For avl tree manipulation */
|
||||
static void parse_query(avl_tree *tree, const char *query, size_t len);
|
||||
static const char *_httpp_get_param(avl_tree *tree, const char *name);
|
||||
static void _httpp_set_param_nocopy(avl_tree *tree, char *name, char *value, int replace);
|
||||
static void _httpp_set_param(avl_tree *tree, const char *name, const char *value);
|
||||
static http_var_t *_httpp_get_param_var(avl_tree *tree, const char *name);
|
||||
static http_var_t *igloo__httpp_get_param_var(avl_tree *tree, const char *name);
|
||||
|
||||
httpp_request_info_t httpp_request_info(httpp_request_type_e req)
|
||||
httpp_request_info_t igloo_httpp_request_info(httpp_request_type_e req)
|
||||
{
|
||||
#if 0
|
||||
#define HTTPP_REQUEST_IS_SAFE ((httpp_request_info_t)0x0001U)
|
||||
@ -116,21 +116,21 @@ httpp_request_info_t httpp_request_info(httpp_request_type_e req)
|
||||
}
|
||||
}
|
||||
|
||||
http_parser_t *httpp_create_parser(void)
|
||||
http_parser_t *igloo_httpp_create_parser(void)
|
||||
{
|
||||
http_parser_t *parser = calloc(1, sizeof(http_parser_t));
|
||||
|
||||
parser->refc = 1;
|
||||
parser->req_type = httpp_req_none;
|
||||
parser->uri = NULL;
|
||||
parser->vars = avl_tree_new(_compare_vars, NULL);
|
||||
parser->queryvars = avl_tree_new(_compare_vars, NULL);
|
||||
parser->postvars = avl_tree_new(_compare_vars, NULL);
|
||||
parser->vars = igloo_avl_tree_new(igloo__compare_vars, NULL);
|
||||
parser->queryvars = igloo_avl_tree_new(igloo__compare_vars, NULL);
|
||||
parser->postvars = igloo_avl_tree_new(igloo__compare_vars, NULL);
|
||||
|
||||
return parser;
|
||||
}
|
||||
|
||||
void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
|
||||
void igloo_httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
|
||||
{
|
||||
http_varlist_t *list;
|
||||
|
||||
@ -140,14 +140,14 @@ void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < list->var.values; i++) {
|
||||
httpp_setvar(parser, list->var.name, list->var.value[i]);
|
||||
igloo_httpp_setvar(parser, list->var.name, list->var.value[i]);
|
||||
}
|
||||
|
||||
list = list->next;
|
||||
}
|
||||
}
|
||||
|
||||
static int split_headers(char *data, unsigned long len, char **line)
|
||||
static int igloo_split_headers(char *data, unsigned long len, char **line)
|
||||
{
|
||||
/* first we count how many lines there are
|
||||
** and set up the line[] array
|
||||
@ -177,7 +177,7 @@ static int split_headers(char *data, unsigned long len, char **line)
|
||||
return lines;
|
||||
}
|
||||
|
||||
static void parse_headers(http_parser_t *parser, char **line, int lines)
|
||||
static void igloo_parse_headers(http_parser_t *parser, char **line, int lines)
|
||||
{
|
||||
int i, l;
|
||||
int whitespace, slen;
|
||||
@ -209,14 +209,14 @@ static void parse_headers(http_parser_t *parser, char **line, int lines)
|
||||
}
|
||||
|
||||
if (name != NULL && value != NULL) {
|
||||
httpp_setvar(parser, _lowercase(name), value);
|
||||
igloo_httpp_setvar(parser, _lowercase(name), value);
|
||||
name = NULL;
|
||||
value = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri)
|
||||
int igloo_httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri)
|
||||
{
|
||||
char *data;
|
||||
char *line[MAX_HEADERS];
|
||||
@ -232,7 +232,7 @@ int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned
|
||||
memcpy(data, http_data, len);
|
||||
data[len] = 0;
|
||||
|
||||
lines = split_headers(data, len, line);
|
||||
lines = igloo_split_headers(data, len, line);
|
||||
|
||||
/* In this case, the first line contains:
|
||||
* VERSION RESPONSE_CODE MESSAGE, such as HTTP/1.0 200 OK
|
||||
@ -260,25 +260,25 @@ int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned
|
||||
return 0;
|
||||
}
|
||||
|
||||
httpp_setvar(parser, HTTPP_VAR_ERROR_CODE, resp_code);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_ERROR_CODE, resp_code);
|
||||
code = atoi(resp_code);
|
||||
if(code < 200 || code >= 300) {
|
||||
httpp_setvar(parser, HTTPP_VAR_ERROR_MESSAGE, message);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_ERROR_MESSAGE, message);
|
||||
}
|
||||
|
||||
httpp_setvar(parser, HTTPP_VAR_URI, uri);
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_URI, uri);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");
|
||||
|
||||
parse_headers(parser, line, lines);
|
||||
igloo_parse_headers(parser, line, lines);
|
||||
|
||||
free(data);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int httpp_parse_postdata(http_parser_t *parser, const char *body_data, size_t len)
|
||||
int igloo_httpp_parse_postdata(http_parser_t *parser, const char *body_data, size_t len)
|
||||
{
|
||||
const char *header = httpp_getvar(parser, "content-type");
|
||||
const char *header = igloo_httpp_getvar(parser, "content-type");
|
||||
|
||||
if (strcasecmp(header, "application/x-www-form-urlencoded") != 0) {
|
||||
return -1;
|
||||
@ -301,7 +301,7 @@ static int hex(char c)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static char *url_unescape(const char *src, size_t len)
|
||||
static char *igloo_url_unescape(const char *src, size_t len)
|
||||
{
|
||||
unsigned char *decoded;
|
||||
size_t i;
|
||||
@ -374,7 +374,7 @@ static void parse_query_element(avl_tree *tree, const char *start, const char *m
|
||||
memcpy(key, start, keylen);
|
||||
key[keylen] = 0;
|
||||
|
||||
value = url_unescape(mid + 1, valuelen);
|
||||
value = igloo_url_unescape(mid + 1, valuelen);
|
||||
|
||||
_httpp_set_param_nocopy(tree, key, value, 0);
|
||||
}
|
||||
@ -404,7 +404,7 @@ static void parse_query(avl_tree *tree, const char *query, size_t len)
|
||||
parse_query_element(tree, start, mid, &(query[i]));
|
||||
}
|
||||
|
||||
int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
int igloo_httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
{
|
||||
char *data, *tmp;
|
||||
char *line[MAX_HEADERS]; /* limited to 32 lines, should be more than enough */
|
||||
@ -424,7 +424,7 @@ int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
memcpy(data, http_data, len);
|
||||
data[len] = 0;
|
||||
|
||||
lines = split_headers(data, len, line);
|
||||
lines = igloo_split_headers(data, len, line);
|
||||
|
||||
/* parse the first line special
|
||||
** the format is:
|
||||
@ -462,13 +462,13 @@ int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
}
|
||||
}
|
||||
|
||||
parser->req_type = httpp_str_to_method(req_type);
|
||||
parser->req_type = igloo_httpp_str_to_method(req_type);
|
||||
|
||||
if (uri != NULL && strlen(uri) > 0) {
|
||||
char *query;
|
||||
if((query = strchr(uri, '?')) != NULL) {
|
||||
httpp_setvar(parser, HTTPP_VAR_RAWURI, uri);
|
||||
httpp_setvar(parser, HTTPP_VAR_QUERYARGS, query);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_RAWURI, uri);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_QUERYARGS, query);
|
||||
*query = 0;
|
||||
query++;
|
||||
parse_query(parser->queryvars, query, strlen(query));
|
||||
@ -483,8 +483,8 @@ int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL)) {
|
||||
tmp[0] = '\0';
|
||||
if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0)) {
|
||||
httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
|
||||
httpp_setvar(parser, HTTPP_VAR_VERSION, &tmp[1]);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_VERSION, &tmp[1]);
|
||||
} else {
|
||||
free(data);
|
||||
return 0;
|
||||
@ -497,37 +497,37 @@ int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
if (parser->req_type != httpp_req_none && parser->req_type != httpp_req_unknown) {
|
||||
switch (parser->req_type) {
|
||||
case httpp_req_get:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
|
||||
break;
|
||||
case httpp_req_post:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
|
||||
break;
|
||||
case httpp_req_put:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PUT");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PUT");
|
||||
break;
|
||||
case httpp_req_head:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
|
||||
break;
|
||||
case httpp_req_options:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "OPTIONS");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "OPTIONS");
|
||||
break;
|
||||
case httpp_req_delete:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "DELETE");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "DELETE");
|
||||
break;
|
||||
case httpp_req_trace:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "TRACE");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "TRACE");
|
||||
break;
|
||||
case httpp_req_connect:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "CONNECT");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "CONNECT");
|
||||
break;
|
||||
case httpp_req_source:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
|
||||
break;
|
||||
case httpp_req_play:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
|
||||
break;
|
||||
case httpp_req_stats:
|
||||
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -538,20 +538,20 @@ int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
|
||||
}
|
||||
|
||||
if (parser->uri != NULL) {
|
||||
httpp_setvar(parser, HTTPP_VAR_URI, parser->uri);
|
||||
igloo_httpp_setvar(parser, HTTPP_VAR_URI, parser->uri);
|
||||
} else {
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
parse_headers(parser, line, lines);
|
||||
igloo_parse_headers(parser, line, lines);
|
||||
|
||||
free(data);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void httpp_deletevar(http_parser_t *parser, const char *name)
|
||||
void igloo_httpp_deletevar(http_parser_t *parser, const char *name)
|
||||
{
|
||||
http_var_t var;
|
||||
|
||||
@ -561,10 +561,10 @@ void httpp_deletevar(http_parser_t *parser, const char *name)
|
||||
|
||||
var.name = (char*)name;
|
||||
|
||||
avl_delete(parser->vars, (void *)&var, _free_vars);
|
||||
igloo_avl_delete(parser->vars, (void *)&var, igloo__free_vars);
|
||||
}
|
||||
|
||||
void httpp_setvar(http_parser_t *parser, const char *name, const char *value)
|
||||
void igloo_httpp_setvar(http_parser_t *parser, const char *name, const char *value)
|
||||
{
|
||||
http_var_t *var;
|
||||
|
||||
@ -584,15 +584,15 @@ void httpp_setvar(http_parser_t *parser, const char *name, const char *value)
|
||||
var->values = 1;
|
||||
var->value[0] = strdup(value);
|
||||
|
||||
if (httpp_getvar(parser, name) == NULL) {
|
||||
avl_insert(parser->vars, (void *)var);
|
||||
if (igloo_httpp_getvar(parser, name) == NULL) {
|
||||
igloo_avl_insert(parser->vars, (void *)var);
|
||||
} else {
|
||||
avl_delete(parser->vars, (void *)var, _free_vars);
|
||||
avl_insert(parser->vars, (void *)var);
|
||||
igloo_avl_delete(parser->vars, (void *)var, igloo__free_vars);
|
||||
igloo_avl_insert(parser->vars, (void *)var);
|
||||
}
|
||||
}
|
||||
|
||||
const char *httpp_getvar(http_parser_t *parser, const char *name)
|
||||
const char *igloo_httpp_getvar(http_parser_t *parser, const char *name)
|
||||
{
|
||||
http_var_t var;
|
||||
http_var_t *found;
|
||||
@ -605,7 +605,7 @@ const char *httpp_getvar(http_parser_t *parser, const char *name)
|
||||
memset(&var, 0, sizeof(var));
|
||||
var.name = (char*)name;
|
||||
|
||||
if (avl_get_by_key(parser->vars, &var, fp) == 0) {
|
||||
if (igloo_avl_get_by_key(parser->vars, &var, fp) == 0) {
|
||||
if (!found->values)
|
||||
return NULL;
|
||||
return found->value[0];
|
||||
@ -622,7 +622,7 @@ static void _httpp_set_param_nocopy(avl_tree *tree, char *name, char *value, int
|
||||
if (name == NULL || value == NULL)
|
||||
return;
|
||||
|
||||
found = _httpp_get_param_var(tree, name);
|
||||
found = igloo__httpp_get_param_var(tree, name);
|
||||
|
||||
if (replace || !found) {
|
||||
var = (http_var_t *)calloc(1, sizeof(http_var_t));
|
||||
@ -652,10 +652,10 @@ static void _httpp_set_param_nocopy(avl_tree *tree, char *name, char *value, int
|
||||
var->value[var->values++] = value;
|
||||
|
||||
if (replace && found) {
|
||||
avl_delete(tree, (void *)found, _free_vars);
|
||||
avl_insert(tree, (void *)var);
|
||||
igloo_avl_delete(tree, (void *)found, igloo__free_vars);
|
||||
igloo_avl_insert(tree, (void *)var);
|
||||
} else if (!found) {
|
||||
avl_insert(tree, (void *)var);
|
||||
igloo_avl_insert(tree, (void *)var);
|
||||
}
|
||||
}
|
||||
|
||||
@ -664,10 +664,10 @@ static void _httpp_set_param(avl_tree *tree, const char *name, const char *value
|
||||
if (name == NULL || value == NULL)
|
||||
return;
|
||||
|
||||
_httpp_set_param_nocopy(tree, strdup(name), url_unescape(value, strlen(value)), 1);
|
||||
_httpp_set_param_nocopy(tree, strdup(name), igloo_url_unescape(value, strlen(value)), 1);
|
||||
}
|
||||
|
||||
static http_var_t *_httpp_get_param_var(avl_tree *tree, const char *name)
|
||||
static http_var_t *igloo__httpp_get_param_var(avl_tree *tree, const char *name)
|
||||
{
|
||||
http_var_t var;
|
||||
http_var_t *found;
|
||||
@ -677,14 +677,14 @@ static http_var_t *_httpp_get_param_var(avl_tree *tree, const char *name)
|
||||
memset(&var, 0, sizeof(var));
|
||||
var.name = (char *)name;
|
||||
|
||||
if (avl_get_by_key(tree, (void *)&var, fp) == 0)
|
||||
if (igloo_avl_get_by_key(tree, (void *)&var, fp) == 0)
|
||||
return found;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
static const char *_httpp_get_param(avl_tree *tree, const char *name)
|
||||
{
|
||||
http_var_t *res = _httpp_get_param_var(tree, name);
|
||||
http_var_t *res = igloo__httpp_get_param_var(tree, name);
|
||||
|
||||
if (!res)
|
||||
return NULL;
|
||||
@ -695,37 +695,37 @@ static const char *_httpp_get_param(avl_tree *tree, const char *name)
|
||||
return res->value[0];
|
||||
}
|
||||
|
||||
void httpp_set_query_param(http_parser_t *parser, const char *name, const char *value)
|
||||
void igloo_httpp_set_query_param(http_parser_t *parser, const char *name, const char *value)
|
||||
{
|
||||
return _httpp_set_param(parser->queryvars, name, value);
|
||||
}
|
||||
|
||||
const char *httpp_get_query_param(http_parser_t *parser, const char *name)
|
||||
const char *igloo_httpp_get_query_param(http_parser_t *parser, const char *name)
|
||||
{
|
||||
return _httpp_get_param(parser->queryvars, name);
|
||||
}
|
||||
|
||||
void httpp_set_post_param(http_parser_t *parser, const char *name, const char *value)
|
||||
void igloo_httpp_set_post_param(http_parser_t *parser, const char *name, const char *value)
|
||||
{
|
||||
return _httpp_set_param(parser->postvars, name, value);
|
||||
}
|
||||
|
||||
const char *httpp_get_post_param(http_parser_t *parser, const char *name)
|
||||
const char *igloo_httpp_get_post_param(http_parser_t *parser, const char *name)
|
||||
{
|
||||
return _httpp_get_param(parser->postvars, name);
|
||||
}
|
||||
|
||||
const http_var_t *httpp_get_param_var(http_parser_t *parser, const char *name)
|
||||
const http_var_t *igloo_httpp_get_param_var(http_parser_t *parser, const char *name)
|
||||
{
|
||||
http_var_t *ret = _httpp_get_param_var(parser->postvars, name);
|
||||
http_var_t *ret = igloo__httpp_get_param_var(parser->postvars, name);
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
return _httpp_get_param_var(parser->queryvars, name);
|
||||
return igloo__httpp_get_param_var(parser->queryvars, name);
|
||||
}
|
||||
|
||||
const http_var_t *httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const char *name)
|
||||
const http_var_t *igloo_httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const char *name)
|
||||
{
|
||||
avl_tree *tree = NULL;
|
||||
|
||||
@ -754,10 +754,10 @@ const http_var_t *httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const
|
||||
if (!tree)
|
||||
return NULL;
|
||||
|
||||
return _httpp_get_param_var(tree, name);
|
||||
return igloo__httpp_get_param_var(tree, name);
|
||||
}
|
||||
|
||||
char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
char ** igloo_httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
{
|
||||
avl_tree *tree = NULL;
|
||||
avl_node *avlnode;
|
||||
@ -790,7 +790,7 @@ char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
|
||||
len = 8;
|
||||
|
||||
for (avlnode = avl_get_first(tree); avlnode; avlnode = avl_get_next(avlnode)) {
|
||||
for (avlnode = igloo_avl_get_first(tree); avlnode; avlnode = igloo_avl_get_next(avlnode)) {
|
||||
http_var_t *var = avlnode->key;
|
||||
|
||||
if (ns == HTTPP_NS_VAR) {
|
||||
@ -806,7 +806,7 @@ char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
if (pos == (len-1)) {
|
||||
char **n = realloc(ret, sizeof(*ret)*(len + 8));
|
||||
if (!n) {
|
||||
httpp_free_any_key(ret);
|
||||
igloo_httpp_free_any_key(ret);
|
||||
return NULL;
|
||||
}
|
||||
memset(n + len, 0, sizeof(*n)*8);
|
||||
@ -816,7 +816,7 @@ char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
|
||||
ret[pos] = strdup(var->name);
|
||||
if (!ret[pos]) {
|
||||
httpp_free_any_key(ret);
|
||||
igloo_httpp_free_any_key(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -826,7 +826,7 @@ char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void httpp_free_any_key(char **keys)
|
||||
void igloo_httpp_free_any_key(char **keys)
|
||||
{
|
||||
char **p;
|
||||
|
||||
@ -839,7 +839,7 @@ void httpp_free_any_key(char **keys)
|
||||
free(keys);
|
||||
}
|
||||
|
||||
const char *httpp_get_param(http_parser_t *parser, const char *name)
|
||||
const char *igloo_httpp_get_param(http_parser_t *parser, const char *name)
|
||||
{
|
||||
const char *ret = _httpp_get_param(parser->postvars, name);
|
||||
|
||||
@ -855,13 +855,13 @@ static void httpp_clear(http_parser_t *parser)
|
||||
if (parser->uri)
|
||||
free(parser->uri);
|
||||
parser->uri = NULL;
|
||||
avl_tree_free(parser->vars, _free_vars);
|
||||
avl_tree_free(parser->queryvars, _free_vars);
|
||||
avl_tree_free(parser->postvars, _free_vars);
|
||||
igloo_avl_tree_free(parser->vars, igloo__free_vars);
|
||||
igloo_avl_tree_free(parser->queryvars, igloo__free_vars);
|
||||
igloo_avl_tree_free(parser->postvars, igloo__free_vars);
|
||||
parser->vars = NULL;
|
||||
}
|
||||
|
||||
int httpp_addref(http_parser_t *parser)
|
||||
int igloo_httpp_addref(http_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return -1;
|
||||
@ -871,7 +871,7 @@ int httpp_addref(http_parser_t *parser)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int httpp_release(http_parser_t *parser)
|
||||
int igloo_httpp_release(http_parser_t *parser)
|
||||
{
|
||||
if (!parser)
|
||||
return -1;
|
||||
@ -895,7 +895,7 @@ static char *_lowercase(char *str)
|
||||
return str;
|
||||
}
|
||||
|
||||
static int _compare_vars(void *compare_arg, void *a, void *b)
|
||||
static int igloo__compare_vars(void *compare_arg, void *a, void *b)
|
||||
{
|
||||
http_var_t *vara, *varb;
|
||||
|
||||
@ -905,7 +905,7 @@ static int _compare_vars(void *compare_arg, void *a, void *b)
|
||||
return strcmp(vara->name, varb->name);
|
||||
}
|
||||
|
||||
static int _free_vars(void *key)
|
||||
static int igloo__free_vars(void *key)
|
||||
{
|
||||
http_var_t *var = (http_var_t *)key;
|
||||
size_t i;
|
||||
@ -921,7 +921,7 @@ static int _free_vars(void *key)
|
||||
return 1;
|
||||
}
|
||||
|
||||
httpp_request_type_e httpp_str_to_method(const char * method) {
|
||||
httpp_request_type_e igloo_httpp_str_to_method(const char * method) {
|
||||
if (strcasecmp("GET", method) == 0) {
|
||||
return httpp_req_get;
|
||||
} else if (strcasecmp("POST", method) == 0) {
|
||||
|
@ -97,31 +97,31 @@ typedef struct http_parser_tag {
|
||||
avl_tree *postvars;
|
||||
} http_parser_t;
|
||||
|
||||
httpp_request_info_t httpp_request_info(httpp_request_type_e req);
|
||||
httpp_request_info_t igloo_httpp_request_info(httpp_request_type_e req);
|
||||
|
||||
http_parser_t *httpp_create_parser(void);
|
||||
void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults);
|
||||
int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len);
|
||||
http_parser_t *igloo_httpp_create_parser(void);
|
||||
void igloo_httpp_initialize(http_parser_t *parser, http_varlist_t *defaults);
|
||||
int igloo_httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len);
|
||||
int httpp_parse_icy(http_parser_t *parser, const char *http_data, unsigned long len);
|
||||
int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri);
|
||||
int httpp_parse_postdata(http_parser_t *parser, const char *body_data, size_t len);
|
||||
void httpp_setvar(http_parser_t *parser, const char *name, const char *value);
|
||||
void httpp_deletevar(http_parser_t *parser, const char *name);
|
||||
const char *httpp_getvar(http_parser_t *parser, const char *name);
|
||||
void httpp_set_query_param(http_parser_t *parser, const char *name, const char *value);
|
||||
const char *httpp_get_query_param(http_parser_t *parser, const char *name);
|
||||
void httpp_set_post_param(http_parser_t *parser, const char *name, const char *value);
|
||||
const char *httpp_get_post_param(http_parser_t *parser, const char *name);
|
||||
const char *httpp_get_param(http_parser_t *parser, const char *name);
|
||||
const http_var_t *httpp_get_param_var(http_parser_t *parser, const char *name);
|
||||
const http_var_t *httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const char *name);
|
||||
char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns);
|
||||
void httpp_free_any_key(char **keys);
|
||||
int httpp_addref(http_parser_t *parser);
|
||||
int httpp_release(http_parser_t *parser);
|
||||
#define httpp_destroy(x) httpp_release((x))
|
||||
int igloo_httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri);
|
||||
int igloo_httpp_parse_postdata(http_parser_t *parser, const char *body_data, size_t len);
|
||||
void igloo_httpp_setvar(http_parser_t *parser, const char *name, const char *value);
|
||||
void igloo_httpp_deletevar(http_parser_t *parser, const char *name);
|
||||
const char *igloo_httpp_getvar(http_parser_t *parser, const char *name);
|
||||
void igloo_httpp_set_query_param(http_parser_t *parser, const char *name, const char *value);
|
||||
const char *igloo_httpp_get_query_param(http_parser_t *parser, const char *name);
|
||||
void igloo_httpp_set_post_param(http_parser_t *parser, const char *name, const char *value);
|
||||
const char *igloo_httpp_get_post_param(http_parser_t *parser, const char *name);
|
||||
const char *igloo_httpp_get_param(http_parser_t *parser, const char *name);
|
||||
const http_var_t *igloo_httpp_get_param_var(http_parser_t *parser, const char *name);
|
||||
const http_var_t *igloo_httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const char *name);
|
||||
char ** igloo_httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns);
|
||||
void igloo_httpp_free_any_key(char **keys);
|
||||
int igloo_httpp_addref(http_parser_t *parser);
|
||||
int igloo_httpp_release(http_parser_t *parser);
|
||||
#define httpp_destroy(x) igloo_httpp_release((x))
|
||||
|
||||
/* util functions */
|
||||
httpp_request_type_e httpp_str_to_method(const char * method);
|
||||
httpp_request_type_e igloo_httpp_str_to_method(const char * method);
|
||||
|
||||
#endif
|
||||
|
@ -12,10 +12,10 @@ int main(int argc, char **argv)
|
||||
avl_node *node;
|
||||
http_var_t *var;
|
||||
|
||||
httpp_initialize(&parser, NULL);
|
||||
igloo_httpp_initialize(&parser, NULL);
|
||||
|
||||
readed = fread(buff, 1, 8192, stdin);
|
||||
if (httpp_parse(&parser, buff, readed)) {
|
||||
if (igloo_httpp_parse(&parser, buff, readed)) {
|
||||
printf("Parse succeeded...\n\n");
|
||||
printf("Request was ");
|
||||
switch (parser.req_type) {
|
||||
@ -37,14 +37,14 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Version was 1.%d\n", parser.version);
|
||||
|
||||
node = avl_get_first(parser.vars);
|
||||
node = igloo_avl_get_first(parser.vars);
|
||||
while (node) {
|
||||
var = (http_var_t *)node->key;
|
||||
|
||||
if (var)
|
||||
printf("Iterating variable(s): %s = %s\n", var->name, var->value);
|
||||
|
||||
node = avl_get_next(node);
|
||||
node = igloo_avl_get_next(node);
|
||||
}
|
||||
} else {
|
||||
printf("Parse failed...\n");
|
||||
|
260
log/log.c
260
log/log.c
@ -64,8 +64,8 @@
|
||||
#define mutex_t pthread_mutex_t
|
||||
#endif
|
||||
|
||||
static mutex_t _logger_mutex;
|
||||
static int _initialized = 0;
|
||||
static mutex_t igloo__logger_mutex;
|
||||
static int igloo__initialized = 0;
|
||||
|
||||
typedef struct _log_entry_t
|
||||
{
|
||||
@ -96,7 +96,7 @@ typedef struct log_tag
|
||||
char *buffer;
|
||||
} log_t;
|
||||
|
||||
static log_t loglist[LOG_MAXLOGS];
|
||||
static log_t igloo_loglist[LOG_MAXLOGS];
|
||||
|
||||
static int _get_log_id(void);
|
||||
static void _lock_logger(void);
|
||||
@ -105,87 +105,87 @@ static void _unlock_logger(void);
|
||||
|
||||
static int _log_open (int id)
|
||||
{
|
||||
if (loglist [id] . in_use == 0)
|
||||
if (igloo_loglist [id] . in_use == 0)
|
||||
return 0;
|
||||
|
||||
/* check for cases where an open of the logfile is wanted */
|
||||
if (loglist [id] . logfile == NULL ||
|
||||
(loglist [id] . trigger_level && loglist [id] . size > loglist [id] . trigger_level))
|
||||
if (igloo_loglist [id] . logfile == NULL ||
|
||||
(igloo_loglist [id] . trigger_level && igloo_loglist [id] . size > igloo_loglist [id] . trigger_level))
|
||||
{
|
||||
if (loglist [id] . filename) /* only re-open files where we have a name */
|
||||
if (igloo_loglist [id] . filename) /* only re-open files where we have a name */
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (loglist [id] . logfile)
|
||||
if (igloo_loglist [id] . logfile)
|
||||
{
|
||||
char new_name [4096];
|
||||
fclose (loglist [id] . logfile);
|
||||
loglist [id] . logfile = NULL;
|
||||
fclose (igloo_loglist [id] . logfile);
|
||||
igloo_loglist [id] . logfile = NULL;
|
||||
/* simple rename, but could use time providing locking were used */
|
||||
if (loglist[id].archive_timestamp)
|
||||
if (igloo_loglist[id].archive_timestamp)
|
||||
{
|
||||
char timestamp [128];
|
||||
time_t now = time(NULL);
|
||||
|
||||
strftime (timestamp, sizeof (timestamp), "%Y%m%d_%H%M%S", localtime (&now));
|
||||
snprintf (new_name, sizeof(new_name), "%s.%s", loglist[id].filename, timestamp);
|
||||
snprintf (new_name, sizeof(new_name), "%s.%s", igloo_loglist[id].filename, timestamp);
|
||||
}
|
||||
else {
|
||||
snprintf (new_name, sizeof(new_name), "%s.old", loglist [id] . filename);
|
||||
snprintf (new_name, sizeof(new_name), "%s.old", igloo_loglist [id] . filename);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
if (stat (new_name, &st) == 0)
|
||||
remove (new_name);
|
||||
#endif
|
||||
rename (loglist [id] . filename, new_name);
|
||||
rename (igloo_loglist [id] . filename, new_name);
|
||||
}
|
||||
loglist [id] . logfile = fopen (loglist [id] . filename, "a");
|
||||
if (loglist [id] . logfile == NULL)
|
||||
igloo_loglist [id] . logfile = fopen (igloo_loglist [id] . filename, "a");
|
||||
if (igloo_loglist [id] . logfile == NULL)
|
||||
return 0;
|
||||
setvbuf (loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
||||
if (stat (loglist [id] . filename, &st) < 0)
|
||||
loglist [id] . size = 0;
|
||||
setvbuf (igloo_loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
||||
if (stat (igloo_loglist [id] . filename, &st) < 0)
|
||||
igloo_loglist [id] . size = 0;
|
||||
else
|
||||
loglist [id] . size = st.st_size;
|
||||
igloo_loglist [id] . size = st.st_size;
|
||||
}
|
||||
else
|
||||
loglist [id] . size = 0;
|
||||
igloo_loglist [id] . size = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void log_initialize(void)
|
||||
void igloo_log_initialize(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (_initialized) return;
|
||||
if (igloo__initialized) return;
|
||||
|
||||
for (i = 0; i < LOG_MAXLOGS; i++) {
|
||||
loglist[i].in_use = 0;
|
||||
loglist[i].level = 2;
|
||||
loglist[i].size = 0;
|
||||
loglist[i].trigger_level = 1000000000;
|
||||
loglist[i].filename = NULL;
|
||||
loglist[i].logfile = NULL;
|
||||
loglist[i].buffer = NULL;
|
||||
loglist[i].total = 0;
|
||||
loglist[i].entries = 0;
|
||||
loglist[i].keep_entries = 0;
|
||||
loglist[i].log_head = NULL;
|
||||
loglist[i].log_tail = &loglist[i].log_head;
|
||||
igloo_loglist[i].in_use = 0;
|
||||
igloo_loglist[i].level = 2;
|
||||
igloo_loglist[i].size = 0;
|
||||
igloo_loglist[i].trigger_level = 1000000000;
|
||||
igloo_loglist[i].filename = NULL;
|
||||
igloo_loglist[i].logfile = NULL;
|
||||
igloo_loglist[i].buffer = NULL;
|
||||
igloo_loglist[i].total = 0;
|
||||
igloo_loglist[i].entries = 0;
|
||||
igloo_loglist[i].keep_entries = 0;
|
||||
igloo_loglist[i].log_head = NULL;
|
||||
igloo_loglist[i].log_tail = &igloo_loglist[i].log_head;
|
||||
}
|
||||
|
||||
/* initialize mutexes */
|
||||
#ifndef _WIN32
|
||||
pthread_mutex_init(&_logger_mutex, NULL);
|
||||
pthread_mutex_init(&igloo__logger_mutex, NULL);
|
||||
#else
|
||||
InitializeCriticalSection(&_logger_mutex);
|
||||
InitializeCriticalSection(&igloo__logger_mutex);
|
||||
#endif
|
||||
|
||||
_initialized = 1;
|
||||
igloo__initialized = 1;
|
||||
}
|
||||
|
||||
int log_open_file(FILE *file)
|
||||
int igloo_log_open_file(FILE *file)
|
||||
{
|
||||
int log_id;
|
||||
|
||||
@ -194,15 +194,15 @@ int log_open_file(FILE *file)
|
||||
log_id = _get_log_id();
|
||||
if (log_id < 0) return LOG_ENOMORELOGS;
|
||||
|
||||
loglist[log_id].logfile = file;
|
||||
loglist[log_id].filename = NULL;
|
||||
loglist[log_id].size = 0;
|
||||
igloo_loglist[log_id].logfile = file;
|
||||
igloo_loglist[log_id].filename = NULL;
|
||||
igloo_loglist[log_id].size = 0;
|
||||
|
||||
return log_id;
|
||||
}
|
||||
|
||||
|
||||
int log_open(const char *filename)
|
||||
int igloo_log_open(const char *filename)
|
||||
{
|
||||
int id;
|
||||
FILE *file;
|
||||
@ -212,19 +212,19 @@ int log_open(const char *filename)
|
||||
|
||||
file = fopen(filename, "a");
|
||||
|
||||
id = log_open_file(file);
|
||||
id = igloo_log_open_file(file);
|
||||
|
||||
if (id >= 0)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
setvbuf (loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
||||
loglist [id] . filename = strdup (filename);
|
||||
if (stat (loglist [id] . filename, &st) == 0)
|
||||
loglist [id] . size = st.st_size;
|
||||
loglist [id] . entries = 0;
|
||||
loglist [id] . log_head = NULL;
|
||||
loglist [id] . log_tail = &loglist [id] . log_head;
|
||||
setvbuf (igloo_loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
||||
igloo_loglist [id] . filename = strdup (filename);
|
||||
if (stat (igloo_loglist [id] . filename, &st) == 0)
|
||||
igloo_loglist [id] . size = st.st_size;
|
||||
igloo_loglist [id] . entries = 0;
|
||||
igloo_loglist [id] . log_head = NULL;
|
||||
igloo_loglist [id] . log_tail = &igloo_loglist [id] . log_head;
|
||||
}
|
||||
|
||||
return id;
|
||||
@ -232,184 +232,184 @@ int log_open(const char *filename)
|
||||
|
||||
|
||||
/* set the trigger level to trigger, represented in kilobytes */
|
||||
void log_set_trigger(int id, unsigned trigger)
|
||||
void igloo_log_set_trigger(int id, unsigned trigger)
|
||||
{
|
||||
if (id >= 0 && id < LOG_MAXLOGS && loglist [id] . in_use)
|
||||
if (id >= 0 && id < LOG_MAXLOGS && igloo_loglist [id] . in_use)
|
||||
{
|
||||
loglist [id] . trigger_level = trigger*1024;
|
||||
igloo_loglist [id] . trigger_level = trigger*1024;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int log_set_filename(int id, const char *filename)
|
||||
int igloo_log_set_filename(int id, const char *filename)
|
||||
{
|
||||
if (id < 0 || id >= LOG_MAXLOGS)
|
||||
return LOG_EINSANE;
|
||||
/* NULL filename is ok, empty filename is not. */
|
||||
if ((filename && !strcmp(filename, "")) || loglist [id] . in_use == 0)
|
||||
if ((filename && !strcmp(filename, "")) || igloo_loglist [id] . in_use == 0)
|
||||
return LOG_EINSANE;
|
||||
_lock_logger();
|
||||
if (loglist [id] . filename)
|
||||
free (loglist [id] . filename);
|
||||
if (igloo_loglist [id] . filename)
|
||||
free (igloo_loglist [id] . filename);
|
||||
if (filename)
|
||||
loglist [id] . filename = strdup (filename);
|
||||
igloo_loglist [id] . filename = strdup (filename);
|
||||
else
|
||||
loglist [id] . filename = NULL;
|
||||
igloo_loglist [id] . filename = NULL;
|
||||
_unlock_logger();
|
||||
return id;
|
||||
}
|
||||
|
||||
int log_set_archive_timestamp(int id, int value)
|
||||
int igloo_log_set_archive_timestamp(int id, int value)
|
||||
{
|
||||
if (id < 0 || id >= LOG_MAXLOGS)
|
||||
return LOG_EINSANE;
|
||||
_lock_logger();
|
||||
loglist[id].archive_timestamp = value;
|
||||
igloo_loglist[id].archive_timestamp = value;
|
||||
_unlock_logger();
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
int log_open_with_buffer(const char *filename, int size)
|
||||
int igloo_log_open_with_buffer(const char *filename, int size)
|
||||
{
|
||||
/* not implemented */
|
||||
return LOG_ENOTIMPL;
|
||||
}
|
||||
|
||||
|
||||
void log_set_lines_kept (int log_id, unsigned int count)
|
||||
void igloo_log_set_lines_kept (int log_id, unsigned int count)
|
||||
{
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
||||
if (loglist[log_id].in_use == 0) return;
|
||||
if (igloo_loglist[log_id].in_use == 0) return;
|
||||
|
||||
_lock_logger ();
|
||||
loglist[log_id].keep_entries = count;
|
||||
while (loglist[log_id].entries > count)
|
||||
igloo_loglist[log_id].keep_entries = count;
|
||||
while (igloo_loglist[log_id].entries > count)
|
||||
{
|
||||
log_entry_t *to_go = loglist [log_id].log_head;
|
||||
loglist [log_id].log_head = to_go->next;
|
||||
loglist [log_id].total -= to_go->len;
|
||||
log_entry_t *to_go = igloo_loglist [log_id].log_head;
|
||||
igloo_loglist [log_id].log_head = to_go->next;
|
||||
igloo_loglist [log_id].total -= to_go->len;
|
||||
free (to_go->line);
|
||||
free (to_go);
|
||||
loglist [log_id].entries--;
|
||||
igloo_loglist [log_id].entries--;
|
||||
}
|
||||
_unlock_logger ();
|
||||
}
|
||||
|
||||
|
||||
void log_set_level(int log_id, unsigned level)
|
||||
void igloo_log_set_level(int log_id, unsigned level)
|
||||
{
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
||||
if (loglist[log_id].in_use == 0) return;
|
||||
if (igloo_loglist[log_id].in_use == 0) return;
|
||||
|
||||
loglist[log_id].level = level;
|
||||
igloo_loglist[log_id].level = level;
|
||||
}
|
||||
|
||||
void log_flush(int log_id)
|
||||
void igloo_log_flush(int log_id)
|
||||
{
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
||||
if (loglist[log_id].in_use == 0) return;
|
||||
if (igloo_loglist[log_id].in_use == 0) return;
|
||||
|
||||
_lock_logger();
|
||||
if (loglist[log_id].logfile)
|
||||
fflush(loglist[log_id].logfile);
|
||||
if (igloo_loglist[log_id].logfile)
|
||||
fflush(igloo_loglist[log_id].logfile);
|
||||
_unlock_logger();
|
||||
}
|
||||
|
||||
void log_reopen(int log_id)
|
||||
void igloo_log_reopen(int log_id)
|
||||
{
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS)
|
||||
return;
|
||||
if (loglist [log_id] . filename && loglist [log_id] . logfile)
|
||||
if (igloo_loglist [log_id] . filename && igloo_loglist [log_id] . logfile)
|
||||
{
|
||||
_lock_logger();
|
||||
|
||||
fclose (loglist [log_id] . logfile);
|
||||
loglist [log_id] . logfile = NULL;
|
||||
fclose (igloo_loglist [log_id] . logfile);
|
||||
igloo_loglist [log_id] . logfile = NULL;
|
||||
|
||||
_unlock_logger();
|
||||
}
|
||||
}
|
||||
|
||||
void log_close(int log_id)
|
||||
void igloo_log_close(int log_id)
|
||||
{
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
||||
|
||||
_lock_logger();
|
||||
|
||||
if (loglist[log_id].in_use == 0)
|
||||
if (igloo_loglist[log_id].in_use == 0)
|
||||
{
|
||||
_unlock_logger();
|
||||
return;
|
||||
}
|
||||
|
||||
loglist[log_id].in_use = 0;
|
||||
loglist[log_id].level = 2;
|
||||
if (loglist[log_id].filename) free(loglist[log_id].filename);
|
||||
if (loglist[log_id].buffer) free(loglist[log_id].buffer);
|
||||
igloo_loglist[log_id].in_use = 0;
|
||||
igloo_loglist[log_id].level = 2;
|
||||
if (igloo_loglist[log_id].filename) free(igloo_loglist[log_id].filename);
|
||||
if (igloo_loglist[log_id].buffer) free(igloo_loglist[log_id].buffer);
|
||||
|
||||
if (loglist [log_id] . logfile)
|
||||
if (igloo_loglist [log_id] . logfile)
|
||||
{
|
||||
fclose (loglist [log_id] . logfile);
|
||||
loglist [log_id] . logfile = NULL;
|
||||
fclose (igloo_loglist [log_id] . logfile);
|
||||
igloo_loglist [log_id] . logfile = NULL;
|
||||
}
|
||||
while (loglist[log_id].entries)
|
||||
while (igloo_loglist[log_id].entries)
|
||||
{
|
||||
log_entry_t *to_go = loglist [log_id].log_head;
|
||||
loglist [log_id].log_head = to_go->next;
|
||||
loglist [log_id].total -= to_go->len;
|
||||
log_entry_t *to_go = igloo_loglist [log_id].log_head;
|
||||
igloo_loglist [log_id].log_head = to_go->next;
|
||||
igloo_loglist [log_id].total -= to_go->len;
|
||||
free (to_go->line);
|
||||
free (to_go);
|
||||
loglist [log_id].entries--;
|
||||
igloo_loglist [log_id].entries--;
|
||||
}
|
||||
_unlock_logger();
|
||||
}
|
||||
|
||||
void log_shutdown(void)
|
||||
void igloo_log_shutdown(void)
|
||||
{
|
||||
/* destroy mutexes */
|
||||
#ifndef _WIN32
|
||||
pthread_mutex_destroy(&_logger_mutex);
|
||||
pthread_mutex_destroy(&igloo__logger_mutex);
|
||||
#else
|
||||
DeleteCriticalSection(&_logger_mutex);
|
||||
DeleteCriticalSection(&igloo__logger_mutex);
|
||||
#endif
|
||||
|
||||
_initialized = 0;
|
||||
igloo__initialized = 0;
|
||||
}
|
||||
|
||||
|
||||
static int create_log_entry (int log_id, const char *pre, const char *line)
|
||||
static int igloo_create_log_entry (int log_id, const char *pre, const char *line)
|
||||
{
|
||||
log_entry_t *entry;
|
||||
|
||||
if (loglist[log_id].keep_entries == 0)
|
||||
return fprintf (loglist[log_id].logfile, "%s%s\n", pre, line);
|
||||
if (igloo_loglist[log_id].keep_entries == 0)
|
||||
return fprintf (igloo_loglist[log_id].logfile, "%s%s\n", pre, line);
|
||||
|
||||
entry = calloc (1, sizeof (log_entry_t));
|
||||
entry->len = strlen (pre) + strlen (line) + 2;
|
||||
entry->line = malloc (entry->len);
|
||||
snprintf (entry->line, entry->len, "%s%s\n", pre, line);
|
||||
loglist [log_id].total += entry->len;
|
||||
fprintf (loglist[log_id].logfile, "%s", entry->line);
|
||||
igloo_loglist [log_id].total += entry->len;
|
||||
fprintf (igloo_loglist[log_id].logfile, "%s", entry->line);
|
||||
|
||||
*loglist [log_id].log_tail = entry;
|
||||
loglist [log_id].log_tail = &entry->next;
|
||||
*igloo_loglist [log_id].log_tail = entry;
|
||||
igloo_loglist [log_id].log_tail = &entry->next;
|
||||
|
||||
if (loglist [log_id].entries >= loglist [log_id].keep_entries)
|
||||
if (igloo_loglist [log_id].entries >= igloo_loglist [log_id].keep_entries)
|
||||
{
|
||||
log_entry_t *to_go = loglist [log_id].log_head;
|
||||
loglist [log_id].log_head = to_go->next;
|
||||
loglist [log_id].total -= to_go->len;
|
||||
log_entry_t *to_go = igloo_loglist [log_id].log_head;
|
||||
igloo_loglist [log_id].log_head = to_go->next;
|
||||
igloo_loglist [log_id].total -= to_go->len;
|
||||
free (to_go->line);
|
||||
free (to_go);
|
||||
}
|
||||
else
|
||||
loglist [log_id].entries++;
|
||||
igloo_loglist [log_id].entries++;
|
||||
return entry->len;
|
||||
}
|
||||
|
||||
|
||||
void log_contents (int log_id, char **_contents, unsigned int *_len)
|
||||
void igloo_log_contents (int log_id, char **_contents, unsigned int *_len)
|
||||
{
|
||||
int remain;
|
||||
log_entry_t *entry;
|
||||
@ -419,12 +419,12 @@ void log_contents (int log_id, char **_contents, unsigned int *_len)
|
||||
if (log_id >= LOG_MAXLOGS) return; /* Bad log number */
|
||||
|
||||
_lock_logger ();
|
||||
remain = loglist [log_id].total + 1;
|
||||
remain = igloo_loglist [log_id].total + 1;
|
||||
*_contents = malloc (remain);
|
||||
**_contents= '\0';
|
||||
*_len = loglist [log_id].total;
|
||||
*_len = igloo_loglist [log_id].total;
|
||||
|
||||
entry = loglist [log_id].log_head;
|
||||
entry = igloo_loglist [log_id].log_head;
|
||||
ptr = *_contents;
|
||||
while (entry)
|
||||
{
|
||||
@ -653,7 +653,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
*str = 0;
|
||||
}
|
||||
|
||||
void log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
void igloo_log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
static const char *prior[] = { "EROR", "WARN", "INFO", "DBUG" };
|
||||
@ -664,7 +664,7 @@ void log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
va_list ap;
|
||||
|
||||
if (log_id < 0 || log_id >= LOG_MAXLOGS) return; /* Bad log number */
|
||||
if (loglist[log_id].level < priority) return;
|
||||
if (igloo_loglist[log_id].level < priority) return;
|
||||
if (!priority || priority > sizeof(prior)/sizeof(prior[0])) return; /* Bad priority */
|
||||
|
||||
|
||||
@ -679,14 +679,14 @@ void log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
_lock_logger();
|
||||
if (_log_open (log_id))
|
||||
{
|
||||
int len = create_log_entry (log_id, pre, line);
|
||||
int len = igloo_create_log_entry (log_id, pre, line);
|
||||
if (len > 0)
|
||||
loglist[log_id].size += len;
|
||||
igloo_loglist[log_id].size += len;
|
||||
}
|
||||
_unlock_logger();
|
||||
}
|
||||
|
||||
void log_write_direct(int log_id, const char *fmt, ...)
|
||||
void igloo_log_write_direct(int log_id, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char line[LOG_MAXLINELEN];
|
||||
@ -699,15 +699,15 @@ void log_write_direct(int log_id, const char *fmt, ...)
|
||||
__vsnprintf(line, LOG_MAXLINELEN, fmt, ap);
|
||||
if (_log_open (log_id))
|
||||
{
|
||||
int len = create_log_entry (log_id, "", line);
|
||||
int len = igloo_create_log_entry (log_id, "", line);
|
||||
if (len > 0)
|
||||
loglist[log_id].size += len;
|
||||
igloo_loglist[log_id].size += len;
|
||||
}
|
||||
_unlock_logger();
|
||||
|
||||
va_end(ap);
|
||||
|
||||
fflush(loglist[log_id].logfile);
|
||||
fflush(igloo_loglist[log_id].logfile);
|
||||
}
|
||||
|
||||
static int _get_log_id(void)
|
||||
@ -719,8 +719,8 @@ static int _get_log_id(void)
|
||||
_lock_logger();
|
||||
|
||||
for (i = 0; i < LOG_MAXLOGS; i++)
|
||||
if (loglist[i].in_use == 0) {
|
||||
loglist[i].in_use = 1;
|
||||
if (igloo_loglist[i].in_use == 0) {
|
||||
igloo_loglist[i].in_use = 1;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
@ -734,17 +734,17 @@ static int _get_log_id(void)
|
||||
static void _lock_logger(void)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
pthread_mutex_lock(&_logger_mutex);
|
||||
pthread_mutex_lock(&igloo__logger_mutex);
|
||||
#else
|
||||
EnterCriticalSection(&_logger_mutex);
|
||||
EnterCriticalSection(&igloo__logger_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void _unlock_logger(void)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
pthread_mutex_unlock(&_logger_mutex);
|
||||
pthread_mutex_unlock(&igloo__logger_mutex);
|
||||
#else
|
||||
LeaveCriticalSection(&_logger_mutex);
|
||||
LeaveCriticalSection(&igloo__logger_mutex);
|
||||
#endif
|
||||
}
|
||||
|
32
log/log.h
32
log/log.h
@ -43,23 +43,23 @@
|
||||
#define IO_BUFFER_TYPE _IOLBF
|
||||
#endif
|
||||
|
||||
void log_initialize(void);
|
||||
int log_open_file(FILE *file);
|
||||
int log_open(const char *filename);
|
||||
int log_open_with_buffer(const char *filename, int size);
|
||||
void log_set_level(int log_id, unsigned level);
|
||||
void log_set_trigger(int id, unsigned trigger);
|
||||
int log_set_filename(int id, const char *filename);
|
||||
void log_set_lines_kept (int log_id, unsigned int count);
|
||||
void log_contents (int log_id, char **_contents, unsigned int *_len);
|
||||
int log_set_archive_timestamp(int id, int value);
|
||||
void log_flush(int log_id);
|
||||
void log_reopen(int log_id);
|
||||
void log_close(int log_id);
|
||||
void log_shutdown(void);
|
||||
void igloo_log_initialize(void);
|
||||
int igloo_log_open_file(FILE *file);
|
||||
int igloo_log_open(const char *filename);
|
||||
int igloo_log_open_with_buffer(const char *filename, int size);
|
||||
void igloo_log_set_level(int log_id, unsigned level);
|
||||
void igloo_log_set_trigger(int id, unsigned trigger);
|
||||
int igloo_log_set_filename(int id, const char *filename);
|
||||
void igloo_log_set_lines_kept (int log_id, unsigned int count);
|
||||
void igloo_log_contents (int log_id, char **_contents, unsigned int *_len);
|
||||
int igloo_log_set_archive_timestamp(int id, int value);
|
||||
void igloo_log_flush(int log_id);
|
||||
void igloo_log_reopen(int log_id);
|
||||
void igloo_log_close(int log_id);
|
||||
void igloo_log_shutdown(void);
|
||||
|
||||
void log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
void igloo_log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
||||
const char *fmt, ...);
|
||||
void log_write_direct(int log_id, const char *fmt, ...);
|
||||
void igloo_log_write_direct(int log_id, const char *fmt, ...);
|
||||
|
||||
#endif /* __LOG_H__ */
|
||||
|
10
log/test.c
10
log/test.c
@ -2,20 +2,20 @@
|
||||
|
||||
#define CATMODULE "test"
|
||||
|
||||
#define LOG_ERR(x, y, z...) log_write(x, 1, CATMODULE "/" __FUNCTION__, y, ##z)
|
||||
#define LOG_ERR(x, y, z...) igloo_log_write(x, 1, CATMODULE "/" __FUNCTION__, y, ##z)
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int lid;
|
||||
|
||||
log_initialize();
|
||||
igloo_log_initialize();
|
||||
|
||||
lid = log_open("test.log");
|
||||
lid = igloo_log_open("test.log");
|
||||
|
||||
LOG_ERR(lid, "The log id is %d, damnit...", lid);
|
||||
|
||||
log_close(lid);
|
||||
igloo_log_close(lid);
|
||||
|
||||
log_shutdown();
|
||||
igloo_log_shutdown();
|
||||
}
|
||||
|
@ -45,7 +45,7 @@
|
||||
#include <thread/thread.h>
|
||||
#else
|
||||
#define thread_mutex_create(x) do{}while(0)
|
||||
#define thread_mutex_destroy(x) do{}while(0)
|
||||
#define igloo_thread_mutex_destroy(x) do{}while(0)
|
||||
#define thread_mutex_lock(x) do{}while(0)
|
||||
#define thread_mutex_unlock(x) do{}while(0)
|
||||
#endif
|
||||
@ -60,9 +60,9 @@ static int _isip(const char *what);
|
||||
/* internal data */
|
||||
|
||||
#ifndef NO_THREAD
|
||||
static mutex_t _resolver_mutex;
|
||||
static mutex_t igloo__resolver_mutex;
|
||||
#endif
|
||||
static int _initialized = 0;
|
||||
static int igloo__initialized = 0;
|
||||
|
||||
#ifdef HAVE_INET_PTON
|
||||
static int _isip(const char *what)
|
||||
@ -89,7 +89,7 @@ static int _isip(const char *what)
|
||||
|
||||
|
||||
#if defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO)
|
||||
char *resolver_getname(const char *ip, char *buff, int len)
|
||||
char *igloo_resolver_getname(const char *ip, char *buff, int len)
|
||||
{
|
||||
struct addrinfo *head = NULL, hints;
|
||||
char *ret = NULL;
|
||||
@ -120,7 +120,7 @@ char *resolver_getname(const char *ip, char *buff, int len)
|
||||
}
|
||||
|
||||
|
||||
char *resolver_getip(const char *name, char *buff, int len)
|
||||
char *igloo_resolver_getip(const char *name, char *buff, int len)
|
||||
{
|
||||
struct addrinfo *head, hints;
|
||||
char *ret = NULL;
|
||||
@ -150,7 +150,7 @@ char *resolver_getip(const char *name, char *buff, int len)
|
||||
|
||||
#else
|
||||
|
||||
char *resolver_getname(const char *ip, char *buff, int len)
|
||||
char *igloo_resolver_getname(const char *ip, char *buff, int len)
|
||||
{
|
||||
struct hostent *host;
|
||||
char *ret = NULL;
|
||||
@ -163,7 +163,7 @@ char *resolver_getname(const char *ip, char *buff, int len)
|
||||
return buff;
|
||||
}
|
||||
|
||||
thread_mutex_lock(&_resolver_mutex);
|
||||
thread_mutex_lock(&igloo__resolver_mutex);
|
||||
if (inet_aton (ip, &addr)) {
|
||||
/* casting &addr to const char* as it is recommended on win* */
|
||||
if ((host=gethostbyaddr ((const char *)&addr, sizeof (struct in_addr), AF_INET)))
|
||||
@ -173,11 +173,11 @@ char *resolver_getname(const char *ip, char *buff, int len)
|
||||
}
|
||||
}
|
||||
|
||||
thread_mutex_unlock(&_resolver_mutex);
|
||||
thread_mutex_unlock(&igloo__resolver_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *resolver_getip(const char *name, char *buff, int len)
|
||||
char *igloo_resolver_getip(const char *name, char *buff, int len)
|
||||
{
|
||||
struct hostent *host;
|
||||
char *ret = NULL;
|
||||
@ -188,7 +188,7 @@ char *resolver_getip(const char *name, char *buff, int len)
|
||||
buff [len-1] = '\0';
|
||||
return buff;
|
||||
}
|
||||
thread_mutex_lock(&_resolver_mutex);
|
||||
thread_mutex_lock(&igloo__resolver_mutex);
|
||||
host = gethostbyname(name);
|
||||
if (host)
|
||||
{
|
||||
@ -196,21 +196,21 @@ char *resolver_getip(const char *name, char *buff, int len)
|
||||
ret = strncpy(buff, temp, len);
|
||||
buff [len-1] = '\0';
|
||||
}
|
||||
thread_mutex_unlock(&_resolver_mutex);
|
||||
thread_mutex_unlock(&igloo__resolver_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void resolver_initialize()
|
||||
void igloo_resolver_initialize()
|
||||
{
|
||||
/* initialize the lib if we havne't done so already */
|
||||
|
||||
if (!_initialized)
|
||||
if (!igloo__initialized)
|
||||
{
|
||||
_initialized = 1;
|
||||
thread_mutex_create (&_resolver_mutex);
|
||||
igloo__initialized = 1;
|
||||
thread_mutex_create (&igloo__resolver_mutex);
|
||||
|
||||
/* keep dns connects (TCP) open */
|
||||
#ifdef HAVE_SETHOSTENT
|
||||
@ -219,12 +219,12 @@ void resolver_initialize()
|
||||
}
|
||||
}
|
||||
|
||||
void resolver_shutdown(void)
|
||||
void igloo_resolver_shutdown(void)
|
||||
{
|
||||
if (_initialized)
|
||||
if (igloo__initialized)
|
||||
{
|
||||
thread_mutex_destroy(&_resolver_mutex);
|
||||
_initialized = 0;
|
||||
igloo_thread_mutex_destroy(&igloo__resolver_mutex);
|
||||
igloo__initialized = 0;
|
||||
#ifdef HAVE_ENDHOSTENT
|
||||
endhostent();
|
||||
#endif
|
||||
|
@ -39,11 +39,11 @@
|
||||
**
|
||||
*/
|
||||
|
||||
void resolver_initialize(void);
|
||||
void resolver_shutdown(void);
|
||||
void igloo_resolver_initialize(void);
|
||||
void igloo_resolver_shutdown(void);
|
||||
|
||||
char *resolver_getname(const char *ip, char *buff, int len);
|
||||
char *resolver_getip(const char *name, char *buff, int len);
|
||||
char *igloo_resolver_getname(const char *ip, char *buff, int len);
|
||||
char *igloo_resolver_getip(const char *name, char *buff, int len);
|
||||
|
||||
#endif
|
||||
|
||||
|
186
net/sock.c
186
net/sock.c
@ -67,60 +67,60 @@
|
||||
# define AI_ADDRCONFIG 0
|
||||
#endif
|
||||
|
||||
/* sock_initialize
|
||||
/* igloo_sock_initialize
|
||||
**
|
||||
** initializes the socket library. you must call this
|
||||
** before using the library!
|
||||
*/
|
||||
void sock_initialize(void)
|
||||
void igloo_sock_initialize(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSADATA wsad;
|
||||
WSAStartup(0x0101, &wsad);
|
||||
#endif
|
||||
|
||||
resolver_initialize();
|
||||
igloo_resolver_initialize();
|
||||
}
|
||||
|
||||
/* sock_shutdown
|
||||
/* igloo_sock_shutdown
|
||||
**
|
||||
** shutdown the socket library. remember to call this when you're
|
||||
** through using the lib
|
||||
*/
|
||||
void sock_shutdown(void)
|
||||
void igloo_sock_shutdown(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
|
||||
resolver_shutdown();
|
||||
igloo_resolver_shutdown();
|
||||
}
|
||||
|
||||
/* sock_get_localip
|
||||
/* igloo_sock_get_localip
|
||||
**
|
||||
** gets the local ip address for the machine
|
||||
** the ip it returns *should* be on the internet.
|
||||
** in any case, it's as close as we can hope to get
|
||||
** unless someone has better ideas on how to do this
|
||||
*/
|
||||
char *sock_get_localip(char *buff, int len)
|
||||
char *igloo_sock_get_localip(char *buff, int len)
|
||||
{
|
||||
char temp[1024];
|
||||
|
||||
if (gethostname(temp, sizeof(temp)) != 0)
|
||||
return NULL;
|
||||
|
||||
if (resolver_getip(temp, buff, len))
|
||||
if (igloo_resolver_getip(temp, buff, len))
|
||||
return buff;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* sock_error
|
||||
/* igloo_sock_error
|
||||
**
|
||||
** returns the last socket error
|
||||
*/
|
||||
int sock_error(void)
|
||||
int igloo_sock_error(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return WSAGetLastError();
|
||||
@ -129,7 +129,7 @@ int sock_error(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
void sock_set_error(int val)
|
||||
void igloo_sock_set_error(int val)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSASetLastError (val);
|
||||
@ -138,12 +138,12 @@ void sock_set_error(int val)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* sock_recoverable
|
||||
/* igloo_sock_recoverable
|
||||
**
|
||||
** determines if the socket error is recoverable
|
||||
** in terms of non blocking sockets
|
||||
*/
|
||||
int sock_recoverable(int error)
|
||||
int igloo_sock_recoverable(int error)
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
@ -169,7 +169,7 @@ int sock_recoverable(int error)
|
||||
}
|
||||
}
|
||||
|
||||
int sock_stalled (int error)
|
||||
int igloo_sock_stalled (int error)
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
@ -200,11 +200,11 @@ static int sock_connect_pending (int error)
|
||||
return error == EINPROGRESS || error == EALREADY;
|
||||
}
|
||||
|
||||
/* sock_valid_socket
|
||||
/* igloo_sock_valid_socket
|
||||
**
|
||||
** determines if a sock_t represents a valid socket
|
||||
*/
|
||||
int sock_valid_socket(sock_t sock)
|
||||
int igloo_sock_valid_socket(sock_t sock)
|
||||
{
|
||||
int ret;
|
||||
int optval;
|
||||
@ -219,7 +219,7 @@ int sock_valid_socket(sock_t sock)
|
||||
|
||||
|
||||
/* determines if the passed socket is still connected */
|
||||
int sock_active (sock_t sock)
|
||||
int igloo_sock_active (sock_t sock)
|
||||
{
|
||||
char c;
|
||||
int l;
|
||||
@ -227,7 +227,7 @@ int sock_active (sock_t sock)
|
||||
l = recv (sock, &c, 1, MSG_PEEK);
|
||||
if (l == 0)
|
||||
return 0;
|
||||
if (l == SOCK_ERROR && sock_recoverable (sock_error()))
|
||||
if (l == SOCK_ERROR && igloo_sock_recoverable (igloo_sock_error()))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
@ -251,13 +251,13 @@ int inet_aton(const char *s, struct in_addr *a)
|
||||
}
|
||||
#endif /* _WIN32 */
|
||||
|
||||
/* sock_set_blocking
|
||||
/* igloo_sock_set_blocking
|
||||
*
|
||||
* set the sock blocking or nonblocking
|
||||
* 1 for blocking
|
||||
* 0 for nonblocking
|
||||
*/
|
||||
int sock_set_blocking(sock_t sock, int block)
|
||||
int igloo_sock_set_blocking(sock_t sock, int block)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#ifdef __MINGW32__
|
||||
@ -267,7 +267,7 @@ int sock_set_blocking(sock_t sock, int block)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if ((!sock_valid_socket(sock)) || (block < 0) || (block > 1))
|
||||
if ((!igloo_sock_valid_socket(sock)) || (block < 0) || (block > 1))
|
||||
return SOCK_ERROR;
|
||||
|
||||
#ifdef _WIN32
|
||||
@ -278,14 +278,14 @@ int sock_set_blocking(sock_t sock, int block)
|
||||
#endif
|
||||
}
|
||||
|
||||
int sock_set_nolinger(sock_t sock)
|
||||
int igloo_sock_set_nolinger(sock_t sock)
|
||||
{
|
||||
struct linger lin = { 0, 0 };
|
||||
return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin,
|
||||
sizeof(struct linger));
|
||||
}
|
||||
|
||||
int sock_set_nodelay(sock_t sock)
|
||||
int igloo_sock_set_nodelay(sock_t sock)
|
||||
{
|
||||
int nodelay = 1;
|
||||
|
||||
@ -293,18 +293,18 @@ int sock_set_nodelay(sock_t sock)
|
||||
sizeof(int));
|
||||
}
|
||||
|
||||
int sock_set_keepalive(sock_t sock)
|
||||
int igloo_sock_set_keepalive(sock_t sock)
|
||||
{
|
||||
int keepalive = 1;
|
||||
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
|
||||
sizeof(int));
|
||||
}
|
||||
|
||||
/* sock_close
|
||||
/* igloo_sock_close
|
||||
**
|
||||
** close the socket
|
||||
*/
|
||||
int sock_close(sock_t sock)
|
||||
int igloo_sock_close(sock_t sock)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return closesocket(sock);
|
||||
@ -313,20 +313,20 @@ int sock_close(sock_t sock)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* sock_writev
|
||||
/* igloo_sock_writev
|
||||
*
|
||||
* write multiple buffers at once, return bytes actually written
|
||||
*/
|
||||
#ifdef HAVE_WRITEV
|
||||
|
||||
ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
ssize_t igloo_sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
{
|
||||
return writev (sock, iov, count);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
ssize_t igloo_sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
{
|
||||
int i = count, accum = 0, ret;
|
||||
const struct iovec *v = iov;
|
||||
@ -335,7 +335,7 @@ ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
{
|
||||
if (v->iov_base && v->iov_len)
|
||||
{
|
||||
ret = sock_write_bytes (sock, v->iov_base, v->iov_len);
|
||||
ret = igloo_sock_write_bytes (sock, v->iov_base, v->iov_len);
|
||||
if (ret == -1 && accum==0)
|
||||
return -1;
|
||||
if (ret == -1)
|
||||
@ -352,55 +352,55 @@ ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count)
|
||||
|
||||
#endif
|
||||
|
||||
/* sock_write_bytes
|
||||
/* igloo_sock_write_bytes
|
||||
**
|
||||
** write bytes to the socket
|
||||
** this function will _NOT_ block
|
||||
*/
|
||||
int sock_write_bytes(sock_t sock, const void *buff, size_t len)
|
||||
int igloo_sock_write_bytes(sock_t sock, const void *buff, size_t len)
|
||||
{
|
||||
/* sanity check */
|
||||
if (!buff) {
|
||||
return SOCK_ERROR;
|
||||
} else if (len <= 0) {
|
||||
return SOCK_ERROR;
|
||||
} /*else if (!sock_valid_socket(sock)) {
|
||||
} /*else if (!igloo_sock_valid_socket(sock)) {
|
||||
return SOCK_ERROR;
|
||||
} */
|
||||
|
||||
return send(sock, buff, len, 0);
|
||||
}
|
||||
|
||||
/* sock_write_string
|
||||
/* igloo_sock_write_string
|
||||
**
|
||||
** writes a string to a socket
|
||||
** This function must only be called with a blocking socket.
|
||||
*/
|
||||
int sock_write_string(sock_t sock, const char *buff)
|
||||
int igloo_sock_write_string(sock_t sock, const char *buff)
|
||||
{
|
||||
return (sock_write_bytes(sock, buff, strlen(buff)) > 0);
|
||||
return (igloo_sock_write_bytes(sock, buff, strlen(buff)) > 0);
|
||||
}
|
||||
|
||||
/* sock_write
|
||||
/* igloo_sock_write
|
||||
**
|
||||
** write a formatted string to the socket
|
||||
** this function must only be called with a blocking socket.
|
||||
** will truncate the string if it's greater than 1024 chars.
|
||||
*/
|
||||
int sock_write(sock_t sock, const char *fmt, ...)
|
||||
int igloo_sock_write(sock_t sock, const char *fmt, ...)
|
||||
{
|
||||
int rc;
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
rc = sock_write_fmt (sock, fmt, ap);
|
||||
rc = igloo_sock_write_fmt (sock, fmt, ap);
|
||||
va_end (ap);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
#ifdef HAVE_OLD_VSNPRINTF
|
||||
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
int igloo_sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
{
|
||||
va_list ap_local;
|
||||
unsigned int len = 1024;
|
||||
@ -419,7 +419,7 @@ int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
ret = vsnprintf (buff, len, fmt, ap_local);
|
||||
if (ret > 0)
|
||||
{
|
||||
ret = sock_write_bytes (sock, buff, ret);
|
||||
ret = igloo_sock_write_bytes (sock, buff, ret);
|
||||
break;
|
||||
}
|
||||
len += 8192;
|
||||
@ -428,7 +428,7 @@ int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
int igloo_sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
{
|
||||
char buffer [1024], *buff = buffer;
|
||||
int len;
|
||||
@ -442,7 +442,7 @@ int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
if (len > 0)
|
||||
{
|
||||
if ((size_t)len < sizeof (buffer)) /* common case */
|
||||
rc = sock_write_bytes(sock, buff, (size_t)len);
|
||||
rc = igloo_sock_write_bytes(sock, buff, (size_t)len);
|
||||
else
|
||||
{
|
||||
/* truncated */
|
||||
@ -451,7 +451,7 @@ int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
{
|
||||
len = vsnprintf (buff, len, fmt, ap_retry);
|
||||
if (len > 0)
|
||||
rc = sock_write_bytes (sock, buff, len);
|
||||
rc = igloo_sock_write_bytes (sock, buff, len);
|
||||
free (buff);
|
||||
}
|
||||
}
|
||||
@ -463,17 +463,17 @@ int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
|
||||
#endif
|
||||
|
||||
|
||||
int sock_read_bytes(sock_t sock, char *buff, size_t len)
|
||||
int igloo_sock_read_bytes(sock_t sock, char *buff, size_t len)
|
||||
{
|
||||
|
||||
/*if (!sock_valid_socket(sock)) return 0; */
|
||||
/*if (!igloo_sock_valid_socket(sock)) return 0; */
|
||||
if (!buff) return 0;
|
||||
if (len <= 0) return 0;
|
||||
|
||||
return recv(sock, buff, len, 0);
|
||||
}
|
||||
|
||||
/* sock_read_line
|
||||
/* igloo_sock_read_line
|
||||
**
|
||||
** Read one line of at max len bytes from sock into buff.
|
||||
** If ok, return 1 and nullterminate buff. Otherwize return 0.
|
||||
@ -481,12 +481,12 @@ int sock_read_bytes(sock_t sock, char *buff, size_t len)
|
||||
**
|
||||
** this function will probably not work on sockets in nonblocking mode
|
||||
*/
|
||||
int sock_read_line(sock_t sock, char *buff, const int len)
|
||||
int igloo_sock_read_line(sock_t sock, char *buff, const int len)
|
||||
{
|
||||
char c = '\0';
|
||||
int read_bytes, pos;
|
||||
|
||||
/*if (!sock_valid_socket(sock)) {
|
||||
/*if (!igloo_sock_valid_socket(sock)) {
|
||||
return 0;
|
||||
} else*/ if (!buff) {
|
||||
return 0;
|
||||
@ -523,7 +523,7 @@ int sock_read_line(sock_t sock, char *buff, const int len)
|
||||
* return 1 for ok
|
||||
*/
|
||||
#ifdef HAVE_POLL
|
||||
int sock_connected (sock_t sock, int timeout)
|
||||
int igloo_sock_connected (sock_t sock, int timeout)
|
||||
{
|
||||
struct pollfd check;
|
||||
int val = SOCK_ERROR;
|
||||
@ -540,11 +540,11 @@ int sock_connected (sock_t sock, int timeout)
|
||||
{
|
||||
if (val == 0)
|
||||
return 1;
|
||||
sock_set_error (val);
|
||||
igloo_sock_set_error (val);
|
||||
}
|
||||
/* fall through */
|
||||
case -1:
|
||||
if (sock_recoverable (sock_error()))
|
||||
if (igloo_sock_recoverable (igloo_sock_error()))
|
||||
return 0;
|
||||
return SOCK_ERROR;
|
||||
}
|
||||
@ -552,7 +552,7 @@ int sock_connected (sock_t sock, int timeout)
|
||||
|
||||
#else
|
||||
|
||||
int sock_connected (sock_t sock, int timeout)
|
||||
int igloo_sock_connected (sock_t sock, int timeout)
|
||||
{
|
||||
fd_set wfds;
|
||||
int val = SOCK_ERROR;
|
||||
@ -580,25 +580,25 @@ int sock_connected (sock_t sock, int timeout)
|
||||
{
|
||||
if (val == 0)
|
||||
return 1;
|
||||
sock_set_error (val);
|
||||
igloo_sock_set_error (val);
|
||||
}
|
||||
/* fall through */
|
||||
case -1:
|
||||
if (sock_recoverable (sock_error()))
|
||||
if (igloo_sock_recoverable (igloo_sock_error()))
|
||||
return 0;
|
||||
return SOCK_ERROR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
sock_t sock_connect_wto (const char *hostname, int port, int timeout)
|
||||
sock_t igloo_sock_connect_wto (const char *hostname, int port, int timeout)
|
||||
{
|
||||
return sock_connect_wto_bind(hostname, port, NULL, timeout);
|
||||
return igloo_sock_connect_wto_bind(hostname, port, NULL, timeout);
|
||||
}
|
||||
|
||||
#ifdef HAVE_GETADDRINFO
|
||||
|
||||
sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
sock_t igloo_sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
{
|
||||
int sock = SOCK_ERROR;
|
||||
struct addrinfo *ai, *head, hints;
|
||||
@ -619,11 +619,11 @@ sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol))
|
||||
> -1)
|
||||
{
|
||||
sock_set_blocking (sock, 0);
|
||||
igloo_sock_set_blocking (sock, 0);
|
||||
if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
|
||||
!sock_connect_pending(sock_error()))
|
||||
!sock_connect_pending(igloo_sock_error()))
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
sock = SOCK_ERROR;
|
||||
}
|
||||
else
|
||||
@ -640,7 +640,7 @@ sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
* timeout is 0 or less then we will wait until the OS gives up on the connect
|
||||
* The socket is returned
|
||||
*/
|
||||
sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
||||
sock_t igloo_sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
||||
{
|
||||
sock_t sock = SOCK_ERROR;
|
||||
struct addrinfo *ai, *head, *b_head=NULL, hints;
|
||||
@ -660,7 +660,7 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
|
||||
{
|
||||
if (timeout > 0)
|
||||
sock_set_blocking (sock, 0);
|
||||
igloo_sock_set_blocking (sock, 0);
|
||||
|
||||
if (bnd)
|
||||
{
|
||||
@ -672,7 +672,7 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
if (getaddrinfo (bnd, NULL, &b_hints, &b_head) ||
|
||||
bind (sock, b_head->ai_addr, b_head->ai_addrlen) < 0)
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
sock = SOCK_ERROR;
|
||||
break;
|
||||
}
|
||||
@ -684,19 +684,19 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
/* loop as the connect maybe async */
|
||||
while (sock != SOCK_ERROR)
|
||||
{
|
||||
if (sock_recoverable (sock_error()))
|
||||
if (igloo_sock_recoverable (igloo_sock_error()))
|
||||
{
|
||||
int connected = sock_connected (sock, timeout);
|
||||
int connected = igloo_sock_connected (sock, timeout);
|
||||
if (connected == 0) /* try again, interrupted */
|
||||
continue;
|
||||
if (connected == 1) /* connected */
|
||||
{
|
||||
if (timeout >= 0)
|
||||
sock_set_blocking(sock, 1);
|
||||
igloo_sock_set_blocking(sock, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
sock = SOCK_ERROR;
|
||||
}
|
||||
if (sock != SOCK_ERROR)
|
||||
@ -712,7 +712,7 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
}
|
||||
|
||||
|
||||
sock_t sock_get_server_socket (int port, const char *sinterface)
|
||||
sock_t igloo_sock_get_server_socket (int port, const char *sinterface)
|
||||
{
|
||||
struct sockaddr_storage sa;
|
||||
struct addrinfo hints, *res, *ai;
|
||||
@ -748,7 +748,7 @@ sock_t sock_get_server_socket (int port, const char *sinterface)
|
||||
|
||||
if (bind (sock, ai->ai_addr, ai->ai_addrlen) < 0)
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
continue;
|
||||
}
|
||||
freeaddrinfo (res);
|
||||
@ -775,15 +775,15 @@ int sock_try_connection (sock_t sock, const char *hostname, unsigned int port)
|
||||
memset(&sin, 0, sizeof(struct sockaddr_in));
|
||||
memset(&server, 0, sizeof(struct sockaddr_in));
|
||||
|
||||
if (!resolver_getip(hostname, ip, MAX_ADDR_LEN))
|
||||
if (!igloo_resolver_getip(hostname, ip, MAX_ADDR_LEN))
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (inet_aton(ip, (struct in_addr *)&sin.sin_addr) == 0)
|
||||
{
|
||||
sock_close(sock);
|
||||
igloo_sock_close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -795,7 +795,7 @@ int sock_try_connection (sock_t sock, const char *hostname, unsigned int port)
|
||||
return connect(sock, (struct sockaddr *)&server, sizeof(server));
|
||||
}
|
||||
|
||||
sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
sock_t igloo_sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
{
|
||||
sock_t sock;
|
||||
|
||||
@ -803,13 +803,13 @@ sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
|
||||
if (sock == SOCK_ERROR)
|
||||
return SOCK_ERROR;
|
||||
|
||||
sock_set_blocking (sock, 0);
|
||||
igloo_sock_set_blocking (sock, 0);
|
||||
sock_try_connection (sock, hostname, port);
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
||||
sock_t igloo_sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
||||
{
|
||||
sock_t sock;
|
||||
|
||||
@ -827,30 +827,30 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
if (inet_aton (bnd, &sa.sin_addr) == 0 ||
|
||||
bind (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
return SOCK_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (timeout)
|
||||
{
|
||||
sock_set_blocking (sock, 0);
|
||||
igloo_sock_set_blocking (sock, 0);
|
||||
if (sock_try_connection (sock, hostname, port) < 0)
|
||||
{
|
||||
int ret = sock_connected (sock, timeout);
|
||||
int ret = igloo_sock_connected (sock, timeout);
|
||||
if (ret <= 0)
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
return SOCK_ERROR;
|
||||
}
|
||||
}
|
||||
sock_set_blocking(sock, 1);
|
||||
igloo_sock_set_blocking(sock, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sock_try_connection (sock, hostname, port) < 0)
|
||||
{
|
||||
sock_close (sock);
|
||||
igloo_sock_close (sock);
|
||||
sock = SOCK_ERROR;
|
||||
}
|
||||
}
|
||||
@ -858,13 +858,13 @@ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, i
|
||||
}
|
||||
|
||||
|
||||
/* sock_get_server_socket
|
||||
/* igloo_sock_get_server_socket
|
||||
**
|
||||
** create a socket for incoming requests on a specified port and
|
||||
** interface. if interface is null, listen on all interfaces.
|
||||
** returns the socket, or SOCK_ERROR on failure
|
||||
*/
|
||||
sock_t sock_get_server_socket(int port, const char *sinterface)
|
||||
sock_t igloo_sock_get_server_socket(int port, const char *sinterface)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
int error, opt;
|
||||
@ -879,7 +879,7 @@ sock_t sock_get_server_socket(int port, const char *sinterface)
|
||||
|
||||
/* set the interface to bind to if specified */
|
||||
if (sinterface != NULL) {
|
||||
if (!resolver_getip(sinterface, ip, sizeof (ip)))
|
||||
if (!igloo_resolver_getip(sinterface, ip, sizeof (ip)))
|
||||
return SOCK_ERROR;
|
||||
|
||||
if (!inet_aton(ip, &sa.sin_addr)) {
|
||||
@ -913,14 +913,14 @@ sock_t sock_get_server_socket(int port, const char *sinterface)
|
||||
|
||||
#endif
|
||||
|
||||
void sock_set_send_buffer (sock_t sock, int win_size)
|
||||
void igloo_sock_set_send_buffer (sock_t sock, int win_size)
|
||||
{
|
||||
setsockopt (sock, SOL_SOCKET, SO_SNDBUF, (char *) &win_size, sizeof(win_size));
|
||||
}
|
||||
|
||||
int sock_listen(sock_t serversock, int backlog)
|
||||
int igloo_sock_listen(sock_t serversock, int backlog)
|
||||
{
|
||||
if (!sock_valid_socket(serversock))
|
||||
if (!igloo_sock_valid_socket(serversock))
|
||||
return 0;
|
||||
|
||||
if (backlog <= 0)
|
||||
@ -929,7 +929,7 @@ int sock_listen(sock_t serversock, int backlog)
|
||||
return (listen(serversock, backlog) == 0);
|
||||
}
|
||||
|
||||
sock_t sock_accept(sock_t serversock, char *ip, size_t len)
|
||||
sock_t igloo_sock_accept(sock_t serversock, char *ip, size_t len)
|
||||
{
|
||||
#ifdef HAVE_GETNAMEINFO
|
||||
struct sockaddr_storage sa;
|
||||
@ -939,7 +939,7 @@ sock_t sock_accept(sock_t serversock, char *ip, size_t len)
|
||||
sock_t ret;
|
||||
socklen_t slen;
|
||||
|
||||
if (ip == NULL || len == 0 || !sock_valid_socket(serversock))
|
||||
if (ip == NULL || len == 0 || !igloo_sock_valid_socket(serversock))
|
||||
return SOCK_ERROR;
|
||||
|
||||
slen = sizeof(sa);
|
||||
@ -954,8 +954,8 @@ sock_t sock_accept(sock_t serversock, char *ip, size_t len)
|
||||
/* inet_ntoa is not reentrant, we should protect this */
|
||||
strncpy(ip, inet_ntoa(sa.sin_addr), len);
|
||||
#endif
|
||||
sock_set_nolinger(ret);
|
||||
sock_set_keepalive(ret);
|
||||
igloo_sock_set_nolinger(ret);
|
||||
igloo_sock_set_keepalive(ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
60
net/sock.h
60
net/sock.h
@ -72,47 +72,47 @@ struct iovec
|
||||
#define SOCK_TIMEOUT -2
|
||||
|
||||
/* sock connect macro */
|
||||
#define sock_connect(h, p) sock_connect_wto(h, p, 0)
|
||||
#define sock_connect(h, p) igloo_sock_connect_wto(h, p, 0)
|
||||
|
||||
/* Misc socket functions */
|
||||
void sock_initialize(void);
|
||||
void sock_shutdown(void);
|
||||
char *sock_get_localip(char *buff, int len);
|
||||
int sock_error(void);
|
||||
int sock_recoverable(int error);
|
||||
int sock_stalled(int error);
|
||||
int sock_valid_socket(sock_t sock);
|
||||
int sock_active (sock_t sock);
|
||||
int sock_set_blocking(sock_t sock, int block);
|
||||
int sock_set_nolinger(sock_t sock);
|
||||
int sock_set_keepalive(sock_t sock);
|
||||
int sock_set_nodelay(sock_t sock);
|
||||
void sock_set_send_buffer (sock_t sock, int win_size);
|
||||
void sock_set_error(int val);
|
||||
int sock_close(sock_t sock);
|
||||
void igloo_sock_initialize(void);
|
||||
void igloo_sock_shutdown(void);
|
||||
char *igloo_sock_get_localip(char *buff, int len);
|
||||
int igloo_sock_error(void);
|
||||
int igloo_sock_recoverable(int error);
|
||||
int igloo_sock_stalled(int error);
|
||||
int igloo_sock_valid_socket(sock_t sock);
|
||||
int igloo_sock_active (sock_t sock);
|
||||
int igloo_sock_set_blocking(sock_t sock, int block);
|
||||
int igloo_sock_set_nolinger(sock_t sock);
|
||||
int igloo_sock_set_keepalive(sock_t sock);
|
||||
int igloo_sock_set_nodelay(sock_t sock);
|
||||
void igloo_sock_set_send_buffer (sock_t sock, int win_size);
|
||||
void igloo_sock_set_error(int val);
|
||||
int igloo_sock_close(sock_t sock);
|
||||
|
||||
/* Connection related socket functions */
|
||||
sock_t sock_connect_wto(const char *hostname, int port, int timeout);
|
||||
sock_t sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout);
|
||||
sock_t sock_connect_non_blocking(const char *host, unsigned port);
|
||||
int sock_connected(sock_t sock, int timeout);
|
||||
sock_t igloo_sock_connect_wto(const char *hostname, int port, int timeout);
|
||||
sock_t igloo_sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout);
|
||||
sock_t igloo_sock_connect_non_blocking(const char *host, unsigned port);
|
||||
int igloo_sock_connected(sock_t sock, int timeout);
|
||||
|
||||
/* Socket write functions */
|
||||
int sock_write_bytes(sock_t sock, const void *buff, size_t len);
|
||||
int sock_write(sock_t sock, const char *fmt, ...);
|
||||
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap);
|
||||
int sock_write_string(sock_t sock, const char *buff);
|
||||
ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count);
|
||||
int igloo_sock_write_bytes(sock_t sock, const void *buff, size_t len);
|
||||
int igloo_sock_write(sock_t sock, const char *fmt, ...);
|
||||
int igloo_sock_write_fmt(sock_t sock, const char *fmt, va_list ap);
|
||||
int igloo_sock_write_string(sock_t sock, const char *buff);
|
||||
ssize_t igloo_sock_writev (sock_t sock, const struct iovec *iov, size_t count);
|
||||
|
||||
|
||||
/* Socket read functions */
|
||||
int sock_read_bytes(sock_t sock, char *buff, size_t len);
|
||||
int sock_read_line(sock_t sock, char *string, const int len);
|
||||
int igloo_sock_read_bytes(sock_t sock, char *buff, size_t len);
|
||||
int igloo_sock_read_line(sock_t sock, char *string, const int len);
|
||||
|
||||
/* server socket functions */
|
||||
sock_t sock_get_server_socket(int port, const char *sinterface);
|
||||
int sock_listen(sock_t serversock, int backlog);
|
||||
sock_t sock_accept(sock_t serversock, char *ip, size_t len);
|
||||
sock_t igloo_sock_get_server_socket(int port, const char *sinterface);
|
||||
int igloo_sock_listen(sock_t serversock, int backlog);
|
||||
sock_t igloo_sock_accept(sock_t serversock, char *ip, size_t len);
|
||||
|
||||
#ifdef _WIN32
|
||||
int inet_aton(const char *s, struct in_addr *a);
|
||||
|
@ -7,10 +7,10 @@ int main()
|
||||
{
|
||||
char buff[1024];
|
||||
|
||||
resolver_initialize();
|
||||
igloo_resolver_initialize();
|
||||
|
||||
printf("I got %s, when looking up %s.\n", resolver_getip("bach.greenwitch.com", buff, 1024), "bach.greenwitch.com");
|
||||
printf("I got %s, when looking up %s.\n", resolver_getname("207.181.249.14", buff, 1024), "207.181.249.14");
|
||||
printf("I got %s, when looking up %s.\n", igloo_resolver_getip("bach.greenwitch.com", buff, 1024), "bach.greenwitch.com");
|
||||
printf("I got %s, when looking up %s.\n", igloo_resolver_getname("207.181.249.14", buff, 1024), "207.181.249.14");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
224
thread/thread.c
224
thread/thread.c
@ -59,22 +59,22 @@
|
||||
|
||||
#ifdef THREAD_DEBUG
|
||||
#define CATMODULE "thread"
|
||||
#define LOG_ERROR(y) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_ERROR3(y, z1, z2, z3) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
||||
#define LOG_ERROR7(y, z1, z2, z3, z4, z5, z6, z7) log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
||||
#define LOG_ERROR(y) igloo_log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_ERROR3(y, z1, z2, z3) igloo_log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
||||
#define LOG_ERROR7(y, z1, z2, z3, z4, z5, z6, z7) igloo_log_write(_logid, 1, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
||||
|
||||
#define LOG_WARN(y) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_WARN3(y, z1, z2, z3) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
||||
#define LOG_WARN5(y, z1, z2, z3, z4, z5) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
#define LOG_WARN7(y, z1, z2, z3, z4, z5, z6, z7) log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
||||
#define LOG_WARN(y) igloo_log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_WARN3(y, z1, z2, z3) igloo_log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3)
|
||||
#define LOG_WARN5(y, z1, z2, z3, z4, z5) igloo_log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
#define LOG_WARN7(y, z1, z2, z3, z4, z5, z6, z7) igloo_log_write(_logid, 2, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5, z6, z7)
|
||||
|
||||
#define LOG_INFO(y) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_INFO4(y, z1, z2, z3, z4) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4)
|
||||
#define LOG_INFO5(y, z1, z2, z3, z4, z5) log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
#define LOG_INFO(y) igloo_log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_INFO4(y, z1, z2, z3, z4) igloo_log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4)
|
||||
#define LOG_INFO5(y, z1, z2, z3, z4, z5) igloo_log_write(_logid, 3, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
|
||||
#define LOG_DEBUG(y) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_DEBUG2(y, z1, z2) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2)
|
||||
#define LOG_DEBUG5(y, z1, z2, z3, z4, z5) log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
#define LOG_DEBUG(y) igloo_log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y)
|
||||
#define LOG_DEBUG2(y, z1, z2) igloo_log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2)
|
||||
#define LOG_DEBUG5(y, z1, z2, z3, z4, z5) igloo_log_write(_logid, 4, CATMODULE "/", __FUNCTION__, y, z1, z2, z3, z4, z5)
|
||||
#endif
|
||||
|
||||
/* thread starting structure */
|
||||
@ -90,15 +90,15 @@ typedef struct thread_start_tag {
|
||||
pthread_t sys_thread;
|
||||
} thread_start_t;
|
||||
|
||||
static long _next_thread_id = 0;
|
||||
static int _initialized = 0;
|
||||
static avl_tree *_threadtree = NULL;
|
||||
static long igloo__next_thread_id = 0;
|
||||
static int igloo__initialized = 0;
|
||||
static avl_tree *igloo__threadtree = NULL;
|
||||
|
||||
#ifdef DEBUG_MUTEXES
|
||||
static mutex_t _threadtree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
||||
static mutex_t igloo__threadtree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
||||
PTHREAD_MUTEX_INITIALIZER};
|
||||
#else
|
||||
static mutex_t _threadtree_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
||||
static mutex_t igloo__threadtree_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
||||
#endif
|
||||
|
||||
|
||||
@ -113,10 +113,10 @@ static mutex_t _mutextree_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_MUTEXES
|
||||
static mutex_t _library_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
||||
static mutex_t igloo__library_mutex = { -1, NULL, MUTEX_STATE_UNINIT, NULL, -1,
|
||||
PTHREAD_MUTEX_INITIALIZER};
|
||||
#else
|
||||
static mutex_t _library_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
||||
static mutex_t igloo__library_mutex = { PTHREAD_MUTEX_INITIALIZER };
|
||||
#endif
|
||||
|
||||
/* INTERNAL FUNCTIONS */
|
||||
@ -127,8 +127,8 @@ static int _compare_mutexes(void *compare_arg, void *a, void *b);
|
||||
static int _free_mutex(void *key);
|
||||
#endif
|
||||
|
||||
static int _compare_threads(void *compare_arg, void *a, void *b);
|
||||
static int _free_thread(void *key);
|
||||
static int igloo__compare_threads(void *compare_arg, void *a, void *b);
|
||||
static int igloo__free_thread(void *key);
|
||||
|
||||
/* mutex fuctions */
|
||||
static void _mutex_create(mutex_t *mutex);
|
||||
@ -136,28 +136,28 @@ static void _mutex_lock(mutex_t *mutex);
|
||||
static void _mutex_unlock(mutex_t *mutex);
|
||||
|
||||
/* misc thread stuff */
|
||||
static void *_start_routine(void *arg);
|
||||
static void *igloo__start_routine(void *arg);
|
||||
static void _catch_signals(void);
|
||||
static void _block_signals(void);
|
||||
|
||||
/* LIBRARY INITIALIZATION */
|
||||
|
||||
void thread_initialize(void)
|
||||
void igloo_thread_initialize(void)
|
||||
{
|
||||
thread_type *thread;
|
||||
|
||||
/* set up logging */
|
||||
|
||||
#ifdef THREAD_DEBUG
|
||||
log_initialize();
|
||||
_logid = log_open("thread.log");
|
||||
log_set_level(_logid, THREAD_DEBUG);
|
||||
igloo_log_initialize();
|
||||
_logid = igloo_log_open("thread.log");
|
||||
igloo_log_set_level(_logid, THREAD_DEBUG);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_MUTEXES
|
||||
/* create all the internal mutexes, and initialize the mutex tree */
|
||||
|
||||
_mutextree = avl_tree_new(_compare_mutexes, NULL);
|
||||
_mutextree = igloo_avl_tree_new(_compare_mutexes, NULL);
|
||||
|
||||
/* we have to create this one by hand, because there's no
|
||||
** mutextree_mutex to lock yet!
|
||||
@ -165,49 +165,49 @@ void thread_initialize(void)
|
||||
_mutex_create(&_mutextree_mutex);
|
||||
|
||||
_mutextree_mutex.mutex_id = _next_mutex_id++;
|
||||
avl_insert(_mutextree, (void *)&_mutextree_mutex);
|
||||
igloo_avl_insert(_mutextree, (void *)&_mutextree_mutex);
|
||||
#endif
|
||||
|
||||
thread_mutex_create(&_threadtree_mutex);
|
||||
thread_mutex_create(&_library_mutex);
|
||||
thread_mutex_create(&igloo__threadtree_mutex);
|
||||
thread_mutex_create(&igloo__library_mutex);
|
||||
|
||||
/* initialize the thread tree and insert the main thread */
|
||||
|
||||
_threadtree = avl_tree_new(_compare_threads, NULL);
|
||||
igloo__threadtree = igloo_avl_tree_new(igloo__compare_threads, NULL);
|
||||
|
||||
thread = (thread_type *)malloc(sizeof(thread_type));
|
||||
|
||||
thread->thread_id = _next_thread_id++;
|
||||
thread->thread_id = igloo__next_thread_id++;
|
||||
thread->line = 0;
|
||||
thread->file = strdup("main.c");
|
||||
thread->sys_thread = pthread_self();
|
||||
thread->create_time = time(NULL);
|
||||
thread->name = strdup("Main Thread");
|
||||
|
||||
avl_insert(_threadtree, (void *)thread);
|
||||
igloo_avl_insert(igloo__threadtree, (void *)thread);
|
||||
|
||||
_catch_signals();
|
||||
|
||||
_initialized = 1;
|
||||
igloo__initialized = 1;
|
||||
}
|
||||
|
||||
void thread_shutdown(void)
|
||||
void igloo_thread_shutdown(void)
|
||||
{
|
||||
if (_initialized == 1) {
|
||||
thread_mutex_destroy(&_library_mutex);
|
||||
thread_mutex_destroy(&_threadtree_mutex);
|
||||
if (igloo__initialized == 1) {
|
||||
igloo_thread_mutex_destroy(&igloo__library_mutex);
|
||||
igloo_thread_mutex_destroy(&igloo__threadtree_mutex);
|
||||
#ifdef THREAD_DEBUG
|
||||
thread_mutex_destroy(&_mutextree_mutex);
|
||||
igloo_thread_mutex_destroy(&_mutextree_mutex);
|
||||
|
||||
avl_tree_free(_mutextree, _free_mutex);
|
||||
igloo_avl_tree_free(_mutextree, _free_mutex);
|
||||
#endif
|
||||
avl_tree_free(_threadtree, _free_thread);
|
||||
_threadtree = NULL;
|
||||
igloo_avl_tree_free(igloo__threadtree, igloo__free_thread);
|
||||
igloo__threadtree = NULL;
|
||||
}
|
||||
|
||||
#ifdef THREAD_DEBUG
|
||||
log_close(_logid);
|
||||
log_shutdown();
|
||||
igloo_log_close(_logid);
|
||||
igloo_log_shutdown();
|
||||
#endif
|
||||
|
||||
}
|
||||
@ -268,7 +268,7 @@ static void _catch_signals(void)
|
||||
}
|
||||
|
||||
|
||||
thread_type *thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
thread_type *igloo_thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
void *arg, int detached, int line, char *file)
|
||||
{
|
||||
thread_type *thread = NULL;
|
||||
@ -288,9 +288,9 @@ thread_type *thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
thread->line = line;
|
||||
thread->file = strdup(file);
|
||||
|
||||
_mutex_lock (&_threadtree_mutex);
|
||||
thread->thread_id = _next_thread_id++;
|
||||
_mutex_unlock (&_threadtree_mutex);
|
||||
_mutex_lock (&igloo__threadtree_mutex);
|
||||
thread->thread_id = igloo__next_thread_id++;
|
||||
_mutex_unlock (&igloo__threadtree_mutex);
|
||||
|
||||
thread->name = strdup(name);
|
||||
thread->create_time = time(NULL);
|
||||
@ -311,7 +311,7 @@ thread_type *thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
thread->detached = 1;
|
||||
}
|
||||
|
||||
if (pthread_create (&thread->sys_thread, &attr, _start_routine, start) == 0)
|
||||
if (pthread_create (&thread->sys_thread, &attr, igloo__start_routine, start) == 0)
|
||||
{
|
||||
pthread_attr_destroy (&attr);
|
||||
return thread;
|
||||
@ -343,35 +343,35 @@ static void _mutex_create(mutex_t *mutex)
|
||||
pthread_mutex_init(&mutex->sys_mutex, NULL);
|
||||
}
|
||||
|
||||
void thread_mutex_create_c(mutex_t *mutex, int line, char *file)
|
||||
void igloo_thread_mutex_create_c(mutex_t *mutex, int line, char *file)
|
||||
{
|
||||
_mutex_create(mutex);
|
||||
|
||||
#ifdef DEBUG_MUTEXES
|
||||
_mutex_lock(&_mutextree_mutex);
|
||||
mutex->mutex_id = _next_mutex_id++;
|
||||
avl_insert(_mutextree, (void *)mutex);
|
||||
igloo_avl_insert(_mutextree, (void *)mutex);
|
||||
_mutex_unlock(&_mutextree_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void thread_mutex_destroy (mutex_t *mutex)
|
||||
void igloo_thread_mutex_destroy (mutex_t *mutex)
|
||||
{
|
||||
pthread_mutex_destroy(&mutex->sys_mutex);
|
||||
|
||||
#ifdef DEBUG_MUTEXES
|
||||
_mutex_lock(&_mutextree_mutex);
|
||||
avl_delete(_mutextree, mutex, _free_mutex);
|
||||
igloo_avl_delete(_mutextree, mutex, _free_mutex);
|
||||
_mutex_unlock(&_mutextree_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
||||
void igloo_thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
||||
{
|
||||
#ifdef DEBUG_MUTEXES
|
||||
thread_type *th = thread_self();
|
||||
thread_type *th = igloo_thread_self();
|
||||
|
||||
if (!th) LOG_WARN("No mt record for %u in lock [%s:%d]", thread_self(), file, line);
|
||||
if (!th) LOG_WARN("No mt record for %u in lock [%s:%d]", igloo_thread_self(), file, line);
|
||||
|
||||
LOG_DEBUG5("Locking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);
|
||||
|
||||
@ -387,7 +387,7 @@ void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
||||
|
||||
_mutex_lock(&_mutextree_mutex);
|
||||
|
||||
node = avl_get_first (_mutextree);
|
||||
node = igloo_avl_get_first (_mutextree);
|
||||
|
||||
while (node) {
|
||||
tmutex = (mutex_t *)node->key;
|
||||
@ -406,7 +406,7 @@ void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
||||
locks++;
|
||||
}
|
||||
|
||||
node = avl_get_next(node);
|
||||
node = igloo_avl_get_next(node);
|
||||
}
|
||||
|
||||
if (locks > 0) {
|
||||
@ -438,13 +438,13 @@ void thread_mutex_lock_c(mutex_t *mutex, int line, char *file)
|
||||
#endif /* DEBUG_MUTEXES */
|
||||
}
|
||||
|
||||
void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file)
|
||||
void igloo_thread_mutex_unlock_c(mutex_t *mutex, int line, char *file)
|
||||
{
|
||||
#ifdef DEBUG_MUTEXES
|
||||
thread_type *th = thread_self();
|
||||
thread_type *th = igloo_thread_self();
|
||||
|
||||
if (!th) {
|
||||
LOG_ERROR3("No record for %u in unlock [%s:%d]", thread_self(), file, line);
|
||||
LOG_ERROR3("No record for %u in unlock [%s:%d]", igloo_thread_self(), file, line);
|
||||
}
|
||||
|
||||
LOG_DEBUG5("Unlocking %p (%s) on line %d in file %s by thread %d", mutex, mutex->name, line, file, th ? th->thread_id : -1);
|
||||
@ -473,7 +473,7 @@ void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file)
|
||||
locks++;
|
||||
}
|
||||
|
||||
node = avl_get_next (node);
|
||||
node = igloo_avl_get_next (node);
|
||||
}
|
||||
|
||||
if ((locks > 0) && (_multi_mutex.thread_id != th->thread_id)) {
|
||||
@ -503,29 +503,29 @@ void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file)
|
||||
#endif /* DEBUG_MUTEXES */
|
||||
}
|
||||
|
||||
void thread_cond_create_c(cond_t *cond, int line, char *file)
|
||||
void igloo_thread_cond_create_c(cond_t *cond, int line, char *file)
|
||||
{
|
||||
pthread_cond_init(&cond->sys_cond, NULL);
|
||||
pthread_mutex_init(&cond->cond_mutex, NULL);
|
||||
}
|
||||
|
||||
void thread_cond_destroy(cond_t *cond)
|
||||
void igloo_thread_cond_destroy(cond_t *cond)
|
||||
{
|
||||
pthread_mutex_destroy(&cond->cond_mutex);
|
||||
pthread_cond_destroy(&cond->sys_cond);
|
||||
}
|
||||
|
||||
void thread_cond_signal_c(cond_t *cond, int line, char *file)
|
||||
void igloo_thread_cond_signal_c(cond_t *cond, int line, char *file)
|
||||
{
|
||||
pthread_cond_signal(&cond->sys_cond);
|
||||
}
|
||||
|
||||
void thread_cond_broadcast_c(cond_t *cond, int line, char *file)
|
||||
void igloo_thread_cond_broadcast_c(cond_t *cond, int line, char *file)
|
||||
{
|
||||
pthread_cond_broadcast(&cond->sys_cond);
|
||||
}
|
||||
|
||||
void thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file)
|
||||
void igloo_thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file)
|
||||
{
|
||||
struct timespec time;
|
||||
|
||||
@ -537,41 +537,41 @@ void thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file)
|
||||
pthread_mutex_unlock(&cond->cond_mutex);
|
||||
}
|
||||
|
||||
void thread_cond_wait_c(cond_t *cond, int line, char *file)
|
||||
void igloo_thread_cond_wait_c(cond_t *cond, int line, char *file)
|
||||
{
|
||||
pthread_mutex_lock(&cond->cond_mutex);
|
||||
pthread_cond_wait(&cond->sys_cond, &cond->cond_mutex);
|
||||
pthread_mutex_unlock(&cond->cond_mutex);
|
||||
}
|
||||
|
||||
void thread_rwlock_create_c(rwlock_t *rwlock, int line, char *file)
|
||||
void igloo_thread_rwlock_create_c(rwlock_t *rwlock, int line, char *file)
|
||||
{
|
||||
pthread_rwlock_init(&rwlock->sys_rwlock, NULL);
|
||||
}
|
||||
|
||||
void thread_rwlock_destroy(rwlock_t *rwlock)
|
||||
void igloo_thread_rwlock_destroy(rwlock_t *rwlock)
|
||||
{
|
||||
pthread_rwlock_destroy(&rwlock->sys_rwlock);
|
||||
}
|
||||
|
||||
void thread_rwlock_rlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
void igloo_thread_rwlock_rlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
{
|
||||
pthread_rwlock_rdlock(&rwlock->sys_rwlock);
|
||||
}
|
||||
|
||||
void thread_rwlock_wlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
void igloo_thread_rwlock_wlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
{
|
||||
pthread_rwlock_wrlock(&rwlock->sys_rwlock);
|
||||
}
|
||||
|
||||
void thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
void igloo_thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file)
|
||||
{
|
||||
pthread_rwlock_unlock(&rwlock->sys_rwlock);
|
||||
}
|
||||
|
||||
void thread_exit_c(long val, int line, char *file)
|
||||
void igloo_thread_exit_c(long val, int line, char *file)
|
||||
{
|
||||
thread_type *th = thread_self();
|
||||
thread_type *th = igloo_thread_self();
|
||||
|
||||
#if defined(DEBUG_MUTEXES) && defined(CHECK_MUTEXES)
|
||||
if (th) {
|
||||
@ -589,7 +589,7 @@ void thread_exit_c(long val, int line, char *file)
|
||||
th->thread_id, th->name, file, line, mutex_to_string(tmutex, name));
|
||||
}
|
||||
|
||||
node = avl_get_next (node);
|
||||
node = igloo_avl_get_next (node);
|
||||
}
|
||||
|
||||
_mutex_unlock(&_mutextree_mutex);
|
||||
@ -602,16 +602,16 @@ void thread_exit_c(long val, int line, char *file)
|
||||
LOG_INFO4("Removing thread %d [%s] started at [%s:%d], reason: 'Thread Exited'", th->thread_id, th->name, th->file, th->line);
|
||||
#endif
|
||||
|
||||
_mutex_lock(&_threadtree_mutex);
|
||||
avl_delete(_threadtree, th, _free_thread);
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
_mutex_lock(&igloo__threadtree_mutex);
|
||||
igloo_avl_delete(igloo__threadtree, th, igloo__free_thread);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
}
|
||||
|
||||
pthread_exit ((void*)val);
|
||||
}
|
||||
|
||||
/* sleep for a number of microseconds */
|
||||
void thread_sleep(unsigned long len)
|
||||
void igloo_thread_sleep(unsigned long len)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
Sleep(len / 1000);
|
||||
@ -642,7 +642,7 @@ void thread_sleep(unsigned long len)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *_start_routine(void *arg)
|
||||
static void *igloo__start_routine(void *arg)
|
||||
{
|
||||
thread_start_t *start = (thread_start_t *)arg;
|
||||
void *(*start_routine)(void *) = start->start_routine;
|
||||
@ -652,10 +652,10 @@ static void *_start_routine(void *arg)
|
||||
_block_signals();
|
||||
|
||||
/* insert thread into thread tree here */
|
||||
_mutex_lock(&_threadtree_mutex);
|
||||
_mutex_lock(&igloo__threadtree_mutex);
|
||||
thread->sys_thread = pthread_self();
|
||||
avl_insert(_threadtree, (void *)thread);
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
igloo_avl_insert(igloo__threadtree, (void *)thread);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
|
||||
#ifdef THREAD_DEBUG
|
||||
LOG_INFO4("Added thread %d [%s] started at [%s:%d]", thread->thread_id, thread->name, thread->file, thread->line);
|
||||
@ -671,43 +671,43 @@ static void *_start_routine(void *arg)
|
||||
|
||||
if (thread->detached)
|
||||
{
|
||||
_mutex_lock (&_threadtree_mutex);
|
||||
avl_delete (_threadtree, thread, _free_thread);
|
||||
_mutex_unlock (&_threadtree_mutex);
|
||||
_mutex_lock (&igloo__threadtree_mutex);
|
||||
igloo_avl_delete (igloo__threadtree, thread, igloo__free_thread);
|
||||
_mutex_unlock (&igloo__threadtree_mutex);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
thread_type *thread_self(void)
|
||||
thread_type *igloo_thread_self(void)
|
||||
{
|
||||
avl_node *node;
|
||||
thread_type *th;
|
||||
pthread_t sys_thread = pthread_self();
|
||||
|
||||
_mutex_lock(&_threadtree_mutex);
|
||||
_mutex_lock(&igloo__threadtree_mutex);
|
||||
|
||||
if (_threadtree == NULL) {
|
||||
if (igloo__threadtree == NULL) {
|
||||
#ifdef THREAD_DEBUG
|
||||
LOG_WARN("Thread tree is empty, this must be wrong!");
|
||||
#endif
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node = avl_get_first(_threadtree);
|
||||
node = igloo_avl_get_first(igloo__threadtree);
|
||||
|
||||
while (node) {
|
||||
th = (thread_type *)node->key;
|
||||
|
||||
if (th && pthread_equal(sys_thread, th->sys_thread)) {
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
return th;
|
||||
}
|
||||
|
||||
node = avl_get_next(node);
|
||||
node = igloo_avl_get_next(node);
|
||||
}
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
|
||||
|
||||
#ifdef THREAD_DEBUG
|
||||
@ -717,11 +717,11 @@ thread_type *thread_self(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void thread_rename(const char *name)
|
||||
void igloo_thread_rename(const char *name)
|
||||
{
|
||||
thread_type *th;
|
||||
|
||||
th = thread_self();
|
||||
th = igloo_thread_self();
|
||||
if (th->name) free(th->name);
|
||||
|
||||
th->name = strdup(name);
|
||||
@ -738,24 +738,24 @@ static void _mutex_unlock(mutex_t *mutex)
|
||||
}
|
||||
|
||||
|
||||
void thread_library_lock(void)
|
||||
void igloo_thread_library_lock(void)
|
||||
{
|
||||
_mutex_lock(&_library_mutex);
|
||||
_mutex_lock(&igloo__library_mutex);
|
||||
}
|
||||
|
||||
void thread_library_unlock(void)
|
||||
void igloo_thread_library_unlock(void)
|
||||
{
|
||||
_mutex_unlock(&_library_mutex);
|
||||
_mutex_unlock(&igloo__library_mutex);
|
||||
}
|
||||
|
||||
void thread_join(thread_type *thread)
|
||||
void igloo_thread_join(thread_type *thread)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
pthread_join(thread->sys_thread, &ret);
|
||||
_mutex_lock(&_threadtree_mutex);
|
||||
avl_delete(_threadtree, thread, _free_thread);
|
||||
_mutex_unlock(&_threadtree_mutex);
|
||||
_mutex_lock(&igloo__threadtree_mutex);
|
||||
igloo_avl_delete(igloo__threadtree, thread, igloo__free_thread);
|
||||
_mutex_unlock(&igloo__threadtree_mutex);
|
||||
}
|
||||
|
||||
/* AVL tree functions */
|
||||
@ -776,7 +776,7 @@ static int _compare_mutexes(void *compare_arg, void *a, void *b)
|
||||
}
|
||||
#endif
|
||||
|
||||
static int _compare_threads(void *compare_arg, void *a, void *b)
|
||||
static int igloo__compare_threads(void *compare_arg, void *a, void *b)
|
||||
{
|
||||
thread_type *t1, *t2;
|
||||
|
||||
@ -808,7 +808,7 @@ static int _free_mutex(void *key)
|
||||
}
|
||||
#endif
|
||||
|
||||
static int _free_thread(void *key)
|
||||
static int igloo__free_thread(void *key)
|
||||
{
|
||||
thread_type *t;
|
||||
|
||||
@ -826,26 +826,26 @@ static int _free_thread(void *key)
|
||||
|
||||
|
||||
#ifdef HAVE_PTHREAD_SPIN_LOCK
|
||||
void thread_spin_create (spin_t *spin)
|
||||
void igloo_thread_spin_create (spin_t *spin)
|
||||
{
|
||||
int x = pthread_spin_init (&spin->lock, PTHREAD_PROCESS_PRIVATE);
|
||||
if (x)
|
||||
abort();
|
||||
}
|
||||
|
||||
void thread_spin_destroy (spin_t *spin)
|
||||
void igloo_thread_spin_destroy (spin_t *spin)
|
||||
{
|
||||
pthread_spin_destroy (&spin->lock);
|
||||
}
|
||||
|
||||
void thread_spin_lock (spin_t *spin)
|
||||
void igloo_thread_spin_lock (spin_t *spin)
|
||||
{
|
||||
int x = pthread_spin_lock (&spin->lock);
|
||||
if (x != 0)
|
||||
abort();
|
||||
}
|
||||
|
||||
void thread_spin_unlock (spin_t *spin)
|
||||
void igloo_thread_spin_unlock (spin_t *spin)
|
||||
{
|
||||
pthread_spin_unlock (&spin->lock);
|
||||
}
|
||||
|
@ -101,32 +101,32 @@ typedef struct
|
||||
pthread_spinlock_t lock;
|
||||
} spin_t;
|
||||
|
||||
void thread_spin_create (spin_t *spin);
|
||||
void thread_spin_destroy (spin_t *spin);
|
||||
void thread_spin_lock (spin_t *spin);
|
||||
void thread_spin_unlock (spin_t *spin);
|
||||
void igloo_thread_spin_create (spin_t *spin);
|
||||
void igloo_thread_spin_destroy (spin_t *spin);
|
||||
void igloo_thread_spin_lock (spin_t *spin);
|
||||
void igloo_thread_spin_unlock (spin_t *spin);
|
||||
#else
|
||||
typedef mutex_t spin_t;
|
||||
#define thread_spin_create(x) thread_mutex_create(x)
|
||||
#define thread_spin_destroy(x) thread_mutex_destroy(x)
|
||||
#define thread_spin_lock(x) thread_mutex_lock(x)
|
||||
#define thread_spin_unlock(x) thread_mutex_unlock(x)
|
||||
#define igloo_thread_spin_create(x) thread_mutex_create(x)
|
||||
#define igloo_thread_spin_destroy(x) igloo_thread_mutex_destroy(x)
|
||||
#define igloo_thread_spin_lock(x) thread_mutex_lock(x)
|
||||
#define igloo_thread_spin_unlock(x) thread_mutex_unlock(x)
|
||||
#endif
|
||||
|
||||
#define thread_create(n,x,y,z) thread_create_c(n,x,y,z,__LINE__,__FILE__)
|
||||
#define thread_mutex_create(x) thread_mutex_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_mutex_lock(x) thread_mutex_lock_c(x,__LINE__,__FILE__)
|
||||
#define thread_mutex_unlock(x) thread_mutex_unlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_create(x) thread_cond_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_signal(x) thread_cond_signal_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_broadcast(x) thread_cond_broadcast_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_wait(x) thread_cond_wait_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_timedwait(x,t) thread_cond_wait_c(x,t,__LINE__,__FILE__)
|
||||
#define thread_rwlock_create(x) thread_rwlock_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_rlock(x) thread_rwlock_rlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_wlock(x) thread_rwlock_wlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_unlock(x) thread_rwlock_unlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_exit(x) thread_exit_c(x,__LINE__,__FILE__)
|
||||
#define thread_create(n,x,y,z) igloo_thread_create_c(n,x,y,z,__LINE__,__FILE__)
|
||||
#define thread_mutex_create(x) igloo_thread_mutex_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_mutex_lock(x) igloo_thread_mutex_lock_c(x,__LINE__,__FILE__)
|
||||
#define thread_mutex_unlock(x) igloo_thread_mutex_unlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_create(x) igloo_thread_cond_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_signal(x) igloo_thread_cond_signal_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_broadcast(x) igloo_thread_cond_broadcast_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_wait(x) igloo_thread_cond_wait_c(x,__LINE__,__FILE__)
|
||||
#define thread_cond_timedwait(x,t) igloo_thread_cond_wait_c(x,t,__LINE__,__FILE__)
|
||||
#define thread_rwlock_create(x) igloo_thread_rwlock_create_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_rlock(x) igloo_thread_rwlock_rlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_wlock(x) igloo_thread_rwlock_wlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_rwlock_unlock(x) igloo_thread_rwlock_unlock_c(x,__LINE__,__FILE__)
|
||||
#define thread_exit(x) igloo_thread_exit_c(x,__LINE__,__FILE__)
|
||||
|
||||
#define MUTEX_STATE_NOTLOCKED -1
|
||||
#define MUTEX_STATE_NEVERLOCKED -2
|
||||
@ -135,45 +135,45 @@ typedef mutex_t spin_t;
|
||||
#define THREAD_ATTACHED 0
|
||||
|
||||
/* init/shutdown of the library */
|
||||
void thread_initialize(void);
|
||||
void igloo_thread_initialize(void);
|
||||
void thread_initialize_with_log_id(int log_id);
|
||||
void thread_shutdown(void);
|
||||
void igloo_thread_shutdown(void);
|
||||
|
||||
/* creation, destruction, locking, unlocking, signalling and waiting */
|
||||
thread_type *thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
thread_type *igloo_thread_create_c(char *name, void *(*start_routine)(void *),
|
||||
void *arg, int detached, int line, char *file);
|
||||
void thread_mutex_create_c(mutex_t *mutex, int line, char *file);
|
||||
void thread_mutex_lock_c(mutex_t *mutex, int line, char *file);
|
||||
void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file);
|
||||
void thread_mutex_destroy(mutex_t *mutex);
|
||||
void thread_cond_create_c(cond_t *cond, int line, char *file);
|
||||
void thread_cond_signal_c(cond_t *cond, int line, char *file);
|
||||
void thread_cond_broadcast_c(cond_t *cond, int line, char *file);
|
||||
void thread_cond_wait_c(cond_t *cond, int line, char *file);
|
||||
void thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file);
|
||||
void thread_cond_destroy(cond_t *cond);
|
||||
void thread_rwlock_create_c(rwlock_t *rwlock, int line, char *file);
|
||||
void thread_rwlock_rlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void thread_rwlock_wlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void thread_rwlock_destroy(rwlock_t *rwlock);
|
||||
void thread_exit_c(long val, int line, char *file);
|
||||
void igloo_thread_mutex_create_c(mutex_t *mutex, int line, char *file);
|
||||
void igloo_thread_mutex_lock_c(mutex_t *mutex, int line, char *file);
|
||||
void igloo_thread_mutex_unlock_c(mutex_t *mutex, int line, char *file);
|
||||
void igloo_thread_mutex_destroy(mutex_t *mutex);
|
||||
void igloo_thread_cond_create_c(cond_t *cond, int line, char *file);
|
||||
void igloo_thread_cond_signal_c(cond_t *cond, int line, char *file);
|
||||
void igloo_thread_cond_broadcast_c(cond_t *cond, int line, char *file);
|
||||
void igloo_thread_cond_wait_c(cond_t *cond, int line, char *file);
|
||||
void igloo_thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file);
|
||||
void igloo_thread_cond_destroy(cond_t *cond);
|
||||
void igloo_thread_rwlock_create_c(rwlock_t *rwlock, int line, char *file);
|
||||
void igloo_thread_rwlock_rlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void igloo_thread_rwlock_wlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void igloo_thread_rwlock_unlock_c(rwlock_t *rwlock, int line, char *file);
|
||||
void igloo_thread_rwlock_destroy(rwlock_t *rwlock);
|
||||
void igloo_thread_exit_c(long val, int line, char *file);
|
||||
|
||||
/* sleeping */
|
||||
void thread_sleep(unsigned long len);
|
||||
void igloo_thread_sleep(unsigned long len);
|
||||
|
||||
/* for using library functions which aren't threadsafe */
|
||||
void thread_library_lock(void);
|
||||
void thread_library_unlock(void);
|
||||
#define PROTECT_CODE(code) { thread_library_lock(); code; thread_library_unlock(); }
|
||||
void igloo_thread_library_lock(void);
|
||||
void igloo_thread_library_unlock(void);
|
||||
#define PROTECT_CODE(code) { igloo_thread_library_lock(); code; igloo_thread_library_unlock(); }
|
||||
|
||||
/* thread information functions */
|
||||
thread_type *thread_self(void);
|
||||
thread_type *igloo_thread_self(void);
|
||||
|
||||
/* renames current thread */
|
||||
void thread_rename(const char *name);
|
||||
void igloo_thread_rename(const char *name);
|
||||
|
||||
/* waits until thread_exit is called for another thread */
|
||||
void thread_join(thread_type *thread);
|
||||
void igloo_thread_join(thread_type *thread);
|
||||
|
||||
#endif /* __THREAD_H__ */
|
||||
|
@ -64,7 +64,7 @@
|
||||
/*
|
||||
* Returns milliseconds no matter what.
|
||||
*/
|
||||
uint64_t timing_get_time(void)
|
||||
uint64_t igloo_timing_get_time(void)
|
||||
{
|
||||
#ifdef HAVE_GETTIMEOFDAY
|
||||
struct timeval mtv;
|
||||
@ -83,7 +83,7 @@ uint64_t timing_get_time(void)
|
||||
}
|
||||
|
||||
|
||||
void timing_sleep(uint64_t sleeptime)
|
||||
void igloo_timing_sleep(uint64_t sleeptime)
|
||||
{
|
||||
struct timeval sleeper;
|
||||
|
||||
|
@ -40,7 +40,7 @@ typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#endif
|
||||
|
||||
uint64_t timing_get_time(void);
|
||||
void timing_sleep(uint64_t sleeptime);
|
||||
uint64_t igloo_timing_get_time(void);
|
||||
void igloo_timing_sleep(uint64_t sleeptime);
|
||||
|
||||
#endif /* __TIMING_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user