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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f8dbc12 was f8dbc12, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Add headers to inet listings.

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