diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 01:33:55 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 01:33:55 +0000 |
commit | 9a2d27249cc2235f7e001a9ea8d4605406bc5f38 (patch) | |
tree | b7b2917c3cf46ac3fa25df5f9a27a9a9fbfb0398 /networking/libiproute/ll_map.c | |
parent | 021fa7db9139bff3b4bf404dfd7d2b1541ed71f8 (diff) | |
download | busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.tar.gz busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.tar.bz2 busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.zip |
IP applet by Bastian Blank <waldi@debian.org>
Diffstat (limited to 'networking/libiproute/ll_map.c')
-rw-r--r-- | networking/libiproute/ll_map.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/networking/libiproute/ll_map.c b/networking/libiproute/ll_map.c new file mode 100644 index 000000000..e5a95e6a4 --- /dev/null +++ b/networking/libiproute/ll_map.c | |||
@@ -0,0 +1,169 @@ | |||
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 | |||
25 | struct idxmap | ||
26 | { | ||
27 | struct idxmap * next; | ||
28 | int index; | ||
29 | int type; | ||
30 | int alen; | ||
31 | unsigned flags; | ||
32 | unsigned char addr[8]; | ||
33 | char name[16]; | ||
34 | }; | ||
35 | |||
36 | static struct idxmap *idxmap[16]; | ||
37 | |||
38 | int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | ||
39 | { | ||
40 | int h; | ||
41 | struct ifinfomsg *ifi = NLMSG_DATA(n); | ||
42 | struct idxmap *im, **imp; | ||
43 | struct rtattr *tb[IFLA_MAX+1]; | ||
44 | |||
45 | if (n->nlmsg_type != RTM_NEWLINK) | ||
46 | return 0; | ||
47 | |||
48 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi))) | ||
49 | return -1; | ||
50 | |||
51 | |||
52 | memset(tb, 0, sizeof(tb)); | ||
53 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); | ||
54 | if (tb[IFLA_IFNAME] == NULL) | ||
55 | return 0; | ||
56 | |||
57 | h = ifi->ifi_index&0xF; | ||
58 | |||
59 | for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next) | ||
60 | if (im->index == ifi->ifi_index) | ||
61 | break; | ||
62 | |||
63 | if (im == NULL) { | ||
64 | im = malloc(sizeof(*im)); | ||
65 | if (im == NULL) | ||
66 | return 0; | ||
67 | im->next = *imp; | ||
68 | im->index = ifi->ifi_index; | ||
69 | *imp = im; | ||
70 | } | ||
71 | |||
72 | im->type = ifi->ifi_type; | ||
73 | im->flags = ifi->ifi_flags; | ||
74 | if (tb[IFLA_ADDRESS]) { | ||
75 | int alen; | ||
76 | im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]); | ||
77 | if (alen > sizeof(im->addr)) | ||
78 | alen = sizeof(im->addr); | ||
79 | memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen); | ||
80 | } else { | ||
81 | im->alen = 0; | ||
82 | memset(im->addr, 0, sizeof(im->addr)); | ||
83 | } | ||
84 | strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME])); | ||
85 | return 0; | ||
86 | } | ||
87 | |||
88 | const char *ll_idx_n2a(int idx, char *buf) | ||
89 | { | ||
90 | struct idxmap *im; | ||
91 | |||
92 | if (idx == 0) | ||
93 | return "*"; | ||
94 | for (im = idxmap[idx&0xF]; im; im = im->next) | ||
95 | if (im->index == idx) | ||
96 | return im->name; | ||
97 | snprintf(buf, 16, "if%d", idx); | ||
98 | return buf; | ||
99 | } | ||
100 | |||
101 | |||
102 | const char *ll_index_to_name(int idx) | ||
103 | { | ||
104 | static char nbuf[16]; | ||
105 | |||
106 | return ll_idx_n2a(idx, nbuf); | ||
107 | } | ||
108 | |||
109 | int ll_index_to_type(int idx) | ||
110 | { | ||
111 | struct idxmap *im; | ||
112 | |||
113 | if (idx == 0) | ||
114 | return -1; | ||
115 | for (im = idxmap[idx&0xF]; im; im = im->next) | ||
116 | if (im->index == idx) | ||
117 | return im->type; | ||
118 | return -1; | ||
119 | } | ||
120 | |||
121 | unsigned ll_index_to_flags(int idx) | ||
122 | { | ||
123 | struct idxmap *im; | ||
124 | |||
125 | if (idx == 0) | ||
126 | return 0; | ||
127 | |||
128 | for (im = idxmap[idx&0xF]; im; im = im->next) | ||
129 | if (im->index == idx) | ||
130 | return im->flags; | ||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | int ll_name_to_index(char *name) | ||
135 | { | ||
136 | static char ncache[16]; | ||
137 | static int icache; | ||
138 | struct idxmap *im; | ||
139 | int i; | ||
140 | |||
141 | if (name == NULL) | ||
142 | return 0; | ||
143 | if (icache && strcmp(name, ncache) == 0) | ||
144 | return icache; | ||
145 | for (i=0; i<16; i++) { | ||
146 | for (im = idxmap[i]; im; im = im->next) { | ||
147 | if (strcmp(im->name, name) == 0) { | ||
148 | icache = im->index; | ||
149 | strcpy(ncache, name); | ||
150 | return im->index; | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | return 0; | ||
155 | } | ||
156 | |||
157 | int ll_init_map(struct rtnl_handle *rth) | ||
158 | { | ||
159 | if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { | ||
160 | perror("Cannot send dump request"); | ||
161 | exit(1); | ||
162 | } | ||
163 | |||
164 | if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) { | ||
165 | fprintf(stderr, "Dump terminated\n"); | ||
166 | exit(1); | ||
167 | } | ||
168 | return 0; | ||
169 | } | ||