source: mainline/uspace/app/inetcfg/inetcfg.c@ 7f95c904

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

Fragmentation of outgoing datagrams, default link MTU.

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