hash.h File Reference

The hash API provides an unpredictable hash value for variable length data. A hash object contains a random vector (which is hidden from clients of this API) to make the actual hash value unpredictable. More...

#include <isc/types.h>

Go to the source code of this file.

Defines

#define ISC_HASH_H   1

Functions

ISC_LANG_BEGINDECLS isc_result_t isc_hash_ctxcreate (isc_mem_t *mctx, isc_entropy_t *entropy, size_t limit, isc_hash_t **hctx)
isc_result_t isc_hash_create (isc_mem_t *mctx, isc_entropy_t *entropy, size_t limit)
 Create a new hash object.
void isc_hash_ctxattach (isc_hash_t *hctx, isc_hash_t **hctxp)
 Attach to a hash object.
void isc_hash_ctxdetach (isc_hash_t **hctxp)
 Detach from a hash object.
void isc_hash_destroy (void)
 This function is for the single-context mode, and is expected to be used as a counterpart of isc_hash_create().
void isc__hash_setvec (const isc_uint16_t *vec)
 Set the contents of the random vector used in hashing.
void isc_hash_ctxinit (isc_hash_t *hctx)
void isc_hash_init (void)
 Initialize a hash object.
unsigned int isc_hash_ctxcalc (isc_hash_t *hctx, const unsigned char *key, unsigned int keylen, isc_boolean_t case_sensitive)
unsigned int isc_hash_calc (const unsigned char *key, unsigned int keylen, isc_boolean_t case_sensitive)
 Calculate a hash value.


Detailed Description

The hash API provides an unpredictable hash value for variable length data. A hash object contains a random vector (which is hidden from clients of this API) to make the actual hash value unpredictable.

The algorithm used in the API guarantees the probability of hash collision; in the current implementation, as long as the values stored in the random vector are unpredictable, the probability of hash collision between arbitrary two different values is at most 1/2^16.

Although the API is generic about the hash keys, it mainly expects DNS names (and sometimes IPv4/v6 addresses) as inputs. It has an upper limit of the input length, and may run slow to calculate the hash values for large inputs.

This API is designed to be general so that it can provide multiple different hash contexts that have different random vectors. However, it should be typical to have a single context for an entire system. To support such cases, the API also provides a single-context mode.

Definition in file hash.h.


Define Documentation

#define ISC_HASH_H   1

Definition at line 21 of file hash.h.


Function Documentation

ISC_LANG_BEGINDECLS isc_result_t isc_hash_ctxcreate ( isc_mem_t mctx,
isc_entropy_t entropy,
size_t  limit,
isc_hash_t **  hctx 
)

Definition at line 142 of file hash.c.

References DESTROYLOCK, isc_hash::entropy, HASH_MAGIC, isc_hash::initialized, isc_entropy_attach(), ISC_FALSE, isc_mem_attach(), isc_mem_get, isc_mem_put, isc_mutex_init, ISC_R_NOMEMORY, ISC_R_RANGE, ISC_R_SUCCESS, isc_refcount_init(), isc_hash::limit, isc_hash::lock, isc_hash::magic, isc_hash::mctx, isc_hash::refcnt, REQUIRE, isc_hash::rndvector, and isc_hash::vectorlen.

Referenced by isc_hash_create().

isc_result_t isc_hash_create ( isc_mem_t mctx,
isc_entropy_t entropy,
size_t  limit 
)

Create a new hash object.

isc_hash_ctxcreate() creates a different object.

isc_hash_create() creates a module-internal object to support the single-context mode. It should be called only once.

'entropy' must be NULL or a valid entropy object. If 'entropy' is NULL, pseudo random values will be used to build the random vector, which may weaken security.

'limit' specifies the maximum number of hash keys. If it is too large, these functions may fail.

Definition at line 219 of file hash.c.

References createlock, initialize_lock(), INSIST, isc_hash_ctxcreate(), isc_once_do, ISC_R_SUCCESS, LOCK, once, REQUIRE, RUNTIME_CHECK, and UNLOCK.

