1 | /*
|
---|
2 | * Copyright (c) 2013 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 | /** @file Internet configuration utility.
|
---|
33 | *
|
---|
34 | * Controls the internet service (@c inet).
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <inet/addr.h>
|
---|
39 | #include <inet/inetcfg.h>
|
---|
40 | #include <loc.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <stdint.h>
|
---|
44 | #include <str_error.h>
|
---|
45 |
|
---|
46 | #define NAME "inet"
|
---|
47 |
|
---|
48 | static void print_syntax(void)
|
---|
49 | {
|
---|
50 | printf("syntax:\n");
|
---|
51 | printf("\t" NAME " create <addr>/<width> <link-name> <addr-name>\n");
|
---|
52 | printf("\t" NAME " delete <link-name> <addr-name>\n");
|
---|
53 | printf("\t" NAME " add-sr <dest-addr>/<width> <router-addr> <route-name>\n");
|
---|
54 | printf("\t" NAME " del-sr <route-name>\n");
|
---|
55 | }
|
---|
56 |
|
---|
57 | static int addr_create_static(int argc, char *argv[])
|
---|
58 | {
|
---|
59 | char *aobj_name;
|
---|
60 | char *addr_spec;
|
---|
61 | char *link_name;
|
---|
62 |
|
---|
63 | inet_naddr_t naddr;
|
---|
64 | sysarg_t link_id;
|
---|
65 | sysarg_t addr_id;
|
---|
66 | int rc;
|
---|
67 |
|
---|
68 | if (argc < 3) {
|
---|
69 | printf(NAME ": Missing arguments.\n");
|
---|
70 | print_syntax();
|
---|
71 | return EINVAL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (argc > 3) {
|
---|
75 | printf(NAME ": Too many arguments.\n");
|
---|
76 | print_syntax();
|
---|
77 | return EINVAL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | addr_spec = argv[0];
|
---|
81 | link_name = argv[1];
|
---|
82 | aobj_name = argv[2];
|
---|
83 |
|
---|
84 | rc = loc_service_get_id(link_name, &link_id, 0);
|
---|
85 | if (rc != EOK) {
|
---|
86 | printf(NAME ": Service '%s' not found (%d).\n", link_name, rc);
|
---|
87 | return ENOENT;
|
---|
88 | }
|
---|
89 |
|
---|
90 | rc = inet_naddr_parse(addr_spec, &naddr, NULL);
|
---|
91 | if (rc != EOK) {
|
---|
92 | printf(NAME ": Invalid network address format '%s'.\n",
|
---|
93 | addr_spec);
|
---|
94 | return EINVAL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | rc = inetcfg_addr_create_static(aobj_name, &naddr, link_id, &addr_id);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf(NAME ": Failed creating static address '%s' (%s)\n",
|
---|
100 | aobj_name, str_error(rc));
|
---|
101 | return EIO;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static int addr_delete(int argc, char *argv[])
|
---|
108 | {
|
---|
109 | char *aobj_name;
|
---|
110 | char *link_name;
|
---|
111 | sysarg_t link_id;
|
---|
112 | sysarg_t addr_id;
|
---|
113 | int rc;
|
---|
114 |
|
---|
115 | if (argc < 2) {
|
---|
116 | printf(NAME ": Missing arguments.\n");
|
---|
117 | print_syntax();
|
---|
118 | return EINVAL;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (argc > 2) {
|
---|
122 | printf(NAME ": Too many arguments.\n");
|
---|
123 | print_syntax();
|
---|
124 | return EINVAL;
|
---|
125 | }
|
---|
126 |
|
---|
127 | link_name = argv[0];
|
---|
128 | aobj_name = argv[1];
|
---|
129 |
|
---|
130 | rc = loc_service_get_id(link_name, &link_id, 0);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf(NAME ": Service '%s' not found (%d).\n", link_name, rc);
|
---|
133 | return ENOENT;
|
---|
134 | }
|
---|
135 |
|
---|
136 | rc = inetcfg_addr_get_id(aobj_name, link_id, &addr_id);
|
---|
137 | if (rc != EOK) {
|
---|
138 | printf(NAME ": Address '%s' not found (%d).\n", aobj_name, rc);
|
---|
139 | return ENOENT;
|
---|
140 | }
|
---|
141 |
|
---|
142 | rc = inetcfg_addr_delete(addr_id);
|
---|
143 | if (rc != EOK) {
|
---|
144 | printf(NAME ": Failed deleting address '%s' (%d)\n", aobj_name,
|
---|
145 | rc);
|
---|
146 | return EIO;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return EOK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static int sroute_create(int argc, char *argv[])
|
---|
153 | {
|
---|
154 | char *dest_str;
|
---|
155 | char *router_str;
|
---|
156 | char *route_name;
|
---|
157 |
|
---|
158 | inet_naddr_t dest;
|
---|
159 | inet_addr_t router;
|
---|
160 | sysarg_t sroute_id;
|
---|
161 | int rc;
|
---|
162 |
|
---|
163 | if (argc < 3) {
|
---|
164 | printf(NAME ": Missing arguments.\n");
|
---|
165 | print_syntax();
|
---|
166 | return EINVAL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (argc > 3) {
|
---|
170 | printf(NAME ": Too many arguments.\n");
|
---|
171 | print_syntax();
|
---|
172 | return EINVAL;
|
---|
173 | }
|
---|
174 |
|
---|
175 | dest_str = argv[0];
|
---|
176 | router_str = argv[1];
|
---|
177 | route_name = argv[2];
|
---|
178 |
|
---|
179 | rc = inet_naddr_parse(dest_str, &dest, NULL);
|
---|
180 | if (rc != EOK) {
|
---|
181 | printf(NAME ": Invalid network address format '%s'.\n",
|
---|
182 | dest_str);
|
---|
183 | return EINVAL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | rc = inet_addr_parse(router_str, &router, NULL);
|
---|
187 | if (rc != EOK) {
|
---|
188 | printf(NAME ": Invalid address format '%s'.\n", router_str);
|
---|
189 | return EINVAL;
|
---|
190 | }
|
---|
191 |
|
---|
192 | rc = inetcfg_sroute_create(route_name, &dest, &router, &sroute_id);
|
---|
193 | if (rc != EOK) {
|
---|
194 | printf(NAME ": Failed creating static route '%s' (%d)\n",
|
---|
195 | route_name, rc);
|
---|
196 | return EIO;
|
---|
197 | }
|
---|
198 |
|
---|
199 | return EOK;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int sroute_delete(int argc, char *argv[])
|
---|
203 | {
|
---|
204 | char *route_name;
|
---|
205 | sysarg_t sroute_id;
|
---|
206 | int rc;
|
---|
207 |
|
---|
208 | if (argc < 1) {
|
---|
209 | printf(NAME ": Missing arguments.\n");
|
---|
210 | print_syntax();
|
---|
211 | return EINVAL;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (argc > 1) {
|
---|
215 | printf(NAME ": Too many arguments.\n");
|
---|
216 | print_syntax();
|
---|
217 | return EINVAL;
|
---|
218 | }
|
---|
219 |
|
---|
220 | route_name = argv[0];
|
---|
221 |
|
---|
222 | rc = inetcfg_sroute_get_id(route_name, &sroute_id);
|
---|
223 | if (rc != EOK) {
|
---|
224 | printf(NAME ": Static route '%s' not found (%d).\n",
|
---|
225 | route_name, rc);
|
---|
226 | return ENOENT;
|
---|
227 | }
|
---|
228 |
|
---|
229 | rc = inetcfg_sroute_delete(sroute_id);
|
---|
230 | if (rc != EOK) {
|
---|
231 | printf(NAME ": Failed deleting static route '%s' (%d)\n",
|
---|
232 | route_name, rc);
|
---|
233 | return EIO;
|
---|
234 | }
|
---|
235 |
|
---|
236 | return EOK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | static int addr_list(void)
|
---|
240 | {
|
---|
241 | sysarg_t *addr_list;
|
---|
242 | inet_addr_info_t ainfo;
|
---|
243 | inet_link_info_t linfo;
|
---|
244 |
|
---|
245 | size_t count;
|
---|
246 | size_t i;
|
---|
247 | int rc;
|
---|
248 | char *astr;
|
---|
249 |
|
---|
250 | rc = inetcfg_get_addr_list(&addr_list, &count);
|
---|
251 | if (rc != EOK) {
|
---|
252 | printf(NAME ": Failed getting address list.\n");
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 | printf("Configured addresses:\n");
|
---|
257 | if (count > 0)
|
---|
258 | printf(" [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n");
|
---|
259 | ainfo.name = NULL;
|
---|
260 | linfo.name = NULL;
|
---|
261 | astr = NULL;
|
---|
262 |
|
---|
263 | for (i = 0; i < count; i++) {
|
---|
264 | rc = inetcfg_addr_get(addr_list[i], &ainfo);
|
---|
265 | if (rc != EOK) {
|
---|
266 | printf("Failed getting properties of address %zu.\n",
|
---|
267 | (size_t)addr_list[i]);
|
---|
268 | ainfo.name = NULL;
|
---|
269 | continue;
|
---|
270 | }
|
---|
271 |
|
---|
272 | rc = inetcfg_link_get(ainfo.ilink, &linfo);
|
---|
273 | if (rc != EOK) {
|
---|
274 | printf("Failed getting properties of link %zu.\n",
|
---|
275 | (size_t)ainfo.ilink);
|
---|
276 | linfo.name = NULL;
|
---|
277 | continue;
|
---|
278 | }
|
---|
279 |
|
---|
280 | rc = inet_naddr_format(&ainfo.naddr, &astr);
|
---|
281 | if (rc != EOK) {
|
---|
282 | printf("Memory allocation failed.\n");
|
---|
283 | astr = NULL;
|
---|
284 | goto out;
|
---|
285 | }
|
---|
286 |
|
---|
287 | printf(" %s %s %s %zu\n", astr, linfo.name,
|
---|
288 | ainfo.name, linfo.def_mtu);
|
---|
289 |
|
---|
290 | free(ainfo.name);
|
---|
291 | free(linfo.name);
|
---|
292 | free(astr);
|
---|
293 |
|
---|
294 | ainfo.name = NULL;
|
---|
295 | linfo.name = NULL;
|
---|
296 | astr = NULL;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (count == 0)
|
---|
300 | printf(" None\n");
|
---|
301 | out:
|
---|
302 | if (ainfo.name != NULL)
|
---|
303 | free(ainfo.name);
|
---|
304 | if (linfo.name != NULL)
|
---|
305 | free(linfo.name);
|
---|
306 | if (astr != NULL)
|
---|
307 | free(astr);
|
---|
308 |
|
---|
309 | free(addr_list);
|
---|
310 |
|
---|
311 | return EOK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | static int link_list(void)
|
---|
315 | {
|
---|
316 | sysarg_t *link_list;
|
---|
317 | inet_link_info_t linfo;
|
---|
318 |
|
---|
319 | size_t count;
|
---|
320 | size_t i;
|
---|
321 | int rc;
|
---|
322 |
|
---|
323 | rc = inetcfg_get_link_list(&link_list, &count);
|
---|
324 | if (rc != EOK) {
|
---|
325 | printf(NAME ": Failed getting link list.\n");
|
---|
326 | return rc;
|
---|
327 | }
|
---|
328 |
|
---|
329 | printf("IP links:\n");
|
---|
330 | if (count > 0)
|
---|
331 | printf(" [Link-layer Address] [Link-Name] [Def-MTU]\n");
|
---|
332 |
|
---|
333 | for (i = 0; i < count; i++) {
|
---|
334 | rc = inetcfg_link_get(link_list[i], &linfo);
|
---|
335 | if (rc != EOK) {
|
---|
336 | printf("Failed getting properties of link %zu.\n",
|
---|
337 | (size_t)link_list[i]);
|
---|
338 | continue;
|
---|
339 | }
|
---|
340 |
|
---|
341 | printf(" %02x:%02x:%02x:%02x:%02x:%02x %s %zu\n",
|
---|
342 | linfo.mac_addr[0], linfo.mac_addr[1],
|
---|
343 | linfo.mac_addr[2], linfo.mac_addr[3],
|
---|
344 | linfo.mac_addr[4], linfo.mac_addr[5],
|
---|
345 | linfo.name, linfo.def_mtu);
|
---|
346 |
|
---|
347 | free(linfo.name);
|
---|
348 |
|
---|
349 | linfo.name = NULL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (count == 0)
|
---|
353 | printf(" None\n");
|
---|
354 |
|
---|
355 | free(link_list);
|
---|
356 |
|
---|
357 | return EOK;
|
---|
358 | }
|
---|
359 |
|
---|
360 | static int sroute_list(void)
|
---|
361 | {
|
---|
362 | sysarg_t *sroute_list;
|
---|
363 | inet_sroute_info_t srinfo;
|
---|
364 |
|
---|
365 | size_t count;
|
---|
366 | size_t i;
|
---|
367 | int rc;
|
---|
368 | char *dest_str;
|
---|
369 | char *router_str;
|
---|
370 |
|
---|
371 | rc = inetcfg_get_sroute_list(&sroute_list, &count);
|
---|
372 | if (rc != EOK) {
|
---|
373 | printf(NAME ": Failed getting address list.\n");
|
---|
374 | return rc;
|
---|
375 | }
|
---|
376 |
|
---|
377 | printf("Static routes:\n");
|
---|
378 | if (count > 0)
|
---|
379 | printf(" [Dest/Width] [Router-Addr] [Route-Name]\n");
|
---|
380 |
|
---|
381 | srinfo.name = NULL;
|
---|
382 | dest_str = NULL;
|
---|
383 | router_str = NULL;
|
---|
384 |
|
---|
385 | for (i = 0; i < count; i++) {
|
---|
386 | rc = inetcfg_sroute_get(sroute_list[i], &srinfo);
|
---|
387 | if (rc != EOK) {
|
---|
388 | printf("Failed getting properties of static route %zu.\n",
|
---|
389 | (size_t)sroute_list[i]);
|
---|
390 | srinfo.name = NULL;
|
---|
391 | continue;
|
---|
392 | }
|
---|
393 |
|
---|
394 | rc = inet_naddr_format(&srinfo.dest, &dest_str);
|
---|
395 | if (rc != EOK) {
|
---|
396 | printf("Memory allocation failed.\n");
|
---|
397 | dest_str = NULL;
|
---|
398 | goto out;
|
---|
399 | }
|
---|
400 |
|
---|
401 | rc = inet_addr_format(&srinfo.router, &router_str);
|
---|
402 | if (rc != EOK) {
|
---|
403 | printf("Memory allocation failed.\n");
|
---|
404 | router_str = NULL;
|
---|
405 | goto out;
|
---|
406 | }
|
---|
407 |
|
---|
408 | printf(" %s %s %s\n", dest_str, router_str, srinfo.name);
|
---|
409 |
|
---|
410 | free(srinfo.name);
|
---|
411 | free(dest_str);
|
---|
412 | free(router_str);
|
---|
413 |
|
---|
414 | router_str = NULL;
|
---|
415 | srinfo.name = NULL;
|
---|
416 | dest_str = NULL;
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (count == 0)
|
---|
420 | printf(" None\n");
|
---|
421 | out:
|
---|
422 | if (srinfo.name != NULL)
|
---|
423 | free(srinfo.name);
|
---|
424 | if (dest_str != NULL)
|
---|
425 | free(dest_str);
|
---|
426 | if (router_str != NULL)
|
---|
427 | free(router_str);
|
---|
428 |
|
---|
429 | free(sroute_list);
|
---|
430 |
|
---|
431 | return EOK;
|
---|
432 | }
|
---|
433 |
|
---|
434 | int main(int argc, char *argv[])
|
---|
435 | {
|
---|
436 | int rc;
|
---|
437 |
|
---|
438 | rc = inetcfg_init();
|
---|
439 | if (rc != EOK) {
|
---|
440 | printf(NAME ": Failed connecting to internet service (%d).\n",
|
---|
441 | rc);
|
---|
442 | return 1;
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (argc < 2) {
|
---|
446 | rc = addr_list();
|
---|
447 | if (rc != EOK)
|
---|
448 | return 1;
|
---|
449 | rc = sroute_list();
|
---|
450 | if (rc != EOK)
|
---|
451 | return 1;
|
---|
452 | rc = link_list();
|
---|
453 | if (rc != EOK)
|
---|
454 | return 1;
|
---|
455 | return 0;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (str_cmp(argv[1], "create") == 0) {
|
---|
459 | rc = addr_create_static(argc - 2, argv + 2);
|
---|
460 | if (rc != EOK)
|
---|
461 | return 1;
|
---|
462 | } else if (str_cmp(argv[1], "delete") == 0) {
|
---|
463 | rc = addr_delete(argc - 2, argv + 2);
|
---|
464 | if (rc != EOK)
|
---|
465 | return 1;
|
---|
466 | } else if (str_cmp(argv[1], "add-sr") == 0) {
|
---|
467 | rc = sroute_create(argc - 2, argv + 2);
|
---|
468 | if (rc != EOK)
|
---|
469 | return 1;
|
---|
470 | } else if (str_cmp(argv[1], "del-sr") == 0) {
|
---|
471 | rc = sroute_delete(argc - 2, argv + 2);
|
---|
472 | if (rc != EOK)
|
---|
473 | return 1;
|
---|
474 | } else {
|
---|
475 | printf(NAME ": Unknown command '%s'.\n", argv[1]);
|
---|
476 | print_syntax();
|
---|
477 | return 1;
|
---|
478 | }
|
---|
479 |
|
---|
480 | return 0;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /** @}
|
---|
484 | */
|
---|