]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/ll_map.c
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>
23 #include "libnetlink.h"
26 extern unsigned int if_nametoindex (const char *);
30 struct ll_cache
*idx_next
;
36 unsigned char addr
[20];
39 #define IDXMAP_SIZE 1024
40 static struct ll_cache
*idx_head
[IDXMAP_SIZE
];
42 static inline struct ll_cache
*idxhead(int idx
)
44 return idx_head
[idx
& (IDXMAP_SIZE
- 1)];
47 int ll_remember_index(const struct sockaddr_nl
*who
,
48 struct nlmsghdr
*n
, void *arg
)
51 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
52 struct ll_cache
*im
, **imp
;
53 struct rtattr
*tb
[IFLA_MAX
+1];
55 if (n
->nlmsg_type
!= RTM_NEWLINK
)
58 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifi
)))
61 memset(tb
, 0, sizeof(tb
));
62 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), IFLA_PAYLOAD(n
));
63 if (tb
[IFLA_IFNAME
] == NULL
)
66 h
= ifi
->ifi_index
& (IDXMAP_SIZE
- 1);
67 for (imp
= &idx_head
[h
]; (im
=*imp
)!=NULL
; imp
= &im
->idx_next
)
68 if (im
->index
== ifi
->ifi_index
)
72 im
= malloc(sizeof(*im
));
76 im
->index
= ifi
->ifi_index
;
80 im
->type
= ifi
->ifi_type
;
81 im
->flags
= ifi
->ifi_flags
;
82 if (tb
[IFLA_ADDRESS
]) {
84 im
->alen
= alen
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
85 if (alen
> sizeof(im
->addr
))
86 alen
= sizeof(im
->addr
);
87 memcpy(im
->addr
, RTA_DATA(tb
[IFLA_ADDRESS
]), alen
);
90 memset(im
->addr
, 0, sizeof(im
->addr
));
92 strcpy(im
->name
, RTA_DATA(tb
[IFLA_IFNAME
]));
96 const char *ll_idx_n2a(unsigned idx
, char *buf
)
98 const struct ll_cache
*im
;
103 for (im
= idxhead(idx
); im
; im
= im
->idx_next
)
104 if (im
->index
== idx
)
107 snprintf(buf
, IFNAMSIZ
, "if%d", idx
);
112 const char *ll_index_to_name(unsigned idx
)
114 static char nbuf
[IFNAMSIZ
];
116 return ll_idx_n2a(idx
, nbuf
);
119 int ll_index_to_type(unsigned idx
)
121 const struct ll_cache
*im
;
125 for (im
= idxhead(idx
); im
; im
= im
->idx_next
)
126 if (im
->index
== idx
)
131 unsigned ll_index_to_flags(unsigned idx
)
133 const struct ll_cache
*im
;
138 for (im
= idxhead(idx
); im
; im
= im
->idx_next
)
139 if (im
->index
== idx
)
144 unsigned ll_index_to_addr(unsigned idx
, unsigned char *addr
,
147 const struct ll_cache
*im
;
152 for (im
= idxhead(idx
); im
; im
= im
->idx_next
) {
153 if (im
->index
== idx
) {
154 if (alen
> sizeof(im
->addr
))
155 alen
= sizeof(im
->addr
);
158 memcpy(addr
, im
->addr
, alen
);
165 unsigned ll_name_to_index(const char *name
)
167 static char ncache
[IFNAMSIZ
];
176 if (icache
&& strcmp(name
, ncache
) == 0)
179 for (i
=0; i
<IDXMAP_SIZE
; i
++) {
180 for (im
= idx_head
[i
]; im
; im
= im
->idx_next
) {
181 if (strcmp(im
->name
, name
) == 0) {
183 strcpy(ncache
, name
);
189 idx
= if_nametoindex(name
);
191 sscanf(name
, "if%u", &idx
);
195 int ll_init_map(struct rtnl_handle
*rth
)
197 static int initialized
;
202 if (rtnl_wilddump_request(rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
203 perror("Cannot send dump request");
207 if (rtnl_dump_filter(rth
, ll_remember_index
, NULL
, NULL
, NULL
) < 0) {
208 fprintf(stderr
, "Dump terminated\n");