source: mainline/uspace/lib/nic/src/nic_wol_virtues.c@ ee8d4d6

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

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 7.9 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 Wake-on-LAN support
36 */
37
38#include "nic_wol_virtues.h"
39#include "nic.h"
40#include <assert.h>
41#include <errno.h>
42
43/*
44 * Hash table helper functions
45 */
46
47static size_t nic_wv_key_hash(void *key)
48{
49 return *(nic_wv_id_t *) key;
50}
51
52static size_t nic_wv_hash(const ht_link_t *item)
53{
54 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
55 return virtue->id;
56}
57
58static bool nic_wv_key_equal(void *key, const ht_link_t *item)
59{
60 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
61 return (virtue->id == *(nic_wv_id_t *) key);
62}
63
64/**
65 * Initializes the WOL virtues structure
66 *
67 * @param wvs
68 *
69 * @return EOK On success
70 * @return ENOMEM On not enough memory
71 */
72errno_t nic_wol_virtues_init(nic_wol_virtues_t *wvs)
73{
74 memset(wvs, 0, sizeof(nic_wol_virtues_t));
75 wvs->table_operations.hash = nic_wv_hash;
76 wvs->table_operations.key_hash = nic_wv_key_hash;
77 wvs->table_operations.key_equal = nic_wv_key_equal;
78 wvs->table_operations.equal = 0;
79 wvs->table_operations.remove_callback = 0;
80
81 if (!hash_table_create(&wvs->table, 0, 0, &wvs->table_operations)) {
82 return ENOMEM;
83 }
84 size_t i;
85 for (i = 0; i < NIC_WV_MAX; ++i) {
86 wvs->caps_max[i] = -1;
87 }
88 wvs->next_id = 0;
89 return EOK;
90}
91
92/**
93 * Reinitializes the structure, destroying all virtues. The next_id is not
94 * changed (some apps could still hold the filter IDs).
95 *
96 * @param wvs
97 */
98void nic_wol_virtues_clear(nic_wol_virtues_t *wvs)
99{
100 hash_table_clear(&wvs->table);
101 nic_wv_type_t type;
102 for (type = NIC_WV_NONE; type < NIC_WV_MAX; ++type) {
103 nic_wol_virtue_t *virtue = wvs->lists[type];
104 while (virtue != NULL) {
105 nic_wol_virtue_t *next = virtue->next;
106 free(virtue->data);
107 free(virtue);
108 virtue = next;
109 }
110 wvs->lists_sizes[type] = 0;
111 }
112}
113
114/**
115 * Verifies that the arguments for the WOL virtues are correct.
116 *
117 * @param type Type of the virtue
118 * @param data Data argument for the virtue
119 * @param length Length of the data
120 *
121 * @return EOK The arguments are correct
122 * @return EINVAL The arguments are incorrect
123 * @return ENOTSUP This type is unknown
124 */
125errno_t nic_wol_virtues_verify(nic_wv_type_t type, const void *data, size_t length)
126{
127 switch (type) {
128 case NIC_WV_ARP_REQUEST:
129 case NIC_WV_BROADCAST:
130 case NIC_WV_LINK_CHANGE:
131 return EOK;
132 case NIC_WV_DESTINATION:
133 return length == sizeof (nic_address_t) ? EOK : EINVAL;
134 case NIC_WV_DIRECTED_IPV4:
135 return length == sizeof (nic_wv_ipv4_data_t) ? EOK : EINVAL;
136 case NIC_WV_DIRECTED_IPV6:
137 return length == sizeof (nic_wv_ipv6_data_t) ? EOK : EINVAL;
138 case NIC_WV_FULL_MATCH:
139 return length % 2 == 0 ? EOK : EINVAL;
140 case NIC_WV_MAGIC_PACKET:
141 return data == NULL || length == sizeof (nic_wv_magic_packet_data_t) ?
142 EOK : EINVAL;
143 default:
144 return ENOTSUP;
145 }
146}
147
148/**
149 * Adds the virtue to the list of known virtues, activating it.
150 *
151 * @param wvs
152 * @param virtue The virtue structure
153 *
154 * @return EOK On success
155 * @return ENOTSUP If the virtue type is not supported
156 * @return EINVAL If the virtue type is a single-filter and there's already
157 * a virtue of this type defined, or there is something wrong
158 * with the data
159 * @return ENOMEM Not enough memory to activate the virtue
160 */
161errno_t nic_wol_virtues_add(nic_wol_virtues_t *wvs, nic_wol_virtue_t *virtue)
162{
163 if (!nic_wv_is_multi(virtue->type) &&
164 wvs->lists[virtue->type] != NULL) {
165 return EINVAL;
166 }
167 do {
168 virtue->id = wvs->next_id++;
169 } while (NULL != hash_table_find(&wvs->table, &virtue->id));
170 hash_table_insert(&wvs->table, &virtue->item);
171 virtue->next = wvs->lists[virtue->type];
172 wvs->lists[virtue->type] = virtue;
173 wvs->lists_sizes[virtue->type]++;
174 return EOK;
175}
176
177/**
178 * Removes the virtue from the list of virtues, but NOT deallocating the
179 * nic_wol_virtue structure.
180 *
181 * @param wvs
182 * @param id Identifier of the removed virtue
183 *
184 * @return Removed virtue structure or NULL if not found.
185 */
186nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id)
187{
188 nic_wol_virtue_t *virtue =
189 (nic_wol_virtue_t *) hash_table_find(&wvs->table, &id);
190 if (virtue == NULL) {
191 return NULL;
192 }
193
194 /* Remove from filter_table */
195 hash_table_remove_item(&wvs->table, &virtue->item);
196
197 /* Remove from filter_types */
198 assert(wvs->lists[virtue->type] != NULL);
199 if (wvs->lists[virtue->type] == virtue) {
200 wvs->lists[virtue->type] = virtue->next;
201 } else {
202 nic_wol_virtue_t *wv = wvs->lists[virtue->type];
203 while (wv->next != virtue) {
204 wv = wv->next;
205 assert(wv != NULL);
206 }
207 wv->next = virtue->next;
208 }
209 wvs->lists_sizes[virtue->type]--;
210
211 virtue->next = NULL;
212 return virtue;
213}
214
215/**
216 * Searches the filters table for a filter with specified ID
217 *
218 * @param wvs
219 * @param id Identifier of the searched virtue
220 *
221 * @return Requested filter or NULL if not found.
222 */
223const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *wvs,
224 nic_wv_id_t id)
225{
226 /*
227 * The hash_table_find cannot be const, because it would require the
228 * returned link to be const as well. But in this case, when we're returning
229 * constant virtue the retyping is correct.
230 */
231 ht_link_t *virtue = hash_table_find(&((nic_wol_virtues_t *) wvs)->table, &id);
232 return (const nic_wol_virtue_t *) virtue;
233}
234
235/**
236 * Fill identifiers of current wol virtues of the specified type into the list.
237 * If the type is set to NIC_WV_NONE, all virtues are used.
238 *
239 * @param wvs
240 * @param[in] type Type of the virtues or NIC_WV_NONE
241 * @param[out] id_list The new vector of filter IDs. Can be NULL.
242 * @param[out] count Number of IDs in the filter_list. Can be NULL.
243 *
244 * @return EOK If it completes successfully
245 * @return EINVAL If the filter type is invalid
246 */
247errno_t nic_wol_virtues_list(const nic_wol_virtues_t *wvs, nic_wv_type_t type,
248 size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
249{
250 size_t count = 0;
251 if (type == NIC_WV_NONE) {
252 size_t i;
253 for (i = NIC_WV_NONE; i < NIC_WV_MAX; ++i) {
254 if (id_list != NULL) {
255 nic_wol_virtue_t *virtue = wvs->lists[i];
256 while (virtue != NULL) {
257 if (count < max_count) {
258 id_list[count] = virtue->id;
259 }
260 ++count;
261 virtue = virtue->next;
262 }
263 } else {
264 count += wvs->lists_sizes[i];
265 }
266 }
267 } else if (type >= NIC_WV_MAX) {
268 return EINVAL;
269 } else {
270 if (id_list != NULL) {
271 nic_wol_virtue_t *virtue = wvs->lists[type];
272 while (virtue != NULL) {
273 if (count < max_count) {
274 id_list[count] = virtue->id;
275 }
276 ++count;
277 virtue = virtue->next;
278 }
279 } else {
280 count = wvs->lists_sizes[type];
281 }
282 }
283 if (id_count != NULL) {
284 *id_count = count;
285 }
286 return EOK;
287}
288
289/** @}
290 */
Note: See TracBrowser for help on using the repository browser.