Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / networking / libiproute / ll_map.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ll_map.c
4  *
5  *              This program is free software; you can redistribute it and/or
6  *              modify it under the terms of the GNU General Public License
7  *              as published by the Free Software Foundation; either version
8  *              2 of the License, or (at your option) any later version.
9  *
10  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11  *
12  */
13
14 #include "libbb.h"
15 #include <string.h>
16
17 #include "libnetlink.h"
18 #include "ll_map.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 = xmalloc(sizeof(*im));
60                 im->next = *imp;
61                 im->index = ifi->ifi_index;
62                 *imp = im;
63         }
64
65         im->type = ifi->ifi_type;
66         im->flags = ifi->ifi_flags;
67         if (tb[IFLA_ADDRESS]) {
68                 int alen;
69                 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
70                 if (alen > sizeof(im->addr))
71                         alen = sizeof(im->addr);
72                 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
73         } else {
74                 im->alen = 0;
75                 memset(im->addr, 0, sizeof(im->addr));
76         }
77         strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
78         return 0;
79 }
80
81 const char *ll_idx_n2a(int idx, char *buf)
82 {
83         struct idxmap *im;
84
85         if (idx == 0)
86                 return "*";
87         for (im = idxmap[idx&0xF]; im; im = im->next)
88                 if (im->index == idx)
89                         return im->name;
90         snprintf(buf, 16, "if%d", idx);
91         return buf;
92 }
93
94
95 const char *ll_index_to_name(int idx)
96 {
97         static char nbuf[16];
98
99         return ll_idx_n2a(idx, nbuf);
100 }
101
102 int ll_index_to_type(int idx)
103 {
104         struct idxmap *im;
105
106         if (idx == 0)
107                 return -1;
108         for (im = idxmap[idx&0xF]; im; im = im->next)
109                 if (im->index == idx)
110                         return im->type;
111         return -1;
112 }
113
114 unsigned ll_index_to_flags(int idx)
115 {
116         struct idxmap *im;
117
118         if (idx == 0)
119                 return 0;
120
121         for (im = idxmap[idx&0xF]; im; im = im->next)
122                 if (im->index == idx)
123                         return im->flags;
124         return 0;
125 }
126
127 int ll_name_to_index(char *name)
128 {
129         static char ncache[16];
130         static int icache;
131         struct idxmap *im;
132         int i;
133
134         if (name == NULL)
135                 return 0;
136         if (icache && strcmp(name, ncache) == 0)
137                 return icache;
138         for (i=0; i<16; i++) {
139                 for (im = idxmap[i]; im; im = im->next) {
140                         if (strcmp(im->name, name) == 0) {
141                                 icache = im->index;
142                                 strcpy(ncache, name);
143                                 return im->index;
144                         }
145                 }
146         }
147         return 0;
148 }
149
150 int ll_init_map(struct rtnl_handle *rth)
151 {
152         if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
153                 perror("Cannot send dump request");
154                 exit(1);
155         }
156
157         if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
158                 fprintf(stderr, "Dump terminated\n");
159                 exit(1);
160         }
161         return 0;
162 }