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 <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <ipc/inet.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <str.h>
|
---|
45 | #include <sys/types.h>
|
---|
46 |
|
---|
47 | #include "addrobj.h"
|
---|
48 | #include "inet.h"
|
---|
49 | #include "inet_link.h"
|
---|
50 | #include "inetcfg.h"
|
---|
51 |
|
---|
52 | static int inetcfg_addr_create_static(char *name, inet_naddr_t *naddr,
|
---|
53 | sysarg_t link_id, sysarg_t *addr_id)
|
---|
54 | {
|
---|
55 | inet_link_t *ilink;
|
---|
56 | inet_addrobj_t *addr;
|
---|
57 | iplink_addr_t iaddr;
|
---|
58 | int rc;
|
---|
59 |
|
---|
60 | ilink = inet_link_get_by_id(link_id);
|
---|
61 | if (ilink == NULL) {
|
---|
62 | log_msg(LVL_DEBUG, "Link %lu not found.",
|
---|
63 | (unsigned long) link_id);
|
---|
64 | return ENOENT;
|
---|
65 | }
|
---|
66 |
|
---|
67 | addr = inet_addrobj_new();
|
---|
68 | addr->naddr = *naddr;
|
---|
69 | addr->ilink = ilink;
|
---|
70 | addr->name = str_dup(name);
|
---|
71 | inet_addrobj_add(addr);
|
---|
72 |
|
---|
73 | iaddr.ipv4 = addr->naddr.ipv4;
|
---|
74 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
75 | if (rc != EOK) {
|
---|
76 | log_msg(LVL_ERROR, "Failed setting IP address on internet link.");
|
---|
77 | inet_addrobj_remove(addr);
|
---|
78 | inet_addrobj_delete(addr);
|
---|
79 | return rc;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return EOK;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static int inetcfg_addr_delete(sysarg_t addr_id)
|
---|
86 | {
|
---|
87 | return ENOTSUP;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo)
|
---|
91 | {
|
---|
92 | inet_addrobj_t *addr;
|
---|
93 |
|
---|
94 | addr = inet_addrobj_get_by_id(addr_id);
|
---|
95 | if (addr == NULL)
|
---|
96 | return ENOENT;
|
---|
97 |
|
---|
98 | ainfo->naddr = addr->naddr;
|
---|
99 | ainfo->ilink = addr->ilink->svc_id;
|
---|
100 | ainfo->name = str_dup(addr->name);
|
---|
101 |
|
---|
102 | return EOK;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)
|
---|
106 | {
|
---|
107 | return inet_addrobj_get_id_list(addrs, count);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static int inetcfg_get_link_list(sysarg_t **addrs, size_t *count)
|
---|
111 | {
|
---|
112 | return ENOTSUP;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
|
---|
116 | {
|
---|
117 | inet_link_t *ilink;
|
---|
118 |
|
---|
119 | ilink = inet_link_get_by_id(link_id);
|
---|
120 | if (ilink == NULL) {
|
---|
121 | return ENOENT;
|
---|
122 | }
|
---|
123 |
|
---|
124 | linfo->name = str_dup(ilink->svc_name);
|
---|
125 | return EOK;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void inetcfg_addr_create_static_srv(ipc_callid_t callid,
|
---|
129 | ipc_call_t *call)
|
---|
130 | {
|
---|
131 | char *name;
|
---|
132 | inet_naddr_t naddr;
|
---|
133 | sysarg_t link_id;
|
---|
134 | sysarg_t addr_id;
|
---|
135 | int rc;
|
---|
136 |
|
---|
137 | log_msg(LVL_DEBUG, "inetcfg_addr_create_static_srv()");
|
---|
138 |
|
---|
139 | rc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN,
|
---|
140 | 0, NULL);
|
---|
141 | if (rc != EOK) {
|
---|
142 | async_answer_0(callid, rc);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | naddr.ipv4 = IPC_GET_ARG1(*call);
|
---|
147 | naddr.bits = IPC_GET_ARG2(*call);
|
---|
148 | link_id = IPC_GET_ARG3(*call);
|
---|
149 |
|
---|
150 | addr_id = 0;
|
---|
151 | rc = inetcfg_addr_create_static(name, &naddr, link_id, &addr_id);
|
---|
152 | free(name);
|
---|
153 | async_answer_1(callid, rc, addr_id);
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void inetcfg_addr_delete_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
157 | {
|
---|
158 | sysarg_t addr_id;
|
---|
159 | int rc;
|
---|
160 |
|
---|
161 | log_msg(LVL_DEBUG, "inetcfg_addr_delete_srv()");
|
---|
162 |
|
---|
163 | addr_id = IPC_GET_ARG1(*call);
|
---|
164 |
|
---|
165 | rc = inetcfg_addr_delete(addr_id);
|
---|
166 | async_answer_0(callid, rc);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void inetcfg_addr_get_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
170 | {
|
---|
171 | ipc_callid_t rcallid;
|
---|
172 | size_t max_size;
|
---|
173 |
|
---|
174 | sysarg_t addr_id;
|
---|
175 | inet_addr_info_t ainfo;
|
---|
176 | int rc;
|
---|
177 |
|
---|
178 | addr_id = IPC_GET_ARG1(*call);
|
---|
179 | log_msg(LVL_DEBUG, "inetcfg_addr_get_srv()");
|
---|
180 |
|
---|
181 | ainfo.naddr.ipv4 = 0;
|
---|
182 | ainfo.naddr.bits = 0;
|
---|
183 | ainfo.ilink = 0;
|
---|
184 | ainfo.name = NULL;
|
---|
185 |
|
---|
186 | if (!async_data_read_receive(&rcallid, &max_size)) {
|
---|
187 | async_answer_0(rcallid, EREFUSED);
|
---|
188 | async_answer_0(callid, EREFUSED);
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | rc = inetcfg_addr_get(addr_id, &ainfo);
|
---|
193 | if (rc != EOK) {
|
---|
194 | async_answer_0(callid, rc);
|
---|
195 | return;
|
---|
196 | }
|
---|
197 |
|
---|
198 | sysarg_t retval = async_data_read_finalize(rcallid, ainfo.name,
|
---|
199 | min(max_size, str_size(ainfo.name)));
|
---|
200 | free(ainfo.name);
|
---|
201 |
|
---|
202 | async_answer_3(callid, retval, ainfo.naddr.ipv4, ainfo.naddr.bits,
|
---|
203 | ainfo.ilink);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | static void inetcfg_get_addr_list_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
208 | {
|
---|
209 | ipc_callid_t rcallid;
|
---|
210 | size_t count;
|
---|
211 | size_t max_size;
|
---|
212 | size_t act_size;
|
---|
213 | size_t size;
|
---|
214 | sysarg_t *id_buf;
|
---|
215 | int rc;
|
---|
216 |
|
---|
217 | log_msg(LVL_DEBUG, "inetcfg_get_addr_list_srv()");
|
---|
218 |
|
---|
219 | if (!async_data_read_receive(&rcallid, &max_size)) {
|
---|
220 | async_answer_0(rcallid, EREFUSED);
|
---|
221 | async_answer_0(callid, EREFUSED);
|
---|
222 | return;
|
---|
223 | }
|
---|
224 |
|
---|
225 | rc = inetcfg_get_addr_list(&id_buf, &count);
|
---|
226 | if (rc != EOK) {
|
---|
227 | async_answer_0(rcallid, rc);
|
---|
228 | async_answer_0(callid, rc);
|
---|
229 | return;
|
---|
230 | }
|
---|
231 |
|
---|
232 | act_size = count * sizeof(sysarg_t);
|
---|
233 | size = min(act_size, max_size);
|
---|
234 |
|
---|
235 | sysarg_t retval = async_data_read_finalize(rcallid, id_buf, size);
|
---|
236 | free(id_buf);
|
---|
237 |
|
---|
238 | async_answer_1(callid, retval, act_size);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static void inetcfg_link_get_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
242 | {
|
---|
243 | ipc_callid_t rcallid;
|
---|
244 | size_t max_size;
|
---|
245 |
|
---|
246 | sysarg_t link_id;
|
---|
247 | inet_link_info_t linfo;
|
---|
248 | int rc;
|
---|
249 |
|
---|
250 | link_id = IPC_GET_ARG1(*call);
|
---|
251 | log_msg(LVL_DEBUG, "inetcfg_link_get_srv()");
|
---|
252 |
|
---|
253 | linfo.name = NULL;
|
---|
254 |
|
---|
255 | if (!async_data_read_receive(&rcallid, &max_size)) {
|
---|
256 | async_answer_0(rcallid, EREFUSED);
|
---|
257 | async_answer_0(callid, EREFUSED);
|
---|
258 | return;
|
---|
259 | }
|
---|
260 |
|
---|
261 | rc = inetcfg_link_get(link_id, &linfo);
|
---|
262 | if (rc != EOK) {
|
---|
263 | async_answer_0(rcallid, rc);
|
---|
264 | async_answer_0(callid, rc);
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | sysarg_t retval = async_data_read_finalize(rcallid, linfo.name,
|
---|
269 | min(max_size, str_size(linfo.name)));
|
---|
270 | free(linfo.name);
|
---|
271 |
|
---|
272 | async_answer_0(callid, retval);
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void inetcfg_get_link_list_srv(ipc_callid_t callid, ipc_call_t *call)
|
---|
276 | {
|
---|
277 | ipc_callid_t rcallid;
|
---|
278 | size_t max_size;
|
---|
279 | size_t act_size;
|
---|
280 | size_t size;
|
---|
281 | sysarg_t *id_buf;
|
---|
282 | int rc;
|
---|
283 |
|
---|
284 | log_msg(LVL_DEBUG, "inetcfg_get_link_list_srv()");
|
---|
285 |
|
---|
286 | if (!async_data_read_receive(&rcallid, &max_size)) {
|
---|
287 | async_answer_0(rcallid, EREFUSED);
|
---|
288 | async_answer_0(callid, EREFUSED);
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | rc = inetcfg_get_link_list(&id_buf, &act_size);
|
---|
293 | if (rc != EOK) {
|
---|
294 | async_answer_0(rcallid, rc);
|
---|
295 | async_answer_0(callid, rc);
|
---|
296 | return;
|
---|
297 | }
|
---|
298 |
|
---|
299 | size = min(act_size, max_size);
|
---|
300 |
|
---|
301 | sysarg_t retval = async_data_read_finalize(rcallid, id_buf, size);
|
---|
302 | free(id_buf);
|
---|
303 |
|
---|
304 | async_answer_1(callid, retval, act_size);
|
---|
305 | }
|
---|
306 |
|
---|
307 | void inet_cfg_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
308 | {
|
---|
309 | log_msg(LVL_DEBUG, "inet_cfg_conn()");
|
---|
310 |
|
---|
311 | /* Accept the connection */
|
---|
312 | async_answer_0(iid, EOK);
|
---|
313 |
|
---|
314 | while (true) {
|
---|
315 | ipc_call_t call;
|
---|
316 | ipc_callid_t callid = async_get_call(&call);
|
---|
317 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
318 |
|
---|
319 | if (!method) {
|
---|
320 | /* The other side has hung up */
|
---|
321 | async_answer_0(callid, EOK);
|
---|
322 | return;
|
---|
323 | }
|
---|
324 |
|
---|
325 | switch (method) {
|
---|
326 | case INETCFG_ADDR_CREATE_STATIC:
|
---|
327 | inetcfg_addr_create_static_srv(callid, &call);
|
---|
328 | break;
|
---|
329 | case INETCFG_ADDR_DELETE:
|
---|
330 | inetcfg_addr_delete_srv(callid, &call);
|
---|
331 | break;
|
---|
332 | case INETCFG_ADDR_GET:
|
---|
333 | inetcfg_addr_get_srv(callid, &call);
|
---|
334 | break;
|
---|
335 | case INETCFG_GET_ADDR_LIST:
|
---|
336 | inetcfg_get_addr_list_srv(callid, &call);
|
---|
337 | break;
|
---|
338 | case INETCFG_GET_LINK_LIST:
|
---|
339 | inetcfg_get_link_list_srv(callid, &call);
|
---|
340 | break;
|
---|
341 | case INETCFG_LINK_GET:
|
---|
342 | inetcfg_link_get_srv(callid, &call);
|
---|
343 | break;
|
---|
344 | default:
|
---|
345 | async_answer_0(callid, EINVAL);
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** @}
|
---|
351 | */
|
---|