Referenced by ATF_TC_BODY(), create_managers(), dns_test_begin(), initialize(), isc_test_begin(), main(), and setup_system().

void isc_hash_ctxattach ( isc_hash_t hctx,
isc_hash_t **  hctxp 
)

Attach to a hash object.

This function is only necessary for the multiple-context mode.

Definition at line 285 of file hash.c.

References isc_refcount_increment, isc_hash::refcnt, REQUIRE, and VALID_HASH.

void isc_hash_ctxdetach ( isc_hash_t **  hctxp  ) 

Detach from a hash object.

This function is for the multiple-context mode, and takes a valid hash object as an argument.

Definition at line 322 of file hash.c.

References destroy(), isc_refcount_decrement, isc_hash::refcnt, REQUIRE, and VALID_HASH.

void isc_hash_destroy ( void   ) 

This function is for the single-context mode, and is expected to be used as a counterpart of isc_hash_create().

A valid module-internal hash object must have been created, and this function should be called only once.

Definition at line 337 of file hash.c.

References destroy(), INSIST, isc_refcount_decrement, isc_hash::refcnt, and VALID_HASH.

Referenced by cleanup(), destroy_managers(), dns_lib_shutdown(), dns_test_end(), initialize(), isc_test_end(), and main().

void isc_hash_ctxinit ( isc_hash_t hctx  ) 

Definition at line 238 of file hash.c.

References isc_hash::entropy, isc_hash::initialized, INSIST, isc_entropy_getdata(), ISC_R_SUCCESS, isc_random_get(), ISC_TRUE, isc_hash::lock, LOCK, isc_hash::rndvector, UNLOCK, and isc_hash::vectorlen.

Referenced by hash_calc(), and isc_hash_init().

void isc_hash_init ( void   ) 

Initialize a hash object.

It fills in the random vector with a proper source of entropy, which is typically from the entropy object specified at the creation. Thus, it is desirable to call these functions after initializing the entropy object with some good entropy sources.

These functions should be called before the first hash calculation.

isc_hash_ctxinit() is for the multiple-context mode, and takes a valid hash object as an argument.

isc_hash_init() is for the single-context mode. A valid module-internal hash object must have been created, and this function should be called only once.

Definition at line 278 of file hash.c.

References INSIST, isc_hash_ctxinit(), and VALID_HASH.

Referenced by run_server(), and setup_system().

unsigned int isc_hash_ctxcalc ( isc_hash_t hctx,
const unsigned char *  key,
unsigned int  keylen,
isc_boolean_t  case_sensitive 
)

Definition at line 374 of file hash.c.

References hash_calc(), REQUIRE, and VALID_HASH.

unsigned int isc_hash_calc ( const unsigned char *  key,
unsigned int  keylen,
isc_boolean_t  case_sensitive 
)

Calculate a hash value.

isc_hash_ctxinit() is for the multiple-context mode, and takes a valid hash object as an argument.

isc_hash_init() is for the single-context mode. A valid module-internal hash object must have been created.

'key' is the hash key, which is a variable length buffer.

'keylen' specifies the key length, which must not be larger than the limit specified for the corresponding hash object.

'case_sensitive' specifies whether the hash key should be treated as case_sensitive values. It should typically be ISC_FALSE if the hash key is a DNS name.

Definition at line 384 of file hash.c.

References hash_calc(), INSIST, REQUIRE, and VALID_HASH.

Referenced by dns_acache_putdb(), dns_acache_setdb(), dns_name_fullhash(), finddbent(), and isc_sockaddr_hash().

void isc__hash_setvec ( const isc_uint16_t vec  ) 

Set the contents of the random vector used in hashing.

WARNING: This function is meant to be used only in testing code. It must not be used anywhere in normally running code.

The hash context must have been created beforehand, otherwise this function is a nop.

'vec' is not documented here on purpose. You should know what you are doing before using this function.

Definition at line 394 of file hash.c.

References isc_hash::rndvector.


Generated on Tue Apr 28 17:41:15 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha