]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
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> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <netinet/in.h> | |
20 | #include <string.h> | |
21 | ||
22 | #include "libnetlink.h" | |
23 | #include "ll_map.h" | |
24 | ||
dcb283c3 TG |
25 | extern unsigned int if_nametoindex (const char *); |
26 | ||
aba5acdf SH |
27 | struct idxmap |
28 | { | |
29 | struct idxmap * next; | |
99f830de | 30 | unsigned index; |
aba5acdf SH |
31 | int type; |
32 | int alen; | |
33 | unsigned flags; | |
e03dcc04 | 34 | unsigned char addr[20]; |
aba5acdf SH |
35 | char name[16]; |
36 | }; | |
37 | ||
1e21ea71 SH |
38 | #define IDXMAP_SIZE 1024 |
39 | static struct idxmap *idxmap[IDXMAP_SIZE]; | |
aba5acdf | 40 | |
ae665a52 | 41 | int ll_remember_index(const struct sockaddr_nl *who, |
50772dc5 | 42 | struct nlmsghdr *n, void *arg) |
aba5acdf SH |
43 | { |
44 | int h; | |
45 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
46 | struct idxmap *im, **imp; | |
47 | struct rtattr *tb[IFLA_MAX+1]; | |
48 | ||
49 | if (n->nlmsg_type != RTM_NEWLINK) | |
50 | return 0; | |
51 | ||
52 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi))) | |
53 | return -1; | |
54 | ||
aba5acdf SH |
55 | memset(tb, 0, sizeof(tb)); |
56 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); | |
57 | if (tb[IFLA_IFNAME] == NULL) | |
58 | return 0; | |
59 | ||
1e21ea71 SH |
60 | h = ifi->ifi_index & (IDXMAP_SIZE - 1); |
61 | for (imp = &idxmap[h]; (im=*imp)!=NULL; imp = &im->next) | |
aba5acdf SH |
62 | if (im->index == ifi->ifi_index) |
63 | break; | |
64 | ||
65 | if (im == NULL) { | |
66 | im = malloc(sizeof(*im)); | |
67 | if (im == NULL) | |
68 | return 0; | |
69 | im->next = *imp; | |
70 | im->index = ifi->ifi_index; | |
71 | *imp = im; | |
72 | } | |
73 | ||
74 | im->type = ifi->ifi_type; | |
75 | im->flags = ifi->ifi_flags; | |
76 | if (tb[IFLA_ADDRESS]) { | |
77 | int alen; | |
78 | im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]); | |
79 | if (alen > sizeof(im->addr)) | |
80 | alen = sizeof(im->addr); | |
81 | memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen); | |
82 | } else { | |
83 | im->alen = 0; | |
84 | memset(im->addr, 0, sizeof(im->addr)); | |
85 | } | |
86 | strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME])); | |
87 | return 0; | |
88 | } | |
89 | ||
99f830de | 90 | const char *ll_idx_n2a(unsigned idx, char *buf) |
aba5acdf SH |
91 | { |
92 | struct idxmap *im; | |
93 | ||
94 | if (idx == 0) | |
95 | return "*"; | |
1e21ea71 SH |
96 | |
97 | for (im = idxmap[idx & (IDXMAP_SIZE - 1)]; im; im = im->next) | |
aba5acdf SH |
98 | if (im->index == idx) |
99 | return im->name; | |
100 | snprintf(buf, 16, "if%d", idx); | |
101 | return buf; | |
102 | } | |
103 | ||
104 | ||
99f830de | 105 | const char *ll_index_to_name(unsigned idx) |
aba5acdf SH |
106 | { |
107 | static char nbuf[16]; | |
108 | ||
109 | return ll_idx_n2a(idx, nbuf); | |
110 | } | |
111 | ||
99f830de | 112 | int ll_index_to_type(unsigned idx) |
aba5acdf SH |
113 | { |
114 | struct idxmap *im; | |
115 | ||
116 | if (idx == 0) | |
117 | return -1; | |
118 | for (im = idxmap[idx&0xF]; im; im = im->next) | |
119 | if (im->index == idx) | |
120 | return im->type; | |
121 | return -1; | |
122 | } | |
123 | ||
99f830de | 124 | unsigned ll_index_to_flags(unsigned idx) |
aba5acdf SH |
125 | { |
126 | struct idxmap *im; | |
127 | ||
128 | if (idx == 0) | |
129 | return 0; | |
130 | ||
131 | for (im = idxmap[idx&0xF]; im; im = im->next) | |
132 | if (im->index == idx) | |
133 | return im->flags; | |
134 | return 0; | |
135 | } | |
136 | ||
ee7ba987 DW |
137 | unsigned ll_index_to_addr(unsigned idx, unsigned char *addr, |
138 | unsigned alen) | |
139 | { | |
140 | struct idxmap *im; | |
141 | ||
142 | if (idx == 0) | |
143 | return 0; | |
144 | ||
145 | for (im = idxmap[idx&0xF]; im; im = im->next) { | |
146 | if (im->index == idx) { | |
147 | if (alen > sizeof(im->addr)) | |
148 | alen = sizeof(im->addr); | |
149 | if (alen > im->alen) | |
150 | alen = im->alen; | |
151 | memcpy(addr, im->addr, alen); | |
152 | return alen; | |
153 | } | |
154 | } | |
155 | return 0; | |
156 | } | |
157 | ||
99f830de | 158 | unsigned ll_name_to_index(const char *name) |
aba5acdf SH |
159 | { |
160 | static char ncache[16]; | |
161 | static int icache; | |
162 | struct idxmap *im; | |
163 | int i; | |
24abb62e | 164 | unsigned idx; |
aba5acdf SH |
165 | |
166 | if (name == NULL) | |
167 | return 0; | |
168 | if (icache && strcmp(name, ncache) == 0) | |
169 | return icache; | |
170 | for (i=0; i<16; i++) { | |
171 | for (im = idxmap[i]; im; im = im->next) { | |
172 | if (strcmp(im->name, name) == 0) { | |
173 | icache = im->index; | |
174 | strcpy(ncache, name); | |
175 | return im->index; | |
176 | } | |
177 | } | |
178 | } | |
99f830de | 179 | |
24abb62e FW |
180 | idx = if_nametoindex(name); |
181 | if (idx == 0) | |
182 | sscanf(name, "if%u", &idx); | |
183 | return idx; | |
aba5acdf SH |
184 | } |
185 | ||
186 | int ll_init_map(struct rtnl_handle *rth) | |
187 | { | |
188 | if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { | |
189 | perror("Cannot send dump request"); | |
190 | exit(1); | |
191 | } | |
192 | ||
193 | if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) { | |
194 | fprintf(stderr, "Dump terminated\n"); | |
195 | exit(1); | |
196 | } | |
197 | return 0; | |
198 | } |