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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 #define AM_LOAD(n) (((n) * 3)/2)
33 /*----- Main code ---------------------------------------------------------*/
35 /* --- @am_create@ --- *
37 * Arguments: @addrmap *m@ = pointer to map
41 * Use: Create an address map, properly set up.
44 void am_create(addrmap
*m
)
46 hash_create(&m
->t
, 16);
47 m
->load
= AM_LOAD(m
->t
.mask
+ 1);
50 /* --- @am_destroy@ --- *
52 * Arguments: @addrmap *m@ = pointer to map
56 * Use: Destroy an address map, throwing away all the entries.
59 void am_destroy(addrmap
*m
)
64 for (hash_mkiter(&i
, &m
->t
); (p
= hash_next(&i
)) != 0; )
71 * Arguments: @const addr *a@ = pointer to address
73 * Returns: The hash of the address.
76 uint32
hash(const addr
*a
)
78 switch (a
->sa
.sa_family
) {
80 return (U32((AF_INET
* 0x4eaac1b7ul
) +
81 (a
->sin
.sin_addr
.s_addr
* 0xa5dbc837) +
82 (a
->sin
.sin_port
* 0x3b049e83)));
90 * Arguments: @const addr *a, *b@ = pointer to addresses
92 * Returns: Nonzero if the addresses are equal.
95 int addreq(const addr
*a
, const addr
*b
)
97 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
99 switch (a
->sa
.sa_family
) {
101 return (a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
&&
102 a
->sin
.sin_port
== b
->sin
.sin_port
);
108 /* --- @am_find@ --- *
110 * Arguments: @addrmap *m@ = pointer to map
111 * @const addr *a@ = address to look up
112 * @size_t sz@ = size of block to allocate
113 * @unsigned *f@ = where to store flags
115 * Returns: Pointer to found item, or null.
117 * Use: Finds a record with the given IP address, set @*f@ nonzero
118 * and returns it. If @sz@ is zero, and no match was found,
119 * return null; otherwise allocate a new block of @sz@ bytes,
120 * clear @*f@ to zero and return the block pointer.
123 void *am_find(addrmap
*m
, const addr
*a
, size_t sz
, unsigned *f
)
129 bb
= HASH_BIN(&m
->t
, h
);
130 for (b
= bb
; *b
; b
= &(*b
)->next
) {
131 i
= (addrmap_base
*)*b
;
132 if (i
->b
.hash
== h
&& addreq(a
, &i
->a
)) {
143 i
= x_alloc(m
->t
.a
, sz
);
151 if (!m
->load
&& hash_extend(&m
->t
))
152 m
->load
= AM_LOAD(m
->t
.mask
+ 1);
156 /* --- @am_remove@ --- *
158 * Arguments: @addrmap *m@ = pointer to map
159 * @void *i@ = pointer to the item
163 * Use: Removes an item from the map.
166 void am_remove(addrmap
*m
, void *i
)
168 hash_remove(&m
->t
, i
);
172 /*----- That's all, folks -------------------------------------------------*/