source: mainline/uspace/lib/nic/src/nic_addr_db.c@ 44ecf89

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44ecf89 was 44ecf89, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Replace deprecated function bcmp() with memcmp().

  • Property mode set to 100644
File size: 6.3 KB
Line 
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 */
37#include "nic_addr_db.h"
38#include "libarch/common.h"
39#include <assert.h>
40#include <stdlib.h>
41#include <stdbool.h>
42#include <errno.h>
43#include <mem.h>
44#include <adt/hash_table.h>
45#include <macros.h>
46#include <stdint.h>
47
48
49/**
50 * Helper structure for keeping the address in the hash set.
51 */
52typedef struct nic_addr_entry {
53 ht_link_t link;
54 uint8_t len;
55 uint8_t addr[1];
56} nic_addr_entry_t;
57
58
59/*
60 * Hash table helper functions
61 */
62typedef struct {
63 size_t len;
64 const uint8_t *addr;
65} addr_key_t;
66
67static bool nic_addr_key_equal(void *key_arg, const ht_link_t *item)
68{
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
72 return memcmp(entry->addr, key->addr, entry->len) == 0;
73}
74
75static 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];
81 }
82
83 return hash;
84}
85
86static 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
92static 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
98static void nic_addr_removed(ht_link_t *item)
99{
100 nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
101
102 free(entry);
103}
104
105static 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,
109 .equal = NULL,
110 .remove_callback = nic_addr_removed
111};
112
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 */
123int nic_addr_db_init(nic_addr_db_t *db, size_t addr_len)
124{
125 assert(db);
126
127 if (addr_len > UCHAR_MAX)
128 return EINVAL;
129
130 if (!hash_table_create(&db->set, 0, 0, &set_ops))
131 return ENOMEM;
132
133 db->addr_len = addr_len;
134 return EOK;
135}
136
137/**
138 * Remove all records from the DB
139 *
140 * @param db
141 */
142void nic_addr_db_clear(nic_addr_db_t *db)
143{
144 assert(db);
145 hash_table_clear(&db->set);
146}
147
148/**
149 * Free the memory used by db, including all records...
150 *
151 * @param db
152 */
153void nic_addr_db_destroy(nic_addr_db_t *db)
154{
155 assert(db);
156 hash_table_destroy(&db->set);
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 */
171int nic_addr_db_insert(nic_addr_db_t *db, const uint8_t *addr)
172{
173 assert(db && addr);
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)
185 return ENOMEM;
186
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;
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 */
203int nic_addr_db_remove(nic_addr_db_t *db, const uint8_t *addr)
204{
205 assert(db && addr);
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;
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 */
226int nic_addr_db_contains(const nic_addr_db_t *db, const uint8_t *addr)
227{
228 assert(db && addr);
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);
236}
237
238/**
239 * Helper structure for nic_addr_db_foreach
240 */
241typedef 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 */
249static bool nic_addr_db_fe_helper(ht_link_t *item, void *arg)
250{
251 nic_addr_db_fe_arg_t *hs = (nic_addr_db_fe_arg_t *) arg;
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;
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 */
265void 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 };
269 hash_table_apply((hash_table_t*)&db->set, nic_addr_db_fe_helper, &hs);
270}
271
272/** @}
273 */
Note: See TracBrowser for help on using the repository browser.