This commit was generated by cvs2svn to compensate for changes in r1650,
[iproute2.git] / lib / ll_map.c
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(const struct sockaddr_nl *who, 
39                       struct nlmsghdr *n, void *arg)
40 {
41         int h;
42         struct ifinfomsg *ifi = NLMSG_DATA(n);
43         struct idxmap *im, **imp;
44         struct rtattr *tb[IFLA_MAX+1];
45
46         if (n->nlmsg_type != RTM_NEWLINK)
47                 return 0;
48
49         if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
50                 return -1;
51
52
53         memset(tb, 0, sizeof(tb));
54         parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
55         if (tb[IFLA_IFNAME] == NULL)
56                 return 0;
57
58         h = ifi->ifi_index&0xF;
59
60         for (imp=&idxmap[h]; (im=*imp)!=NULL; imp = &im->next)
61                 if (im->index == ifi->ifi_index)
62                         break;
63
64         if (im == NULL) {
65                 im = malloc(sizeof(*im));
66                 if (im == NULL)
67                         return 0;
68                 im->next = *imp;
69                 im->index = ifi->ifi_index;
70                 *imp = im;
71         }
72
73         im->type = ifi->ifi_type;
74         im->flags = ifi->ifi_flags;
75         if (tb[IFLA_ADDRESS]) {
76                 int alen;
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);
81         } else {
82                 im->alen = 0;
83                 memset(im->addr, 0, sizeof(im->addr));
84         }
85         strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
86         return 0;
87 }
88
89 const char *ll_idx_n2a(int idx, char *buf)
90 {
91         struct idxmap *im;
92
93         if (idx == 0)
94                 return "*";
95         for (im = idxmap[idx&0xF]; im; im = im->next)
96                 if (im->index == idx)
97                         return im->name;
98         snprintf(buf, 16, "if%d", idx);
99         return buf;
100 }
101
102
103 const char *ll_index_to_name(int idx)
104 {
105         static char nbuf[16];
106
107         return ll_idx_n2a(idx, nbuf);
108 }
109
110 int ll_index_to_type(int idx)
111 {
112         struct idxmap *im;
113
114         if (idx == 0)
115                 return -1;
116         for (im = idxmap[idx&0xF]; im; im = im->next)
117                 if (im->index == idx)
118                         return im->type;
119         return -1;
120 }
121
122 unsigned ll_index_to_flags(int idx)
123 {
124         struct idxmap *im;
125
126         if (idx == 0)
127                 return 0;
128
129         for (im = idxmap[idx&0xF]; im; im = im->next)
130                 if (im->index == idx)
131                         return im->flags;
132         return 0;
133 }
134
135 int ll_name_to_index(const char *name)
136 {
137         static char ncache[16];
138         static int icache;
139         struct idxmap *im;
140         int i;
141
142         if (name == NULL)
143                 return 0;
144         if (icache && strcmp(name, ncache) == 0)
145                 return icache;
146         for (i=0; i<16; i++) {
147                 for (im = idxmap[i]; im; im = im->next) {
148                         if (strcmp(im->name, name) == 0) {
149                                 icache = im->index;
150                                 strcpy(ncache, name);
151                                 return im->index;
152                         }
153                 }
154         }
155         return 0;
156 }
157
158 int ll_init_map(struct rtnl_handle *rth)
159 {
160         if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
161                 perror("Cannot send dump request");
162                 exit(1);
163         }
164
165         if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
166                 fprintf(stderr, "Dump terminated\n");
167                 exit(1);
168         }
169         return 0;
170 }