source: mainline/uspace/srv/inet/addrobj.c@ 291c792

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

Listing configured addresses.

  • Property mode set to 100644
File size: 5.0 KB
Line 
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
44#include "addrobj.h"
45#include "inet.h"
46#include "inet_link.h"
47
48static FIBRIL_MUTEX_INITIALIZE(addr_list_lock);
49static LIST_INITIALIZE(addr_list);
50static sysarg_t addr_id = 0;
51
52static uint32_t inet_netmask(int bits)
53{
54 assert(bits >= 1);
55 assert(bits < 32);
56
57 return BIT_RANGE(uint32_t, 31, 31 - (bits - 1));
58}
59
60inet_addrobj_t *inet_addrobj_new(void)
61{
62 inet_addrobj_t *addr = calloc(1, sizeof(inet_addrobj_t));
63
64 if (addr == NULL) {
65 log_msg(LVL_ERROR, "Failed allocating address object. "
66 "Out of memory.");
67 return NULL;
68 }
69
70 link_initialize(&addr->addr_list);
71 fibril_mutex_lock(&addr_list_lock);
72 addr->id = ++addr_id;
73 fibril_mutex_unlock(&addr_list_lock);
74
75 return addr;
76}
77
78void inet_addrobj_delete(inet_addrobj_t *addr)
79{
80 if (addr->name != NULL)
81 free(addr->name);
82 free(addr);
83}
84
85void inet_addrobj_add(inet_addrobj_t *addr)
86{
87 fibril_mutex_lock(&addr_list_lock);
88 list_append(&addr->addr_list, &addr_list);
89 fibril_mutex_unlock(&addr_list_lock);
90}
91
92void inet_addrobj_remove(inet_addrobj_t *addr)
93{
94 fibril_mutex_lock(&addr_list_lock);
95 list_remove(&addr->addr_list);
96 fibril_mutex_unlock(&addr_list_lock);
97}
98
99/** Find address object matching address @a addr.
100 *
101 * @param addr Address
102 * @oaram find iaf_net to find network (using mask),
103 * iaf_addr to find local address (exact match)
104 */
105inet_addrobj_t *inet_addrobj_find(inet_addr_t *addr, inet_addrobj_find_t find)
106{
107 uint32_t mask;
108
109 log_msg(LVL_DEBUG, "inet_addrobj_find(%x)", (unsigned)addr->ipv4);
110
111 fibril_mutex_lock(&addr_list_lock);
112
113 list_foreach(addr_list, link) {
114 inet_addrobj_t *naddr = list_get_instance(link,
115 inet_addrobj_t, addr_list);
116
117 mask = inet_netmask(naddr->naddr.bits);
118 if ((naddr->naddr.ipv4 & mask) == (addr->ipv4 & mask)) {
119 fibril_mutex_unlock(&addr_list_lock);
120 log_msg(LVL_DEBUG, "inet_addrobj_find: found %p",
121 addr);
122 return naddr;
123 }
124 }
125
126 log_msg(LVL_DEBUG, "inet_addrobj_find: Not found");
127 fibril_mutex_unlock(&addr_list_lock);
128
129 return NULL;
130}
131
132/** Find address object matching address @a addr.
133 *
134 * @param id Address object ID
135 * @return Address object
136 */
137inet_addrobj_t *inet_addrobj_get_by_id(sysarg_t id)
138{
139 log_msg(LVL_DEBUG, "inet_addrobj_get_by_id(%zu)", (size_t)id);
140
141 fibril_mutex_lock(&addr_list_lock);
142
143 list_foreach(addr_list, link) {
144 inet_addrobj_t *naddr = list_get_instance(link,
145 inet_addrobj_t, addr_list);
146
147 if (naddr->id == id) {
148 fibril_mutex_unlock(&addr_list_lock);
149 return naddr;
150 }
151 }
152
153 fibril_mutex_unlock(&addr_list_lock);
154
155 return NULL;
156}
157
158/** Send datagram to directly reachable destination */
159int inet_addrobj_send_dgram(inet_addrobj_t *addr, inet_dgram_t *dgram,
160 uint8_t proto, uint8_t ttl, int df)
161{
162 inet_addr_t lsrc_addr;
163 inet_addr_t *ldest_addr;
164
165 lsrc_addr.ipv4 = addr->naddr.ipv4;
166 ldest_addr = &dgram->dest;
167
168 return inet_link_send_dgram(addr->ilink, &lsrc_addr, ldest_addr, dgram,
169 proto, ttl, df);
170}
171
172/** Get IDs of all address objects. */
173int inet_addrobj_get_id_list(sysarg_t **rid_list, size_t *rcount)
174{
175 sysarg_t *id_list;
176 size_t count, i;
177
178 fibril_mutex_lock(&addr_list_lock);
179 count = list_count(&addr_list);
180
181 id_list = calloc(count, sizeof(sysarg_t));
182 if (id_list == NULL) {
183 fibril_mutex_unlock(&addr_list_lock);
184 return ENOMEM;
185 }
186
187 i = 0;
188 list_foreach(addr_list, link) {
189 inet_addrobj_t *addr = list_get_instance(link,
190 inet_addrobj_t, addr_list);
191
192 id_list[i++] = addr->id;
193 }
194
195 fibril_mutex_unlock(&addr_list_lock);
196
197 *rid_list = id_list;
198 *rcount = count;
199
200 return EOK;
201}
202
203/** @}
204 */
Note: See TracBrowser for help on using the repository browser.