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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a46e56b 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
RevLine 
[0e25780]1/*
[3495654]2 * Copyright (c) 2013 Jiri Svoboda
[0e25780]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
[b4ec1ea]29/** @addtogroup inet
[0e25780]30 * @{
31 */
32/** @file Internet configuration utility.
33 *
34 * Controls the internet service (@c inet).
35 */
36
37#include <errno.h>
[3495654]38#include <inet/addr.h>
[0e25780]39#include <inet/inetcfg.h>
[5e962ad]40#include <io/table.h>
[45aa22c]41#include <loc.h>
[0e25780]42#include <stdio.h>
[0e94b979]43#include <stdlib.h>
[8d2dd7f2]44#include <stdint.h>
[1d6dd2a]45#include <str.h>
[0e25780]46#include <str_error.h>
47
[257feec]48#define NAME "inet"
[0e25780]49
50static void print_syntax(void)
51{
[75c3830]52 printf("%s: Internet configuration utility.\n", NAME);
[5e962ad]53 printf("Syntax:\n");
[75c3830]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);
[0e25780]61}
62
[b7fd2a0]63static errno_t addr_create_static(int argc, char *argv[])
[45aa22c]64{
65 char *aobj_name;
66 char *addr_spec;
67 char *link_name;
68
[0e25780]69 inet_naddr_t naddr;
[45aa22c]70 sysarg_t link_id;
[0e25780]71 sysarg_t addr_id;
[b7fd2a0]72 errno_t rc;
[0e25780]73
[45aa22c]74 if (argc < 3) {
75 printf(NAME ": Missing arguments.\n");
[0e25780]76 print_syntax();
[45aa22c]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) {
[c1694b6b]92 printf(NAME ": Service '%s' not found: %s.\n", link_name, str_error(rc));
[45aa22c]93 return ENOENT;
94 }
95
[a62ceaf]96 rc = inet_naddr_parse(addr_spec, &naddr, NULL);
[45aa22c]97 if (rc != EOK) {
[8bf672d]98 printf(NAME ": Invalid network address format '%s'.\n",
99 addr_spec);
[45aa22c]100 return EINVAL;
101 }
102
103 rc = inetcfg_addr_create_static(aobj_name, &naddr, link_id, &addr_id);
104 if (rc != EOK) {
[bf9e6fc]105 printf(NAME ": Failed creating static address '%s' (%s)\n",
106 aobj_name, str_error(rc));
[fa101c4]107 return EIO;
108 }
109
110 return EOK;
111}
112
[b7fd2a0]113static errno_t addr_delete(int argc, char *argv[])
[fa101c4]114{
115 char *aobj_name;
116 char *link_name;
117 sysarg_t link_id;
118 sysarg_t addr_id;
[b7fd2a0]119 errno_t rc;
[fa101c4]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) {
[c1694b6b]138 printf(NAME ": Service '%s' not found: %s.\n", link_name,
139 str_error(rc));
[fa101c4]140 return ENOENT;
141 }
142
143 rc = inetcfg_addr_get_id(aobj_name, link_id, &addr_id);
144 if (rc != EOK) {
[c1694b6b]145 printf(NAME ": Address '%s' not found: %s.\n", aobj_name,
146 str_error(rc));
[fa101c4]147 return ENOENT;
148 }
149
150 rc = inetcfg_addr_delete(addr_id);
151 if (rc != EOK) {
[c1694b6b]152 printf(NAME ": Failed deleting address '%s': %s\n", aobj_name,
153 str_error(rc));
[0e94b979]154 return EIO;
[0e25780]155 }
156
[45aa22c]157 return EOK;
158}
159
[b7fd2a0]160static errno_t sroute_create(int argc, char *argv[])
[8bf672d]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;
[b7fd2a0]169 errno_t rc;
[8bf672d]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
[a62ceaf]187 rc = inet_naddr_parse(dest_str, &dest, NULL);
[8bf672d]188 if (rc != EOK) {
189 printf(NAME ": Invalid network address format '%s'.\n",
190 dest_str);
191 return EINVAL;
192 }
193
[a62ceaf]194 rc = inet_addr_parse(router_str, &router, NULL);
[8bf672d]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) {
[c1694b6b]202 printf(NAME ": Failed creating static route '%s': %s\n",
203 route_name, str_error(rc));
[8bf672d]204 return EIO;
205 }
206
207 return EOK;
208}
209
[b7fd2a0]210static errno_t sroute_delete(int argc, char *argv[])
[8bf672d]211{
212 char *route_name;
213 sysarg_t sroute_id;
[b7fd2a0]214 errno_t rc;
[8bf672d]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) {
[c1694b6b]232 printf(NAME ": Static route '%s' not found: %s.\n",
233 route_name, str_error(rc));
[8bf672d]234 return ENOENT;
235 }
236
237 rc = inetcfg_sroute_delete(sroute_id);
238 if (rc != EOK) {
[c1694b6b]239 printf(NAME ": Failed deleting static route '%s': %s\n",
240 route_name, str_error(rc));
[8bf672d]241 return EIO;
242 }
243
244 return EOK;
245}
246
[b7fd2a0]247static errno_t addr_list(void)
[45aa22c]248{
[0e94b979]249 sysarg_t *addr_list;
250 inet_addr_info_t ainfo;
251 inet_link_info_t linfo;
[5e962ad]252 table_t *table = NULL;
[0e94b979]253
254 size_t count;
255 size_t i;
[b7fd2a0]256 errno_t rc;
[5e962ad]257 char *astr = NULL;
258
259 ainfo.name = NULL;
260 linfo.name = NULL;
[45aa22c]261
[0e94b979]262 rc = inetcfg_get_addr_list(&addr_list, &count);
263 if (rc != EOK) {
264 printf(NAME ": Failed getting address list.\n");
265 return rc;
[45aa22c]266 }
[0e25780]267
[5e962ad]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");
[8bf672d]277
[0e94b979]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]);
[8bf672d]283 ainfo.name = NULL;
[0e94b979]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);
[8bf672d]291 linfo.name = NULL;
[0e94b979]292 continue;
293 }
294
[3495654]295 rc = inet_naddr_format(&ainfo.naddr, &astr);
[0e94b979]296 if (rc != EOK) {
297 printf("Memory allocation failed.\n");
[8bf672d]298 astr = NULL;
[0e94b979]299 goto out;
300 }
301
[5e962ad]302 table_printf(table, "%s\t" "%s\t" "%s\t" "%zu\n", astr,
303 linfo.name, ainfo.name, linfo.def_mtu);
[0e94b979]304
305 free(ainfo.name);
306 free(linfo.name);
[8bf672d]307 free(astr);
308
[257feec]309 ainfo.name = NULL;
310 linfo.name = NULL;
311 astr = NULL;
[0e94b979]312 }
[8bf672d]313
[75c3830]314 if (count != 0) {
[5e962ad]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;
[0e94b979]323out:
[5e962ad]324 table_destroy(table);
[8bf672d]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
[0e94b979]332 free(addr_list);
333
[5e962ad]334 return rc;
[0e94b979]335}
336
[b7fd2a0]337static errno_t link_list(void)
[b8b1adb1]338{
[5e962ad]339 sysarg_t *link_list = NULL;
[b8b1adb1]340 inet_link_info_t linfo;
[5e962ad]341 table_t *table = NULL;
[b8b1adb1]342
343 size_t count;
344 size_t i;
[b7fd2a0]345 errno_t rc;
[b8b1adb1]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
[5e962ad]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");
[b8b1adb1]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
[5e962ad]370 table_printf(table, "%02x:%02x:%02x:%02x:%02x:%02x\t"
371 "%s\t" "%zu\n",
[b8b1adb1]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
[75c3830]382 if (count != 0) {
[5e962ad]383 rc = table_print_out(table, stdout);
384 if (rc != EOK) {
385 printf("Error printing table.\n");
386 goto out;
387 }
388 }
[b8b1adb1]389
[5e962ad]390 rc = EOK;
391out:
392 table_destroy(table);
[b8b1adb1]393 free(link_list);
394
[5e962ad]395 return rc;
[b8b1adb1]396}
397
[b7fd2a0]398static errno_t sroute_list(void)
[8bf672d]399{
[5e962ad]400 sysarg_t *sroute_list = NULL;
[8bf672d]401 inet_sroute_info_t srinfo;
[5e962ad]402 table_t *table = NULL;
[8bf672d]403
404 size_t count;
405 size_t i;
[b7fd2a0]406 errno_t rc;
[5e962ad]407 char *dest_str = NULL;
408 char *router_str = NULL;
409
410 srinfo.name = NULL;
[8bf672d]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
[5e962ad]418 rc = table_create(&table);
419 if (rc != EOK) {
420 printf("Memory allocation failed.\n");
421 goto out;
422 }
[8bf672d]423
[5e962ad]424 table_header_row(table);
425 table_printf(table, "Dest/Width\t" "Router-Addr\t" "Route-Name\n");
[8bf672d]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
[3495654]436 rc = inet_naddr_format(&srinfo.dest, &dest_str);
[8bf672d]437 if (rc != EOK) {
438 printf("Memory allocation failed.\n");
439 dest_str = NULL;
440 goto out;
441 }
442
[3495654]443 rc = inet_addr_format(&srinfo.router, &router_str);
[8bf672d]444 if (rc != EOK) {
445 printf("Memory allocation failed.\n");
446 router_str = NULL;
447 goto out;
448 }
449
[5e962ad]450 table_printf(table, "%s\t" "%s\t" "%s\n", dest_str, router_str,
451 srinfo.name);
[8bf672d]452
453 free(srinfo.name);
454 free(dest_str);
455 free(router_str);
456
[257feec]457 router_str = NULL;
458 srinfo.name = NULL;
459 dest_str = NULL;
[8bf672d]460 }
461
[75c3830]462 if (count != 0) {
[5e962ad]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;
[8bf672d]471out:
[5e962ad]472 table_destroy(table);
[8bf672d]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
[5e962ad]482 return rc;
[8bf672d]483}
484
[0e94b979]485int main(int argc, char *argv[])
486{
[b7fd2a0]487 errno_t rc;
[0e94b979]488
[0e25780]489 rc = inetcfg_init();
490 if (rc != EOK) {
[c1694b6b]491 printf(NAME ": Failed connecting to internet service: %s.\n",
492 str_error(rc));
[0e25780]493 return 1;
494 }
495
[75c3830]496 if (argc < 2 || str_cmp(argv[1], "-h") == 0) {
497 print_syntax();
[0e94b979]498 return 0;
499 }
500
[75c3830]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) {
[45aa22c]506 rc = addr_create_static(argc - 2, argv + 2);
507 if (rc != EOK)
508 return 1;
[75c3830]509 } else if (str_cmp(argv[1], "delete-addr") == 0) {
[fa101c4]510 rc = addr_delete(argc - 2, argv + 2);
511 if (rc != EOK)
512 return 1;
[75c3830]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) {
[8bf672d]518 rc = sroute_create(argc - 2, argv + 2);
519 if (rc != EOK)
520 return 1;
[75c3830]521 } else if (str_cmp(argv[1], "delete-sr") == 0) {
[8bf672d]522 rc = sroute_delete(argc - 2, argv + 2);
523 if (rc != EOK)
524 return 1;
[75c3830]525 } else if (str_cmp(argv[1], "list-link") == 0) {
526 rc = link_list();
527 if (rc != EOK)
528 return 1;
[45aa22c]529 } else {
530 printf(NAME ": Unknown command '%s'.\n", argv[1]);
531 print_syntax();
[0e25780]532 return 1;
533 }
534
535 return 0;
536}
537
538/** @}
539 */
Note: See TracBrowser for help on using the repository browser.