From patchwork Thu Mar 1 11:03:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhey Popovych X-Patchwork-Id: 879725 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="C30nvq3h"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zsV0w0p6wz9s19 for ; Thu, 1 Mar 2018 22:03:44 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967629AbeCALD1 (ORCPT ); Thu, 1 Mar 2018 06:03:27 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:37753 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967435AbeCALDV (ORCPT ); Thu, 1 Mar 2018 06:03:21 -0500 Received: by mail-lf0-f66.google.com with SMTP id y19so8193250lfd.4 for ; Thu, 01 Mar 2018 03:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=VcrTLexQK87RB2z1DtS9QHOWqBaBcfJeP88i18pEShY=; b=C30nvq3h6/SHVgR+wSjk5GdatFJhLMRJhVMpu/bB4D4iE/oCldWYhaaC2yztdh9Dy1 WSay49GL+zGrMwVZFGmkDLs4SRCoFv9XK/swAWIvTfDGlUTTkpy6PybQIcSyjuPTMPwT tlgzFRuWuFeANx0M3XVYOH/y1f1IBQaojrm4hlVd70viwpcUZj1TWfmKDB9sM+prkx65 3dI6KWZoG8VCwRDycw95UmjK9ZZ711YDKtTyd2SIGoUBUUeGxcJDTcUBK0l7r6Jj4F8J YNE80EwvsEdwfFFxfQSA4ywqSLLQUThF7FqimGcFsom7QeLA/YjZkwYnEi9Gd8a3nsSF f7Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=VcrTLexQK87RB2z1DtS9QHOWqBaBcfJeP88i18pEShY=; b=kOH7WbX2ndrTtHaKAYAcxh7xtPHrrkBSkE9nv6unl1metYfe1CwLCEyQz0br4go8We NCHR9jV0ePR/2/+tp/Yvur3ZYaGUTTGZSxKU7pmQWhren7R1UW+84DJQLZUs4fIsbbTZ vbg5ijDDPGPUUhi7lZIKFWC3BTIdVrGD53KleU/ycX6co54VEKmdBNX1vwD61dhoxdOB O/0G9FLO09iHKW6UIG2VNlnpsbdEtun1Dw9dq9remMggM+l06HvpNdiFaoBMqAwp1DQD c8B4Q55jpCPwVpYFpfkK4PBvQox37qKiQxaTwvUnvTs2UBHyuhS19Mp7qshI92nsrsYj mPKw== X-Gm-Message-State: AElRT7EQBCPeKvY2kZ1oTGpgkzBphJgm+JESADVQUffhTN+c1MyVCLAn k+qKHz/ZN6GjKD2oTDY4M4EdQw== X-Google-Smtp-Source: AG47ELt57LqT1HWqsoJPFG1aH6cUAwCNKft3XR+JNPDdV6N3Fwlu328EW1c5Z2D1JtMJPJgSTfQBDg== X-Received: by 10.25.20.195 with SMTP id 64mr1084947lfu.122.1519902199075; Thu, 01 Mar 2018 03:03:19 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id g17sm813846ljf.22.2018.03.01.03.03.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Mar 2018 03:03:17 -0800 (PST) From: Serhey Popovych To: netfilter-devel@vger.kernel.org Subject: [PATCH iptables 3/3] xtables: Introduce and use common function to print val[/mask] arguments Date: Thu, 1 Mar 2018 13:03:12 +0200 Message-Id: <1519902192-17312-4-git-send-email-serhe.popovych@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1519902192-17312-1-git-send-email-serhe.popovych@gmail.com> References: <1519902192-17312-1-git-send-email-serhe.popovych@gmail.com> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org There are number of places where argument is in val[/mask] format printed in extensions and some of them may print corresponding symbolic name. By introducing common function for this task we eliminate custom code parts in extensions to perform printing of arguments in required formats. Use xtables_print_mark_mask() helper for extensions without symbolic name for val[/mask]. Signed-off-by: Serhey Popovych --- extensions/libipt_realm.c | 21 ++------------------- extensions/libxt_connmark.c | 18 ++++++------------ extensions/libxt_devgroup.c | 27 ++++++--------------------- extensions/libxt_mark.c | 17 +++++------------ include/xtables.h | 9 +++++++++ libxtables/xtables.c | 20 ++++++++++++++++++++ 6 files changed, 48 insertions(+), 64 deletions(-) diff --git a/extensions/libipt_realm.c b/extensions/libipt_realm.c index 545b258..e01d048 100644 --- a/extensions/libipt_realm.c +++ b/extensions/libipt_realm.c @@ -47,23 +47,6 @@ static void realm_parse(struct xt_option_call *cb) ri->invert = 1; } -static void -print_realm(unsigned long id, unsigned long mask, int numeric) -{ - const char *name = NULL; - - if (mask != 0xffffffff) - printf(" 0x%lx/0x%lx", id, mask); - else { - if (numeric == 0) - name = xtables_lmap_id2name(realms, id); - if (name) - printf(" %s", name); - else - printf(" 0x%lx", id); - } -} - static void realm_print(const void *ip, const struct xt_entry_match *match, int numeric) { @@ -73,7 +56,7 @@ static void realm_print(const void *ip, const struct xt_entry_match *match, printf(" !"); printf(" realm"); - print_realm(ri->id, ri->mask, numeric); + xtables_print_val_mask(ri->id, ri->mask, numeric ? NULL : realms); } static void realm_save(const void *ip, const struct xt_entry_match *match) @@ -84,7 +67,7 @@ static void realm_save(const void *ip, const struct xt_entry_match *match) printf(" !"); printf(" --realm"); - print_realm(ri->id, ri->mask, 0); + xtables_print_val_mask(ri->id, ri->mask, realms); } static void diff --git a/extensions/libxt_connmark.c b/extensions/libxt_connmark.c index be3499b..cb4264e 100644 --- a/extensions/libxt_connmark.c +++ b/extensions/libxt_connmark.c @@ -69,14 +69,6 @@ static void connmark_parse(struct xt_option_call *cb) markinfo->invert = 1; } -static void print_mark(unsigned int mark, unsigned int mask) -{ - if (mask != 0xffffffffU) - printf(" 0x%x/0x%x", mark, mask); - else - printf(" 0x%x", mark); -} - static void connmark_print(const void *ip, const struct xt_entry_match *match, int numeric) { @@ -85,7 +77,8 @@ connmark_print(const void *ip, const struct xt_entry_match *match, int numeric) printf(" CONNMARK match "); if (info->invert) printf("!"); - print_mark(info->mark, info->mask); + + xtables_print_mark_mask(info->mark, info->mask); } static void @@ -97,7 +90,8 @@ connmark_mt_print(const void *ip, const struct xt_entry_match *match, printf(" connmark match "); if (info->invert) printf("!"); - print_mark(info->mark, info->mask); + + xtables_print_mark_mask(info->mark, info->mask); } static void connmark_save(const void *ip, const struct xt_entry_match *match) @@ -108,7 +102,7 @@ static void connmark_save(const void *ip, const struct xt_entry_match *match) printf(" !"); printf(" --mark"); - print_mark(info->mark, info->mask); + xtables_print_mark_mask(info->mark, info->mask); } static void @@ -120,7 +114,7 @@ connmark_mt_save(const void *ip, const struct xt_entry_match *match) printf(" !"); printf(" --mark"); - print_mark(info->mark, info->mask); + xtables_print_mark_mask(info->mark, info->mask); } static void print_mark_xlate(unsigned int mark, unsigned int mask, diff --git a/extensions/libxt_devgroup.c b/extensions/libxt_devgroup.c index 99a4d50..a88211c 100644 --- a/extensions/libxt_devgroup.c +++ b/extensions/libxt_devgroup.c @@ -61,38 +61,23 @@ static void devgroup_parse(struct xt_option_call *cb) } } -static void -print_devgroup(unsigned int id, unsigned int mask, int numeric) -{ - const char *name = NULL; - - if (mask != 0xffffffff) - printf("0x%x/0x%x", id, mask); - else { - if (numeric == 0) - name = xtables_lmap_id2name(devgroups, id); - if (name) - printf("%s", name); - else - printf("0x%x", id); - } -} - static void devgroup_show(const char *pfx, const struct xt_devgroup_info *info, int numeric) { if (info->flags & XT_DEVGROUP_MATCH_SRC) { if (info->flags & XT_DEVGROUP_INVERT_SRC) printf(" !"); - printf(" %ssrc-group ", pfx); - print_devgroup(info->src_group, info->src_mask, numeric); + printf(" %ssrc-group", pfx); + xtables_print_val_mask(info->src_group, info->src_mask, + numeric ? NULL : devgroups); } if (info->flags & XT_DEVGROUP_MATCH_DST) { if (info->flags & XT_DEVGROUP_INVERT_DST) printf(" !"); - printf(" %sdst-group ", pfx); - print_devgroup(info->dst_group, info->dst_mask, numeric); + printf(" %sdst-group", pfx); + xtables_print_val_mask(info->dst_group, info->dst_mask, + numeric ? NULL : devgroups); } } diff --git a/extensions/libxt_mark.c b/extensions/libxt_mark.c index e1d00de..134ad43 100644 --- a/extensions/libxt_mark.c +++ b/extensions/libxt_mark.c @@ -47,14 +47,6 @@ static void mark_parse(struct xt_option_call *cb) markinfo->mask = cb->val.mask; } -static void print_mark(unsigned int mark, unsigned int mask) -{ - if (mask != 0xffffffffU) - printf(" 0x%x/0x%x", mark, mask); - else - printf(" 0x%x", mark); -} - static void mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric) { @@ -63,7 +55,8 @@ mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric) printf(" mark match"); if (info->invert) printf(" !"); - print_mark(info->mark, info->mask); + + xtables_print_mark_mask(info->mark, info->mask); } static void @@ -76,7 +69,7 @@ mark_print(const void *ip, const struct xt_entry_match *match, int numeric) if (info->invert) printf(" !"); - print_mark(info->mark, info->mask); + xtables_print_mark_mask(info->mark, info->mask); } static void mark_mt_save(const void *ip, const struct xt_entry_match *match) @@ -87,7 +80,7 @@ static void mark_mt_save(const void *ip, const struct xt_entry_match *match) printf(" !"); printf(" --mark"); - print_mark(info->mark, info->mask); + xtables_print_mark_mask(info->mark, info->mask); } static void @@ -99,7 +92,7 @@ mark_save(const void *ip, const struct xt_entry_match *match) printf(" !"); printf(" --mark"); - print_mark(info->mark, info->mask); + xtables_print_mark_mask(info->mark, info->mask); } static void diff --git a/include/xtables.h b/include/xtables.h index 4712948..304828a 100644 --- a/include/xtables.h +++ b/include/xtables.h @@ -548,6 +548,15 @@ static inline void xtables_parse_mark_mask(struct xt_option_call *cb, xtables_parse_val_mask(cb, mark, mask, NULL); } +extern void xtables_print_val_mask(unsigned int val, unsigned int mask, + const struct xtables_lmap *lmap); + +static inline void xtables_print_mark_mask(unsigned int mark, + unsigned int mask) +{ + xtables_print_val_mask(mark, mask, NULL); +} + #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS) # ifdef _INIT # undef _init diff --git a/libxtables/xtables.c b/libxtables/xtables.c index f93e88d..60a0231 100644 --- a/libxtables/xtables.c +++ b/libxtables/xtables.c @@ -2012,6 +2012,26 @@ name2val: cb->ext_name, cb->arg, cb->entry->name); } +void xtables_print_val_mask(unsigned int val, unsigned int mask, + const struct xtables_lmap *lmap) +{ + if (mask != ~0U) { + printf(" 0x%x/0x%x", val, mask); + return; + } + + if (lmap) { + const char *name = xtables_lmap_id2name(lmap, val); + + if (name) { + printf(" %s", name); + return; + } + } + + printf(" 0x%x", val); +} + int kernel_version; void get_kernel_version(void)