2 * Copyright (c) 2012 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
39 #include <fibril_synch.h>
47 #include "inet_link.h"
48 #include "inet_util.h"
50 static inet_addrobj_t
*inet_addrobj_find_by_name_locked(const char *, inet_link_t
*);
52 static FIBRIL_MUTEX_INITIALIZE(addr_list_lock
);
53 static LIST_INITIALIZE(addr_list
);
54 static sysarg_t addr_id
= 0;
56 inet_addrobj_t
*inet_addrobj_new(void)
58 inet_addrobj_t
*addr
= calloc(1, sizeof(inet_addrobj_t
));
61 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed allocating address object. "
66 link_initialize(&addr
->addr_list
);
67 fibril_mutex_lock(&addr_list_lock
);
69 fibril_mutex_unlock(&addr_list_lock
);
74 void inet_addrobj_delete(inet_addrobj_t
*addr
)
76 if (addr
->name
!= NULL
)
81 int inet_addrobj_add(inet_addrobj_t
*addr
)
85 fibril_mutex_lock(&addr_list_lock
);
86 aobj
= inet_addrobj_find_by_name_locked(addr
->name
, addr
->ilink
);
88 /* Duplicate address name */
89 fibril_mutex_unlock(&addr_list_lock
);
93 list_append(&addr
->addr_list
, &addr_list
);
94 fibril_mutex_unlock(&addr_list_lock
);
99 void inet_addrobj_remove(inet_addrobj_t
*addr
)
101 fibril_mutex_lock(&addr_list_lock
);
102 list_remove(&addr
->addr_list
);
103 fibril_mutex_unlock(&addr_list_lock
);
106 /** Find address object matching address @a addr.
108 * @param addr Address
109 * @oaram find iaf_net to find network (using mask),
110 * iaf_addr to find local address (exact match)
112 inet_addrobj_t
*inet_addrobj_find(inet_addr_t
*addr
, inet_addrobj_find_t find
)
116 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find(%x)", (unsigned)addr
->ipv4
);
118 fibril_mutex_lock(&addr_list_lock
);
120 list_foreach(addr_list
, link
) {
121 inet_addrobj_t
*naddr
= list_get_instance(link
,
122 inet_addrobj_t
, addr_list
);
124 mask
= inet_netmask(naddr
->naddr
.bits
);
125 if ((naddr
->naddr
.ipv4
& mask
) == (addr
->ipv4
& mask
)) {
126 fibril_mutex_unlock(&addr_list_lock
);
127 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find: found %p",
133 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find: Not found");
134 fibril_mutex_unlock(&addr_list_lock
);
139 /** Find address object on a link, with a specific name.
141 * @param name Address object name
142 * @param ilink Inet link
143 * @return Address object
145 static inet_addrobj_t
*inet_addrobj_find_by_name_locked(const char *name
, inet_link_t
*ilink
)
147 assert(fibril_mutex_is_locked(&addr_list_lock
));
149 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find_by_name_locked('%s', '%s')",
150 name
, ilink
->svc_name
);
152 list_foreach(addr_list
, link
) {
153 inet_addrobj_t
*naddr
= list_get_instance(link
,
154 inet_addrobj_t
, addr_list
);
156 if (naddr
->ilink
== ilink
&& str_cmp(naddr
->name
, name
) == 0) {
157 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find_by_name_locked: found %p",
163 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find_by_name_locked: Not found");
169 /** Find address object on a link, with a specific name.
171 * @param name Address object name
172 * @param ilink Inet link
173 * @return Address object
175 inet_addrobj_t
*inet_addrobj_find_by_name(const char *name
, inet_link_t
*ilink
)
177 inet_addrobj_t
*aobj
;
179 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_find_by_name('%s', '%s')",
180 name
, ilink
->svc_name
);
182 fibril_mutex_lock(&addr_list_lock
);
183 aobj
= inet_addrobj_find_by_name_locked(name
, ilink
);
184 fibril_mutex_unlock(&addr_list_lock
);
189 /** Find address object with the given ID.
191 * @param id Address object ID
192 * @return Address object
194 inet_addrobj_t
*inet_addrobj_get_by_id(sysarg_t id
)
196 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "inet_addrobj_get_by_id(%zu)", (size_t)id
);
198 fibril_mutex_lock(&addr_list_lock
);
200 list_foreach(addr_list
, link
) {
201 inet_addrobj_t
*naddr
= list_get_instance(link
,
202 inet_addrobj_t
, addr_list
);
204 if (naddr
->id
== id
) {
205 fibril_mutex_unlock(&addr_list_lock
);
210 fibril_mutex_unlock(&addr_list_lock
);
215 /** Send datagram from address object */
216 int inet_addrobj_send_dgram(inet_addrobj_t
*addr
, inet_addr_t
*ldest
,
217 inet_dgram_t
*dgram
, uint8_t proto
, uint8_t ttl
, int df
)
219 inet_addr_t lsrc_addr
;
220 inet_addr_t
*ldest_addr
;
222 lsrc_addr
.ipv4
= addr
->naddr
.ipv4
;
223 ldest_addr
= &dgram
->dest
;
225 return inet_link_send_dgram(addr
->ilink
, &lsrc_addr
, ldest_addr
, dgram
,
229 /** Get IDs of all address objects. */
230 int inet_addrobj_get_id_list(sysarg_t
**rid_list
, size_t *rcount
)
235 fibril_mutex_lock(&addr_list_lock
);
236 count
= list_count(&addr_list
);
238 id_list
= calloc(count
, sizeof(sysarg_t
));
239 if (id_list
== NULL
) {
240 fibril_mutex_unlock(&addr_list_lock
);
245 list_foreach(addr_list
, link
) {
246 inet_addrobj_t
*addr
= list_get_instance(link
,
247 inet_addrobj_t
, addr_list
);
249 id_list
[i
++] = addr
->id
;
252 fibril_mutex_unlock(&addr_list_lock
);