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
)
80 switch (a
->sa
.sa_family
) {
82 return (U32(0x4eaac1b7ul
*AF_INET
+
83 0xa5dbc837ul
*a
->sin
.sin_addr
.s_addr
+
84 0x3b049e83ul
*a
->sin
.sin_port
));
86 for (i
= 0, h
= 0; i
< 16; i
++)
87 h
= 0x6bd26a67ul
*h
+ a
->sin6
.sin6_addr
.s6_addr
[i
];
88 return (U32(0x4eaac1b7ul
*AF_INET6
+
90 0x1d94eab4ul
*a
->sin6
.sin6_scope_id
+
91 0x3b049e83ul
*a
->sin6
.sin6_port
));
99 * Arguments: @const addr *a, *b@ = pointer to addresses
101 * Returns: Nonzero if the addresses are equal.
104 static int addreq(const addr
*a
, const addr
*b
)
106 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
108 switch (a
->sa
.sa_family
) {
110 return (a
->sin
.sin_addr
.s_addr
== b
->sin
.sin_addr
.s_addr
&&
111 a
->sin
.sin_port
== b
->sin
.sin_port
);
113 return (!memcmp(a
->sin6
.sin6_addr
.s6_addr
,
114 b
->sin6
.sin6_addr
.s6_addr
, 16) &&
115 a
->sin6
.sin6_port
== b
->sin6
.sin6_port
);
121 /* --- @am_find@ --- *
123 * Arguments: @addrmap *m@ = pointer to map
124 * @const addr *a@ = address to look up
125 * @size_t sz@ = size of block to allocate
126 * @unsigned *f@ = where to store flags
128 * Returns: Pointer to found item, or null.
130 * Use: Finds a record with the given IP address, set @*f@ nonzero
131 * and returns it. If @sz@ is zero, and no match was found,
132 * return null; otherwise allocate a new block of @sz@ bytes,
133 * clear @*f@ to zero and return the block pointer.
136 void *am_find(addrmap
*m
, const addr
*a
, size_t sz
, unsigned *f
)
142 bb
= HASH_BIN(&m
->t
, h
);
143 for (b
= bb
; *b
; b
= &(*b
)->next
) {
144 i
= (addrmap_base
*)*b
;
145 if (i
->b
.hash
== h
&& addreq(a
, &i
->a
)) {
156 i
= x_alloc(m
->t
.a
, sz
);
164 if (!m
->load
&& hash_extend(&m
->t
))
165 m
->load
= AM_LOAD(m
->t
.mask
+ 1);
169 /* --- @am_remove@ --- *
171 * Arguments: @addrmap *m@ = pointer to map
172 * @void *i@ = pointer to the item
176 * Use: Removes an item from the map.
179 void am_remove(addrmap
*m
, void *i
)
181 hash_remove(&m
->t
, i
);
185 /*----- That's all, folks -------------------------------------------------*/