From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH iproute2-next v2 8/8] ip/tunnel: Unify local/remote endpoint address printing
Date: Tue, 23 Jan 2018 21:19:30 +0200 [thread overview]
Message-ID: <1516735170-20921-9-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1516735170-20921-1-git-send-email-serhe.popovych@gmail.com>
Introduce and use tnl_print_endpoint() helper to print
of tunnel endpoint address.
Note that for AF_INET and AF_INET6 inet_ntop(3) is used
that may return NULL in case of failure and while unlikely
format_host_rta() might return NULL too. Handle this case
when passing local/remote to print_string().
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
---
ip/link_gre.c | 21 ++-------------------
ip/link_gre6.c | 26 ++------------------------
ip/link_ip6tnl.c | 15 ++-------------
ip/link_iptnl.c | 21 ++-------------------
ip/link_vti.c | 21 ++-------------------
ip/link_vti6.c | 21 ++-------------------
ip/tunnel.c | 27 +++++++++++++++++++++++++++
ip/tunnel.h | 2 ++
8 files changed, 41 insertions(+), 113 deletions(-)
diff --git a/ip/link_gre.c b/ip/link_gre.c
index a430ab8..6b2c49d 100644
--- a/ip/link_gre.c
+++ b/ip/link_gre.c
@@ -398,30 +398,13 @@ get_failed:
static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
unsigned int iflags = 0;
unsigned int oflags = 0;
__u8 ttl = 0;
__u8 tos = 0;
- if (tb[IFLA_GRE_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_GRE_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET);
if (tb[IFLA_GRE_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
diff --git a/ip/link_gre6.c b/ip/link_gre6.c
index 5f465fe..78e7a91 100644
--- a/ip/link_gre6.c
+++ b/ip/link_gre6.c
@@ -425,13 +425,10 @@ get_failed:
static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
unsigned int iflags = 0;
unsigned int oflags = 0;
unsigned int flags = 0;
__u32 flowinfo = 0;
- struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT;
__u8 ttl = 0;
if (!tb)
@@ -448,27 +445,8 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
if (tb[IFLA_GRE_FLOWINFO])
flowinfo = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]);
- if (tb[IFLA_GRE_REMOTE]) {
- struct in6_addr addr;
-
- memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr));
-
- if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
- remote = format_host(AF_INET6, sizeof(addr), &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_GRE_LOCAL]) {
- struct in6_addr addr;
-
- memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr));
-
- if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
- local = format_host(AF_INET6, sizeof(addr), &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET6);
if (tb[IFLA_GRE_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index 8f5c9bd..562ef7d 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -364,19 +364,8 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
}
}
- if (tb[IFLA_IPTUN_REMOTE]) {
- print_string(PRINT_ANY,
- "remote",
- "remote %s ",
- rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
- }
-
- if (tb[IFLA_IPTUN_LOCAL]) {
- print_string(PRINT_ANY,
- "local",
- "local %s ",
- rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
- }
+ tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET6);
if (tb[IFLA_IPTUN_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c
index ce3855c..eb0a9f4 100644
--- a/ip/link_iptnl.c
+++ b/ip/link_iptnl.c
@@ -361,8 +361,6 @@ get_failed:
static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
__u16 prefixlen;
__u8 ttl = 0;
__u8 tos = 0;
@@ -390,23 +388,8 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
}
}
- if (tb[IFLA_IPTUN_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_IPTUN_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET);
if (tb[IFLA_IPTUN_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
diff --git a/ip/link_vti.c b/ip/link_vti.c
index 1439e53..f756140 100644
--- a/ip/link_vti.c
+++ b/ip/link_vti.c
@@ -165,30 +165,13 @@ get_failed:
static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
- const char *local = "any";
- const char *remote = "any";
char s2[64];
if (!tb)
return;
- if (tb[IFLA_VTI_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_VTI_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_VTI_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_VTI_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_VTI_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_VTI_LOCAL], AF_INET);
if (tb[IFLA_VTI_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]);
diff --git a/ip/link_vti6.c b/ip/link_vti6.c
index 2a86d59..d786e8c 100644
--- a/ip/link_vti6.c
+++ b/ip/link_vti6.c
@@ -169,30 +169,13 @@ get_failed:
static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
- const char *local = "any";
- const char *remote = "any";
- struct in6_addr saddr;
- struct in6_addr daddr;
char s2[64];
if (!tb)
return;
- if (tb[IFLA_VTI_REMOTE]) {
- memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr));
-
- remote = format_host(AF_INET6, 16, &daddr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_VTI_LOCAL]) {
- memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr));
-
- local = format_host(AF_INET6, 16, &saddr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_VTI_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_VTI_LOCAL], AF_INET6);
if (tb[IFLA_VTI_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]);
diff --git a/ip/tunnel.c b/ip/tunnel.c
index 0414804..39f22df 100644
--- a/ip/tunnel.c
+++ b/ip/tunnel.c
@@ -280,6 +280,33 @@ void tnl_print_encap(struct rtattr *tb[],
}
}
+void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
+{
+ const char *value;
+ inet_prefix dst;
+
+ if (!rta) {
+ value = "any";
+ } else if (get_addr_rta(&dst, rta, family)) {
+ value = "unknown";
+ } else if (dst.flags & ADDRTYPE_UNSPEC) {
+ value = "any";
+ } else {
+ value = format_host(family, dst.bytelen, dst.data);
+ if (!value)
+ value = "unknown";
+ }
+
+ if (is_json_context()) {
+ print_string(PRINT_JSON, name, NULL, value);
+ } else {
+ SPRINT_BUF(b1);
+
+ snprintf(b1, sizeof(b1), "%s %%s ", name);
+ print_string(PRINT_FP, NULL, b1, value);
+ }
+}
+
/* tnl_print_stats - print tunnel statistics
*
* @buf - tunnel interface's line in /proc/net/dev,
diff --git a/ip/tunnel.h b/ip/tunnel.h
index a5c537c..5bd27c3 100644
--- a/ip/tunnel.h
+++ b/ip/tunnel.h
@@ -37,6 +37,8 @@ __be32 tnl_parse_key(const char *name, const char *key);
void tnl_print_encap(struct rtattr *tb[],
int encap_type, int encap_flags,
int encap_sport, int encap_dport);
+void tnl_print_endpoint(const char *name,
+ const struct rtattr *rta, int family);
void tnl_print_stats(const char *buf);
#endif
--
1.7.10.4
next prev parent reply other threads:[~2018-01-23 19:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-23 19:19 [PATCH iproute2-next v2 0/8] ip: Introduce and use get_addr_rta()/inet_addr_match_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 1/8] utils: Introduce get_addr_rta() and inet_addr_match_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 2/8] ipaddress: Use inet_addr_match_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 3/8] iprule: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 4/8] ipmroute: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 5/8] ipneigh: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 6/8] ipl2tp: Use get_addr_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 7/8] tcp_metric: " Serhey Popovych
2018-01-23 19:19 ` Serhey Popovych [this message]
2018-01-24 18:40 ` [PATCH iproute2-next v2 8/8] ip/tunnel: Unify local/remote endpoint address printing David Ahern
2018-01-24 3:45 ` [PATCH iproute2-next v2 0/8] ip: Introduce and use get_addr_rta()/inet_addr_match_rta() David Ahern
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=1516735170-20921-9-git-send-email-serhe.popovych@gmail.com \
--to=serhe.popovych@gmail.com \
--cc=netdev@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).