+/* -*-c-*-
+ *
+ * Hashtables keyed by network addresses
+ *
+ * (c) 2007 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Trivial IP Encryption (TrIPE).
+ *
+ * TrIPE is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * TrIPE is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with TrIPE; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "tripe.h"
+
+#define AM_LOAD(n) (((n) * 3)/2)
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @am_create@ --- *
+ *
+ * Arguments: @addrmap *m@ = pointer to map
+ *
+ * Returns: ---
+ *
+ * Use: Create an address map, properly set up.
+ */
+
+void am_create(addrmap *m)
+{
+ hash_create(&m->t, 16);
+ m->load = AM_LOAD(m->t.mask + 1);
+}
+
+/* --- @am_destroy@ --- *
+ *
+ * Arguments: @addrmap *m@ = pointer to map
+ *
+ * Returns: ---
+ *
+ * Use: Destroy an address map, throwing away all the entries.
+ */
+
+void am_destroy(addrmap *m)
+{
+ hash_base *p;
+ hash_iter i;
+
+ for (hash_mkiter(&i, &m->t); (p = hash_next(&i)) != 0; )
+ x_free(m->t.a, p);
+ hash_destroy(&m->t);
+}
+
+/* --- @hash@ --- *
+ *
+ * Arguments: @const addr *a@ = pointer to address
+ *
+ * Returns: The hash of the address.
+ */
+
+uint32 hash(const addr *a)
+{
+ switch (a->sa.sa_family) {
+ case AF_INET:
+ return (U32((AF_INET * 0x4eaac1b7ul) +
+ (a->sin.sin_addr.s_addr * 0xa5dbc837) +
+ (a->sin.sin_port * 0x3b049e83)));
+ default:
+ abort();
+ }
+}
+
+/* --- @addreq@ --- *
+ *
+ * Arguments: @const addr *a, *b@ = pointer to addresses
+ *
+ * Returns: Nonzero if the addresses are equal.
+ */
+
+int addreq(const addr *a, const addr *b)
+{
+ if (a->sa.sa_family != b->sa.sa_family)
+ return (0);
+ switch (a->sa.sa_family) {
+ case AF_INET:
+ return (a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr &&
+ a->sin.sin_port == b->sin.sin_port);
+ default:
+ abort();
+ }
+}
+
+/* --- @am_find@ --- *
+ *
+ * Arguments: @addrmap *m@ = pointer to map
+ * @const addr *a@ = address to look up
+ * @size_t sz@ = size of block to allocate
+ * @unsigned *f@ = where to store flags
+ *
+ * Returns: Pointer to found item, or null.
+ *
+ * Use: Finds a record with the given IP address, set @*f@ nonzero
+ * and returns it. If @sz@ is zero, and no match was found,
+ * return null; otherwise allocate a new block of @sz@ bytes,
+ * clear @*f@ to zero and return the block pointer.
+ */
+
+void *am_find(addrmap *m, const addr *a, size_t sz, unsigned *f)
+{
+ uint32 h = hash(a);
+ hash_base **b, **bb;
+ addrmap_base *i;
+
+ bb = HASH_BIN(&m->t, h);
+ for (b = bb; *b; b = &(*b)->next) {
+ i = (addrmap_base *)*b;
+ if (i->b.hash == h && addreq(a, &i->a)) {
+ *b = i->b.next;
+ i->b.next = *bb;
+ *bb = &i->b;
+ if (f) *f = 1;
+ return (i);
+ }
+ }
+
+ if (f) *f = 0;
+ if (!sz) return (0);
+ i = x_alloc(m->t.a, sz);
+ i->b.hash = h;
+ i->b.next = *bb;
+ *bb = &i->b;
+ i->a = *a;
+
+ if (m->load)
+ m->load--;
+ if (!m->load && hash_extend(&m->t))
+ m->load = AM_LOAD(m->t.mask + 1);
+ return (i);
+}
+
+/* --- @am_remove@ --- *
+ *
+ * Arguments: @addrmap *m@ = pointer to map
+ * @void *i@ = pointer to the item
+ *
+ * Returns: ---
+ *
+ * Use: Removes an item from the map.
+ */
+
+void am_remove(addrmap *m, void *i)
+{
+ hash_remove(&m->t, i);
+ x_free(m->t.a, i);
+}
+
+/*----- That's all, folks -------------------------------------------------*/