2 * Copyright (c) 2013 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 /** @file Internet configuration utility.
34 * Controls the internet service (@c inet).
38 #include <inet/addr.h>
39 #include <inet/inetcfg.h>
46 #include <str_error.h>
50 static void print_syntax(void)
52 printf("%s: Internet configuration utility.\n", NAME
);
54 printf(" %s list-addr\n", NAME
);
55 printf(" %s create-addr <addr>/<width> <link-name> <addr-name>\n", NAME
);
56 printf(" %s delete-addr <link-name> <addr-name>\n", NAME
);
57 printf(" %s list-sr\n", NAME
);
58 printf(" %s create-sr <dest-addr>/<width> <router-addr> <route-name>\n", NAME
);
59 printf(" %s delete-sr <route-name>\n", NAME
);
60 printf(" %s list-link\n", NAME
);
63 static errno_t
addr_create_static(int argc
, char *argv
[])
75 printf(NAME
": Missing arguments.\n");
81 printf(NAME
": Too many arguments.\n");
90 rc
= loc_service_get_id(link_name
, &link_id
, 0);
92 printf(NAME
": Service '%s' not found: %s.\n", link_name
, str_error(rc
));
96 rc
= inet_naddr_parse(addr_spec
, &naddr
, NULL
);
98 printf(NAME
": Invalid network address format '%s'.\n",
103 rc
= inetcfg_addr_create_static(aobj_name
, &naddr
, link_id
, &addr_id
);
105 printf(NAME
": Failed creating static address '%s' (%s)\n",
106 aobj_name
, str_error(rc
));
113 static errno_t
addr_delete(int argc
, char *argv
[])
122 printf(NAME
": Missing arguments.\n");
128 printf(NAME
": Too many arguments.\n");
136 rc
= loc_service_get_id(link_name
, &link_id
, 0);
138 printf(NAME
": Service '%s' not found: %s.\n", link_name
,
143 rc
= inetcfg_addr_get_id(aobj_name
, link_id
, &addr_id
);
145 printf(NAME
": Address '%s' not found: %s.\n", aobj_name
,
150 rc
= inetcfg_addr_delete(addr_id
);
152 printf(NAME
": Failed deleting address '%s': %s\n", aobj_name
,
160 static errno_t
sroute_create(int argc
, char *argv
[])
172 printf(NAME
": Missing arguments.\n");
178 printf(NAME
": Too many arguments.\n");
184 router_str
= argv
[1];
185 route_name
= argv
[2];
187 rc
= inet_naddr_parse(dest_str
, &dest
, NULL
);
189 printf(NAME
": Invalid network address format '%s'.\n",
194 rc
= inet_addr_parse(router_str
, &router
, NULL
);
196 printf(NAME
": Invalid address format '%s'.\n", router_str
);
200 rc
= inetcfg_sroute_create(route_name
, &dest
, &router
, &sroute_id
);
202 printf(NAME
": Failed creating static route '%s': %s\n",
203 route_name
, str_error(rc
));
210 static errno_t
sroute_delete(int argc
, char *argv
[])
217 printf(NAME
": Missing arguments.\n");
223 printf(NAME
": Too many arguments.\n");
228 route_name
= argv
[0];
230 rc
= inetcfg_sroute_get_id(route_name
, &sroute_id
);
232 printf(NAME
": Static route '%s' not found: %s.\n",
233 route_name
, str_error(rc
));
237 rc
= inetcfg_sroute_delete(sroute_id
);
239 printf(NAME
": Failed deleting static route '%s': %s\n",
240 route_name
, str_error(rc
));
247 static errno_t
addr_list(void)
250 inet_addr_info_t ainfo
;
251 inet_link_info_t linfo
;
252 table_t
*table
= NULL
;
262 rc
= inetcfg_get_addr_list(&addr_list
, &count
);
264 printf(NAME
": Failed getting address list.\n");
268 rc
= table_create(&table
);
270 printf("Memory allocation failed.\n");
274 table_header_row(table
);
275 table_printf(table
, "Addr/Width\t" "Link-Name\t" "Addr-Name\t"
278 for (i
= 0; i
< count
; i
++) {
279 rc
= inetcfg_addr_get(addr_list
[i
], &ainfo
);
281 printf("Failed getting properties of address %zu.\n",
282 (size_t)addr_list
[i
]);
287 rc
= inetcfg_link_get(ainfo
.ilink
, &linfo
);
289 printf("Failed getting properties of link %zu.\n",
290 (size_t)ainfo
.ilink
);
295 rc
= inet_naddr_format(&ainfo
.naddr
, &astr
);
297 printf("Memory allocation failed.\n");
302 table_printf(table
, "%s\t" "%s\t" "%s\t" "%zu\n", astr
,
303 linfo
.name
, ainfo
.name
, linfo
.def_mtu
);
315 rc
= table_print_out(table
, stdout
);
317 printf("Error printing table.\n");
324 table_destroy(table
);
325 if (ainfo
.name
!= NULL
)
327 if (linfo
.name
!= NULL
)
337 static errno_t
link_list(void)
339 sysarg_t
*link_list
= NULL
;
340 inet_link_info_t linfo
;
341 table_t
*table
= NULL
;
347 rc
= inetcfg_get_link_list(&link_list
, &count
);
349 printf(NAME
": Failed getting link list.\n");
353 rc
= table_create(&table
);
355 printf("Memory allocation failed.\n");
359 table_header_row(table
);
360 table_printf(table
, "Link-layer Address\t" "Link-Name\t" "Def-MTU\n");
362 for (i
= 0; i
< count
; i
++) {
363 rc
= inetcfg_link_get(link_list
[i
], &linfo
);
365 printf("Failed getting properties of link %zu.\n",
366 (size_t)link_list
[i
]);
370 table_printf(table
, "%02x:%02x:%02x:%02x:%02x:%02x\t"
372 linfo
.mac_addr
[0], linfo
.mac_addr
[1],
373 linfo
.mac_addr
[2], linfo
.mac_addr
[3],
374 linfo
.mac_addr
[4], linfo
.mac_addr
[5],
375 linfo
.name
, linfo
.def_mtu
);
383 rc
= table_print_out(table
, stdout
);
385 printf("Error printing table.\n");
392 table_destroy(table
);
398 static errno_t
sroute_list(void)
400 sysarg_t
*sroute_list
= NULL
;
401 inet_sroute_info_t srinfo
;
402 table_t
*table
= NULL
;
407 char *dest_str
= NULL
;
408 char *router_str
= NULL
;
412 rc
= inetcfg_get_sroute_list(&sroute_list
, &count
);
414 printf(NAME
": Failed getting address list.\n");
418 rc
= table_create(&table
);
420 printf("Memory allocation failed.\n");
424 table_header_row(table
);
425 table_printf(table
, "Dest/Width\t" "Router-Addr\t" "Route-Name\n");
427 for (i
= 0; i
< count
; i
++) {
428 rc
= inetcfg_sroute_get(sroute_list
[i
], &srinfo
);
430 printf("Failed getting properties of static route %zu.\n",
431 (size_t)sroute_list
[i
]);
436 rc
= inet_naddr_format(&srinfo
.dest
, &dest_str
);
438 printf("Memory allocation failed.\n");
443 rc
= inet_addr_format(&srinfo
.router
, &router_str
);
445 printf("Memory allocation failed.\n");
450 table_printf(table
, "%s\t" "%s\t" "%s\n", dest_str
, router_str
,
463 rc
= table_print_out(table
, stdout
);
465 printf("Error printing table.\n");
472 table_destroy(table
);
473 if (srinfo
.name
!= NULL
)
475 if (dest_str
!= NULL
)
477 if (router_str
!= NULL
)
485 int main(int argc
, char *argv
[])
491 printf(NAME
": Failed connecting to internet service: %s.\n",
496 if (argc
< 2 || str_cmp(argv
[1], "-h") == 0) {
501 if (str_cmp(argv
[1], "list-addr") == 0) {
505 } else if (str_cmp(argv
[1], "create-addr") == 0) {
506 rc
= addr_create_static(argc
- 2, argv
+ 2);
509 } else if (str_cmp(argv
[1], "delete-addr") == 0) {
510 rc
= addr_delete(argc
- 2, argv
+ 2);
513 } else if (str_cmp(argv
[1], "list-sr") == 0) {
517 } else if (str_cmp(argv
[1], "create-sr") == 0) {
518 rc
= sroute_create(argc
- 2, argv
+ 2);
521 } else if (str_cmp(argv
[1], "delete-sr") == 0) {
522 rc
= sroute_delete(argc
- 2, argv
+ 2);
525 } else if (str_cmp(argv
[1], "list-link") == 0) {
530 printf(NAME
": Unknown command '%s'.\n", argv
[1]);