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 <netinet/in.h>
18 #include "libnetlink.h"
27 unsigned char addr[8];
31 static struct idxmap *idxmap[16];
33 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
36 struct ifinfomsg *ifi = NLMSG_DATA(n);
37 struct idxmap *im, **imp;
38 struct rtattr *tb[IFLA_MAX+1];
40 if (n->nlmsg_type != RTM_NEWLINK)
43 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
47 memset(tb, 0, sizeof(tb));
48 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
49 if (tb[IFLA_IFNAME] == NULL)
52 h = ifi->ifi_index&0xF;
54 for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
55 if (im->index == ifi->ifi_index)
59 im = malloc(sizeof(*im));
63 im->index = ifi->ifi_index;
67 im->type = ifi->ifi_type;
68 im->flags = ifi->ifi_flags;
69 if (tb[IFLA_ADDRESS]) {
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);
77 memset(im->addr, 0, sizeof(im->addr));
79 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
83 const char *ll_idx_n2a(int idx, char *buf)
89 for (im = idxmap[idx&0xF]; im; im = im->next)
92 snprintf(buf, 16, "if%d", idx);
97 const char *ll_index_to_name(int idx)
101 return ll_idx_n2a(idx, nbuf);
104 int ll_index_to_type(int idx)
110 for (im = idxmap[idx&0xF]; im; im = im->next)
111 if (im->index == idx)
116 unsigned ll_index_to_flags(int idx)
123 for (im = idxmap[idx&0xF]; im; im = im->next)
124 if (im->index == idx)
129 int ll_name_to_index(char *name)
131 static char ncache[16];
138 if (icache && strcmp(name, ncache) == 0)
140 for (i=0; i<16; i++) {
141 for (im = idxmap[i]; im; im = im->next) {
142 if (strcmp(im->name, name) == 0) {
144 strcpy(ncache, name);
152 int ll_init_map(struct rtnl_handle *rth)
154 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
155 perror("Cannot send dump request");
159 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
160 fprintf(stderr, "Dump terminated\n");