*
*/
-//#include <sys/socket.h> /* socket() */
#include <net/if.h> /* struct ifreq and co. */
-//#include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */
#include "libbb.h"
#include "libnetlink.h"
#include "ll_map.h"
struct idxmap {
- struct idxmap * next;
- int index;
- int type;
- int alen;
- unsigned flags;
- unsigned char addr[8];
- char name[16];
+ struct idxmap *next;
+ int index;
+ int type;
+ int alen;
+ unsigned flags;
+ unsigned char addr[8];
+ char name[16];
};
static struct idxmap *idxmap[16];
+static struct idxmap *find_by_index(int idx)
+{
+ struct idxmap *im;
+
+ for (im = idxmap[idx & 0xF]; im; im = im->next)
+ if (im->index == idx)
+ return im;
+ return NULL;
+}
+
int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
int h;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
return -1;
-
memset(tb, 0, sizeof(tb));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
if (tb[IFLA_IFNAME] == NULL)
return 0;
- h = ifi->ifi_index&0xF;
+ h = ifi->ifi_index & 0xF;
for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
if (im->index == ifi->ifi_index)
- break;
-
- if (im == NULL) {
- im = xmalloc(sizeof(*im));
- im->next = *imp;
- im->index = ifi->ifi_index;
- *imp = im;
- }
+ goto found;
+ im = xmalloc(sizeof(*im));
+ im->next = *imp;
+ im->index = ifi->ifi_index;
+ *imp = im;
+ found:
im->type = ifi->ifi_type;
im->flags = ifi->ifi_flags;
if (tb[IFLA_ADDRESS]) {
if (idx == 0)
return "*";
- for (im = idxmap[idx & 0xF]; im; im = im->next)
- if (im->index == idx)
- return im->name;
+ im = find_by_index(idx);
+ if (im)
+ return im->name;
snprintf(buf, 16, "if%d", idx);
return buf;
}
return ll_idx_n2a(idx, nbuf);
}
+#ifdef UNUSED
int ll_index_to_type(int idx)
{
struct idxmap *im;
if (idx == 0)
return -1;
- for (im = idxmap[idx & 0xF]; im; im = im->next)
- if (im->index == idx)
- return im->type;
+ im = find_by_index(idx);
+ if (im)
+ return im->type;
return -1;
}
+#endif
unsigned ll_index_to_flags(int idx)
{
if (idx == 0)
return 0;
-
- for (im = idxmap[idx & 0xF]; im; im = im->next)
- if (im->index == idx)
- return im->flags;
+ im = find_by_index(idx);
+ if (im)
+ return im->flags;
return 0;
}
-// TODO: caching is not warranted - no users which repeatedly call it
int xll_name_to_index(const char * const name)
{
+ int ret = 0;
+ int sock_fd;
+
+/* caching is not warranted - no users which repeatedly call it */
+#ifdef UNUSED
static char ncache[16];
static int icache;
struct idxmap *im;
- int sock_fd;
int i;
- int ret = 0;
if (name == NULL)
goto out;
* we explicitely request it (check for dev_load() in net/core/dev.c).
* I can think of other similar scenario, but they are less common...
* Jean II */
+#endif
+
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd) {
struct ifreq ifr;
int tmp;
+
strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_ifindex = -1;
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
* to the reader... Jean II */
ret = ifr.ifr_ifindex;
}
-out:
+/* out:*/
if (ret <= 0)
bb_error_msg_and_die("cannot find device \"%s\"", name);
return ret;
#ifndef __LL_MAP_H__
#define __LL_MAP_H__ 1
-extern int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);
-extern int ll_init_map(struct rtnl_handle *rth);
-extern int xll_name_to_index(const char * const name);
-extern const char *ll_index_to_name(int idx);
-extern const char *ll_idx_n2a(int idx, char *buf);
-extern int ll_index_to_type(int idx);
-extern unsigned ll_index_to_flags(int idx);
+int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg);
+int ll_init_map(struct rtnl_handle *rth);
+int xll_name_to_index(const char * const name);
+const char *ll_index_to_name(int idx);
+const char *ll_idx_n2a(int idx, char *buf);
+/* int ll_index_to_type(int idx); */
+unsigned ll_index_to_flags(int idx);
#endif /* __LL_MAP_H__ */