Changeset 8bf672d in mainline for uspace/app/inetcfg/inetcfg.c


Ignore:
Timestamp:
2012-03-30T17:42:11Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
66a272f8
Parents:
3b3c689
Message:

Static route configuration.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/inetcfg/inetcfg.c

    r3b3c689 r8bf672d  
    5151        printf("\t" NAME " create <addr>/<width> <link-name> <addr-name>\n");
    5252        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");
    5355}
    5456
     
    8284        }
    8385
    84         if (bits < 1 || bits > 31)
     86        if (bits > 31)
    8587                return EINVAL;
    8688
    8789        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
    88117        return EOK;
    89118}
     
    103132}
    104133
     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
    105148static int addr_create_static(int argc, char *argv[])
    106149{
     
    138181        rc = naddr_parse(addr_spec, &naddr);
    139182        if (rc != EOK) {
    140                 printf(NAME ": Invalid address format '%s'.\n", addr_spec);
     183                printf(NAME ": Invalid network address format '%s'.\n",
     184                    addr_spec);
    141185                return EINVAL;
    142186        }
     
    197241}
    198242
     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
    199330static int addr_list(void)
    200331{
     
    213344                return rc;
    214345        }
     346
     347        printf("Configured addresses:\n");
     348
     349        ainfo.name = linfo.name = astr = NULL;
    215350
    216351        for (i = 0; i < count; i++) {
     
    219354                        printf("Failed getting properties of address %zu.\n",
    220355                            (size_t)addr_list[i]);
     356                        ainfo.name = NULL;
    221357                        continue;
    222358                }
     
    226362                        printf("Failed getting properties of link %zu.\n",
    227363                            (size_t)ainfo.ilink);
     364                        linfo.name = NULL;
    228365                        continue;
    229366                }
     
    232369                if (rc != EOK) {
    233370                        printf("Memory allocation failed.\n");
     371                        astr = NULL;
    234372                        goto out;
    235373                }
    236374
    237                 printf("%s %s %s\n", astr, linfo.name,
     375                printf("    %s %s %s\n", astr, linfo.name,
    238376                    ainfo.name);
    239377
    240                 free(astr);
    241378                free(ainfo.name);
    242379                free(linfo.name);
    243         }
     380                free(astr);
     381
     382                ainfo.name = linfo.name = astr = NULL;
     383        }
     384
     385        if (count == 0)
     386                printf("    None\n");
    244387out:
     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
    245395        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);
    246464
    247465        return EOK;
     
    261479        if (argc < 2) {
    262480                rc = addr_list();
     481                if (rc != EOK)
     482                        return 1;
     483                rc = sroute_list();
    263484                if (rc != EOK)
    264485                        return 1;
     
    274495                if (rc != EOK)
    275496                        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;
    276505        } else {
    277506                printf(NAME ": Unknown command '%s'.\n", argv[1]);
Note: See TracChangeset for help on using the changeset viewer.