source: mainline/uspace/app/inet/inet.c@ 5a6cc679

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5a6cc679 was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

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