blob: 46c873faa7df7f87d48760fdd4aabbfb7bd3b9d6 [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * ll_map.c
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include <string.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000015#include <stdlib.h>
16#include <netinet/in.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
18#include "libnetlink.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000019#include "ll_map.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020
21struct idxmap
22{
23 struct idxmap * next;
24 int index;
25 int type;
26 int alen;
27 unsigned flags;
28 unsigned char addr[8];
29 char name[16];
30};
31
32static struct idxmap *idxmap[16];
33
34int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
35{
36 int h;
37 struct ifinfomsg *ifi = NLMSG_DATA(n);
38 struct idxmap *im, **imp;
39 struct rtattr *tb[IFLA_MAX+1];
40
41 if (n->nlmsg_type != RTM_NEWLINK)
42 return 0;
43
44 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
45 return -1;
46
47
48 memset(tb, 0, sizeof(tb));
49 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
50 if (tb[IFLA_IFNAME] == NULL)
51 return 0;
52
53 h = ifi->ifi_index&0xF;
54
55 for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
56 if (im->index == ifi->ifi_index)
57 break;
58
59 if (im == NULL) {
60 im = malloc(sizeof(*im));
61 if (im == NULL)
62 return 0;
63 im->next = *imp;
64 im->index = ifi->ifi_index;
65 *imp = im;
66 }
67
68 im->type = ifi->ifi_type;
69 im->flags = ifi->ifi_flags;
70 if (tb[IFLA_ADDRESS]) {
71 int alen;
72 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
73 if (alen > sizeof(im->addr))
74 alen = sizeof(im->addr);
75 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
76 } else {
77 im->alen = 0;
78 memset(im->addr, 0, sizeof(im->addr));
79 }
80 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
81 return 0;
82}
83
84const char *ll_idx_n2a(int idx, char *buf)
85{
86 struct idxmap *im;
87
88 if (idx == 0)
89 return "*";
90 for (im = idxmap[idx&0xF]; im; im = im->next)
91 if (im->index == idx)
92 return im->name;
93 snprintf(buf, 16, "if%d", idx);
94 return buf;
95}
96
97
98const char *ll_index_to_name(int idx)
99{
100 static char nbuf[16];
101
102 return ll_idx_n2a(idx, nbuf);
103}
104
105int ll_index_to_type(int idx)
106{
107 struct idxmap *im;
108
109 if (idx == 0)
110 return -1;
111 for (im = idxmap[idx&0xF]; im; im = im->next)
112 if (im->index == idx)
113 return im->type;
114 return -1;
115}
116
117unsigned ll_index_to_flags(int idx)
118{
119 struct idxmap *im;
120
121 if (idx == 0)
122 return 0;
123
124 for (im = idxmap[idx&0xF]; im; im = im->next)
125 if (im->index == idx)
126 return im->flags;
127 return 0;
128}
129
130int ll_name_to_index(char *name)
131{
132 static char ncache[16];
133 static int icache;
134 struct idxmap *im;
135 int i;
136
137 if (name == NULL)
138 return 0;
139 if (icache && strcmp(name, ncache) == 0)
140 return icache;
141 for (i=0; i<16; i++) {
142 for (im = idxmap[i]; im; im = im->next) {
143 if (strcmp(im->name, name) == 0) {
144 icache = im->index;
145 strcpy(ncache, name);
146 return im->index;
147 }
148 }
149 }
150 return 0;
151}
152
153int ll_init_map(struct rtnl_handle *rth)
154{
155 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
156 perror("Cannot send dump request");
157 exit(1);
158 }
159
160 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
161 fprintf(stderr, "Dump terminated\n");
162 exit(1);
163 }
164 return 0;
165}