source: mainline/uspace/srv/net/inetsrv/sroute.c@ b4ec1ea

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

Rename app/inetcfg to app/inet, srv/inet to srv/inetsrv. Administration tools are run more often from the command line than the services they configure. It makes sense for them to have the shorter name.

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[8bf672d]1/*
2 * Copyright (c) 2012 Jiri Svoboda
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/** @addtogroup inet
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <bitops.h>
38#include <errno.h>
39#include <fibril_synch.h>
40#include <io/log.h>
41#include <ipc/loc.h>
42#include <stdlib.h>
43#include <str.h>
44
45#include "sroute.h"
[b4ec1ea]46#include "inetsrv.h"
[8bf672d]47#include "inet_link.h"
48#include "inet_util.h"
49
50static FIBRIL_MUTEX_INITIALIZE(sroute_list_lock);
51static LIST_INITIALIZE(sroute_list);
52static sysarg_t sroute_id = 0;
53
54inet_sroute_t *inet_sroute_new(void)
55{
56 inet_sroute_t *sroute = calloc(1, sizeof(inet_sroute_t));
57
58 if (sroute == NULL) {
59 log_msg(LVL_ERROR, "Failed allocating static route object. "
60 "Out of memory.");
61 return NULL;
62 }
63
64 link_initialize(&sroute->sroute_list);
65 fibril_mutex_lock(&sroute_list_lock);
66 sroute->id = ++sroute_id;
67 fibril_mutex_unlock(&sroute_list_lock);
68
69 return sroute;
70}
71
72void inet_sroute_delete(inet_sroute_t *sroute)
73{
74 if (sroute->name != NULL)
75 free(sroute->name);
76 free(sroute);
77}
78
79void inet_sroute_add(inet_sroute_t *sroute)
80{
81 fibril_mutex_lock(&sroute_list_lock);
82 list_append(&sroute->sroute_list, &sroute_list);
83 fibril_mutex_unlock(&sroute_list_lock);
84}
85
86void inet_sroute_remove(inet_sroute_t *sroute)
87{
88 fibril_mutex_lock(&sroute_list_lock);
89 list_remove(&sroute->sroute_list);
90 fibril_mutex_unlock(&sroute_list_lock);
91}
92
93/** Find address object matching address @a addr.
94 *
95 * @param addr Address
96 */
97inet_sroute_t *inet_sroute_find(inet_addr_t *addr)
98{
99 uint32_t mask;
100 inet_sroute_t *best;
101
102 log_msg(LVL_DEBUG, "inet_sroute_find(%x)", (unsigned)addr->ipv4);
103
104 fibril_mutex_lock(&sroute_list_lock);
105
106 best = NULL;
107
108 list_foreach(sroute_list, link) {
109 inet_sroute_t *sroute = list_get_instance(link,
110 inet_sroute_t, sroute_list);
111
112 /* Look for the most specific route */
113 if (best != NULL && best->dest.bits >= sroute->dest.bits)
114 continue;
115
116 mask = inet_netmask(sroute->dest.bits);
117 if ((sroute->dest.ipv4 & mask) == (addr->ipv4 & mask)) {
118 fibril_mutex_unlock(&sroute_list_lock);
119 log_msg(LVL_DEBUG, "inet_sroute_find: found %p",
120 sroute);
121 return sroute;
122 }
123 }
124
125 log_msg(LVL_DEBUG, "inet_sroute_find: Not found");
126 fibril_mutex_unlock(&sroute_list_lock);
127
128 return NULL;
129}
130
131/** Find static route with a specific name.
132 *
133 * @param name Address object name
134 * @return Address object
135 */
136inet_sroute_t *inet_sroute_find_by_name(const char *name)
137{
138 log_msg(LVL_DEBUG, "inet_sroute_find_by_name('%s')",
139 name);
140
141 fibril_mutex_lock(&sroute_list_lock);
142
143 list_foreach(sroute_list, link) {
144 inet_sroute_t *sroute = list_get_instance(link,
145 inet_sroute_t, sroute_list);
146
147 if (str_cmp(sroute->name, name) == 0) {
148 fibril_mutex_unlock(&sroute_list_lock);
149 log_msg(LVL_DEBUG, "inet_sroute_find_by_name: found %p",
150 sroute);
151 return sroute;
152 }
153 }
154
155 log_msg(LVL_DEBUG, "inet_sroute_find_by_name: Not found");
156 fibril_mutex_unlock(&sroute_list_lock);
157
158 return NULL;
159}
160
161/** Find static route with the given ID.
162 *
163 * @param id Address object ID
164 * @return Address object
165 */
166inet_sroute_t *inet_sroute_get_by_id(sysarg_t id)
167{
168 log_msg(LVL_DEBUG, "inet_sroute_get_by_id(%zu)", (size_t)id);
169
170 fibril_mutex_lock(&sroute_list_lock);
171
172 list_foreach(sroute_list, link) {
173 inet_sroute_t *sroute = list_get_instance(link,
174 inet_sroute_t, sroute_list);
175
176 if (sroute->id == id) {
177 fibril_mutex_unlock(&sroute_list_lock);
178 return sroute;
179 }
180 }
181
182 fibril_mutex_unlock(&sroute_list_lock);
183
184 return NULL;
185}
186
187/** Get IDs of all static routes. */
188int inet_sroute_get_id_list(sysarg_t **rid_list, size_t *rcount)
189{
190 sysarg_t *id_list;
191 size_t count, i;
192
193 fibril_mutex_lock(&sroute_list_lock);
194 count = list_count(&sroute_list);
195
196 id_list = calloc(count, sizeof(sysarg_t));
197 if (id_list == NULL) {
198 fibril_mutex_unlock(&sroute_list_lock);
199 return ENOMEM;
200 }
201
202 i = 0;
203 list_foreach(sroute_list, link) {
204 inet_sroute_t *sroute = list_get_instance(link,
205 inet_sroute_t, sroute_list);
206
207 id_list[i++] = sroute->id;
208 }
209
210 fibril_mutex_unlock(&sroute_list_lock);
211
212 *rid_list = id_list;
213 *rcount = count;
214
215 return EOK;
216}
217
218/** @}
219 */
Note: See TracBrowser for help on using the repository browser.