HelenOS sources
This source file includes following definitions.
- nic_wv_key_hash
- nic_wv_hash
- nic_wv_key_equal
- nic_wol_virtues_init
- nic_wol_virtues_clear
- nic_wol_virtues_verify
- nic_wol_virtues_add
- nic_wol_virtues_remove
- nic_wol_virtues_find
- nic_wol_virtues_list
#include "nic_wol_virtues.h"
#include "nic.h"
#include <assert.h>
#include <errno.h>
static size_t nic_wv_key_hash(const void *key)
{
const nic_wv_id_t *k = key;
return *k;
}
static size_t nic_wv_hash(const ht_link_t *item)
{
nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
return virtue->id;
}
static bool nic_wv_key_equal(const void *key, const ht_link_t *item)
{
const nic_wv_id_t *k = key;
const nic_wol_virtue_t *virtue = (const nic_wol_virtue_t *) item;
return (virtue->id == *k);
}
errno_t nic_wol_virtues_init(nic_wol_virtues_t *wvs)
{
memset(wvs, 0, sizeof(nic_wol_virtues_t));
wvs->table_operations.hash = nic_wv_hash;
wvs->table_operations.key_hash = nic_wv_key_hash;
wvs->table_operations.key_equal = nic_wv_key_equal;
wvs->table_operations.equal = 0;
wvs->table_operations.remove_callback = 0;
if (!hash_table_create(&wvs->table, 0, 0, &wvs->table_operations)) {
return ENOMEM;
}
size_t i;
for (i = 0; i < NIC_WV_MAX; ++i) {
wvs->caps_max[i] = -1;
}
wvs->next_id = 0;
return EOK;
}
void nic_wol_virtues_clear(nic_wol_virtues_t *wvs)
{
hash_table_clear(&wvs->table);
nic_wv_type_t type;
for (type = NIC_WV_NONE; type < NIC_WV_MAX; ++type) {
nic_wol_virtue_t *virtue = wvs->lists[type];
while (virtue != NULL) {
nic_wol_virtue_t *next = virtue->next;
free(virtue->data);
free(virtue);
virtue = next;
}
wvs->lists_sizes[type] = 0;
}
}
errno_t nic_wol_virtues_verify(nic_wv_type_t type, const void *data, size_t length)
{
switch (type) {
case NIC_WV_ARP_REQUEST:
case NIC_WV_BROADCAST:
case NIC_WV_LINK_CHANGE:
return EOK;
case NIC_WV_DESTINATION:
return length == sizeof (nic_address_t) ? EOK : EINVAL;
case NIC_WV_DIRECTED_IPV4:
return length == sizeof (nic_wv_ipv4_data_t) ? EOK : EINVAL;
case NIC_WV_DIRECTED_IPV6:
return length == sizeof (nic_wv_ipv6_data_t) ? EOK : EINVAL;
case NIC_WV_FULL_MATCH:
return length % 2 == 0 ? EOK : EINVAL;
case NIC_WV_MAGIC_PACKET:
return data == NULL || length == sizeof (nic_wv_magic_packet_data_t) ?
EOK : EINVAL;
default:
return ENOTSUP;
}
}
errno_t nic_wol_virtues_add(nic_wol_virtues_t *wvs, nic_wol_virtue_t *virtue)
{
if (!nic_wv_is_multi(virtue->type) &&
wvs->lists[virtue->type] != NULL) {
return EINVAL;
}
do {
virtue->id = wvs->next_id++;
} while (NULL != hash_table_find(&wvs->table, &virtue->id));
hash_table_insert(&wvs->table, &virtue->item);
virtue->next = wvs->lists[virtue->type];
wvs->lists[virtue->type] = virtue;
wvs->lists_sizes[virtue->type]++;
return EOK;
}
nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id)
{
nic_wol_virtue_t *virtue =
(nic_wol_virtue_t *) hash_table_find(&wvs->table, &id);
if (virtue == NULL) {
return NULL;
}
hash_table_remove_item(&wvs->table, &virtue->item);
assert(wvs->lists[virtue->type] != NULL);
if (wvs->lists[virtue->type] == virtue) {
wvs->lists[virtue->type] = virtue->next;
} else {
nic_wol_virtue_t *wv = wvs->lists[virtue->type];
while (wv->next != virtue) {
wv = wv->next;
assert(wv != NULL);
}
wv->next = virtue->next;
}
wvs->lists_sizes[virtue->type]--;
virtue->next = NULL;
return virtue;
}
const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *wvs,
nic_wv_id_t id)
{
ht_link_t *virtue = hash_table_find(&((nic_wol_virtues_t *) wvs)->table, &id);
return (const nic_wol_virtue_t *) virtue;
}
errno_t nic_wol_virtues_list(const nic_wol_virtues_t *wvs, nic_wv_type_t type,
size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
{
size_t count = 0;
if (type == NIC_WV_NONE) {
size_t i;
for (i = NIC_WV_NONE; i < NIC_WV_MAX; ++i) {
if (id_list != NULL) {
nic_wol_virtue_t *virtue = wvs->lists[i];
while (virtue != NULL) {
if (count < max_count) {
id_list[count] = virtue->id;
}
++count;
virtue = virtue->next;
}
} else {
count += wvs->lists_sizes[i];
}
}
} else if (type >= NIC_WV_MAX) {
return EINVAL;
} else {
if (id_list != NULL) {
nic_wol_virtue_t *virtue = wvs->lists[type];
while (virtue != NULL) {
if (count < max_count) {
id_list[count] = virtue->id;
}
++count;
virtue = virtue->next;
}
} else {
count = wvs->lists_sizes[type];
}
}
if (id_count != NULL) {
*id_count = count;
}
return EOK;
}
HelenOS homepage, sources at GitHub