source: mainline/uspace/app/inet/inet.c@ 207d5da

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207d5da was 207d5da, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix extraneous empty line printed after table.

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