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