1/*
2 * rt_names.c		rtnetlink names DB.
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#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
17#include <string.h>
18#include <sys/time.h>
19#include <sys/socket.h>
20
21#include <asm/types.h>
22#include <linux/rtnetlink.h>
23
24#include "rt_names.h"
25
26static char * rtnl_rttable_tab[256] =
27{
28	"unspec",
29};
30
31static int rtnl_rttable_init;
32
33static void rtnl_rttable_initialize(void)
34{
35	rtnl_rttable_init = 1;
36
37	rtnl_rttable_tab[255] = "local";
38	rtnl_rttable_tab[254] = "main";
39	rtnl_rttable_tab[253] = "default";
40
41	/* Add the table name here, simple is the best, RTFSC!*/
42	rtnl_rttable_tab[201] = "PPP1";
43}
44
45char * rtnl_rttable_n2a(int id, char *buf, int len)
46{
47	if (id<0 || id>=256)
48	{
49		snprintf(buf, len, "%d", id);
50		return buf;
51	}
52
53	if (!rtnl_rttable_tab[id])
54	{
55		if (!rtnl_rttable_init)
56			rtnl_rttable_initialize();
57	}
58
59	if (rtnl_rttable_tab[id])
60		return rtnl_rttable_tab[id];
61
62	snprintf(buf, len, "%d", id);
63	return buf;
64}
65
66int rtnl_rttable_a2n(unsigned int  * id, char * arg)
67{
68	static char * cache = NULL;
69	static unsigned long res;
70	char * end;
71	int i;
72
73	if (cache && strcmp(cache, arg) == 0)
74	{
75		*id = res;
76		return 0;
77	}
78
79	if (!rtnl_rttable_init)
80		rtnl_rttable_initialize();
81
82	for (i=0; i<256; i++)
83	{
84		if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0)
85		{
86			cache = rtnl_rttable_tab[i];
87			res = i;
88			*id = res;
89			return 0;
90		}
91	}
92
93	i = strtoul(arg, &end, 0);
94	if (!end || end == arg || *end || i > 255)
95		return -1;
96
97	*id = i;
98	return 0;
99}
100