iproute2/ip/rtm_map.c
<<
>>
Prefs
   1/*
   2 * rtm_map.c
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10 *
  11 */
  12
  13#include <stdio.h>
  14#include <stdlib.h>
  15#include <unistd.h>
  16#include <fcntl.h>
  17#include <string.h>
  18#include <sys/socket.h>
  19#include <netinet/in.h>
  20
  21#include "rt_names.h"
  22#include "utils.h"
  23
  24char *rtnl_rtntype_n2a(int id, char *buf, int len)
  25{
  26
  27        if (numeric) {
  28                snprintf(buf, len, "%d", id);
  29                return buf;
  30        }
  31
  32        switch (id) {
  33        case RTN_UNSPEC:
  34                return "none";
  35        case RTN_UNICAST:
  36                return "unicast";
  37        case RTN_LOCAL:
  38                return "local";
  39        case RTN_BROADCAST:
  40                return "broadcast";
  41        case RTN_ANYCAST:
  42                return "anycast";
  43        case RTN_MULTICAST:
  44                return "multicast";
  45        case RTN_BLACKHOLE:
  46                return "blackhole";
  47        case RTN_UNREACHABLE:
  48                return "unreachable";
  49        case RTN_PROHIBIT:
  50                return "prohibit";
  51        case RTN_THROW:
  52                return "throw";
  53        case RTN_NAT:
  54                return "nat";
  55        case RTN_XRESOLVE:
  56                return "xresolve";
  57        default:
  58                snprintf(buf, len, "%d", id);
  59                return buf;
  60        }
  61}
  62
  63
  64int rtnl_rtntype_a2n(int *id, char *arg)
  65{
  66        char *end;
  67        unsigned long res;
  68
  69        if (strcmp(arg, "local") == 0)
  70                res = RTN_LOCAL;
  71        else if (strcmp(arg, "nat") == 0)
  72                res = RTN_NAT;
  73        else if (matches(arg, "broadcast") == 0 ||
  74                 strcmp(arg, "brd") == 0)
  75                res = RTN_BROADCAST;
  76        else if (matches(arg, "anycast") == 0)
  77                res = RTN_ANYCAST;
  78        else if (matches(arg, "multicast") == 0)
  79                res = RTN_MULTICAST;
  80        else if (matches(arg, "prohibit") == 0)
  81                res = RTN_PROHIBIT;
  82        else if (matches(arg, "unreachable") == 0)
  83                res = RTN_UNREACHABLE;
  84        else if (matches(arg, "blackhole") == 0)
  85                res = RTN_BLACKHOLE;
  86        else if (matches(arg, "xresolve") == 0)
  87                res = RTN_XRESOLVE;
  88        else if (matches(arg, "unicast") == 0)
  89                res = RTN_UNICAST;
  90        else if (strcmp(arg, "throw") == 0)
  91                res = RTN_THROW;
  92        else {
  93                res = strtoul(arg, &end, 0);
  94                if (!end || end == arg || *end || res > 255)
  95                        return -1;
  96        }
  97        *id = res;
  98        return 0;
  99}
 100
 101static int get_rt_realms(__u32 *realms, char *arg)
 102{
 103        __u32 realm = 0;
 104        char *p = strchr(arg, '/');
 105
 106        *realms = 0;
 107        if (p) {
 108                *p = 0;
 109                if (rtnl_rtrealm_a2n(realms, arg)) {
 110                        *p = '/';
 111                        return -1;
 112                }
 113                *realms <<= 16;
 114                *p = '/';
 115                arg = p+1;
 116        }
 117        if (*arg && rtnl_rtrealm_a2n(&realm, arg))
 118                return -1;
 119        *realms |= realm;
 120        return 0;
 121}
 122
 123int get_rt_realms_or_raw(__u32 *realms, char *arg)
 124{
 125        if (!get_rt_realms(realms, arg))
 126                return 0;
 127
 128        return get_unsigned(realms, arg, 0);
 129}
 130