Changeset 4c53333 in mainline for uspace/app/inet/inet.c


Ignore:
Timestamp:
2013-07-11T08:21:10Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
64e63ce1
Parents:
80445cf (diff), c8bb1633 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge mainline changes

File:
1 moved

Legend:

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

    r80445cf r4c53333  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup inetcfg
     29/** @addtogroup inet
    3030 * @{
    3131 */
     
    3535 */
    3636
    37 #include <async.h>
    3837#include <errno.h>
     38#include <inet/addr.h>
    3939#include <inet/inetcfg.h>
    4040#include <loc.h>
     
    4444#include <sys/types.h>
    4545
    46 #define NAME "inetcfg"
     46#define NAME  "inet"
    4747
    4848static void print_syntax(void)
     
    5555}
    5656
    57 static 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 
    93 static 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 
    120 static 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 
    134 static 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 
    14857static int addr_create_static(int argc, char *argv[])
    14958{
     
    17988        }
    18089
    181         rc = naddr_parse(addr_spec, &naddr);
     90        rc = inet_naddr_parse(addr_spec, &naddr);
    18291        if (rc != EOK) {
    18392                printf(NAME ": Invalid network address format '%s'.\n",
     
    18897        rc = inetcfg_addr_create_static(aobj_name, &naddr, link_id, &addr_id);
    18998        if (rc != EOK) {
    190                 printf(NAME ": Failed creating static address '%s' (%d)\n",
    191                     aobj_name, rc);
     99                printf(NAME ": Failed creating static address '%s' (%s)\n",
     100                    aobj_name, str_error(rc));
    192101                return EIO;
    193102        }
     
    268177        route_name = argv[2];
    269178
    270         rc = naddr_parse(dest_str, &dest);
     179        rc = inet_naddr_parse(dest_str, &dest);
    271180        if (rc != EOK) {
    272181                printf(NAME ": Invalid network address format '%s'.\n",
     
    275184        }
    276185
    277         rc = addr_parse(router_str, &router);
     186        rc = inet_addr_parse(router_str, &router);
    278187        if (rc != EOK) {
    279188                printf(NAME ": Invalid address format '%s'.\n", router_str);
     
    346255
    347256        printf("Configured addresses:\n");
    348 
    349         ainfo.name = linfo.name = astr = NULL;
     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;
    350262
    351263        for (i = 0; i < count; i++) {
     
    366278                }
    367279
    368                 rc = naddr_format(&ainfo.naddr, &astr);
     280                rc = inet_naddr_format(&ainfo.naddr, &astr);
    369281                if (rc != EOK) {
    370282                        printf("Memory allocation failed.\n");
     
    380292                free(astr);
    381293
    382                 ainfo.name = linfo.name = astr = NULL;
     294                ainfo.name = NULL;
     295                linfo.name = NULL;
     296                astr = NULL;
    383297        }
    384298
     
    416330
    417331        printf("Static routes:\n");
    418 
    419         srinfo.name = dest_str = router_str = NULL;
     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;
    420338
    421339        for (i = 0; i < count; i++) {
     
    428346                }
    429347
    430                 rc = naddr_format(&srinfo.dest, &dest_str);
     348                rc = inet_naddr_format(&srinfo.dest, &dest_str);
    431349                if (rc != EOK) {
    432350                        printf("Memory allocation failed.\n");
     
    435353                }
    436354
    437                 rc = addr_format(&srinfo.router, &router_str);
     355                rc = inet_addr_format(&srinfo.router, &router_str);
    438356                if (rc != EOK) {
    439357                        printf("Memory allocation failed.\n");
     
    448366                free(router_str);
    449367
    450                 router_str = srinfo.name = dest_str = NULL;
     368                router_str = NULL;
     369                srinfo.name = NULL;
     370                dest_str = NULL;
    451371        }
    452372
Note: See TracChangeset for help on using the changeset viewer.