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.popov...@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

Reply via email to