Home | History | Annotate | Download | only in ip
      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 (at) ms2.inr.ac.ru>
     10  *
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <syslog.h>
     17 #include <fcntl.h>
     18 #include <string.h>
     19 #include <sys/socket.h>
     20 #include <netinet/in.h>
     21 
     22 #include "rt_names.h"
     23 #include "utils.h"
     24 
     25 char *rtnl_rtntype_n2a(int id, char *buf, int len)
     26 {
     27 	switch (id) {
     28 	case RTN_UNSPEC:
     29 		return "none";
     30 	case RTN_UNICAST:
     31 		return "unicast";
     32 	case RTN_LOCAL:
     33 		return "local";
     34 	case RTN_BROADCAST:
     35 		return "broadcast";
     36 	case RTN_ANYCAST:
     37 		return "anycast";
     38 	case RTN_MULTICAST:
     39 		return "multicast";
     40 	case RTN_BLACKHOLE:
     41 		return "blackhole";
     42 	case RTN_UNREACHABLE:
     43 		return "unreachable";
     44 	case RTN_PROHIBIT:
     45 		return "prohibit";
     46 	case RTN_THROW:
     47 		return "throw";
     48 	case RTN_NAT:
     49 		return "nat";
     50 	case RTN_XRESOLVE:
     51 		return "xresolve";
     52 	default:
     53 		snprintf(buf, len, "%d", id);
     54 		return buf;
     55 	}
     56 }
     57 
     58 
     59 int rtnl_rtntype_a2n(int *id, char *arg)
     60 {
     61 	char *end;
     62 	unsigned long res;
     63 
     64 	if (strcmp(arg, "local") == 0)
     65 		res = RTN_LOCAL;
     66 	else if (strcmp(arg, "nat") == 0)
     67 		res = RTN_NAT;
     68 	else if (matches(arg, "broadcast") == 0 ||
     69 		 strcmp(arg, "brd") == 0)
     70 		res = RTN_BROADCAST;
     71 	else if (matches(arg, "anycast") == 0)
     72 		res = RTN_ANYCAST;
     73 	else if (matches(arg, "multicast") == 0)
     74 		res = RTN_MULTICAST;
     75 	else if (matches(arg, "prohibit") == 0)
     76 		res = RTN_PROHIBIT;
     77 	else if (matches(arg, "unreachable") == 0)
     78 		res = RTN_UNREACHABLE;
     79 	else if (matches(arg, "blackhole") == 0)
     80 		res = RTN_BLACKHOLE;
     81 	else if (matches(arg, "xresolve") == 0)
     82 		res = RTN_XRESOLVE;
     83 	else if (matches(arg, "unicast") == 0)
     84 		res = RTN_UNICAST;
     85 	else if (strcmp(arg, "throw") == 0)
     86 		res = RTN_THROW;
     87 	else {
     88 		res = strtoul(arg, &end, 0);
     89 		if (!end || end == arg || *end || res > 255)
     90 			return -1;
     91 	}
     92 	*id = res;
     93 	return 0;
     94 }
     95 
     96 static int get_rt_realms(__u32 *realms, char *arg)
     97 {
     98 	__u32 realm = 0;
     99 	char *p = strchr(arg, '/');
    100 
    101 	*realms = 0;
    102 	if (p) {
    103 		*p = 0;
    104 		if (rtnl_rtrealm_a2n(realms, arg)) {
    105 			*p = '/';
    106 			return -1;
    107 		}
    108 		*realms <<= 16;
    109 		*p = '/';
    110 		arg = p+1;
    111 	}
    112 	if (*arg && rtnl_rtrealm_a2n(&realm, arg))
    113 		return -1;
    114 	*realms |= realm;
    115 	return 0;
    116 }
    117 
    118 int get_rt_realms_or_raw(__u32 *realms, char *arg)
    119 {
    120 	if (!get_rt_realms(realms, arg))
    121 		return 0;
    122 
    123 	return get_unsigned(realms, arg, 0);
    124 }
    125