1 | =pod
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | LHASH, DECLARE_LHASH_OF,
|
---|
6 | OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC,
|
---|
7 | LHASH_DOALL_ARG_FN_TYPE,
|
---|
8 | IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN,
|
---|
9 | lh_TYPE_new, lh_TYPE_free,
|
---|
10 | lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
|
---|
11 | lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error - dynamic hash table
|
---|
12 |
|
---|
13 | =head1 SYNOPSIS
|
---|
14 |
|
---|
15 | =for comment generic
|
---|
16 |
|
---|
17 | #include <openssl/lhash.h>
|
---|
18 |
|
---|
19 | DECLARE_LHASH_OF(TYPE);
|
---|
20 |
|
---|
21 | LHASH *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
|
---|
22 | void lh_TYPE_free(LHASH_OF(TYPE) *table);
|
---|
23 |
|
---|
24 | TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
25 | TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
26 | TYPE *lh_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
|
---|
27 |
|
---|
28 | void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
|
---|
29 | void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
|
---|
30 | TYPE *arg);
|
---|
31 |
|
---|
32 | int lh_TYPE_error(LHASH_OF(TYPE) *table);
|
---|
33 |
|
---|
34 | typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
|
---|
35 | typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
|
---|
36 | typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
|
---|
37 | typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
|
---|
38 |
|
---|
39 | =head1 DESCRIPTION
|
---|
40 |
|
---|
41 | This library implements type-checked dynamic hash tables. The hash
|
---|
42 | table entries can be arbitrary structures. Usually they consist of key
|
---|
43 | and value fields. In the description here, I<TYPE> is used a placeholder
|
---|
44 | for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
|
---|
45 |
|
---|
46 | lh_TYPE_new() creates a new B<LHASH_OF(TYPE)> structure to store
|
---|
47 | arbitrary data entries, and specifies the 'hash' and 'compare'
|
---|
48 | callbacks to be used in organising the table's entries. The B<hash>
|
---|
49 | callback takes a pointer to a table entry as its argument and returns
|
---|
50 | an unsigned long hash value for its key field. The hash value is
|
---|
51 | normally truncated to a power of 2, so make sure that your hash
|
---|
52 | function returns well mixed low order bits. The B<compare> callback
|
---|
53 | takes two arguments (pointers to two hash table entries), and returns
|
---|
54 | 0 if their keys are equal, non-zero otherwise.
|
---|
55 |
|
---|
56 | If your hash table
|
---|
57 | will contain items of some particular type and the B<hash> and
|
---|
58 | B<compare> callbacks hash/compare these types, then the
|
---|
59 | B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
|
---|
60 | used to create callback wrappers of the prototypes required by
|
---|
61 | lh_TYPE_new() as shown in this example:
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Implement the hash and compare functions; "stuff" can be any word.
|
---|
65 | */
|
---|
66 | static unsigned long stuff_hash(const TYPE *a)
|
---|
67 | {
|
---|
68 | ...
|
---|
69 | }
|
---|
70 | static int stuff_cmp(const TYPE *a, const TYPE *b)
|
---|
71 | {
|
---|
72 | ...
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Implement the wrapper functions.
|
---|
77 | */
|
---|
78 | static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
|
---|
79 | static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
|
---|
80 |
|
---|
81 | If the type is going to be used in several places, the following macros
|
---|
82 | can be used in a common header file to declare the function wrappers:
|
---|
83 |
|
---|
84 | DECLARE_LHASH_HASH_FN(stuff, TYPE)
|
---|
85 | DECLARE_LHASH_COMP_FN(stuff, TYPE)
|
---|
86 |
|
---|
87 | Then a hash table of TYPE objects can be created using this:
|
---|
88 |
|
---|
89 | LHASH_OF(TYPE) *htable;
|
---|
90 |
|
---|
91 | htable = lh_TYPE_new(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
|
---|
92 |
|
---|
93 | lh_TYPE_free() frees the B<LHASH_OF(TYPE)> structure
|
---|
94 | B<table>. Allocated hash table entries will not be freed; consider
|
---|
95 | using lh_TYPE_doall() to deallocate any remaining entries in the
|
---|
96 | hash table (see below).
|
---|
97 |
|
---|
98 | lh_TYPE_insert() inserts the structure pointed to by B<data> into
|
---|
99 | B<table>. If there already is an entry with the same key, the old
|
---|
100 | value is replaced. Note that lh_TYPE_insert() stores pointers, the
|
---|
101 | data are not copied.
|
---|
102 |
|
---|
103 | lh_TYPE_delete() deletes an entry from B<table>.
|
---|
104 |
|
---|
105 | lh_TYPE_retrieve() looks up an entry in B<table>. Normally, B<data>
|
---|
106 | is a structure with the key field(s) set; the function will return a
|
---|
107 | pointer to a fully populated structure.
|
---|
108 |
|
---|
109 | lh_TYPE_doall() will, for every entry in the hash table, call
|
---|
110 | B<func> with the data item as its parameter.
|
---|
111 | For example:
|
---|
112 |
|
---|
113 | /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
|
---|
114 | void TYPE_cleanup_doall(TYPE *a);
|
---|
115 |
|
---|
116 | /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
|
---|
117 | IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
|
---|
118 |
|
---|
119 | /* Call "TYPE_cleanup" against all items in a hash table. */
|
---|
120 | lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
|
---|
121 |
|
---|
122 | /* Then the hash table itself can be deallocated */
|
---|
123 | lh_TYPE_free(hashtable);
|
---|
124 |
|
---|
125 | When doing this, be careful if you delete entries from the hash table
|
---|
126 | in your callbacks: the table may decrease in size, moving the item
|
---|
127 | that you are currently on down lower in the hash table - this could
|
---|
128 | cause some entries to be skipped during the iteration. The second
|
---|
129 | best solution to this problem is to set hash-E<gt>down_load=0 before
|
---|
130 | you start (which will stop the hash table ever decreasing in size).
|
---|
131 | The best solution is probably to avoid deleting items from the hash
|
---|
132 | table inside a "doall" callback!
|
---|
133 |
|
---|
134 | lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that
|
---|
135 | B<func> will be called with B<arg> as the second argument and B<func>
|
---|
136 | should be of type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype
|
---|
137 | that is passed both the table entry and an extra argument). As with
|
---|
138 | lh_doall(), you can instead choose to declare your callback with a
|
---|
139 | prototype matching the types you are dealing with and use the
|
---|
140 | declare/implement macros to create compatible wrappers that cast
|
---|
141 | variables before calling your type-specific callbacks. An example of
|
---|
142 | this is demonstrated here (printing all hash table entries to a BIO
|
---|
143 | that is provided by the caller):
|
---|
144 |
|
---|
145 | /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
|
---|
146 | void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
|
---|
147 |
|
---|
148 | /* Implement a prototype-compatible wrapper for "TYPE_print" */
|
---|
149 | static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
|
---|
150 |
|
---|
151 | /* Print out the entire hashtable to a particular BIO */
|
---|
152 | lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
|
---|
153 | logging_bio);
|
---|
154 |
|
---|
155 |
|
---|
156 | lh_TYPE_error() can be used to determine if an error occurred in the last
|
---|
157 | operation.
|
---|
158 |
|
---|
159 | =head1 RETURN VALUES
|
---|
160 |
|
---|
161 | lh_TYPE_new() returns B<NULL> on error, otherwise a pointer to the new
|
---|
162 | B<LHASH> structure.
|
---|
163 |
|
---|
164 | When a hash table entry is replaced, lh_TYPE_insert() returns the value
|
---|
165 | being replaced. B<NULL> is returned on normal operation and on error.
|
---|
166 |
|
---|
167 | lh_TYPE_delete() returns the entry being deleted. B<NULL> is returned if
|
---|
168 | there is no such value in the hash table.
|
---|
169 |
|
---|
170 | lh_TYPE_retrieve() returns the hash table entry if it has been found,
|
---|
171 | B<NULL> otherwise.
|
---|
172 |
|
---|
173 | lh_TYPE_error() returns 1 if an error occurred in the last operation, 0
|
---|
174 | otherwise. It's meaningful only after non-retrieve operations.
|
---|
175 |
|
---|
176 | lh_TYPE_free(), lh_TYPE_doall() and lh_TYPE_doall_arg() return no values.
|
---|
177 |
|
---|
178 | =head1 NOTE
|
---|
179 |
|
---|
180 | The LHASH code is not thread safe. All updating operations, as well as
|
---|
181 | lh_TYPE_error call must be performed under a write lock. All retrieve
|
---|
182 | operations should be performed under a read lock, I<unless> accurate
|
---|
183 | usage statistics are desired. In which case, a write lock should be used
|
---|
184 | for retrieve operations as well. For output of the usage statistics,
|
---|
185 | using the functions from L<OPENSSL_LH_stats(3)>, a read lock suffices.
|
---|
186 |
|
---|
187 | The LHASH code regards table entries as constant data. As such, it
|
---|
188 | internally represents lh_insert()'d items with a "const void *"
|
---|
189 | pointer type. This is why callbacks such as those used by lh_doall()
|
---|
190 | and lh_doall_arg() declare their prototypes with "const", even for the
|
---|
191 | parameters that pass back the table items' data pointers - for
|
---|
192 | consistency, user-provided data is "const" at all times as far as the
|
---|
193 | LHASH code is concerned. However, as callers are themselves providing
|
---|
194 | these pointers, they can choose whether they too should be treating
|
---|
195 | all such parameters as constant.
|
---|
196 |
|
---|
197 | As an example, a hash table may be maintained by code that, for
|
---|
198 | reasons of encapsulation, has only "const" access to the data being
|
---|
199 | indexed in the hash table (ie. it is returned as "const" from
|
---|
200 | elsewhere in their code) - in this case the LHASH prototypes are
|
---|
201 | appropriate as-is. Conversely, if the caller is responsible for the
|
---|
202 | life-time of the data in question, then they may well wish to make
|
---|
203 | modifications to table item passed back in the lh_doall() or
|
---|
204 | lh_doall_arg() callbacks (see the "TYPE_cleanup" example above). If
|
---|
205 | so, the caller can either cast the "const" away (if they're providing
|
---|
206 | the raw callbacks themselves) or use the macros to declare/implement
|
---|
207 | the wrapper functions without "const" types.
|
---|
208 |
|
---|
209 | Callers that only have "const" access to data they're indexing in a
|
---|
210 | table, yet declare callbacks without constant types (or cast the
|
---|
211 | "const" away themselves), are therefore creating their own risks/bugs
|
---|
212 | without being encouraged to do so by the API. On a related note,
|
---|
213 | those auditing code should pay special attention to any instances of
|
---|
214 | DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
|
---|
215 | without any "const" qualifiers.
|
---|
216 |
|
---|
217 | =head1 BUGS
|
---|
218 |
|
---|
219 | lh_TYPE_insert() returns B<NULL> both for success and error.
|
---|
220 |
|
---|
221 | =head1 SEE ALSO
|
---|
222 |
|
---|
223 | L<OPENSSL_LH_stats(3)>
|
---|
224 |
|
---|
225 | =head1 HISTORY
|
---|
226 |
|
---|
227 | In OpenSSL 1.0.0, the lhash interface was revamped for better
|
---|
228 | type checking.
|
---|
229 |
|
---|
230 | =head1 COPYRIGHT
|
---|
231 |
|
---|
232 | Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
|
---|
233 |
|
---|
234 | Licensed under the OpenSSL license (the "License"). You may not use
|
---|
235 | this file except in compliance with the License. You can obtain a copy
|
---|
236 | in the file LICENSE in the source distribution or at
|
---|
237 | L<https://www.openssl.org/source/license.html>.
|
---|
238 |
|
---|
239 | =cut
|
---|