]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
meta: use if_nametoindex and if_indextoname
authorPablo Neira Ayuso <pablo@netfilter.org>
Sun, 16 Jun 2013 22:43:43 +0000 (00:43 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 24 Jun 2013 09:50:00 +0000 (11:50 +0200)
Instead of having a cache of ifindex based on libnl. Those functions
basically use rtnetlink as well to perform the translation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/meta.c

index 54d4d64ef149e57857fa9a33161d81c29aeebc2d..c5719b90cf0a0fb7e944de3221b12fd175381a01 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <net/if.h>
 #include <net/if_arp.h>
 #include <pwd.h>
 #include <grp.h>
@@ -96,43 +97,13 @@ static const struct datatype tchandle_type = {
        .parse          = tchandle_type_parse,
 };
 
-static struct nl_cache *link_cache;
-
-static int link_cache_init(void)
-{
-       struct nl_sock *rt_sock;
-       int err;
-
-       rt_sock = nl_socket_alloc();
-       if (rt_sock == NULL)
-               memory_allocation_error();
-
-       err = nl_connect(rt_sock, NETLINK_ROUTE);
-       if (err < 0)
-               goto err;
-       err = rtnl_link_alloc_cache(rt_sock, &link_cache);
-       if (err < 0)
-               goto err;
-       nl_cache_mngt_provide(link_cache);
-       nl_socket_free(rt_sock);
-       return 0;
-
-err:
-       nl_socket_free(rt_sock);
-       return err;
-}
-
 static void ifindex_type_print(const struct expr *expr)
 {
        char name[IFNAMSIZ];
        int ifindex;
 
-       if (link_cache == NULL)
-               link_cache_init();
-
        ifindex = mpz_get_uint32(expr->value);
-       if (link_cache != NULL &&
-           rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
+       if (if_indextoname(ifindex, name))
                printf("%s", name);
        else
                printf("%d", ifindex);
@@ -141,15 +112,9 @@ static void ifindex_type_print(const struct expr *expr)
 static struct error_record *ifindex_type_parse(const struct expr *sym,
                                               struct expr **res)
 {
-       int ifindex, err;
-
-       if (link_cache == NULL &&
-           (err = link_cache_init()) < 0)
-               return error(&sym->location,
-                            "Could not initialize link cache: %s",
-                            nl_geterror(err));
+       int ifindex;
 
-       ifindex = rtnl_link_name2i(link_cache, sym->identifier);
+       ifindex = if_nametoindex(sym->identifier);
        if (ifindex == 0)
                return error(&sym->location, "Interface does not exist");
 
@@ -159,11 +124,6 @@ static struct error_record *ifindex_type_parse(const struct expr *sym,
        return NULL;
 }
 
-static void __exit ifindex_table_free(void)
-{
-       nl_cache_free(link_cache);
-}
-
 static const struct datatype ifindex_type = {
        .type           = TYPE_IFINDEX,
        .name           = "ifindex",