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

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

Add str_error() in numerous places.

  • 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_error.h>
46
47#define NAME "inet"
48
49static void print_syntax(void)
50{
51 printf("%s: Internet configuration utility.\n", NAME);
52 printf("Syntax:\n");
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);
60}
61
62static int addr_create_static(int argc, char *argv[])
63{
64 char *aobj_name;
65 char *addr_spec;
66 char *link_name;
67
68 inet_naddr_t naddr;
69 sysarg_t link_id;
70 sysarg_t addr_id;
71 int rc;
72
73 if (argc < 3) {
74 printf(NAME ": Missing arguments.\n");
75 print_syntax();
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) {
91 printf(NAME ": Service '%s' not found: %s.\n", link_name, str_error(rc));
92 return ENOENT;
93 }
94
95 rc = inet_naddr_parse(addr_spec, &naddr, NULL);
96 if (rc != EOK) {
97 printf(NAME ": Invalid network address format '%s'.\n",
98 addr_spec);
99 return EINVAL;
100 }
101
102 rc = inetcfg_addr_create_static(aobj_name, &naddr, link_id, &addr_id);
103 if (rc != EOK) {
104 printf(NAME ": Failed creating static address '%s' (%s)\n",
105 aobj_name, str_error(rc));
106 return EIO;
107 }
108
109 return EOK;
110}
111
112static int addr_delete(int argc, char *argv[])
113{
114 char *aobj_name;
115 char *link_name;
116 sysarg_t link_id;
117 sysarg_t addr_id;
118 int rc;
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) {
137 printf(NAME ": Service '%s' not found: %s.\n", link_name,
138 str_error(rc));
139 return ENOENT;
140 }
141
142 rc = inetcfg_addr_get_id(aobj_name, link_id, &addr_id);
143 if (rc != EOK) {
144 printf(NAME ": Address '%s' not found: %s.\n", aobj_name,
145 str_error(rc));
146 return ENOENT;
147 }
148
149 rc = inetcfg_addr_delete(addr_id);
150 if (rc != EOK) {
151 printf(NAME ": Failed deleting address '%s': %s\n", aobj_name,
152 str_error(rc));
153 return EIO;
154 }
155
156 return EOK;
157}
158
159static int sroute_create(int argc, char *argv[])
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;
168 int rc;
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
186 rc = inet_naddr_parse(dest_str, &dest, NULL);
187 if (rc != EOK) {
188 printf(NAME ": Invalid network address format '%s'.\n",
189 dest_str);
190 return EINVAL;
191 }
192
193 rc = inet_addr_parse(router_str, &router, NULL);
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) {
201 printf(NAME ": Failed creating static route '%s': %s\n",
202 route_name, str_error(rc));
203 return EIO;
204 }
205
206 return EOK;
207}
208
209static int sroute_delete(int argc, char *argv[])
210{
211 char *route_name;
212 sysarg_t sroute_id;
213 int rc;
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) {
231 printf(NAME ": Static route '%s' not found: %s.\n",
232 route_name, str_error(rc));
233 return ENOENT;
234 }
235
236 rc = inetcfg_sroute_delete(sroute_id);
237 if (rc != EOK) {
238 printf(NAME ": Failed deleting static route '%s': %s\n",
239 route_name, str_error(rc));
240 return EIO;
241 }
242
243 return EOK;
244}
245
246static int addr_list(void)
247{
248 sysarg_t *addr_list;
249 inet_addr_info_t ainfo;
250 inet_link_info_t linfo;
251 table_t *table = NULL;
252
253 size_t count;
254 size_t i;
255 int rc;
256 char *astr = NULL;
257
258 ainfo.name = NULL;
259 linfo.name = NULL;
260
261 rc = inetcfg_get_addr_list(&addr_list, &count);
262 if (rc != EOK) {
263 printf(NAME ": Failed getting address list.\n");
264 return rc;
265 }
266
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");
276
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]);
282 ainfo.name = NULL;
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);
290 linfo.name = NULL;
291 continue;
292 }
293
294 rc = inet_naddr_format(&ainfo.naddr, &astr);
295 if (rc != EOK) {
296 printf("Memory allocation failed.\n");
297 astr = NULL;
298 goto out;
299 }
300
301 table_printf(table, "%s\t" "%s\t" "%s\t" "%zu\n", astr,
302 linfo.name, ainfo.name, linfo.def_mtu);
303
304 free(ainfo.name);
305 free(linfo.name);
306 free(astr);
307
308 ainfo.name = NULL;
309 linfo.name = NULL;
310 astr = NULL;
311 }
312
313 if (count != 0) {
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;
322out:
323 table_destroy(table);
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
331 free(addr_list);
332
333 return rc;
334}
335
336static int link_list(void)
337{
338 sysarg_t *link_list = NULL;
339 inet_link_info_t linfo;
340 table_t *table = NULL;
341
342 size_t count;
343 size_t i;
344 int rc;
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
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");
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
369 table_printf(table, "%02x:%02x:%02x:%02x:%02x:%02x\t"
370 "%s\t" "%zu\n",
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
381 if (count != 0) {
382 rc = table_print_out(table, stdout);
383 if (rc != EOK) {
384 printf("Error printing table.\n");
385 goto out;
386 }
387 }
388
389 rc = EOK;
390out:
391 table_destroy(table);
392 free(link_list);
393
394 return rc;
395}
396
397static int sroute_list(void)
398{
399 sysarg_t *sroute_list = NULL;
400 inet_sroute_info_t srinfo;
401 table_t *table = NULL;
402
403 size_t count;
404 size_t i;
405 int rc;
406 char *dest_str = NULL;
407 char *router_str = NULL;
408
409 srinfo.name = NULL;
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
417 rc = table_create(&table);
418 if (rc != EOK) {
419 printf("Memory allocation failed.\n");
420 goto out;
421 }
422
423 table_header_row(table);
424 table_printf(table, "Dest/Width\t" "Router-Addr\t" "Route-Name\n");
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
435 rc = inet_naddr_format(&srinfo.dest, &dest_str);
436 if (rc != EOK) {
437 printf("Memory allocation failed.\n");
438 dest_str = NULL;
439 goto out;
440 }
441
442 rc = inet_addr_format(&srinfo.router, &router_str);
443 if (rc != EOK) {
444 printf("Memory allocation failed.\n");
445 router_str = NULL;
446 goto out;
447 }
448
449 table_printf(table, "%s\t" "%s\t" "%s\n", dest_str, router_str,
450 srinfo.name);
451
452 free(srinfo.name);
453 free(dest_str);
454 free(router_str);
455
456 router_str = NULL;
457 srinfo.name = NULL;
458 dest_str = NULL;
459 }
460
461 if (count != 0) {
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;
470out:
471 table_destroy(table);
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
481 return rc;
482}
483
484int main(int argc, char *argv[])
485{
486 int rc;
487
488 rc = inetcfg_init();
489 if (rc != EOK) {
490 printf(NAME ": Failed connecting to internet service: %s.\n",
491 str_error(rc));
492 return 1;
493 }
494
495 if (argc < 2 || str_cmp(argv[1], "-h") == 0) {
496 print_syntax();
497 return 0;
498 }
499
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) {
505 rc = addr_create_static(argc - 2, argv + 2);
506 if (rc != EOK)
507 return 1;
508 } else if (str_cmp(argv[1], "delete-addr") == 0) {
509 rc = addr_delete(argc - 2, argv + 2);
510 if (rc != EOK)
511 return 1;
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) {
517 rc = sroute_create(argc - 2, argv + 2);
518 if (rc != EOK)
519 return 1;
520 } else if (str_cmp(argv[1], "delete-sr") == 0) {
521 rc = sroute_delete(argc - 2, argv + 2);
522 if (rc != EOK)
523 return 1;
524 } else if (str_cmp(argv[1], "list-link") == 0) {
525 rc = link_list();
526 if (rc != EOK)
527 return 1;
528 } else {
529 printf(NAME ": Unknown command '%s'.\n", argv[1]);
530 print_syntax();
531 return 1;
532 }
533
534 return 0;
535}
536
537/** @}
538 */
Note: See TracBrowser for help on using the repository browser.