Changes in uspace/app/inet/inet.c [3495654:bf9e6fc] in mainline


Ignore:
File:
1 edited

Legend:

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

    r3495654 rbf9e6fc  
    11/*
    2  * Copyright (c) 2013 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636
    3737#include <errno.h>
    38 #include <inet/addr.h>
    3938#include <inet/inetcfg.h>
    4039#include <loc.h>
     
    5554}
    5655
     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
    57147static int addr_create_static(int argc, char *argv[])
    58148{
     
    88178        }
    89179
    90         rc = inet_naddr_parse(addr_spec, &naddr);
     180        rc = naddr_parse(addr_spec, &naddr);
    91181        if (rc != EOK) {
    92182                printf(NAME ": Invalid network address format '%s'.\n",
     
    177267        route_name = argv[2];
    178268
    179         rc = inet_naddr_parse(dest_str, &dest);
     269        rc = naddr_parse(dest_str, &dest);
    180270        if (rc != EOK) {
    181271                printf(NAME ": Invalid network address format '%s'.\n",
     
    184274        }
    185275
    186         rc = inet_addr_parse(router_str, &router);
     276        rc = addr_parse(router_str, &router);
    187277        if (rc != EOK) {
    188278                printf(NAME ": Invalid address format '%s'.\n", router_str);
     
    276366                }
    277367
    278                 rc = inet_naddr_format(&ainfo.naddr, &astr);
     368                rc = naddr_format(&ainfo.naddr, &astr);
    279369                if (rc != EOK) {
    280370                        printf("Memory allocation failed.\n");
     
    340430                }
    341431
    342                 rc = inet_naddr_format(&srinfo.dest, &dest_str);
     432                rc = naddr_format(&srinfo.dest, &dest_str);
    343433                if (rc != EOK) {
    344434                        printf("Memory allocation failed.\n");
     
    347437                }
    348438
    349                 rc = inet_addr_format(&srinfo.router, &router_str);
     439                rc = addr_format(&srinfo.router, &router_str);
    350440                if (rc != EOK) {
    351441                        printf("Memory allocation failed.\n");
Note: See TracChangeset for help on using the changeset viewer.