source: mainline/uspace/app/inet/inet.c@ 257feec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 257feec was 257feec, checked in by Martin Decky <martin@…>, 12 years ago

cstyle (no change in functionality)

  • Property mode set to 100644
File size: 9.1 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 <loc.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str_error.h>
44#include <sys/types.h>
45
46#define NAME "inet"
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 addr_create_static(int argc, char *argv[])
58{
59 char *aobj_name;
60 char *addr_spec;
61 char *link_name;
62
63 inet_naddr_t naddr;
64 sysarg_t link_id;
65 sysarg_t addr_id;
66 int rc;
67
68 if (argc < 3) {
69 printf(NAME ": Missing arguments.\n");
70 print_syntax();
71 return EINVAL;
72 }
73
74 if (argc > 3) {
75 printf(NAME ": Too many arguments.\n");
76 print_syntax();
77 return EINVAL;
78 }
79
80 addr_spec = argv[0];
81 link_name = argv[1];
82 aobj_name = argv[2];
83
84 rc = loc_service_get_id(link_name, &link_id, 0);
85 if (rc != EOK) {
86 printf(NAME ": Service '%s' not found (%d).\n", link_name, rc);
87 return ENOENT;
88 }
89
90 rc = inet_naddr_parse(addr_spec, &naddr);
91 if (rc != EOK) {
92 printf(NAME ": Invalid network address format '%s'.\n",
93 addr_spec);
94 return EINVAL;
95 }
96
97 rc = inetcfg_addr_create_static(aobj_name, &naddr, link_id, &addr_id);
98 if (rc != EOK) {
99 printf(NAME ": Failed creating static address '%s' (%s)\n",
100 aobj_name, str_error(rc));
101 return EIO;
102 }
103
104 return EOK;
105}
106
107static int addr_delete(int argc, char *argv[])
108{
109 char *aobj_name;
110 char *link_name;
111 sysarg_t link_id;
112 sysarg_t addr_id;
113 int rc;
114
115 if (argc < 2) {
116 printf(NAME ": Missing arguments.\n");
117 print_syntax();
118 return EINVAL;
119 }
120
121 if (argc > 2) {
122 printf(NAME ": Too many arguments.\n");
123 print_syntax();
124 return EINVAL;
125 }
126
127 link_name = argv[0];
128 aobj_name = argv[1];
129
130 rc = loc_service_get_id(link_name, &link_id, 0);
131 if (rc != EOK) {
132 printf(NAME ": Service '%s' not found (%d).\n", link_name, rc);
133 return ENOENT;
134 }
135
136 rc = inetcfg_addr_get_id(aobj_name, link_id, &addr_id);
137 if (rc != EOK) {
138 printf(NAME ": Address '%s' not found (%d).\n", aobj_name, rc);
139 return ENOENT;
140 }
141
142 rc = inetcfg_addr_delete(addr_id);
143 if (rc != EOK) {
144 printf(NAME ": Failed deleting address '%s' (%d)\n", aobj_name,
145 rc);
146 return EIO;
147 }
148
149 return EOK;
150}
151
152static int sroute_create(int argc, char *argv[])
153{
154 char *dest_str;
155 char *router_str;
156 char *route_name;
157
158 inet_naddr_t dest;
159 inet_addr_t router;
160 sysarg_t sroute_id;
161 int rc;
162
163 if (argc < 3) {
164 printf(NAME ": Missing arguments.\n");
165 print_syntax();
166 return EINVAL;
167 }
168
169 if (argc > 3) {
170 printf(NAME ": Too many arguments.\n");
171 print_syntax();
172 return EINVAL;
173 }
174
175 dest_str = argv[0];
176 router_str = argv[1];
177 route_name = argv[2];
178
179 rc = inet_naddr_parse(dest_str, &dest);
180 if (rc != EOK) {
181 printf(NAME ": Invalid network address format '%s'.\n",
182 dest_str);
183 return EINVAL;
184 }
185
186 rc = inet_addr_parse(router_str, &router);
187 if (rc != EOK) {
188 printf(NAME ": Invalid address format '%s'.\n", router_str);
189 return EINVAL;
190 }
191
192 rc = inetcfg_sroute_create(route_name, &dest, &router, &sroute_id);
193 if (rc != EOK) {
194 printf(NAME ": Failed creating static route '%s' (%d)\n",
195 route_name, rc);
196 return EIO;
197 }
198
199 return EOK;
200}
201
202static int sroute_delete(int argc, char *argv[])
203{
204 char *route_name;
205 sysarg_t sroute_id;
206 int rc;
207
208 if (argc < 1) {
209 printf(NAME ": Missing arguments.\n");
210 print_syntax();
211 return EINVAL;
212 }
213
214 if (argc > 1) {
215 printf(NAME ": Too many arguments.\n");
216 print_syntax();
217 return EINVAL;
218 }
219
220 route_name = argv[0];
221
222 rc = inetcfg_sroute_get_id(route_name, &sroute_id);
223 if (rc != EOK) {
224 printf(NAME ": Static route '%s' not found (%d).\n",
225 route_name, rc);
226 return ENOENT;
227 }
228
229 rc = inetcfg_sroute_delete(sroute_id);
230 if (rc != EOK) {
231 printf(NAME ": Failed deleting static route '%s' (%d)\n",
232 route_name, rc);
233 return EIO;
234 }
235
236 return EOK;
237}
238
239static int addr_list(void)
240{
241 sysarg_t *addr_list;
242 inet_addr_info_t ainfo;
243 inet_link_info_t linfo;
244
245 size_t count;
246 size_t i;
247 int rc;
248 char *astr;
249
250 rc = inetcfg_get_addr_list(&addr_list, &count);
251 if (rc != EOK) {
252 printf(NAME ": Failed getting address list.\n");
253 return rc;
254 }
255
256 printf("Configured addresses:\n");
257 if (count > 0)
258 printf(" [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n");
259 ainfo.name = NULL;
260 linfo.name = NULL;
261 astr = NULL;
262
263 for (i = 0; i < count; i++) {
264 rc = inetcfg_addr_get(addr_list[i], &ainfo);
265 if (rc != EOK) {
266 printf("Failed getting properties of address %zu.\n",
267 (size_t)addr_list[i]);
268 ainfo.name = NULL;
269 continue;
270 }
271
272 rc = inetcfg_link_get(ainfo.ilink, &linfo);
273 if (rc != EOK) {
274 printf("Failed getting properties of link %zu.\n",
275 (size_t)ainfo.ilink);
276 linfo.name = NULL;
277 continue;
278 }
279
280 rc = inet_naddr_format(&ainfo.naddr, &astr);
281 if (rc != EOK) {
282 printf("Memory allocation failed.\n");
283 astr = NULL;
284 goto out;
285 }
286
287 printf(" %s %s %s %zu\n", astr, linfo.name,
288 ainfo.name, linfo.def_mtu);
289
290 free(ainfo.name);
291 free(linfo.name);
292 free(astr);
293
294 ainfo.name = NULL;
295 linfo.name = NULL;
296 astr = NULL;
297 }
298
299 if (count == 0)
300 printf(" None\n");
301out:
302 if (ainfo.name != NULL)
303 free(ainfo.name);
304 if (linfo.name != NULL)
305 free(linfo.name);
306 if (astr != NULL)
307 free(astr);
308
309 free(addr_list);
310
311 return EOK;
312}
313
314static int sroute_list(void)
315{
316 sysarg_t *sroute_list;
317 inet_sroute_info_t srinfo;
318
319 size_t count;
320 size_t i;
321 int rc;
322 char *dest_str;
323 char *router_str;
324
325 rc = inetcfg_get_sroute_list(&sroute_list, &count);
326 if (rc != EOK) {
327 printf(NAME ": Failed getting address list.\n");
328 return rc;
329 }
330
331 printf("Static routes:\n");
332 if (count > 0)
333 printf(" [Dest/Width] [Router-Addr] [Route-Name]\n");
334
335 srinfo.name = NULL;
336 dest_str = NULL;
337 router_str = NULL;
338
339 for (i = 0; i < count; i++) {
340 rc = inetcfg_sroute_get(sroute_list[i], &srinfo);
341 if (rc != EOK) {
342 printf("Failed getting properties of static route %zu.\n",
343 (size_t)sroute_list[i]);
344 srinfo.name = NULL;
345 continue;
346 }
347
348 rc = inet_naddr_format(&srinfo.dest, &dest_str);
349 if (rc != EOK) {
350 printf("Memory allocation failed.\n");
351 dest_str = NULL;
352 goto out;
353 }
354
355 rc = inet_addr_format(&srinfo.router, &router_str);
356 if (rc != EOK) {
357 printf("Memory allocation failed.\n");
358 router_str = NULL;
359 goto out;
360 }
361
362 printf(" %s %s %s\n", dest_str, router_str, srinfo.name);
363
364 free(srinfo.name);
365 free(dest_str);
366 free(router_str);
367
368 router_str = NULL;
369 srinfo.name = NULL;
370 dest_str = NULL;
371 }
372
373 if (count == 0)
374 printf(" None\n");
375out:
376 if (srinfo.name != NULL)
377 free(srinfo.name);
378 if (dest_str != NULL)
379 free(dest_str);
380 if (router_str != NULL)
381 free(router_str);
382
383 free(sroute_list);
384
385 return EOK;
386}
387
388int main(int argc, char *argv[])
389{
390 int rc;
391
392 rc = inetcfg_init();
393 if (rc != EOK) {
394 printf(NAME ": Failed connecting to internet service (%d).\n",
395 rc);
396 return 1;
397 }
398
399 if (argc < 2) {
400 rc = addr_list();
401 if (rc != EOK)
402 return 1;
403 rc = sroute_list();
404 if (rc != EOK)
405 return 1;
406 return 0;
407 }
408
409 if (str_cmp(argv[1], "create") == 0) {
410 rc = addr_create_static(argc - 2, argv + 2);
411 if (rc != EOK)
412 return 1;
413 } else if (str_cmp(argv[1], "delete") == 0) {
414 rc = addr_delete(argc - 2, argv + 2);
415 if (rc != EOK)
416 return 1;
417 } else if (str_cmp(argv[1], "add-sr") == 0) {
418 rc = sroute_create(argc - 2, argv + 2);
419 if (rc != EOK)
420 return 1;
421 } else if (str_cmp(argv[1], "del-sr") == 0) {
422 rc = sroute_delete(argc - 2, argv + 2);
423 if (rc != EOK)
424 return 1;
425 } else {
426 printf(NAME ": Unknown command '%s'.\n", argv[1]);
427 print_syntax();
428 return 1;
429 }
430
431 return 0;
432}
433
434/** @}
435 */
Note: See TracBrowser for help on using the repository browser.