3 * Hashtables keyed by network addresses
5 * (c) 2007 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 #define AM_LOAD(n) (((n) * 3)/2)
32 /*----- Main code ---------------------------------------------------------*/
34 /* --- @am_create@ --- *
36 * Arguments: @addrmap *m@ = pointer to map
40 * Use: Create an address map, properly set up.
43 void am_create(addrmap *m)
45 hash_create(&m->t, 16);
46 m->load = AM_LOAD(m->t.mask + 1);
49 /* --- @am_destroy@ --- *
51 * Arguments: @addrmap *m@ = pointer to map
55 * Use: Destroy an address map, throwing away all the entries.
58 void am_destroy(addrmap *m)
63 for (hash_mkiter(&i, &m->t); (p = hash_next(&i)) != 0; )
70 * Arguments: @const addr *a@ = pointer to address
72 * Returns: The hash of the address.
75 static uint32 hash(const addr *a)
77 switch (a->sa.sa_family) {
79 return (U32(0x4eaac1b7ul*AF_INET +
80 0xa5dbc837ul*a->sin.sin_addr.s_addr +
81 0x3b049e83ul*a->sin.sin_port));
89 * Arguments: @const addr *a, *b@ = pointer to addresses
91 * Returns: Nonzero if the addresses are equal.
94 static int addreq(const addr *a, const addr *b)
96 if (a->sa.sa_family != b->sa.sa_family)
98 switch (a->sa.sa_family) {
100 return (a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr &&
101 a->sin.sin_port == b->sin.sin_port);
107 /* --- @am_find@ --- *
109 * Arguments: @addrmap *m@ = pointer to map
110 * @const addr *a@ = address to look up
111 * @size_t sz@ = size of block to allocate
112 * @unsigned *f@ = where to store flags
114 * Returns: Pointer to found item, or null.
116 * Use: Finds a record with the given IP address, set @*f@ nonzero
117 * and returns it. If @sz@ is zero, and no match was found,
118 * return null; otherwise allocate a new block of @sz@ bytes,
119 * clear @*f@ to zero and return the block pointer.
122 void *am_find(addrmap *m, const addr *a, size_t sz, unsigned *f)
128 bb = HASH_BIN(&m->t, h);
129 for (b = bb; *b; b = &(*b)->next) {
130 i = (addrmap_base *)*b;
131 if (i->b.hash == h && addreq(a, &i->a)) {
142 i = x_alloc(m->t.a, sz);
150 if (!m->load && hash_extend(&m->t))
151 m->load = AM_LOAD(m->t.mask + 1);
155 /* --- @am_remove@ --- *
157 * Arguments: @addrmap *m@ = pointer to map
158 * @void *i@ = pointer to the item
162 * Use: Removes an item from the map.
165 void am_remove(addrmap *m, void *i)
167 hash_remove(&m->t, i);
171 /*----- That's all, folks -------------------------------------------------*/