[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 | */
|
---|
[062d900] | 37 | #include "nic_addr_db.h"
|
---|
| 38 | #include "libarch/common.h"
|
---|
[00d7e1b] | 39 | #include <assert.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[00d7e1b] | 42 | #include <errno.h>
|
---|
| 43 | #include <mem.h>
|
---|
[062d900] | 44 | #include <adt/hash_table.h>
|
---|
| 45 | #include <macros.h>
|
---|
| 46 | #include <stdint.h>
|
---|
[00d7e1b] | 47 |
|
---|
| 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;
|
---|
| 54 | uint8_t len;
|
---|
| 55 | uint8_t addr[1];
|
---|
| 56 | } nic_addr_entry_t;
|
---|
[00d7e1b] | 57 |
|
---|
| 58 |
|
---|
[062d900] | 59 | /*
|
---|
| 60 | * Hash table helper functions
|
---|
[00d7e1b] | 61 | */
|
---|
[062d900] | 62 | typedef struct {
|
---|
| 63 | size_t len;
|
---|
| 64 | const uint8_t *addr;
|
---|
| 65 | } addr_key_t;
|
---|
| 66 |
|
---|
| 67 | static bool nic_addr_key_equal(void *key_arg, const ht_link_t *item)
|
---|
[00d7e1b] | 68 | {
|
---|
[062d900] | 69 | addr_key_t *key = (addr_key_t*)key_arg;
|
---|
| 70 | nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
|
---|
| 71 |
|
---|
[44ecf89] | 72 | return 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;
|
---|
| 78 |
|
---|
| 79 | for (size_t i = 0; i < len; ++i) {
|
---|
| 80 | hash = (hash << 5) ^ addr[i];
|
---|
[00d7e1b] | 81 | }
|
---|
[062d900] | 82 |
|
---|
[00d7e1b] | 83 | return hash;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[062d900] | 86 | static size_t nic_addr_key_hash(void *k)
|
---|
| 87 | {
|
---|
| 88 | addr_key_t *key = (addr_key_t*)k;
|
---|
| 89 | return addr_hash(key->len, key->addr);
|
---|
| 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);
|
---|
| 95 | return addr_hash(entry->len, entry->addr);
|
---|
| 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 |
|
---|
| 102 | free(entry);
|
---|
[00d7e1b] | 103 | }
|
---|
| 104 |
|
---|
[062d900] | 105 | static hash_table_ops_t set_ops = {
|
---|
| 106 | .hash = nic_addr_hash,
|
---|
| 107 | .key_hash = nic_addr_key_hash,
|
---|
| 108 | .key_equal = nic_addr_key_equal,
|
---|
[4e00f87] | 109 | .equal = NULL,
|
---|
[062d900] | 110 | .remove_callback = nic_addr_removed
|
---|
| 111 | };
|
---|
| 112 |
|
---|
[00d7e1b] | 113 | /**
|
---|
| 114 | * Initialize the database
|
---|
| 115 | *
|
---|
| 116 | * @param[in,out] db Uninitialized storage
|
---|
| 117 | * @param[in] addr_len Size of addresses in the db
|
---|
| 118 | *
|
---|
| 119 | * @return EOK If successfully initialized
|
---|
| 120 | * @return EINVAL If the address length is too big
|
---|
| 121 | * @return ENOMEM If there was not enough memory to initialize the storage
|
---|
| 122 | */
|
---|
| 123 | int nic_addr_db_init(nic_addr_db_t *db, size_t addr_len)
|
---|
| 124 | {
|
---|
| 125 | assert(db);
|
---|
[062d900] | 126 |
|
---|
| 127 | if (addr_len > UCHAR_MAX)
|
---|
[00d7e1b] | 128 | return EINVAL;
|
---|
[062d900] | 129 |
|
---|
| 130 | if (!hash_table_create(&db->set, 0, 0, &set_ops))
|
---|
[00d7e1b] | 131 | return ENOMEM;
|
---|
[062d900] | 132 |
|
---|
[00d7e1b] | 133 | db->addr_len = addr_len;
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Remove all records from the DB
|
---|
| 139 | *
|
---|
| 140 | * @param db
|
---|
| 141 | */
|
---|
| 142 | void nic_addr_db_clear(nic_addr_db_t *db)
|
---|
| 143 | {
|
---|
| 144 | assert(db);
|
---|
[062d900] | 145 | hash_table_clear(&db->set);
|
---|
[00d7e1b] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Free the memory used by db, including all records...
|
---|
| 150 | *
|
---|
| 151 | * @param db
|
---|
| 152 | */
|
---|
| 153 | void nic_addr_db_destroy(nic_addr_db_t *db)
|
---|
| 154 | {
|
---|
| 155 | assert(db);
|
---|
[062d900] | 156 | hash_table_destroy(&db->set);
|
---|
[00d7e1b] | 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Insert an address to the db
|
---|
| 162 | *
|
---|
| 163 | * @param db
|
---|
| 164 | * @param addr Inserted address. Length is implicitly concluded from the
|
---|
| 165 | * db's address length.
|
---|
| 166 | *
|
---|
| 167 | * @return EOK If the address was inserted
|
---|
| 168 | * @return ENOMEM If there was not enough memory
|
---|
| 169 | * @return EEXIST If this adress already is in the db
|
---|
| 170 | */
|
---|
| 171 | int nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr)
|
---|
| 172 | {
|
---|
| 173 | assert(db && addr);
|
---|
[062d900] | 174 |
|
---|
| 175 | addr_key_t key = {
|
---|
| 176 | .len = db->addr_len,
|
---|
| 177 | .addr = addr
|
---|
| 178 | };
|
---|
| 179 |
|
---|
| 180 | if (hash_table_find(&db->set, &key))
|
---|
| 181 | return EEXIST;
|
---|
| 182 |
|
---|
| 183 | nic_addr_entry_t *entry = malloc(sizeof(nic_addr_entry_t) + db->addr_len - 1);
|
---|
| 184 | if (entry == NULL)
|
---|
[00d7e1b] | 185 | return ENOMEM;
|
---|
| 186 |
|
---|
[062d900] | 187 | entry->len = (uint8_t) db->addr_len;
|
---|
| 188 | memcpy(entry->addr, addr, db->addr_len);
|
---|
| 189 |
|
---|
| 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 | */
|
---|
| 203 | int nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr)
|
---|
| 204 | {
|
---|
| 205 | assert(db && addr);
|
---|
[062d900] | 206 |
|
---|
| 207 | addr_key_t key = {
|
---|
| 208 | .len = db->addr_len,
|
---|
| 209 | .addr = addr
|
---|
| 210 | };
|
---|
| 211 |
|
---|
| 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 | */
|
---|
| 226 | int nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr)
|
---|
| 227 | {
|
---|
| 228 | assert(db && addr);
|
---|
[062d900] | 229 |
|
---|
| 230 | addr_key_t key = {
|
---|
| 231 | .len = db->addr_len,
|
---|
| 232 | .addr = addr
|
---|
| 233 | };
|
---|
| 234 |
|
---|
| 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 | */
|
---|
[062d900] | 249 | static bool nic_addr_db_fe_helper(ht_link_t *item, void *arg)
|
---|
| 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,
|
---|
| 266 | void (*func)(const uint8_t *, void *), void *arg)
|
---|
| 267 | {
|
---|
| 268 | nic_addr_db_fe_arg_t hs = { .func = func, .arg = arg };
|
---|
[062d900] | 269 | hash_table_apply((hash_table_t*)&db->set, nic_addr_db_fe_helper, &hs);
|
---|
[00d7e1b] | 270 | }
|
---|
| 271 |
|
---|
| 272 | /** @}
|
---|
| 273 | */
|
---|