source: mainline/uspace/srv/net/inetsrv/addrobj.c@ 984a9ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 984a9ba was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[ceba4bed]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>
[0e94b979]38#include <errno.h>
[ceba4bed]39#include <fibril_synch.h>
40#include <io/log.h>
41#include <ipc/loc.h>
42#include <stdlib.h>
[fa101c4]43#include <str.h>
[ceba4bed]44#include "addrobj.h"
[b4ec1ea]45#include "inetsrv.h"
[ceba4bed]46#include "inet_link.h"
[a17356fd]47#include "ndp.h"
[ceba4bed]48
[bf9e6fc]49static inet_addrobj_t *inet_addrobj_find_by_name_locked(const char *, inet_link_t *);
50
[ceba4bed]51static FIBRIL_MUTEX_INITIALIZE(addr_list_lock);
52static LIST_INITIALIZE(addr_list);
[0e94b979]53static sysarg_t addr_id = 0;
[ceba4bed]54
55inet_addrobj_t *inet_addrobj_new(void)
56{
57 inet_addrobj_t *addr = calloc(1, sizeof(inet_addrobj_t));
58
59 if (addr == NULL) {
[a1a101d]60 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating address object. "
[ceba4bed]61 "Out of memory.");
62 return NULL;
63 }
64
65 link_initialize(&addr->addr_list);
[0e94b979]66 fibril_mutex_lock(&addr_list_lock);
67 addr->id = ++addr_id;
68 fibril_mutex_unlock(&addr_list_lock);
[ceba4bed]69
70 return addr;
71}
72
73void inet_addrobj_delete(inet_addrobj_t *addr)
74{
[0e94b979]75 if (addr->name != NULL)
76 free(addr->name);
[ceba4bed]77 free(addr);
78}
79
[b7fd2a0]80errno_t inet_addrobj_add(inet_addrobj_t *addr)
[ceba4bed]81{
[bf9e6fc]82 inet_addrobj_t *aobj;
83
[ceba4bed]84 fibril_mutex_lock(&addr_list_lock);
[bf9e6fc]85 aobj = inet_addrobj_find_by_name_locked(addr->name, addr->ilink);
86 if (aobj != NULL) {
87 /* Duplicate address name */
88 fibril_mutex_unlock(&addr_list_lock);
[8a637a4]89 return EEXIST;
[bf9e6fc]90 }
91
[ceba4bed]92 list_append(&addr->addr_list, &addr_list);
93 fibril_mutex_unlock(&addr_list_lock);
[bf9e6fc]94
95 return EOK;
[ceba4bed]96}
97
98void inet_addrobj_remove(inet_addrobj_t *addr)
99{
100 fibril_mutex_lock(&addr_list_lock);
101 list_remove(&addr->addr_list);
102 fibril_mutex_unlock(&addr_list_lock);
103}
104
[e767dbf]105/** Find address object matching address @a addr.
106 *
[02a09ed]107 * @param addr Address
108 * @oaram find iaf_net to find network (using mask),
109 * iaf_addr to find local address (exact match)
110 *
[e767dbf]111 */
112inet_addrobj_t *inet_addrobj_find(inet_addr_t *addr, inet_addrobj_find_t find)
[ceba4bed]113{
114 fibril_mutex_lock(&addr_list_lock);
[a35b458]115
[feeac0d]116 list_foreach(addr_list, addr_list, inet_addrobj_t, naddr) {
[30c5d13]117 switch (find) {
118 case iaf_net:
119 if (inet_naddr_compare_mask(&naddr->naddr, addr)) {
120 fibril_mutex_unlock(&addr_list_lock);
121 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find: found %p",
122 naddr);
123 return naddr;
124 }
125 break;
126 case iaf_addr:
127 if (inet_naddr_compare(&naddr->naddr, addr)) {
128 fibril_mutex_unlock(&addr_list_lock);
129 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find: found %p",
130 naddr);
131 return naddr;
132 }
133 break;
[ceba4bed]134 }
135 }
[a35b458]136
[a1a101d]137 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find: Not found");
[ceba4bed]138 fibril_mutex_unlock(&addr_list_lock);
[a35b458]139
[ceba4bed]140 return NULL;
141}
142
[fa101c4]143/** Find address object on a link, with a specific name.
144 *
145 * @param name Address object name
146 * @param ilink Inet link
147 * @return Address object
148 */
[bf9e6fc]149static inet_addrobj_t *inet_addrobj_find_by_name_locked(const char *name, inet_link_t *ilink)
[fa101c4]150{
[bf9e6fc]151 assert(fibril_mutex_is_locked(&addr_list_lock));
[fa101c4]152
[a1a101d]153 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find_by_name_locked('%s', '%s')",
[bf9e6fc]154 name, ilink->svc_name);
[fa101c4]155
[feeac0d]156 list_foreach(addr_list, addr_list, inet_addrobj_t, naddr) {
[fa101c4]157 if (naddr->ilink == ilink && str_cmp(naddr->name, name) == 0) {
[a1a101d]158 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find_by_name_locked: found %p",
[fa101c4]159 naddr);
160 return naddr;
161 }
162 }
163
[a1a101d]164 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find_by_name_locked: Not found");
[fa101c4]165
166 return NULL;
167}
168
[bf9e6fc]169
170/** Find address object on a link, with a specific name.
171 *
172 * @param name Address object name
173 * @param ilink Inet link
174 * @return Address object
175 */
176inet_addrobj_t *inet_addrobj_find_by_name(const char *name, inet_link_t *ilink)
177{
178 inet_addrobj_t *aobj;
179
[a1a101d]180 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find_by_name('%s', '%s')",
[bf9e6fc]181 name, ilink->svc_name);
182
183 fibril_mutex_lock(&addr_list_lock);
184 aobj = inet_addrobj_find_by_name_locked(name, ilink);
185 fibril_mutex_unlock(&addr_list_lock);
186
187 return aobj;
188}
189
[8bf672d]190/** Find address object with the given ID.
[0e94b979]191 *
192 * @param id Address object ID
193 * @return Address object
194 */
195inet_addrobj_t *inet_addrobj_get_by_id(sysarg_t id)
196{
[a1a101d]197 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_get_by_id(%zu)", (size_t)id);
[0e94b979]198
199 fibril_mutex_lock(&addr_list_lock);
200
[feeac0d]201 list_foreach(addr_list, addr_list, inet_addrobj_t, naddr) {
[0e94b979]202 if (naddr->id == id) {
203 fibril_mutex_unlock(&addr_list_lock);
204 return naddr;
205 }
206 }
207
208 fibril_mutex_unlock(&addr_list_lock);
209
210 return NULL;
211}
212
[8bf672d]213/** Send datagram from address object */
[b7fd2a0]214errno_t inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_addr_t *ldest,
[8bf672d]215 inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
[ceba4bed]216{
217 inet_addr_t lsrc_addr;
[a2e3ee6]218 inet_naddr_addr(&addr->naddr, &lsrc_addr);
[f023251]219
[a17356fd]220 addr32_t lsrc_v4;
221 addr128_t lsrc_v6;
[f023251]222 ip_ver_t lsrc_ver = inet_addr_get(&lsrc_addr, &lsrc_v4, &lsrc_v6);
223
[a17356fd]224 addr32_t ldest_v4;
225 addr128_t ldest_v6;
[f023251]226 ip_ver_t ldest_ver = inet_addr_get(ldest, &ldest_v4, &ldest_v6);
227
228 if (lsrc_ver != ldest_ver)
[a17356fd]229 return EINVAL;
[f023251]230
[b7fd2a0]231 errno_t rc;
[a17356fd]232 addr48_t ldest_mac;
[f023251]233
234 switch (ldest_ver) {
235 case ip_v4:
[a17356fd]236 return inet_link_send_dgram(addr->ilink, lsrc_v4, ldest_v4,
237 dgram, proto, ttl, df);
[f023251]238 case ip_v6:
[a17356fd]239 /*
240 * Translate local destination IPv6 address.
241 */
242 rc = ndp_translate(lsrc_v6, ldest_v6, ldest_mac, addr->ilink);
243 if (rc != EOK)
244 return rc;
[f023251]245
[a17356fd]246 return inet_link_send_dgram6(addr->ilink, ldest_mac, dgram,
247 proto, ttl, df);
[f023251]248 default:
249 assert(false);
250 break;
[a17356fd]251 }
[f023251]252
[a17356fd]253 return ENOTSUP;
[ceba4bed]254}
255
[0e94b979]256/** Get IDs of all address objects. */
[b7fd2a0]257errno_t inet_addrobj_get_id_list(sysarg_t **rid_list, size_t *rcount)
[0e94b979]258{
259 sysarg_t *id_list;
260 size_t count, i;
261
262 fibril_mutex_lock(&addr_list_lock);
263 count = list_count(&addr_list);
264
265 id_list = calloc(count, sizeof(sysarg_t));
266 if (id_list == NULL) {
267 fibril_mutex_unlock(&addr_list_lock);
268 return ENOMEM;
269 }
270
271 i = 0;
[feeac0d]272 list_foreach(addr_list, addr_list, inet_addrobj_t, addr) {
[0e94b979]273 id_list[i++] = addr->id;
274 }
275
276 fibril_mutex_unlock(&addr_list_lock);
277
278 *rid_list = id_list;
279 *rcount = count;
280
281 return EOK;
282}
283
[ceba4bed]284/** @}
285 */
Note: See TracBrowser for help on using the repository browser.