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 | #include <async.h>
|
---|
30 | #include <assert.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <inet/inetcfg.h>
|
---|
33 | #include <ipc/inet.h>
|
---|
34 | #include <ipc/services.h>
|
---|
35 | #include <loc.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <str.h>
|
---|
38 |
|
---|
39 | static async_sess_t *inetcfg_sess = NULL;
|
---|
40 |
|
---|
41 | static int inetcfg_get_ids_once(sysarg_t method, sysarg_t arg1,
|
---|
42 | sysarg_t *id_buf, size_t buf_size, size_t *act_size)
|
---|
43 | {
|
---|
44 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
45 |
|
---|
46 | ipc_call_t answer;
|
---|
47 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
48 | int rc = async_data_read_start(exch, id_buf, buf_size);
|
---|
49 |
|
---|
50 | async_exchange_end(exch);
|
---|
51 |
|
---|
52 | if (rc != EOK) {
|
---|
53 | async_forget(req);
|
---|
54 | return rc;
|
---|
55 | }
|
---|
56 |
|
---|
57 | sysarg_t retval;
|
---|
58 | async_wait_for(req, &retval);
|
---|
59 |
|
---|
60 | if (retval != EOK) {
|
---|
61 | return retval;
|
---|
62 | }
|
---|
63 |
|
---|
64 | *act_size = IPC_GET_ARG1(answer);
|
---|
65 | return EOK;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Get list of IDs.
|
---|
69 | *
|
---|
70 | * Returns an allocated array of service IDs.
|
---|
71 | *
|
---|
72 | * @param method IPC method
|
---|
73 | * @param arg1 IPC argument 1
|
---|
74 | * @param data Place to store pointer to array of IDs
|
---|
75 | * @param count Place to store number of IDs
|
---|
76 | * @return EOK on success or negative error code
|
---|
77 | */
|
---|
78 | static int inetcfg_get_ids_internal(sysarg_t method, sysarg_t arg1,
|
---|
79 | sysarg_t **data, size_t *count)
|
---|
80 | {
|
---|
81 | *data = NULL;
|
---|
82 | *count = 0;
|
---|
83 |
|
---|
84 | size_t act_size = 0;
|
---|
85 | int rc = inetcfg_get_ids_once(method, arg1, NULL, 0,
|
---|
86 | &act_size);
|
---|
87 | if (rc != EOK)
|
---|
88 | return rc;
|
---|
89 |
|
---|
90 | size_t alloc_size = act_size;
|
---|
91 | service_id_t *ids = malloc(alloc_size);
|
---|
92 | if (ids == NULL)
|
---|
93 | return ENOMEM;
|
---|
94 |
|
---|
95 | while (true) {
|
---|
96 | rc = inetcfg_get_ids_once(method, arg1, ids, alloc_size,
|
---|
97 | &act_size);
|
---|
98 | if (rc != EOK)
|
---|
99 | return rc;
|
---|
100 |
|
---|
101 | if (act_size <= alloc_size)
|
---|
102 | break;
|
---|
103 |
|
---|
104 | alloc_size = act_size;
|
---|
105 | ids = realloc(ids, alloc_size);
|
---|
106 | if (ids == NULL)
|
---|
107 | return ENOMEM;
|
---|
108 | }
|
---|
109 |
|
---|
110 | *count = act_size / sizeof(sysarg_t);
|
---|
111 | *data = ids;
|
---|
112 | return EOK;
|
---|
113 | }
|
---|
114 |
|
---|
115 | int inetcfg_init(void)
|
---|
116 | {
|
---|
117 | service_id_t inet_svc;
|
---|
118 | int rc;
|
---|
119 |
|
---|
120 | assert(inetcfg_sess == NULL);
|
---|
121 |
|
---|
122 | rc = loc_service_get_id(SERVICE_NAME_INETCFG, &inet_svc,
|
---|
123 | IPC_FLAG_BLOCKING);
|
---|
124 | if (rc != EOK)
|
---|
125 | return ENOENT;
|
---|
126 |
|
---|
127 | inetcfg_sess = loc_service_connect(EXCHANGE_SERIALIZE, inet_svc,
|
---|
128 | IPC_FLAG_BLOCKING);
|
---|
129 | if (inetcfg_sess == NULL)
|
---|
130 | return ENOENT;
|
---|
131 |
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int inetcfg_addr_create_static(const char *name, inet_naddr_t *naddr,
|
---|
136 | sysarg_t link_id, sysarg_t *addr_id)
|
---|
137 | {
|
---|
138 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
139 |
|
---|
140 | ipc_call_t answer;
|
---|
141 | aid_t req = async_send_3(exch, INETCFG_ADDR_CREATE_STATIC, naddr->ipv4,
|
---|
142 | naddr->bits, link_id, &answer);
|
---|
143 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
144 |
|
---|
145 | async_exchange_end(exch);
|
---|
146 |
|
---|
147 | if (retval != EOK) {
|
---|
148 | async_forget(req);
|
---|
149 | return retval;
|
---|
150 | }
|
---|
151 |
|
---|
152 | async_wait_for(req, &retval);
|
---|
153 | *addr_id = IPC_GET_ARG1(answer);
|
---|
154 |
|
---|
155 | return retval;
|
---|
156 | }
|
---|
157 |
|
---|
158 | int inetcfg_addr_delete(sysarg_t addr_id)
|
---|
159 | {
|
---|
160 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
161 |
|
---|
162 | int rc = async_req_1_0(exch, INETCFG_ADDR_DELETE, addr_id);
|
---|
163 | async_exchange_end(exch);
|
---|
164 |
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo)
|
---|
169 | {
|
---|
170 | ipc_call_t dreply;
|
---|
171 | sysarg_t dretval;
|
---|
172 | size_t act_size;
|
---|
173 | char name_buf[LOC_NAME_MAXLEN + 1];
|
---|
174 |
|
---|
175 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
176 |
|
---|
177 | ipc_call_t answer;
|
---|
178 | aid_t req = async_send_1(exch, INETCFG_ADDR_GET, addr_id, &answer);
|
---|
179 | aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);
|
---|
180 | async_wait_for(dreq, &dretval);
|
---|
181 |
|
---|
182 | async_exchange_end(exch);
|
---|
183 |
|
---|
184 | if (dretval != EOK) {
|
---|
185 | async_forget(req);
|
---|
186 | return dretval;
|
---|
187 | }
|
---|
188 |
|
---|
189 | sysarg_t retval;
|
---|
190 | async_wait_for(req, &retval);
|
---|
191 |
|
---|
192 | if (retval != EOK)
|
---|
193 | return retval;
|
---|
194 |
|
---|
195 | act_size = IPC_GET_ARG2(dreply);
|
---|
196 | assert(act_size <= LOC_NAME_MAXLEN);
|
---|
197 | name_buf[act_size] = '\0';
|
---|
198 |
|
---|
199 | ainfo->naddr.ipv4 = IPC_GET_ARG1(answer);
|
---|
200 | ainfo->naddr.bits = IPC_GET_ARG2(answer);
|
---|
201 | ainfo->ilink = IPC_GET_ARG3(answer);
|
---|
202 | ainfo->name = str_dup(name_buf);
|
---|
203 |
|
---|
204 | return EOK;
|
---|
205 | }
|
---|
206 |
|
---|
207 | int inetcfg_addr_get_id(const char *name, sysarg_t link_id, sysarg_t *addr_id)
|
---|
208 | {
|
---|
209 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
210 |
|
---|
211 | ipc_call_t answer;
|
---|
212 | aid_t req = async_send_1(exch, INETCFG_ADDR_GET_ID, link_id, &answer);
|
---|
213 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
214 |
|
---|
215 | async_exchange_end(exch);
|
---|
216 |
|
---|
217 | if (retval != EOK) {
|
---|
218 | async_forget(req);
|
---|
219 | return retval;
|
---|
220 | }
|
---|
221 |
|
---|
222 | async_wait_for(req, &retval);
|
---|
223 | *addr_id = IPC_GET_ARG1(answer);
|
---|
224 |
|
---|
225 | return retval;
|
---|
226 | }
|
---|
227 |
|
---|
228 | int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)
|
---|
229 | {
|
---|
230 | return inetcfg_get_ids_internal(INETCFG_GET_ADDR_LIST,
|
---|
231 | 0, addrs, count);
|
---|
232 | }
|
---|
233 |
|
---|
234 | int inetcfg_get_link_list(sysarg_t **links, size_t *count)
|
---|
235 | {
|
---|
236 | return inetcfg_get_ids_internal(INETCFG_GET_LINK_LIST,
|
---|
237 | 0, links, count);
|
---|
238 | }
|
---|
239 |
|
---|
240 | int inetcfg_get_sroute_list(sysarg_t **sroutes, size_t *count)
|
---|
241 | {
|
---|
242 | return inetcfg_get_ids_internal(INETCFG_GET_SROUTE_LIST,
|
---|
243 | 0, sroutes, count);
|
---|
244 | }
|
---|
245 |
|
---|
246 | int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)
|
---|
247 | {
|
---|
248 | ipc_call_t dreply;
|
---|
249 | sysarg_t dretval;
|
---|
250 | size_t act_size;
|
---|
251 | char name_buf[LOC_NAME_MAXLEN + 1];
|
---|
252 |
|
---|
253 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
254 |
|
---|
255 | ipc_call_t answer;
|
---|
256 | aid_t req = async_send_1(exch, INETCFG_LINK_GET, link_id, &answer);
|
---|
257 | aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);
|
---|
258 | async_wait_for(dreq, &dretval);
|
---|
259 |
|
---|
260 | async_exchange_end(exch);
|
---|
261 |
|
---|
262 | if (dretval != EOK) {
|
---|
263 | async_forget(req);
|
---|
264 | return dretval;
|
---|
265 | }
|
---|
266 |
|
---|
267 | sysarg_t retval;
|
---|
268 | async_wait_for(req, &retval);
|
---|
269 |
|
---|
270 | if (retval != EOK)
|
---|
271 | return retval;
|
---|
272 |
|
---|
273 | act_size = IPC_GET_ARG2(dreply);
|
---|
274 | assert(act_size <= LOC_NAME_MAXLEN);
|
---|
275 | name_buf[act_size] = '\0';
|
---|
276 |
|
---|
277 | linfo->name = str_dup(name_buf);
|
---|
278 | linfo->def_mtu = IPC_GET_ARG1(answer);
|
---|
279 |
|
---|
280 | return EOK;
|
---|
281 | }
|
---|
282 |
|
---|
283 | int inetcfg_sroute_create(const char *name, inet_naddr_t *dest,
|
---|
284 | inet_addr_t *router, sysarg_t *sroute_id)
|
---|
285 | {
|
---|
286 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
287 |
|
---|
288 | ipc_call_t answer;
|
---|
289 | aid_t req = async_send_3(exch, INETCFG_SROUTE_CREATE,
|
---|
290 | dest->ipv4, dest->bits, router->ipv4, &answer);
|
---|
291 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
292 |
|
---|
293 | async_exchange_end(exch);
|
---|
294 |
|
---|
295 | if (retval != EOK) {
|
---|
296 | async_forget(req);
|
---|
297 | return retval;
|
---|
298 | }
|
---|
299 |
|
---|
300 | async_wait_for(req, &retval);
|
---|
301 | *sroute_id = IPC_GET_ARG1(answer);
|
---|
302 |
|
---|
303 | return retval;
|
---|
304 | }
|
---|
305 |
|
---|
306 | int inetcfg_sroute_delete(sysarg_t sroute_id)
|
---|
307 | {
|
---|
308 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
309 |
|
---|
310 | int rc = async_req_1_0(exch, INETCFG_SROUTE_DELETE, sroute_id);
|
---|
311 | async_exchange_end(exch);
|
---|
312 |
|
---|
313 | return rc;
|
---|
314 | }
|
---|
315 |
|
---|
316 | int inetcfg_sroute_get(sysarg_t sroute_id, inet_sroute_info_t *srinfo)
|
---|
317 | {
|
---|
318 | ipc_call_t dreply;
|
---|
319 | sysarg_t dretval;
|
---|
320 | size_t act_size;
|
---|
321 | char name_buf[LOC_NAME_MAXLEN + 1];
|
---|
322 |
|
---|
323 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
324 |
|
---|
325 | ipc_call_t answer;
|
---|
326 | aid_t req = async_send_1(exch, INETCFG_SROUTE_GET, sroute_id, &answer);
|
---|
327 | aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);
|
---|
328 | async_wait_for(dreq, &dretval);
|
---|
329 |
|
---|
330 | async_exchange_end(exch);
|
---|
331 |
|
---|
332 | if (dretval != EOK) {
|
---|
333 | async_forget(req);
|
---|
334 | return dretval;
|
---|
335 | }
|
---|
336 |
|
---|
337 | sysarg_t retval;
|
---|
338 | async_wait_for(req, &retval);
|
---|
339 |
|
---|
340 | if (retval != EOK)
|
---|
341 | return retval;
|
---|
342 |
|
---|
343 | act_size = IPC_GET_ARG2(dreply);
|
---|
344 | assert(act_size <= LOC_NAME_MAXLEN);
|
---|
345 | name_buf[act_size] = '\0';
|
---|
346 |
|
---|
347 | srinfo->dest.ipv4 = IPC_GET_ARG1(answer);
|
---|
348 | srinfo->dest.bits = IPC_GET_ARG2(answer);
|
---|
349 | srinfo->router.ipv4 = IPC_GET_ARG3(answer);
|
---|
350 | srinfo->name = str_dup(name_buf);
|
---|
351 |
|
---|
352 | return EOK;
|
---|
353 | }
|
---|
354 |
|
---|
355 | int inetcfg_sroute_get_id(const char *name, sysarg_t *sroute_id)
|
---|
356 | {
|
---|
357 | async_exch_t *exch = async_exchange_begin(inetcfg_sess);
|
---|
358 |
|
---|
359 | ipc_call_t answer;
|
---|
360 | aid_t req = async_send_0(exch, INETCFG_SROUTE_GET_ID, &answer);
|
---|
361 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
362 |
|
---|
363 | async_exchange_end(exch);
|
---|
364 |
|
---|
365 | if (retval != EOK) {
|
---|
366 | async_forget(req);
|
---|
367 | return retval;
|
---|
368 | }
|
---|
369 |
|
---|
370 | async_wait_for(req, &retval);
|
---|
371 | *sroute_id = IPC_GET_ARG1(answer);
|
---|
372 |
|
---|
373 | return retval;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /** @}
|
---|
377 | */
|
---|