From mboxrd@z Thu Jan 1 00:00:00 1970 From: Serhey Popovych Subject: [PATCH iproute2-next 1/9] ipaddress: Abstract IFA_LABEL matching code Date: Mon, 5 Feb 2018 21:49:26 +0200 Message-ID: <1517860174-18333-2-git-send-email-serhe.popovych@gmail.com> References: <1517860174-18333-1-git-send-email-serhe.popovych@gmail.com> To: netdev@vger.kernel.org Return-path: Received: from mail-lf0-f66.google.com ([209.85.215.66]:39765 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751824AbeBETuM (ORCPT ); Mon, 5 Feb 2018 14:50:12 -0500 Received: by mail-lf0-f66.google.com with SMTP id w27so43442423lfd.6 for ; Mon, 05 Feb 2018 11:50:10 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id h39sm1941284lji.72.2018.02.05.11.50.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 11:50:09 -0800 (PST) In-Reply-To: <1517860174-18333-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: There at least two places in ip/ipaddress.c where we match IFA_LABEL against filter.label if that is given. Get rid of "common" if () statement for inet_addr_match_rta() and ifa_label_match_rta(): it is not common because first will check for filter.pfx.family != AF_UNSPEC inside and second for filter.label being non NULL. This allows us to further simplify down code and prepare for ll_idx_n2a() replacement with ll_index_to_name() without 80 columns checkpatch notice. Signed-off-by: Serhey Popovych --- ip/ipaddress.c | 57 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 4707c2b..38ef5d7 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1470,6 +1470,22 @@ static int get_filter(const char *arg) return 0; } +static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) +{ + const char *label; + SPRINT_BUF(b1); + + if (!filter.label) + return 0; + + if (rta) + label = RTA_DATA(rta); + else + label = ll_idx_n2a(ifindex, b1); + + return fnmatch(filter.label, label, 0); +} + int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { @@ -1508,21 +1524,13 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, return 0; if ((filter.flags ^ ifa_flags) & filter.flagmask) return 0; - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (rta_tb[IFA_LABEL]) - label = RTA_DATA(rta_tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - return 0; - } if (filter.family && filter.family != ifa->ifa_family) return 0; + if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL])) + return 0; + if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL])) return 0; @@ -1878,25 +1886,14 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo) if ((filter.flags ^ ifa_flags) & filter.flagmask) continue; - if (filter.pfx.family || filter.label) { - struct rtattr *rta = - tb[IFA_LOCAL] ? : tb[IFA_ADDRESS]; - - if (inet_addr_match_rta(&filter.pfx, rta)) - continue; - - if (filter.label) { - SPRINT_BUF(b1); - const char *label; - - if (tb[IFA_LABEL]) - label = RTA_DATA(tb[IFA_LABEL]); - else - label = ll_idx_n2a(ifa->ifa_index, b1); - if (fnmatch(filter.label, label, 0) != 0) - continue; - } - } + + if (ifa_label_match_rta(ifa->ifa_index, tb[IFA_LABEL])) + continue; + + if (!tb[IFA_LOCAL]) + tb[IFA_LOCAL] = tb[IFA_ADDRESS]; + if (inet_addr_match_rta(&filter.pfx, tb[IFA_LOCAL])) + continue; ok = 1; break; -- 1.7.10.4