This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
[oweals/busybox.git] / busybox / networking / libiproute / ll_map.c
1 /*
2  * ll_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 <string.h>
15 #include <stdlib.h>
16 #include <netinet/in.h>
17
18 #include "libnetlink.h"
19
20 struct idxmap
21 {
22         struct idxmap * next;
23         int             index;
24         int             type;
25         int             alen;
26         unsigned        flags;
27         unsigned char   addr[8];
28         char            name[16];
29 };
30
31 static struct idxmap *idxmap[16];
32
33 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
34 {
35         int h;
36         struct ifinfomsg *ifi = NLMSG_DATA(n);
37         struct idxmap *im, **imp;
38         struct rtattr *tb[IFLA_MAX+1];
39
40         if (n->nlmsg_type != RTM_NEWLINK)
41                 return 0;
42
43         if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
44                 return -1;
45
46
47         memset(tb, 0, sizeof(tb));
48         parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
49         if (tb[IFLA_IFNAME] == NULL)
50                 return 0;
51
52         h = ifi->ifi_index&0xF;
53
54         for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
55                 if (im->index == ifi->ifi_index)
56                         break;
57
58         if (im == NULL) {
59                 im = malloc(sizeof(*im));
60                 if (im == NULL)
61                         return 0;
62                 im->next = *imp;
63                 im->index = ifi->ifi_index;
64                 *imp = im;
65         }
66
67         im->type = ifi->ifi_type;
68         im->flags = ifi->ifi_flags;
69         if (tb[IFLA_ADDRESS]) {
70                 int alen;
71                 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
72                 if (alen > sizeof(im->addr))
73                         alen = sizeof(im->addr);
74                 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
75         } else {
76                 im->alen = 0;
77                 memset(im->addr, 0, sizeof(im->addr));
78         }
79         strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
80         return 0;
81 }
82
83 const char *ll_idx_n2a(int idx, char *buf)
84 {
85         struct idxmap *im;
86
87         if (idx == 0)
88                 return "*";
89         for (im = idxmap[idx&0xF]; im; im = im->next)
90                 if (im->index == idx)
91                         return im->name;
92         snprintf(buf, 16, "if%d", idx);
93         return buf;
94 }
95
96
97 const char *ll_index_to_name(int idx)
98 {
99         static char nbuf[16];
100
101         return ll_idx_n2a(idx, nbuf);
102 }
103
104 int ll_index_to_type(int idx)
105 {
106         struct idxmap *im;
107
108         if (idx == 0)
109                 return -1;
110         for (im = idxmap[idx&0xF]; im; im = im->next)
111                 if (im->index == idx)
112                         return im->type;
113         return -1;
114 }
115
116 unsigned ll_index_to_flags(int idx)
117 {
118         struct idxmap *im;
119
120         if (idx == 0)
121                 return 0;
122
123         for (im = idxmap[idx&0xF]; im; im = im->next)
124                 if (im->index == idx)
125                         return im->flags;
126         return 0;
127 }
128
129 int ll_name_to_index(char *name)
130 {
131         static char ncache[16];
132         static int icache;
133         struct idxmap *im;
134         int i;
135
136         if (name == NULL)
137                 return 0;
138         if (icache && strcmp(name, ncache) == 0)
139                 return icache;
140         for (i=0; i<16; i++) {
141                 for (im = idxmap[i]; im; im = im->next) {
142                         if (strcmp(im->name, name) == 0) {
143                                 icache = im->index;
144                                 strcpy(ncache, name);
145                                 return im->index;
146                         }
147                 }
148         }
149         return 0;
150 }
151
152 int ll_init_map(struct rtnl_handle *rth)
153 {
154         if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
155                 perror("Cannot send dump request");
156                 exit(1);
157         }
158
159         if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
160                 fprintf(stderr, "Dump terminated\n");
161                 exit(1);
162         }
163         return 0;
164 }