1 /* vi: set sw=4 ts=4: */
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 #include <net/if.h> /* struct ifreq and co. */
13 #include "libnetlink.h"
22 unsigned char addr[8];
26 static struct idxmap **idxmap; /* treat as *idxmap[16] */
28 static struct idxmap *find_by_index(int idx)
33 for (im = idxmap[idx & 0xF]; im; im = im->next)
39 int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
41 void *arg UNUSED_PARAM)
44 struct ifinfomsg *ifi = NLMSG_DATA(n);
45 struct idxmap *im, **imp;
46 struct rtattr *tb[IFLA_MAX+1];
48 if (n->nlmsg_type != RTM_NEWLINK)
51 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
54 //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
55 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
56 if (tb[IFLA_IFNAME] == NULL)
60 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
62 h = ifi->ifi_index & 0xF;
63 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
64 if (im->index == ifi->ifi_index)
67 im = xmalloc(sizeof(*im));
69 im->index = ifi->ifi_index;
72 im->type = ifi->ifi_type;
73 im->flags = ifi->ifi_flags;
74 if (tb[IFLA_ADDRESS]) {
76 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
77 if (alen > (int)sizeof(im->addr))
78 alen = sizeof(im->addr);
79 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
82 memset(im->addr, 0, sizeof(im->addr));
84 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
89 const char FAST_FUNC *ll_idx_n2a(int idx/*, char *buf*/)
95 im = find_by_index(idx);
98 //snprintf(buf, 16, "if%d", idx);
100 return auto_string(xasprintf("if%d", idx));
103 const char FAST_FUNC *ll_index_to_name(int idx)
105 //static char nbuf[16];
106 return ll_idx_n2a(idx/*, nbuf*/);
110 int ll_index_to_type(int idx)
116 im = find_by_index(idx);
123 unsigned FAST_FUNC ll_index_to_flags(int idx)
129 im = find_by_index(idx);
135 int FAST_FUNC xll_name_to_index(const char *name)
139 /* caching is not warranted - no users which repeatedly call it */
141 static char ncache[16];
149 if (icache && strcmp(name, ncache) == 0) {
154 for (i = 0; i < 16; i++) {
155 for (im = idxmap[i]; im; im = im->next) {
156 if (strcmp(im->name, name) == 0) {
158 strcpy(ncache, name);
166 ret = if_nametoindex(name);
169 bb_error_msg_and_die("can't find device '%s'", name);
173 int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
175 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
176 xrtnl_dump_filter(rth, ll_remember_index, NULL);