source: mainline/uspace/app/inet/inet.c@ ec7902d

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ec7902d was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

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