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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8562724 was 00d7e1b, checked in by Martin Decky <martin@…>, 14 years ago

networking improvements

  • start the networking stack from init
  • add loopback network interface driver (cherrypicked and sanitized from lp:~helenos-nicf/helenos/nicf)
  • add libnic and various small pieces from lp:~helenos-nicf/helenos/nicf
  • fix client side of NIC_GET_ADDRESS
  • net binary overhaul

Note: "ping 127.0.0.1" works, but the first three pings timeout for some reason

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