[00d7e1b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Radim Vansa
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @addtogroup libnic
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Generic hash-set based database of addresses
|
---|
| 36 | */
|
---|
[36795edf] | 37 |
|
---|
[062d900] | 38 | #include "nic_addr_db.h"
|
---|
[00d7e1b] | 39 | #include <assert.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[00d7e1b] | 42 | #include <errno.h>
|
---|
| 43 | #include <mem.h>
|
---|
[36795edf] | 44 | #include <member.h>
|
---|
[062d900] | 45 | #include <adt/hash_table.h>
|
---|
| 46 | #include <macros.h>
|
---|
| 47 | #include <stdint.h>
|
---|
[00d7e1b] | 48 |
|
---|
| 49 | /**
|
---|
[062d900] | 50 | * Helper structure for keeping the address in the hash set.
|
---|
[00d7e1b] | 51 | */
|
---|
[062d900] | 52 | typedef struct nic_addr_entry {
|
---|
| 53 | ht_link_t link;
|
---|
[0db0df2] | 54 | size_t hash;
|
---|
[062d900] | 55 | uint8_t len;
|
---|
[0db0df2] | 56 | uint8_t addr[];
|
---|
[062d900] | 57 | } nic_addr_entry_t;
|
---|
[00d7e1b] | 58 |
|
---|
[1b20da0] | 59 | /*
|
---|
| 60 | * Hash table helper functions
|
---|
[00d7e1b] | 61 | */
|
---|
[062d900] | 62 | typedef struct {
|
---|
[0db0df2] | 63 | size_t hash;
|
---|
[062d900] | 64 | size_t len;
|
---|
| 65 | const uint8_t *addr;
|
---|
| 66 | } addr_key_t;
|
---|
| 67 |
|
---|
[0db0df2] | 68 | static bool nic_addr_key_equal(const void *key_arg, size_t hash, const ht_link_t *item)
|
---|
[00d7e1b] | 69 | {
|
---|
[5e801dc] | 70 | const addr_key_t *key = key_arg;
|
---|
[062d900] | 71 | nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
|
---|
[0db0df2] | 72 | return entry->hash == hash && memcmp(entry->addr, key->addr, entry->len) == 0;
|
---|
[062d900] | 73 | }
|
---|
[00d7e1b] | 74 |
|
---|
[062d900] | 75 | static size_t addr_hash(size_t len, const uint8_t *addr)
|
---|
| 76 | {
|
---|
| 77 | size_t hash = 0;
|
---|
[a35b458] | 78 |
|
---|
[062d900] | 79 | for (size_t i = 0; i < len; ++i) {
|
---|
| 80 | hash = (hash << 5) ^ addr[i];
|
---|
[00d7e1b] | 81 | }
|
---|
[a35b458] | 82 |
|
---|
[00d7e1b] | 83 | return hash;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[5e801dc] | 86 | static size_t nic_addr_key_hash(const void *k)
|
---|
[062d900] | 87 | {
|
---|
[5e801dc] | 88 | const addr_key_t *key = k;
|
---|
[0db0df2] | 89 | return key->hash ? key->hash : addr_hash(key->len, key->addr);
|
---|
[062d900] | 90 | }
|
---|
| 91 |
|
---|
| 92 | static size_t nic_addr_hash(const ht_link_t *item)
|
---|
| 93 | {
|
---|
| 94 | nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
|
---|
[0db0df2] | 95 | return entry->hash;
|
---|
[062d900] | 96 | }
|
---|
| 97 |
|
---|
| 98 | static void nic_addr_removed(ht_link_t *item)
|
---|
[00d7e1b] | 99 | {
|
---|
[062d900] | 100 | nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
|
---|
| 101 | free(entry);
|
---|
[00d7e1b] | 102 | }
|
---|
| 103 |
|
---|
[61eb2ce2] | 104 | static const hash_table_ops_t set_ops = {
|
---|
[062d900] | 105 | .hash = nic_addr_hash,
|
---|
| 106 | .key_hash = nic_addr_key_hash,
|
---|
| 107 | .key_equal = nic_addr_key_equal,
|
---|
[4e00f87] | 108 | .equal = NULL,
|
---|
[062d900] | 109 | .remove_callback = nic_addr_removed
|
---|
| 110 | };
|
---|
| 111 |
|
---|
[00d7e1b] | 112 | /**
|
---|
| 113 | * Initialize the database
|
---|
| 114 | *
|
---|
| 115 | * @param[in,out] db Uninitialized storage
|
---|
| 116 | * @param[in] addr_len Size of addresses in the db
|
---|
| 117 | *
|
---|
| 118 | * @return EOK If successfully initialized
|
---|
| 119 | * @return EINVAL If the address length is too big
|
---|
| 120 | * @return ENOMEM If there was not enough memory to initialize the storage
|
---|
| 121 | */
|
---|
[b7fd2a0] | 122 | errno_t nic_addr_db_init(nic_addr_db_t *db, size_t addr_len)
|
---|
[00d7e1b] | 123 | {
|
---|
| 124 | assert(db);
|
---|
[a35b458] | 125 |
|
---|
[062d900] | 126 | if (addr_len > UCHAR_MAX)
|
---|
[00d7e1b] | 127 | return EINVAL;
|
---|
[a35b458] | 128 |
|
---|
[062d900] | 129 | if (!hash_table_create(&db->set, 0, 0, &set_ops))
|
---|
[00d7e1b] | 130 | return ENOMEM;
|
---|
[a35b458] | 131 |
|
---|
[00d7e1b] | 132 | db->addr_len = addr_len;
|
---|
| 133 | return EOK;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Remove all records from the DB
|
---|
| 138 | *
|
---|
| 139 | * @param db
|
---|
| 140 | */
|
---|
| 141 | void nic_addr_db_clear(nic_addr_db_t *db)
|
---|
| 142 | {
|
---|
| 143 | assert(db);
|
---|
[062d900] | 144 | hash_table_clear(&db->set);
|
---|
[00d7e1b] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Free the memory used by db, including all records...
|
---|
| 149 | *
|
---|
| 150 | * @param db
|
---|
| 151 | */
|
---|
| 152 | void nic_addr_db_destroy(nic_addr_db_t *db)
|
---|
| 153 | {
|
---|
| 154 | assert(db);
|
---|
[062d900] | 155 | hash_table_destroy(&db->set);
|
---|
[00d7e1b] | 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Insert an address to the db
|
---|
| 160 | *
|
---|
| 161 | * @param db
|
---|
| 162 | * @param addr Inserted address. Length is implicitly concluded from the
|
---|
| 163 | * db's address length.
|
---|
| 164 | *
|
---|
| 165 | * @return EOK If the address was inserted
|
---|
| 166 | * @return ENOMEM If there was not enough memory
|
---|
[c477c80] | 167 | * @return EEXIST If this address already is in the db
|
---|
[00d7e1b] | 168 | */
|
---|
[b7fd2a0] | 169 | errno_t nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr)
|
---|
[00d7e1b] | 170 | {
|
---|
| 171 | assert(db && addr);
|
---|
[062d900] | 172 |
|
---|
| 173 | addr_key_t key = {
|
---|
| 174 | .len = db->addr_len,
|
---|
[0db0df2] | 175 | .addr = addr,
|
---|
| 176 | .hash = addr_hash(db->addr_len, addr),
|
---|
[062d900] | 177 | };
|
---|
[a35b458] | 178 |
|
---|
[062d900] | 179 | if (hash_table_find(&db->set, &key))
|
---|
| 180 | return EEXIST;
|
---|
[a35b458] | 181 |
|
---|
[0db0df2] | 182 | nic_addr_entry_t *entry = malloc(offsetof(nic_addr_entry_t, addr) + db->addr_len);
|
---|
[1b20da0] | 183 | if (entry == NULL)
|
---|
[00d7e1b] | 184 | return ENOMEM;
|
---|
| 185 |
|
---|
[062d900] | 186 | entry->len = (uint8_t) db->addr_len;
|
---|
| 187 | memcpy(entry->addr, addr, db->addr_len);
|
---|
[0db0df2] | 188 | entry->hash = key.hash;
|
---|
[a35b458] | 189 |
|
---|
[062d900] | 190 | hash_table_insert(&db->set, &entry->link);
|
---|
| 191 | return EOK;
|
---|
[00d7e1b] | 192 | }
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * Remove the address from the db
|
---|
| 196 | *
|
---|
| 197 | * @param db
|
---|
| 198 | * @param addr Removed address.
|
---|
| 199 | *
|
---|
| 200 | * @return EOK If the address was removed
|
---|
| 201 | * @return ENOENT If there is no address
|
---|
| 202 | */
|
---|
[b7fd2a0] | 203 | errno_t nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr)
|
---|
[00d7e1b] | 204 | {
|
---|
| 205 | assert(db && addr);
|
---|
[a35b458] | 206 |
|
---|
[062d900] | 207 | addr_key_t key = {
|
---|
| 208 | .len = db->addr_len,
|
---|
| 209 | .addr = addr
|
---|
| 210 | };
|
---|
[a35b458] | 211 |
|
---|
[062d900] | 212 | if (hash_table_remove(&db->set, &key))
|
---|
| 213 | return EOK;
|
---|
| 214 | else
|
---|
| 215 | return ENOENT;
|
---|
[00d7e1b] | 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * Test if the address is contained in the db
|
---|
| 220 | *
|
---|
| 221 | * @param db
|
---|
| 222 | * @param addr Tested addresss
|
---|
| 223 | *
|
---|
| 224 | * @return true if the address is in the db, false otherwise
|
---|
| 225 | */
|
---|
[d5c1051] | 226 | bool nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr)
|
---|
[00d7e1b] | 227 | {
|
---|
| 228 | assert(db && addr);
|
---|
[a35b458] | 229 |
|
---|
[062d900] | 230 | addr_key_t key = {
|
---|
| 231 | .len = db->addr_len,
|
---|
| 232 | .addr = addr
|
---|
| 233 | };
|
---|
[a35b458] | 234 |
|
---|
[062d900] | 235 | return 0 != hash_table_find(&db->set, &key);
|
---|
[00d7e1b] | 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * Helper structure for nic_addr_db_foreach
|
---|
| 240 | */
|
---|
| 241 | typedef struct {
|
---|
| 242 | void (*func)(const uint8_t *, void *);
|
---|
| 243 | void *arg;
|
---|
| 244 | } nic_addr_db_fe_arg_t;
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * Helper function for nic_addr_db_foreach
|
---|
| 248 | */
|
---|
[1b20da0] | 249 | static bool nic_addr_db_fe_helper(ht_link_t *item, void *arg)
|
---|
[062d900] | 250 | {
|
---|
[00d7e1b] | 251 | nic_addr_db_fe_arg_t *hs = (nic_addr_db_fe_arg_t *) arg;
|
---|
[062d900] | 252 | nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
|
---|
| 253 | hs->func(entry->addr, hs->arg);
|
---|
| 254 | return true;
|
---|
[00d7e1b] | 255 | }
|
---|
| 256 |
|
---|
| 257 | /**
|
---|
| 258 | * Executes a user-defined function on all addresses in the DB. The function
|
---|
| 259 | * must not change the addresses.
|
---|
| 260 | *
|
---|
| 261 | * @param db
|
---|
| 262 | * @param func User-defined function
|
---|
| 263 | * @param arg Custom argument passed to the function
|
---|
| 264 | */
|
---|
| 265 | void nic_addr_db_foreach(const nic_addr_db_t *db,
|
---|
[3bacee1] | 266 | void (*func)(const uint8_t *, void *), void *arg)
|
---|
[00d7e1b] | 267 | {
|
---|
| 268 | nic_addr_db_fe_arg_t hs = { .func = func, .arg = arg };
|
---|
[3bacee1] | 269 | hash_table_apply((hash_table_t *)&db->set, nic_addr_db_fe_helper, &hs);
|
---|
[00d7e1b] | 270 | }
|
---|
| 271 |
|
---|
| 272 | /** @}
|
---|
| 273 | */
|
---|