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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
16 #include "libnetlink.h"
26 unsigned char addr[8];
30 static struct idxmap *idxmap[16];
32 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
35 struct ifinfomsg *ifi = NLMSG_DATA(n);
36 struct idxmap *im, **imp;
37 struct rtattr *tb[IFLA_MAX+1];
39 if (n->nlmsg_type != RTM_NEWLINK)
42 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
46 memset(tb, 0, sizeof(tb));
47 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
48 if (tb[IFLA_IFNAME] == NULL)
51 h = ifi->ifi_index&0xF;
53 for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
54 if (im->index == ifi->ifi_index)
58 im = xmalloc(sizeof(*im));
60 im->index = ifi->ifi_index;
64 im->type = ifi->ifi_type;
65 im->flags = ifi->ifi_flags;
66 if (tb[IFLA_ADDRESS]) {
68 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
69 if (alen > sizeof(im->addr))
70 alen = sizeof(im->addr);
71 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
74 memset(im->addr, 0, sizeof(im->addr));
76 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
80 const char *ll_idx_n2a(int idx, char *buf)
86 for (im = idxmap[idx&0xF]; im; im = im->next)
89 snprintf(buf, 16, "if%d", idx);
94 const char *ll_index_to_name(int idx)
98 return ll_idx_n2a(idx, nbuf);
101 int ll_index_to_type(int idx)
107 for (im = idxmap[idx&0xF]; im; im = im->next)
108 if (im->index == idx)
113 unsigned ll_index_to_flags(int idx)
120 for (im = idxmap[idx&0xF]; im; im = im->next)
121 if (im->index == idx)
126 int ll_name_to_index(char *name)
128 static char ncache[16];
135 if (icache && strcmp(name, ncache) == 0)
137 for (i=0; i<16; i++) {
138 for (im = idxmap[i]; im; im = im->next) {
139 if (strcmp(im->name, name) == 0) {
141 strcpy(ncache, name);
149 int ll_init_map(struct rtnl_handle *rth)
151 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
152 perror("Cannot send dump request");
156 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
157 fprintf(stderr, "Dump terminated\n");