From patchwork Fri Jun 21 14:42:17 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 253231 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id EBB902C008C for ; Sat, 22 Jun 2013 00:42:32 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161518Ab3FUOma (ORCPT ); Fri, 21 Jun 2013 10:42:30 -0400 Received: from mail.us.es ([193.147.175.20]:41717 "EHLO mail.us.es" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161506Ab3FUOm3 (ORCPT ); Fri, 21 Jun 2013 10:42:29 -0400 Received: (qmail 716 invoked from network); 21 Jun 2013 16:42:28 +0200 Received: from unknown (HELO us.es) (192.168.2.12) by us.es with SMTP; 21 Jun 2013 16:42:28 +0200 Received: (qmail 2882 invoked by uid 507); 21 Jun 2013 14:42:28 -0000 X-Qmail-Scanner-Diagnostics: from 127.0.0.1 by antivirus2 (envelope-from , uid 501) with qmail-scanner-2.10 (clamdscan: 0.97.8/17391. spamassassin: 3.3.2. Clear:RC:1(127.0.0.1):SA:0(-97.8/7.5):. Processed in 1.74106 secs); 21 Jun 2013 14:42:28 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on antivirus2 X-Spam-Level: X-Spam-Status: No, score=-97.8 required=7.5 tests=BAYES_50,RCVD_IN_PBL, RCVD_IN_RP_RNBL,RDNS_DYNAMIC,USER_IN_WHITELIST autolearn=disabled version=3.3.2 X-Envelope-From: pablo@netfilter.org Received: from unknown (HELO antivirus2) (127.0.0.1) by us.es with SMTP; 21 Jun 2013 14:42:26 -0000 Received: from 192.168.1.13 (192.168.1.13) by antivirus2 (F-Secure/fsigk_smtp/410/antivirus2); Fri, 21 Jun 2013 16:42:26 +0200 (CEST) X-Virus-Status: clean(F-Secure/fsigk_smtp/410/antivirus2) Received: (qmail 23987 invoked from network); 21 Jun 2013 16:42:26 +0200 Received: from 114.233.78.188.dynamic.jazztel.es (HELO localhost.localdomain) (pneira@us.es@188.78.233.114) by us.es with SMTP; 21 Jun 2013 16:42:26 +0200 From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Cc: kaber@trash.net, eric@regit.org, tomasz.bursztyka@linux.intel.com Subject: [PATCH 3/5] meta: use if_nametoindex and if_indextoname Date: Fri, 21 Jun 2013 16:42:17 +0200 Message-Id: <1371825739-3669-3-git-send-email-pablo@netfilter.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1371825739-3669-1-git-send-email-pablo@netfilter.org> References: <1371825739-3669-1-git-send-email-pablo@netfilter.org> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org 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 --- src/meta.c | 48 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/src/meta.c b/src/meta.c index 54d4d64..c5719b9 100644 --- a/src/meta.c +++ b/src/meta.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -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",