Get rid of sys/time.h
[helenos.git] / uspace / app / inet / inet.c
blob033c70ee95cb25578c8af34379f4cf7ef4ec23a0
1 /*
2 * Copyright (c) 2013 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup inet
30 * @{
32 /** @file Internet configuration utility.
34 * Controls the internet service (@c inet).
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>
48 #define NAME "inet"
50 static void print_syntax(void)
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);
63 static errno_t addr_create_static(int argc, char *argv[])
65 char *aobj_name;
66 char *addr_spec;
67 char *link_name;
69 inet_naddr_t naddr;
70 sysarg_t link_id;
71 sysarg_t addr_id;
72 errno_t rc;
74 if (argc < 3) {
75 printf(NAME ": Missing arguments.\n");
76 print_syntax();
77 return EINVAL;
80 if (argc > 3) {
81 printf(NAME ": Too many arguments.\n");
82 print_syntax();
83 return EINVAL;
86 addr_spec = argv[0];
87 link_name = argv[1];
88 aobj_name = argv[2];
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;
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;
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;
110 return EOK;
113 static errno_t addr_delete(int argc, char *argv[])
115 char *aobj_name;
116 char *link_name;
117 sysarg_t link_id;
118 sysarg_t addr_id;
119 errno_t rc;
121 if (argc < 2) {
122 printf(NAME ": Missing arguments.\n");
123 print_syntax();
124 return EINVAL;
127 if (argc > 2) {
128 printf(NAME ": Too many arguments.\n");
129 print_syntax();
130 return EINVAL;
133 link_name = argv[0];
134 aobj_name = argv[1];
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;
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;
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;
157 return EOK;
160 static errno_t sroute_create(int argc, char *argv[])
162 char *dest_str;
163 char *router_str;
164 char *route_name;
166 inet_naddr_t dest;
167 inet_addr_t router;
168 sysarg_t sroute_id;
169 errno_t rc;
171 if (argc < 3) {
172 printf(NAME ": Missing arguments.\n");
173 print_syntax();
174 return EINVAL;
177 if (argc > 3) {
178 printf(NAME ": Too many arguments.\n");
179 print_syntax();
180 return EINVAL;
183 dest_str = argv[0];
184 router_str = argv[1];
185 route_name = argv[2];
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;
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;
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;
207 return EOK;
210 static errno_t sroute_delete(int argc, char *argv[])
212 char *route_name;
213 sysarg_t sroute_id;
214 errno_t rc;
216 if (argc < 1) {
217 printf(NAME ": Missing arguments.\n");
218 print_syntax();
219 return EINVAL;
222 if (argc > 1) {
223 printf(NAME ": Too many arguments.\n");
224 print_syntax();
225 return EINVAL;
228 route_name = argv[0];
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;
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;
244 return EOK;
247 static errno_t addr_list(void)
249 sysarg_t *addr_list;
250 inet_addr_info_t ainfo;
251 inet_link_info_t linfo;
252 table_t *table = NULL;
254 size_t count;
255 size_t i;
256 errno_t rc;
257 char *astr = NULL;
259 ainfo.name = NULL;
260 linfo.name = NULL;
262 rc = inetcfg_get_addr_list(&addr_list, &count);
263 if (rc != EOK) {
264 printf(NAME ": Failed getting address list.\n");
265 return rc;
268 rc = table_create(&table);
269 if (rc != EOK) {
270 printf("Memory allocation failed.\n");
271 goto out;
274 table_header_row(table);
275 table_printf(table, "Addr/Width\t" "Link-Name\t" "Addr-Name\t"
276 "Def-MTU\n");
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;
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;
295 rc = inet_naddr_format(&ainfo.naddr, &astr);
296 if (rc != EOK) {
297 printf("Memory allocation failed.\n");
298 astr = NULL;
299 goto out;
302 table_printf(table, "%s\t" "%s\t" "%s\t" "%zu\n", astr,
303 linfo.name, ainfo.name, linfo.def_mtu);
305 free(ainfo.name);
306 free(linfo.name);
307 free(astr);
309 ainfo.name = NULL;
310 linfo.name = NULL;
311 astr = NULL;
314 if (count != 0) {
315 rc = table_print_out(table, stdout);
316 if (rc != EOK) {
317 printf("Error printing table.\n");
318 goto out;
322 rc = EOK;
323 out:
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);
332 free(addr_list);
334 return rc;
337 static errno_t link_list(void)
339 sysarg_t *link_list = NULL;
340 inet_link_info_t linfo;
341 table_t *table = NULL;
343 size_t count;
344 size_t i;
345 errno_t rc;
347 rc = inetcfg_get_link_list(&link_list, &count);
348 if (rc != EOK) {
349 printf(NAME ": Failed getting link list.\n");
350 return rc;
353 rc = table_create(&table);
354 if (rc != EOK) {
355 printf("Memory allocation failed.\n");
356 goto out;
359 table_header_row(table);
360 table_printf(table, "Link-layer Address\t" "Link-Name\t" "Def-MTU\n");
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;
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);
377 free(linfo.name);
379 linfo.name = NULL;
382 if (count != 0) {
383 rc = table_print_out(table, stdout);
384 if (rc != EOK) {
385 printf("Error printing table.\n");
386 goto out;
390 rc = EOK;
391 out:
392 table_destroy(table);
393 free(link_list);
395 return rc;
398 static errno_t sroute_list(void)
400 sysarg_t *sroute_list = NULL;
401 inet_sroute_info_t srinfo;
402 table_t *table = NULL;
404 size_t count;
405 size_t i;
406 errno_t rc;
407 char *dest_str = NULL;
408 char *router_str = NULL;
410 srinfo.name = NULL;
412 rc = inetcfg_get_sroute_list(&sroute_list, &count);
413 if (rc != EOK) {
414 printf(NAME ": Failed getting address list.\n");
415 return rc;
418 rc = table_create(&table);
419 if (rc != EOK) {
420 printf("Memory allocation failed.\n");
421 goto out;
424 table_header_row(table);
425 table_printf(table, "Dest/Width\t" "Router-Addr\t" "Route-Name\n");
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;
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;
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;
450 table_printf(table, "%s\t" "%s\t" "%s\n", dest_str, router_str,
451 srinfo.name);
453 free(srinfo.name);
454 free(dest_str);
455 free(router_str);
457 router_str = NULL;
458 srinfo.name = NULL;
459 dest_str = NULL;
462 if (count != 0) {
463 rc = table_print_out(table, stdout);
464 if (rc != EOK) {
465 printf("Error printing table.\n");
466 goto out;
470 rc = EOK;
471 out:
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);
480 free(sroute_list);
482 return rc;
485 int main(int argc, char *argv[])
487 errno_t rc;
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;
496 if (argc < 2 || str_cmp(argv[1], "-h") == 0) {
497 print_syntax();
498 return 0;
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;
535 return 0;
538 /** @}