]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip/tunnel: Unify local/remote endpoint address printing
authorSerhey Popovych <serhe.popovych@gmail.com>
Wed, 24 Jan 2018 18:56:40 +0000 (20:56 +0200)
committerDavid Ahern <dsahern@gmail.com>
Thu, 25 Jan 2018 17:31:29 +0000 (09:31 -0800)
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>
Signed-off-by: David Ahern <dsahern@gmail.com>
ip/link_gre.c
ip/link_gre6.c
ip/link_ip6tnl.c
ip/link_iptnl.c
ip/link_vti.c
ip/link_vti6.c
ip/tunnel.c
ip/tunnel.h

index d91a435bca74ce5f81c8155a173723592d9fcaa2..8fb8fe5d679294de290d05f1418496f9044d85fc 100644 (file)
@@ -395,8 +395,6 @@ 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;
        __u8 ttl = 0;
@@ -410,23 +408,8 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                return;
        }
 
-       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]);
index b3f070ba63121ca891e1c6fce074b8c19d9fcf2b..4045f656157233f2d214959ace2fdfe2f9d75878 100644 (file)
@@ -429,13 +429,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)
@@ -452,27 +449,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]);
index ce3bcdec4b39fbb1099d00d6125676fd14c2940c..ccc79ff91d6c49ac5ac2efca9212c4e4009055b9 100644 (file)
@@ -366,19 +366,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]);
index d7957637b9e4b267321d828205c4860daa0c143d..622c6f143632f58756c23a00cd1327e9f32ba77f 100644 (file)
@@ -362,8 +362,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;
@@ -393,23 +391,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]);
index f1a1123458c188418dc62250fecade9a8fb126e5..a5b84a0759d127e13256d9db04a1fb1846a64325 100644 (file)
@@ -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]);
index c394dbca62824344d8ed70f3a5a2213b7f565ec5..39d12e63a4cb97b9c7887376df25791354eeab2c 100644 (file)
@@ -171,30 +171,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]);
index 46c9102457791864817c198045ccfba27a48d035..948d5f7c90f6de48d2fe76f8a03a2f2d953e4155 100644 (file)
@@ -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,
index a5c537c1e3a46629e3069fe064c99ad01c42b118..5bd27c324e6efc083d4ddd8026656251803a6116 100644 (file)
@@ -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