From: Stephen Hemminger <stephen@networkplumber.org>
To: dsahern@gmail.com
Cc: netdev@vger.kernel.org,
Stephen Hemminger <sthemmin@microsoft.com>,
Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC iproute2-next 07/16] iproute2: refactor newdst, gateway and via printing
Date: Thu, 1 Feb 2018 17:19:37 -0800 [thread overview]
Message-ID: <20180202011946.21929-8-sthemmin@microsoft.com> (raw)
In-Reply-To: <20180202011946.21929-1-sthemmin@microsoft.com>
Since these fields are printed in both route and multipath case;
avoid duplicating code.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
ip/iproute.c | 73 +++++++++++++++++++++++++++++++++---------------------------
1 file changed, 40 insertions(+), 33 deletions(-)
diff --git a/ip/iproute.c b/ip/iproute.c
index 7482f04c1852..3f8238b7f457 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -459,6 +459,32 @@ static void print_rta_flow(FILE *fp, const struct rtattr *rta)
rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
}
+static void print_rta_newdst(FILE *fp, const struct rtmsg *r,
+ const struct rtattr *rta)
+{
+ const char *newdst = format_host_rta(r->rtm_family, rta);
+
+ fprintf(fp, "as to %s ", newdst);
+}
+
+static void print_rta_gateway(FILE *fp, const struct rtmsg *r,
+ const struct rtattr *rta)
+{
+ const char *gateway = format_host_rta(r->rtm_family, rta);
+
+ fprintf(fp, "via %s ", gateway);
+}
+
+static void print_rta_via(FILE *fp, const struct rtattr *rta)
+{
+ const struct rtvia *via = RTA_DATA(rta);
+ size_t len = RTA_PAYLOAD(rta);
+
+ fprintf(fp, "via %s %s ",
+ family_name(via->rtvia_family),
+ format_host(via->rtvia_family, len, via->rtvia_addr));
+}
+
static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
{
struct rtattr *mxrta[RTAX_MAX+1];
@@ -603,10 +629,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
} else if (r->rtm_src_len) {
fprintf(fp, "from 0/%u ", r->rtm_src_len);
}
- if (tb[RTA_NEWDST]) {
- fprintf(fp, "as to %s ",
- format_host_rta(r->rtm_family, tb[RTA_NEWDST]));
- }
+
+ if (tb[RTA_NEWDST])
+ print_rta_newdst(fp, r, tb[RTA_NEWDST]);
if (tb[RTA_ENCAP])
lwt_print_encap(fp, tb[RTA_ENCAP_TYPE], tb[RTA_ENCAP]);
@@ -616,18 +641,12 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
}
- if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
- fprintf(fp, "via %s ",
- format_host_rta(r->rtm_family, tb[RTA_GATEWAY]));
- }
- if (tb[RTA_VIA]) {
- size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
- struct rtvia *via = RTA_DATA(tb[RTA_VIA]);
+ if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len)
+ print_rta_gateway(fp, r, tb[RTA_GATEWAY]);
+
+ if (tb[RTA_VIA])
+ print_rta_via(fp, tb[RTA_VIA]);
- fprintf(fp, "via %s %s ",
- family_name(via->rtvia_family),
- format_host(via->rtvia_family, len, via->rtvia_addr));
- }
if (tb[RTA_OIF] && filter.oifmask != -1)
fprintf(fp, "dev %s ", ll_index_to_name(rta_getattr_u32(tb[RTA_OIF])));
@@ -717,24 +736,12 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
lwt_print_encap(fp,
tb[RTA_ENCAP_TYPE],
tb[RTA_ENCAP]);
- if (tb[RTA_NEWDST]) {
- fprintf(fp, "as to %s ",
- format_host_rta(r->rtm_family,
- tb[RTA_NEWDST]));
- }
- if (tb[RTA_GATEWAY]) {
- fprintf(fp, "via %s ",
- format_host_rta(r->rtm_family,
- tb[RTA_GATEWAY]));
- }
- if (tb[RTA_VIA]) {
- size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
- struct rtvia *via = RTA_DATA(tb[RTA_VIA]);
-
- fprintf(fp, "via %s %s ",
- family_name(via->rtvia_family),
- format_host(via->rtvia_family, len, via->rtvia_addr));
- }
+ if (tb[RTA_NEWDST])
+ print_rta_newdst(fp, r, tb[RTA_NEWDST]);
+ if (tb[RTA_GATEWAY])
+ print_rta_gateway(fp, r, tb[RTA_GATEWAY]);
+ if (tb[RTA_VIA])
+ print_rta_via(fp, tb[RTA_VIA]);
if (tb[RTA_FLOW])
print_rta_flow(fp, tb[RTA_FLOW]);
}
--
2.15.1
next prev parent reply other threads:[~2018-02-02 1:20 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-02 1:19 [RFC iproute2-next 00/16] iproute printing enhancements Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 01/16] iproute: refactor printing flags Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 02/16] iproute: make printing icmpv6 a function Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 03/16] iproute: make printing IPv4 cache flags " Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 04/16] iproute: refactor cacheinfo printing Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 05/16] iproute: refactor metrics print Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 06/16] iproute: refactor printing flow info Stephen Hemminger
2018-02-02 1:19 ` Stephen Hemminger [this message]
2018-02-02 1:19 ` [RFC iproute2-next 08/16] iproute: refactor multipath print Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 09/16] iproute: refactor printing of interface Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 10/16] iproute: whitespace fixes Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 11/16] iproute: don't do assignment in condition Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 12/16] iproute: make flush a separate function Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 13/16] iproute: implement JSON and color output Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 14/16] json: make pretty printing optional Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 15/16] man: add documentation for json and pretty flags Stephen Hemminger
2018-02-02 1:19 ` [RFC iproute2-next 16/16] json: fix newline at end of array Stephen Hemminger
2018-02-02 2:59 ` [RFC iproute2-next 00/16] iproute printing enhancements David Ahern
2018-02-02 3:08 ` David Ahern
2018-02-02 22:24 ` Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180202011946.21929-8-sthemmin@microsoft.com \
--to=stephen@networkplumber.org \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=sthemmin@microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.