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>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
22 #include "libnetlink.h"
32 unsigned char addr[8];
36 static struct idxmap *idxmap[16];
38 int ll_remember_index(const struct sockaddr_nl *who,
39 struct nlmsghdr *n, void *arg)
42 struct ifinfomsg *ifi = NLMSG_DATA(n);
43 struct idxmap *im, **imp;
44 struct rtattr *tb[IFLA_MAX+1];
46 if (n->nlmsg_type != RTM_NEWLINK)
49 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
53 memset(tb, 0, sizeof(tb));
54 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
55 if (tb[IFLA_IFNAME] == NULL)
58 h = ifi->ifi_index&0xF;
60 for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
61 if (im->index == ifi->ifi_index)
65 im = malloc(sizeof(*im));
69 im->index = ifi->ifi_index;
73 im->type = ifi->ifi_type;
74 im->flags = ifi->ifi_flags;
75 if (tb[IFLA_ADDRESS]) {
77 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
78 if (alen > sizeof(im->addr))
79 alen = sizeof(im->addr);
80 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
83 memset(im->addr, 0, sizeof(im->addr));
85 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
89 const char *ll_idx_n2a(int idx, char *buf)
95 for (im = idxmap[idx&0xF]; im; im = im->next)
98 snprintf(buf, 16, "if%d", idx);
103 const char *ll_index_to_name(int idx)
105 static char nbuf[16];
107 return ll_idx_n2a(idx, nbuf);
110 int ll_index_to_type(int idx)
116 for (im = idxmap[idx&0xF]; im; im = im->next)
117 if (im->index == idx)
122 unsigned ll_index_to_flags(int idx)
129 for (im = idxmap[idx&0xF]; im; im = im->next)
130 if (im->index == idx)
135 int ll_name_to_index(const char *name)
137 static char ncache[16];
144 if (icache && strcmp(name, ncache) == 0)
146 for (i=0; i<16; i++) {
147 for (im = idxmap[i]; im; im = im->next) {
148 if (strcmp(im->name, name) == 0) {
150 strcpy(ncache, name);
158 int ll_init_map(struct rtnl_handle *rth)
160 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
161 perror("Cannot send dump request");
165 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
166 fprintf(stderr, "Dump terminated\n");