From: Stephen Hemminger Subject: [PATCH iproute2-next 7/9] iproute: refactor newdst, gateway and via printing Date: Wed, 7 Feb 2018 09:10:15 -0800 Message-ID: <20180207171017.20558-8-sthemmin@microsoft.com> References: <20180207171017.20558-1-sthemmin@microsoft.com> Cc: netdev@vger.kernel.org, Stephen Hemminger , Stephen Hemminger To: dsahern@gmail.com Return-path: Received: from mail-pl0-f65.google.com ([209.85.160.65]:41757 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754731AbeBGRKa (ORCPT ); Wed, 7 Feb 2018 12:10:30 -0500 Received: by mail-pl0-f65.google.com with SMTP id k8-v6so390713pli.8 for ; Wed, 07 Feb 2018 09:10:30 -0800 (PST) In-Reply-To: <20180207171017.20558-1-sthemmin@microsoft.com> Sender: netdev-owner@vger.kernel.org List-ID: Since these fields are printed in both route and multipath case; avoid duplicating code. Signed-off-by: Stephen Hemminger --- ip/iproute.c | 73 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/ip/iproute.c b/ip/iproute.c index 1635a9e29bcf..74216e73648f 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -460,6 +460,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]; @@ -604,10 +630,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]); @@ -617,18 +642,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]))); @@ -718,24 +737,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