diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/hash.c | 200 | ||||
-rw-r--r-- | common/hash.h | 136 | ||||
-rw-r--r-- | common/sock_any.c | 91 | ||||
-rw-r--r-- | common/sock_any.h | 59 |
4 files changed, 309 insertions, 177 deletions
diff --git a/common/hash.c b/common/hash.c index 9bbe8ac..5f3308c 100644 --- a/common/hash.c +++ b/common/hash.c @@ -22,11 +22,11 @@ #include <stdlib.h> #include "hash.h" -#ifdef HASH_TIMESTAMP +#ifdef HSH_TIMESTAMP #include <time.h> #endif -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS #define KEY_DATA(he) (void*)(((unsigned char*)(he)) + sizeof(*(he))) #else #define KEY_DATA(he) ((he)->key) @@ -41,18 +41,18 @@ * isn't too bad given that pools have a low allocation overhead. */ -typedef struct hash_entry_t hash_entry_t; +typedef struct hsh_entry_t hsh_entry_t; -struct hash_entry_t +struct hsh_entry_t { - hash_entry_t* next; + hsh_entry_t* next; unsigned int hash; -#ifndef HASH_COPYKEYS +#ifndef HSH_COPYKEYS const void* key; size_t klen; #endif const void* val; -#ifdef HASH_TIMESTAMP +#ifdef HSH_TIMESTAMP time_t stamp; #endif }; @@ -62,13 +62,13 @@ struct hash_entry_t * * We keep a pointer to the next hash entry here to allow the current * hash entry to be freed or otherwise mangled between calls to - * hash_next(). + * hsh_next(). */ -struct hash_index_t +struct hsh_index_t { - hash_t* ht; - hash_entry_t* ths; - hash_entry_t* next; + hsh_t* ht; + hsh_entry_t* ths; + hsh_entry_t* next; unsigned int index; }; @@ -79,30 +79,30 @@ struct hash_index_t * The count of hash entries may be greater depending on the chosen * collision rate. */ -struct hash_t +struct hsh_t { - hash_entry_t** array; - hash_index_t iterator; /* For hash_first(...) */ + hsh_entry_t** array; + hsh_index_t iterator; /* For hsh_first(...) */ unsigned int count; unsigned int max; -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS unsigned int klen; #endif -#ifdef HASH_CALLBACKS - hash_table_calls_t calls; +#ifdef HSH_CALLBACKS + hsh_table_calls_t calls; #endif }; #define INITIAL_MAX 15 /* tunable == 2^n - 1 */ -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS /* A copy of the memory call table we've been set to */ -static hash_memory_calls_t g_memory_calls_cpy; +static hsh_memory_calls_t g_memory_calls_cpy; /* Pointer to above. This indicates when we actually are set */ -static hash_memory_calls_t* g_memory_calls = NULL; +static hsh_memory_calls_t* g_memory_calls = NULL; static void* int_malloc(size_t len) { @@ -131,7 +131,7 @@ static void int_free(void* ptr) free(ptr); } -void hash_set_memory_calls(hash_memory_calls_t* hmc) +void hsh_set_memory_calls(hsh_memory_calls_t* hmc) { if(hmc == NULL) { @@ -144,7 +144,7 @@ void hash_set_memory_calls(hash_memory_calls_t* hmc) } } -void hash_set_table_calls(hash_t* ht, hash_table_calls_t* htc) +void hsh_set_table_calls(hsh_t* ht, hsh_table_calls_t* htc) { memcpy(&(ht->calls), htc, sizeof(ht->calls)); } @@ -161,24 +161,24 @@ void hash_set_table_calls(hash_t* ht, hash_table_calls_t* htc) * Hash creation functions. */ -static hash_entry_t** alloc_array(hash_t* ht, unsigned int max) +static hsh_entry_t** alloc_array(hsh_t* ht, unsigned int max) { - return int_malloc(sizeof(*(ht->array)) * (max + 1)); + return int_calloc(sizeof(*(ht->array)) * (max + 1)); } -#ifdef HASH_COPYKEYS -hash_t* hash_create(size_t klen) +#ifdef HSH_COPYKEYS +hsh_t* hsh_create(size_t klen) #else -hash_t* hash_create() +hsh_t* hsh_create() #endif { - hash_t* ht = int_malloc(sizeof(hash_t)); + hsh_t* ht = int_malloc(sizeof(hsh_t)); if(ht) { ht->count = 0; ht->max = INITIAL_MAX; ht->array = alloc_array(ht, ht->max); -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS ht->klen = klen; #endif if(!ht->array) @@ -190,13 +190,13 @@ hash_t* hash_create() return ht; } -void hash_free(hash_t* ht) +void hsh_free(hsh_t* ht) { - hash_index_t* hi; + hsh_index_t* hi; - for(hi = hash_first(ht); hi; hi = hash_next(hi)) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS if(hi->ths->val && ht->calls.f_freeval) (ht->calls.f_freeval)(ht->calls.arg, (void*)hi->ths->val); #endif @@ -213,7 +213,7 @@ void hash_free(hash_t* ht) * Hash iteration functions. */ -hash_index_t* hash_next(hash_index_t* hi) +hsh_index_t* hsh_next(hsh_index_t* hi) { hi->ths = hi->next; while(!hi->ths) @@ -227,27 +227,27 @@ hash_index_t* hash_next(hash_index_t* hi) return hi; } -hash_index_t* hash_first(hash_t* ht) +hsh_index_t* hsh_first(hsh_t* ht) { - hash_index_t* hi = &ht->iterator; + hsh_index_t* hi = &ht->iterator; hi->ht = ht; hi->index = 0; hi->ths = NULL; hi->next = NULL; - return hash_next(hi); + return hsh_next(hi); } -#ifdef HASH_COPYKEYS -void* hash_this(hash_index_t* hi, const void** key) +#ifdef HSH_COPYKEYS +void* hsh_this(hsh_index_t* hi, const void** key) #else -void* hash_this(hash_index_t* hi, const void** key, size_t* klen) +void* hsh_this(hsh_index_t* hi, const void** key, size_t* klen) #endif { if(key) *key = KEY_DATA(hi->ths); -#ifndef HASH_COPYKEYS +#ifndef HSH_COPYKEYS if(klen) *klen = hi->ths->klen; #endif @@ -260,10 +260,10 @@ void* hash_this(hash_index_t* hi, const void** key, size_t* klen) * Expanding a hash table */ -static int expand_array(hash_t* ht) +static int expand_array(hsh_t* ht) { - hash_index_t* hi; - hash_entry_t** new_array; + hsh_index_t* hi; + hsh_entry_t** new_array; unsigned int new_max; new_max = ht->max * 2 + 1; @@ -272,7 +272,7 @@ static int expand_array(hash_t* ht) if(!new_array) return 0; - for(hi = hash_first(ht); hi; hi = hash_next(hi)) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { unsigned int i = hi->ths->hash & new_max; hi->ths->next = new_array[i]; @@ -296,19 +296,19 @@ static int expand_array(hash_t* ht) * that hash entries can be removed. */ -#ifdef HASH_COPYKEYS -static hash_entry_t** find_entry(hash_t* ht, const void* key, const void* val) +#ifdef HSH_COPYKEYS +static hsh_entry_t** find_entry(hsh_t* ht, const void* key, const void* val) #else -static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const void* val) +static hsh_entry_t** find_entry(hsh_t* ht, const void* key, size_t klen, const void* val) #endif { - hash_entry_t** hep; - hash_entry_t* he; + hsh_entry_t** hep; + hsh_entry_t* he; const unsigned char* p; unsigned int hash; size_t i; -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS size_t klen = ht->klen; #endif @@ -351,8 +351,8 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const */ hash = 0; -#ifndef HASH_COPYKEYS - if(klen == HASH_KEY_STRING) +#ifndef HSH_COPYKEYS + if(klen == HSH_KEY_STRING) { for(p = key; *p; p++) hash = hash * 33 + *p; @@ -371,7 +371,7 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const he; hep = &he->next, he = *hep) { if(he->hash == hash && -#ifndef HASH_COPYKEYS +#ifndef HSH_COPYKEYS he->klen == klen && #endif memcmp(KEY_DATA(he), key, klen) == 0) @@ -382,7 +382,7 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const return hep; /* add a new entry for non-NULL val */ -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS he = int_malloc(sizeof(*he) + klen); #else he = int_malloc(sizeof(*he)); @@ -390,7 +390,7 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const if(he) { -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS /* Key data points past end of entry */ memcpy(KEY_DATA(he), key, klen); #else @@ -403,7 +403,7 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const he->hash = hash; he->val = val; -#ifdef HASH_TIMESTAMP +#ifdef HSH_TIMESTAMP he->stamp = 0; #endif @@ -414,14 +414,14 @@ static hash_entry_t** find_entry(hash_t* ht, const void* key, size_t klen, const return hep; } -#ifdef HASH_COPYKEYS -void* hash_get(hash_t* ht, const void *key) +#ifdef HSH_COPYKEYS +void* hsh_get(hsh_t* ht, const void *key) { - hash_entry_t** he = find_entry(ht, key, NULL); + hsh_entry_t** he = find_entry(ht, key, NULL); #else -void* hash_get(hash_t* ht, const void *key, size_t klen) +void* hsh_get(hsh_t* ht, const void *key, size_t klen) { - hash_entry_t** he = find_entry(ht, key, klen, NULL); + hsh_entry_t** he = find_entry(ht, key, klen, NULL); #endif if(he && *he) @@ -430,19 +430,19 @@ void* hash_get(hash_t* ht, const void *key, size_t klen) return NULL; } -#ifdef HASH_COPYKEYS -int hash_set(hash_t* ht, const void* key, void* val) +#ifdef HSH_COPYKEYS +int hsh_set(hsh_t* ht, const void* key, void* val) { - hash_entry_t** hep = find_entry(ht, key, val); + hsh_entry_t** hep = find_entry(ht, key, val); #else -int hash_set(hash_t* ht, const void* key, size_t klen, void* val) +int hsh_set(hsh_t* ht, const void* key, size_t klen, void* val) { - hash_entry_t** hep = find_entry(ht, key, klen, val); + hsh_entry_t** hep = find_entry(ht, key, klen, val); #endif if(hep && *hep) { -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS if((*hep)->val && (*hep)->val != val && ht->calls.f_freeval) (ht->calls.f_freeval)(ht->calls.arg, (void*)((*hep)->val)); #endif @@ -450,7 +450,7 @@ int hash_set(hash_t* ht, const void* key, size_t klen, void* val) /* replace entry */ (*hep)->val = val; -#ifdef HASH_TIMESTAMP +#ifdef HSH_TIMESTAMP /* Update or set the timestamp */ (*hep)->stamp = time(NULL); #endif @@ -468,20 +468,20 @@ int hash_set(hash_t* ht, const void* key, size_t klen, void* val) return 0; } -#ifdef HASH_COPYKEYS -void* hash_rem(hash_t* ht, const void* key) +#ifdef HSH_COPYKEYS +void* hsh_rem(hsh_t* ht, const void* key) { - hash_entry_t** hep = find_entry(ht, key, NULL); + hsh_entry_t** hep = find_entry(ht, key, NULL); #else -void* hash_rem(hash_t* ht, const void* key, size_t klen) +void* hsh_rem(hsh_t* ht, const void* key, size_t klen) { - hash_entry_t** hep = find_entry(ht, key, klen, NULL); + hsh_entry_t** hep = find_entry(ht, key, klen, NULL); #endif void* val = NULL; if(hep && *hep) { - hash_entry_t* old = *hep; + hsh_entry_t* old = *hep; *hep = (*hep)->next; --ht->count; val = (void*)old->val; @@ -491,30 +491,30 @@ void* hash_rem(hash_t* ht, const void* key, size_t klen) return val; } -unsigned int hash_count(hash_t* ht) +unsigned int hsh_count(hsh_t* ht) { return ht->count; } -#ifdef HASH_TIMESTAMP -int hash_purge(hash_t* ht, time_t stamp) +#ifdef HSH_TIMESTAMP +int hsh_purge(hsh_t* ht, time_t stamp) { - hash_index_t* hi; + hsh_index_t* hi; int r = 0; void* val; - for(hi = hash_first(ht); hi; hi = hash_next(hi)) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { if(hi->ths->stamp < stamp) { /* No need to check for errors as we're deleting */ -#ifdef HASH_COPYKEYS - val = hash_rem(ht, KEY_DATA(hi->ths)); +#ifdef HSH_COPYKEYS + val = hsh_rem(ht, KEY_DATA(hi->ths)); #else - val = hash_rem(ht, hi->ths->key, hi->ths->klen); + val = hsh_rem(ht, hi->ths->key, hi->ths->klen); #endif -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS if(val && ht->calls.f_freeval) (ht->calls.f_freeval)(ht->calls.arg, val); #endif @@ -526,37 +526,37 @@ int hash_purge(hash_t* ht, time_t stamp) return r; } -#ifdef HASH_COPYKEYS -void hash_touch(hash_t* ht, const void* key) +#ifdef HSH_COPYKEYS +void hsh_touch(hsh_t* ht, const void* key) { - hash_entry_t** hep = find_entry(ht, key, NULL); + hsh_entry_t** hep = find_entry(ht, key, NULL); #else -void hash_touch(hash_t* ht, const void* key, size_t* klen) +void hsh_touch(hsh_t* ht, const void* key, size_t* klen) { - hash_entry_t** hep = find_entry(ht, key, klen, NULL); + hsh_entry_t** hep = find_entry(ht, key, klen, NULL); #endif if(hep && *hep) ((*hep)->stamp) = time(NULL); } -int hash_bump(hash_t* ht) +int hsh_bump(hsh_t* ht) { - hash_index_t* hi; + hsh_index_t* hi; void* key = NULL; void* val = NULL; time_t least = 0; -#ifndef HASH_COPYKEYS +#ifndef HSH_COPYKEYS size_t klen = 0; #endif - for(hi = hash_first(ht); hi; hi = hash_next(hi)) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { if(least == 0 || hi->ths->stamp < least) { least = hi->ths->stamp; key = KEY_DATA(hi->ths); -#ifndef HASH_COPYKEYS +#ifndef HSH_COPYKEYS klen = hi->this->klen; #endif } @@ -564,13 +564,13 @@ int hash_bump(hash_t* ht) if(key) { -#ifdef HASH_COPYKEYS - val = hash_rem(ht, key); +#ifdef HSH_COPYKEYS + val = hsh_rem(ht, key); #else - val = hash_rem(ht, key, klen); + val = hsh_rem(ht, key, klen); #endif -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS if(val && ht->calls.f_freeval) (ht->calls.f_freeval)(ht->calls.arg, (void*)val); #endif @@ -581,4 +581,4 @@ int hash_bump(hash_t* ht) return 0; } -#endif /* HASH_TIMESTAMP */ +#endif /* HSH_TIMESTAMP */ diff --git a/common/hash.h b/common/hash.h index 174c209..fef2803 100644 --- a/common/hash.h +++ b/common/hash.h @@ -18,8 +18,8 @@ * limitations under the License. */ -#ifndef __HASH_H__ -#define __HASH_H__ +#ifndef __HSH_H__ +#define __HSH_H__ #ifdef __cplusplus extern "C" { @@ -35,13 +35,13 @@ extern "C" { */ /* Keep timestamps for the entries */ -#define HASH_TIMESTAMP +#define HSH_TIMESTAMP /* Keep key values internally */ -#define HASH_COPYKEYS +#define HSH_COPYKEYS /* Hash callback functionality */ -#define HASH_CALLBACKS +#define HSH_CALLBACKS /* * ARGUMENT DOCUMENTATION @@ -60,10 +60,10 @@ extern "C" { */ /* Abstract type for hash tables. */ -typedef struct hash_t hash_t; +typedef struct hsh_t hsh_t; /* Abstract type for scanning hash tables. */ -typedef struct hash_index_t hash_index_t; +typedef struct hsh_index_t hsh_index_t; /* ---------------------------------------------------------------------------------- @@ -73,60 +73,60 @@ typedef struct hash_index_t hash_index_t; * keys stored in the hashtable itself. */ -#ifdef HASH_COPYKEYS +#ifdef HSH_COPYKEYS /* - * hash_create : Create a hash table + * hsh_create : Create a hash table * - returns an allocated hashtable */ -hash_t* hash_create(size_t klen); +hsh_t* hsh_create(size_t klen); /* - * hash_free : Free a hash table + * hsh_free : Free a hash table */ -void hash_free(hash_t* ht); +void hsh_free(hsh_t* ht); /* - * hash_count: Number of values in hash table + * hsh_count: Number of values in hash table * - returns the number of entries in hash table */ -unsigned int hash_count(hash_t* ht); +unsigned int hsh_count(hsh_t* ht); /* - * hash_get: Retrieves a value from the hash table + * hsh_get: Retrieves a value from the hash table * - returns the value of the entry */ -void* hash_get(hash_t* ht, const void* key); +void* hsh_get(hsh_t* ht, const void* key); /* - * hash_set: Set a value in the hash table + * hsh_set: Set a value in the hash table * - returns 1 if the entry was added properly */ -int hash_set(hash_t* ht, const void* key, void* val); +int hsh_set(hsh_t* ht, const void* key, void* val); /* - * hash_rem: Remove a value from the hash table + * hsh_rem: Remove a value from the hash table * - returns the value of the removed entry */ -void* hash_rem(hash_t* ht, const void* key); +void* hsh_rem(hsh_t* ht, const void* key); /* - * hash_first: Start enumerating through the hash table + * hsh_first: Start enumerating through the hash table * - returns a hash iterator */ -hash_index_t* hash_first(hash_t* ht); +hsh_index_t* hsh_first(hsh_t* ht); /* - * hash_next: Enumerate through hash table + * hsh_next: Enumerate through hash table * - returns the hash iterator or null when no more entries */ -hash_index_t* hash_next(hash_index_t* hi); +hsh_index_t* hsh_next(hsh_index_t* hi); /* - * hash_this: While enumerating get current value + * hsh_this: While enumerating get current value * - returns the value that the iterator currently points to */ -void* hash_this(hash_index_t* hi, const void** key); +void* hsh_this(hsh_index_t* hi, const void** key); @@ -140,63 +140,63 @@ void* hash_this(hash_index_t* hi, const void** key); #else /* - * hash_create : Create a hash table + * hsh_create : Create a hash table * - returns an allocated hashtable */ -hash_t* hash_create(); +hsh_t* hsh_create(); /* - * hash_free : Free a hash table + * hsh_free : Free a hash table */ -void hash_free(hash_t* ht); +void hsh_free(hsh_t* ht); /* - * hash_count: Number of values in hash table + * hsh_count: Number of values in hash table * - returns the number of entries in hash table */ -unsigned int hash_count(hash_t* ht); +unsigned int hsh_count(hsh_t* ht); /* - * hash_get: Retrieves a value from the hash table + * hsh_get: Retrieves a value from the hash table * - returns the value of the entry */ -void* hash_get(hash_t* ht, const void* key, size_t klen); +void* hsh_get(hsh_t* ht, const void* key, size_t klen); /* - * hash_set: Set a value in the hash table + * hsh_set: Set a value in the hash table * - returns 1 if the entry was added properly */ -int hash_set(hash_t* ht, const void* key, size_t klen, void* val); +int hsh_set(hsh_t* ht, const void* key, size_t klen, void* val); /* - * hash_rem: Remove a value from the hash table + * hsh_rem: Remove a value from the hash table * - returns the value of the removed entry */ -void* hash_rem(hash_t* ht, const void* key, size_t klen); +void* hsh_rem(hsh_t* ht, const void* key, size_t klen); /* - * hash_first: Start enumerating through the hash table + * hsh_first: Start enumerating through the hash table * - returns a hash iterator */ -hash_index_t* hash_first(hash_t* ht); +hsh_index_t* hsh_first(hsh_t* ht); /* - * hash_next: Enumerate through hash table + * hsh_next: Enumerate through hash table * - returns the hash iterator or null when no more entries */ -hash_index_t* hash_next(hash_index_t* hi); +hsh_index_t* hsh_next(hsh_index_t* hi); /* - * hash_this: While enumerating get current value + * hsh_this: While enumerating get current value * - returns the value that the iterator currently points to */ -void* hash_this(hash_index_t* hi, const void** key, size_t* klen); +void* hsh_this(hsh_index_t* hi, const void** key, size_t* klen); /* * This can be passed as 'klen' in any of the above functions to indicate * a string-valued key, and have hash compute the length automatically. */ -#define HASH_KEY_STRING (-1) +#define HSH_KEY_STRING (-1) #endif @@ -210,27 +210,27 @@ void* hash_this(hash_index_t* hi, const void** key, size_t* klen); * functions in addition to one of the above 'key' modes. */ -#ifdef HASH_TIMESTAMP +#ifdef HSH_TIMESTAMP /* - * hash_purge: Purge entries before a certain timestamp + * hsh_purge: Purge entries before a certain timestamp * - returns the number of entries purged */ -int hash_purge(hash_t* ht, time_t stamp); +int hsh_purge(hsh_t* ht, time_t stamp); /* - * hash_touch: Touch an entry to make it's timestamp current + * hsh_touch: Touch an entry to make it's timestamp current */ -#ifdef HASH_COPYKEYS - void hash_touch(hash_t* ht, const void* key); +#ifdef HSH_COPYKEYS + void hsh_touch(hsh_t* ht, const void* key); #else - void hash_touch(hash_t* ht, const void* key, size_t* klen); + void hsh_touch(hsh_t* ht, const void* key, size_t* klen); #endif /* - * hash_bump: Bumps the oldest entry out + * hsh_bump: Bumps the oldest entry out */ -int hash_bump(hash_t* ht); +int hsh_bump(hsh_t* ht); #endif @@ -243,34 +243,34 @@ int hash_bump(hash_t* ht); * well as to get callbacks when an item is discarded. */ -#ifdef HASH_CALLBACKS +#ifdef HSH_CALLBACKS -typedef void* (*hash_falloc)(void* arg, size_t len); -typedef void (*hash_ffree)(void* arg, void* ptr); +typedef void* (*hsh_falloc)(void* arg, size_t len); +typedef void (*hsh_ffree)(void* arg, void* ptr); -typedef struct hash_memory_calls +typedef struct hsh_memory_calls { - hash_falloc f_alloc; - hash_ffree f_free; + hsh_falloc f_alloc; + hsh_ffree f_free; void* arg; } -hash_memory_calls_t; +hsh_memory_calls_t; /* Set the global memory calls */ -void hash_set_memory_calls(hash_memory_calls_t* hmc); +void hsh_set_memory_calls(hsh_memory_calls_t* hmc); -typedef void (*hash_ffreeval)(void* arg, void* val); +typedef void (*hsh_ffreeval)(void* arg, void* val); -typedef struct hash_table_calls +typedef struct hsh_table_calls { - hash_ffreeval f_freeval; + hsh_ffreeval f_freeval; void* arg; } -hash_table_calls_t; +hsh_table_calls_t; /* Set the per table free value call */ -void hash_set_table_calls(hash_t* ht, hash_table_calls_t* htc); +void hsh_set_table_calls(hsh_t* ht, hsh_table_calls_t* htc); #endif @@ -281,4 +281,4 @@ void hash_set_table_calls(hash_t* ht, hash_table_calls_t* htc); } #endif -#endif /* __HASH_H__ */ +#endif /* __HSH_H__ */ diff --git a/common/sock_any.c b/common/sock_any.c index acac8ee..0018318 100644 --- a/common/sock_any.c +++ b/common/sock_any.c @@ -1,3 +1,40 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * CONTRIBUTORS + * Nate Nielsen <nielsen@memberwebs.com> + * + */ #include <stdlib.h> #include <errno.h> @@ -10,12 +47,15 @@ #include <arpa/inet.h> -int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport) +#define LOCALHOST_ADDR 0x7F000001 + +int sock_any_pton(const char* addr, struct sockaddr_any* any, int opts) { size_t l; - char buf[256]; /* TODO: Use a constant */ + char buf[256]; char* t; char* t2; + int defport = (opts & 0xFFFF); memset(any, 0, sizeof(*any)); @@ -32,13 +72,35 @@ int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport) if(l < PORT_MIN || l > PORT_MAX || addr[l] != 0) break; - port = strtol(t, &t2, 10); + port = strtol(addr, &t2, 10); if(*t2 || port <= 0 || port >= 65536) break; - any->s.in.sin_family = AF_INET; - any->s.in.sin_port = htons((unsigned short)(port <= 0 ? defport : port)); - any->s.in.sin_addr.s_addr = 0; + any->s.in.sin_port = htons(port); + + /* Fill in the type based on defaults */ +#ifdef HAVE_INET6 + if(opts & SANY_OPT_DEFINET6) + any->s.in.sin_family = AF_INET6; + else +#endif + any->s.in.sin_family = AF_INET; + + /* Fill in the address based on defaults */ + if(opts & SANY_OPT_DEFLOCAL) + { +#ifdef HAVE_INET6 + if(opts & SANY_OPT_DEFINET6) + memcpy(&(any->s.in.sin6_addr), &in6addr_loopback, sizeof(struct in6_addr)); + else +#endif + any->s.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + } + + /* + * Note the 'any' option is the default since we zero out + * the entire structure above. + */ any->namelen = sizeof(any->s.in); return AF_INET; @@ -237,9 +299,10 @@ int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport) return -1; } -int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen) +int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen, int opts) { int len = 0; + int port = 0; switch(any->s.a.sa_family) { @@ -257,12 +320,14 @@ int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen) case AF_INET: if(inet_ntop(any->s.a.sa_family, &(any->s.in.sin_addr), addr, addrlen) == NULL) return -1; + port = ntohs(any->s.in.sin_port); break; #ifdef HAVE_INET6 case AF_INET6: if(inet_ntop(any->s.a.sa_family, &(any->s.in6.sin6_addr), addr, addrlen) == NULL) return -1; + port = ntohs(any->s.in6.sin6_port); break; #endif @@ -271,5 +336,17 @@ int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen) return -1; } + if(!(opts & SANY_OPT_NOPORT) && port != 0) + { + strncat(addr, ":", addrlen); + addr[addrlen - 1] = 0; + + len = strlen(addr); + addr += len; + addrlen -= len; + + snprintf(addr, addrlen, "%d", port); + } + return 0; } diff --git a/common/sock_any.h b/common/sock_any.h index 693bd2a..0b492b4 100644 --- a/common/sock_any.h +++ b/common/sock_any.h @@ -1,3 +1,40 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * CONTRIBUTORS + * Nate Nielsen <nielsen@memberwebs.com> + * + */ #ifndef __SOCK_ANY_H__ #define __SOCK_ANY_H__ @@ -27,7 +64,25 @@ struct sockaddr_any #define SANY_LEN(any) ((any).namelen) #define SANY_TYPE(any) ((any).s.a.sa_family) -int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport); -int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen); +int sock_any_pton(const char* addr, struct sockaddr_any* any, int opts); + +/* The default port to fill in when no IP/IPv6 port specified */ +#define SANY_OPT_DEFPORT(p) (int)((p) & 0xFFFF) + +/* When only port specified default to IPANY */ +#define SANY_OPT_DEFANY 0x00000000 + +/* When only port specified default to LOCALHOST */ +#define SANY_OPT_DEFLOCAL 0x00100000 + +/* When only port specified default to IPv6 */ +#ifdef HAVE_INET6 +#define SANY_OPT_DEFINET6 0x00200000 +#endif + +int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen, int opts); + +/* Don't print the port */ +#define SANY_OPT_NOPORT 0x01000000 #endif /* __SOCK_ANY_H__ */ |