All of lore.kernel.org
 help / color / mirror / Atom feed
* [iproute PATCH 0/7] Refactor some internal library functions
@ 2016-03-22 18:35 Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR Phil Sutter
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

The following series is a result of reviewing color output support and
some formatting helpers usually used with struct rtattr fields.

Phil Sutter (7):
  color: introduce color helpers and COLOR_CLEAR
  ipaddress: colorize peer, broadcast and anycast addresses as well
  make format_host non-reentrant by default
  utils: make rt_addr_n2a() non-reentrant by default
  lib/utils: introduce format_host_rta()
  lib/utils: introduce rt_addr_n2a_rta()
  lib/ll_addr: improve ll_addr_n2a() a bit

 bridge/fdb.c          |  4 +--
 include/color.h       |  5 ++-
 include/utils.h       | 10 ++++--
 ip/ip6tunnel.c        |  4 +--
 ip/ipaddress.c        | 87 ++++++++++++++++++---------------------------------
 ip/ipaddrlabel.c      |  7 ++---
 ip/iplink_bond.c      |  7 +----
 ip/iplink_geneve.c    |  5 ++-
 ip/iplink_vxlan.c     | 13 ++++----
 ip/ipmaddr.c          |  6 +---
 ip/ipmroute.c         | 11 ++-----
 ip/ipneigh.c          |  6 +---
 ip/ipprefix.c         | 17 ++--------
 ip/iproute.c          | 68 ++++++++++++----------------------------
 ip/iproute_lwtunnel.c | 29 ++++-------------
 ip/iprule.c           | 39 +++++++----------------
 ip/iptoken.c          | 11 ++-----
 ip/iptunnel.c         |  8 ++---
 ip/ipxfrm.c           | 29 ++++-------------
 ip/link_gre.c         |  5 ++-
 ip/link_gre6.c        |  5 ++-
 ip/link_ip6tnl.c      | 11 ++-----
 ip/link_iptnl.c       |  7 ++---
 ip/link_vti.c         |  5 ++-
 ip/link_vti6.c        |  5 ++-
 ip/tcp_metrics.c      |  6 ++--
 ip/xfrm_monitor.c     | 16 ++--------
 lib/color.c           | 30 +++++++++++++++++-
 lib/ll_addr.c         | 15 ++-------
 lib/utils.c           | 20 ++++++++++--
 misc/ss.c             |  4 +--
 tc/f_flower.c         | 11 ++-----
 tc/m_nat.c            |  4 +--
 33 files changed, 187 insertions(+), 323 deletions(-)

-- 
2.7.2

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well Phil Sutter
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

This adds two helper functions which map a given data field to a color,
so color_fprintf() statements don't have to be duplicated with only a
different color value depending on that data field's value. In order for
this to work in a generic way, COLOR_CLEAR has been added to serve as a
fallback default of uncolored output.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/color.h |  5 ++++-
 ip/ipaddress.c  | 47 +++++++++++++----------------------------------
 lib/color.c     | 30 +++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/include/color.h b/include/color.h
index b85003aed19f8..c1c29831159af 100644
--- a/include/color.h
+++ b/include/color.h
@@ -7,10 +7,13 @@ enum color_attr {
 	COLOR_INET,
 	COLOR_INET6,
 	COLOR_OPERSTATE_UP,
-	COLOR_OPERSTATE_DOWN
+	COLOR_OPERSTATE_DOWN,
+	COLOR_CLEAR
 };
 
 void enable_color(void);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
+enum color_attr ifa_family_color(__u8 ifa_family);
+enum color_attr oper_state_color(__u8 state);
 
 #endif
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index f8c5029400949..7aab8e781eae8 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -135,25 +135,15 @@ static const char *oper_states[] = {
 
 static void print_operstate(FILE *f, __u8 state)
 {
-	if (state >= ARRAY_SIZE(oper_states))
+	if (state >= ARRAY_SIZE(oper_states)) {
 		fprintf(f, "state %#x ", state);
-	else {
-		if (brief) {
-			if (strcmp(oper_states[state], "UP") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_UP, "%-14s ", oper_states[state]);
-			else if (strcmp(oper_states[state], "DOWN") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_DOWN, "%-14s ", oper_states[state]);
-			else
-				fprintf(f, "%-14s ", oper_states[state]);
-		} else {
-			fprintf(f, "state ");
-			if (strcmp(oper_states[state], "UP") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_UP, "%s ", oper_states[state]);
-			else if (strcmp(oper_states[state], "DOWN") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_DOWN, "%s ", oper_states[state]);
-			else
-				fprintf(f, "%s ", oper_states[state]);
-		}
+	} else if (brief) {
+		color_fprintf(f, oper_state_color(state),
+		              "%-14s ", oper_states[state]);
+	} else {
+		fprintf(f, "state ");
+		color_fprintf(f, oper_state_color(state),
+		              "%s ", oper_states[state]);
 	}
 }
 
@@ -1067,22 +1057,11 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 	}
 
 	if (rta_tb[IFA_LOCAL]) {
-		if (ifa->ifa_family == AF_INET)
-			color_fprintf(fp, COLOR_INET, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-		else if (ifa->ifa_family == AF_INET6)
-			color_fprintf(fp, COLOR_INET6, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-		else
-			fprintf(fp, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-
+		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
+		              format_host(ifa->ifa_family,
+		                          RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
+		                          RTA_DATA(rta_tb[IFA_LOCAL]),
+		                          abuf, sizeof(abuf)));
 		if (rta_tb[IFA_ADDRESS] == NULL ||
 		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
 			   ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
diff --git a/lib/color.c b/lib/color.c
index 8c9a48ba702bf..95596be236a05 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -1,5 +1,8 @@
 #include <stdio.h>
 #include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <linux/if.h>
 
 #include "color.h"
 
@@ -32,7 +35,8 @@ static enum color attr_colors[] = {
 	C_MAGENTA,
 	C_BLUE,
 	C_GREEN,
-	C_RED
+	C_RED,
+	C_CLEAR
 };
 
 static int color_is_enabled;
@@ -62,3 +66,27 @@ end:
 	va_end(args);
 	return ret;
 }
+
+enum color_attr ifa_family_color(__u8 ifa_family)
+{
+	switch (ifa_family) {
+	case AF_INET:
+		return COLOR_INET;
+	case AF_INET6:
+		return COLOR_INET6;
+	default:
+		return COLOR_CLEAR;
+	}
+}
+
+enum color_attr oper_state_color(__u8 state)
+{
+	switch (state) {
+	case IF_OPER_UP:
+		return COLOR_OPERSTATE_UP;
+	case IF_OPER_DOWN:
+		return COLOR_OPERSTATE_DOWN;
+	default:
+		return COLOR_CLEAR;
+	}
+}
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 3/7] make format_host non-reentrant by default Phil Sutter
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 ip/ipaddress.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 7aab8e781eae8..90d7b1096c3aa 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -862,7 +862,8 @@ int print_linkinfo(const struct sockaddr_nl *who,
 				fprintf(fp, " peer ");
 			else
 				fprintf(fp, " brd ");
-			fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
+			color_fprintf(fp, COLOR_MAC, "%s",
+					ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
 						      RTA_PAYLOAD(tb[IFLA_BROADCAST]),
 						      ifi->ifi_type,
 						      b1, sizeof(b1)));
@@ -1062,32 +1063,34 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 		                          RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
 		                          RTA_DATA(rta_tb[IFA_LOCAL]),
 		                          abuf, sizeof(abuf)));
-		if (rta_tb[IFA_ADDRESS] == NULL ||
-		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
-			   ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
-			fprintf(fp, "/%d ", ifa->ifa_prefixlen);
-		} else {
-			fprintf(fp, " peer %s/%d ",
-				format_host(ifa->ifa_family,
-					    RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
-					    RTA_DATA(rta_tb[IFA_ADDRESS]),
-					    abuf, sizeof(abuf)),
-				ifa->ifa_prefixlen);
+		if (rta_tb[IFA_ADDRESS] &&
+		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
+		           RTA_DATA(rta_tb[IFA_LOCAL]),
+		           ifa->ifa_family == AF_INET ? 4 : 16)) {
+			fprintf(fp, " peer ");
+			color_fprintf(fp, ifa_family_color(ifa->ifa_family),
+			              "%s", format_host(ifa->ifa_family,
+			              RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
+			              RTA_DATA(rta_tb[IFA_ADDRESS]),
+			              abuf, sizeof(abuf)));
 		}
+		fprintf(fp, "/%d ", ifa->ifa_prefixlen);
 	}
 
 	if (brief)
 		goto brief_exit;
 
 	if (rta_tb[IFA_BROADCAST]) {
-		fprintf(fp, "brd %s ",
+		fprintf(fp, "brd ");
+		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
 			format_host(ifa->ifa_family,
 				    RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
 				    RTA_DATA(rta_tb[IFA_BROADCAST]),
 				    abuf, sizeof(abuf)));
 	}
 	if (rta_tb[IFA_ANYCAST]) {
-		fprintf(fp, "any %s ",
+		fprintf(fp, "any ");
+		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
 			format_host(ifa->ifa_family,
 				    RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
 				    RTA_DATA(rta_tb[IFA_ANYCAST]),
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 3/7] make format_host non-reentrant by default
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 4/7] utils: make rt_addr_n2a() " Phil Sutter
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

There are only three users which require it to be reentrant, the rest is
fine without. Instead, provide a reentrant format_host_r() for users
which need it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 bridge/fdb.c          |  4 +---
 include/utils.h       |  3 ++-
 ip/ip6tunnel.c        |  2 +-
 ip/ipaddress.c        | 13 ++++---------
 ip/ipaddrlabel.c      |  4 +---
 ip/iplink_geneve.c    |  5 ++---
 ip/iplink_vxlan.c     | 13 ++++++-------
 ip/ipmaddr.c          |  6 +-----
 ip/ipneigh.c          |  4 +---
 ip/iproute.c          | 21 +++++++--------------
 ip/iproute_lwtunnel.c |  4 +---
 ip/iprule.c           |  9 +++------
 ip/iptoken.c          |  4 +---
 ip/iptunnel.c         |  6 +++---
 ip/link_gre.c         |  5 ++---
 ip/link_gre6.c        |  5 ++---
 ip/link_iptnl.c       |  7 +++----
 ip/link_vti.c         |  5 ++---
 ip/link_vti6.c        |  5 ++---
 ip/tcp_metrics.c      |  6 ++----
 lib/utils.c           |  9 ++++++++-
 misc/ss.c             |  4 ++--
 tc/m_nat.c            |  4 ++--
 23 files changed, 59 insertions(+), 89 deletions(-)

diff --git a/bridge/fdb.c b/bridge/fdb.c
index 88f1b63c233e9..e8c314a3c0771 100644
--- a/bridge/fdb.c
+++ b/bridge/fdb.c
@@ -104,7 +104,6 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex));
 
 	if (tb[NDA_DST]) {
-		SPRINT_BUF(abuf);
 		int family = AF_INET;
 
 		if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr))
@@ -113,8 +112,7 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, "dst %s ",
 			format_host(family,
 				    RTA_PAYLOAD(tb[NDA_DST]),
-				    RTA_DATA(tb[NDA_DST]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[NDA_DST])));
 	}
 
 	if (tb[NDA_VLAN]) {
diff --git a/include/utils.h b/include/utils.h
index c43427c35a6cc..84083b0dbba71 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -122,8 +122,9 @@ int addr64_n2a(__u64 addr, char *buff, size_t len);
 int af_bit_len(int af);
 int af_byte_len(int af);
 
-const char *format_host(int af, int len, const void *addr,
+const char *format_host_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
+const char *format_host(int af, int lne, const void *addr);
 const char *rt_addr_n2a(int af, int len, const void *addr,
 			       char *buf, int buflen);
 
diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c
index 2e9d3ed40d003..d588645eb942a 100644
--- a/ip/ip6tunnel.c
+++ b/ip/ip6tunnel.c
@@ -77,7 +77,7 @@ static void print_tunnel(struct ip6_tnl_parm2 *p)
 	printf("%s: %s/ipv6 remote %s local %s",
 	       p->name,
 	       tnl_strproto(p->proto),
-	       format_host(AF_INET6, 16, &p->raddr, s1, sizeof(s1)),
+	       format_host_r(AF_INET6, 16, &p->raddr, s1, sizeof(s1)),
 	       rt_addr_n2a(AF_INET6, 16, &p->laddr, s2, sizeof(s2)));
 	if (p->link) {
 		const char *n = ll_index_to_name(p->link);
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 90d7b1096c3aa..03c8c03cd4a17 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -964,7 +964,6 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 	/* Use local copy of ifa_flags to not interfere with filtering code */
 	unsigned int ifa_flags;
 	struct rtattr *rta_tb[IFA_MAX+1];
-	char abuf[256];
 
 	SPRINT_BUF(b1);
 
@@ -1061,8 +1060,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
 		              format_host(ifa->ifa_family,
 		                          RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-		                          RTA_DATA(rta_tb[IFA_LOCAL]),
-		                          abuf, sizeof(abuf)));
+		                          RTA_DATA(rta_tb[IFA_LOCAL])));
 		if (rta_tb[IFA_ADDRESS] &&
 		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
 		           RTA_DATA(rta_tb[IFA_LOCAL]),
@@ -1071,8 +1069,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 			color_fprintf(fp, ifa_family_color(ifa->ifa_family),
 			              "%s", format_host(ifa->ifa_family,
 			              RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
-			              RTA_DATA(rta_tb[IFA_ADDRESS]),
-			              abuf, sizeof(abuf)));
+			              RTA_DATA(rta_tb[IFA_ADDRESS])));
 		}
 		fprintf(fp, "/%d ", ifa->ifa_prefixlen);
 	}
@@ -1085,16 +1082,14 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
 			format_host(ifa->ifa_family,
 				    RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
-				    RTA_DATA(rta_tb[IFA_BROADCAST]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(rta_tb[IFA_BROADCAST])));
 	}
 	if (rta_tb[IFA_ANYCAST]) {
 		fprintf(fp, "any ");
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
 			format_host(ifa->ifa_family,
 				    RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
-				    RTA_DATA(rta_tb[IFA_ANYCAST]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(rta_tb[IFA_ANYCAST])));
 	}
 	fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
 	if (ifa_flags & IFA_F_SECONDARY) {
diff --git a/ip/ipaddrlabel.c b/ip/ipaddrlabel.c
index 05c44978ff37a..6076bb952297f 100644
--- a/ip/ipaddrlabel.c
+++ b/ip/ipaddrlabel.c
@@ -60,7 +60,6 @@ int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
 	struct ifaddrlblmsg *ifal = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[IFAL_MAX+1];
-	char abuf[256];
 
 	if (n->nlmsg_type != RTM_NEWADDRLABEL && n->nlmsg_type != RTM_DELADDRLABEL)
 		return 0;
@@ -78,8 +77,7 @@ int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
 		fprintf(fp, "prefix %s/%u ",
 			format_host(ifal->ifal_family,
 				    RTA_PAYLOAD(tb[IFAL_ADDRESS]),
-				    RTA_DATA(tb[IFAL_ADDRESS]),
-				    abuf, sizeof(abuf)),
+				    RTA_DATA(tb[IFAL_ADDRESS])),
 			ifal->ifal_prefixlen);
 	}
 
diff --git a/ip/iplink_geneve.c b/ip/iplink_geneve.c
index 40c08ec270d15..c6e7bbe0ce20e 100644
--- a/ip/iplink_geneve.c
+++ b/ip/iplink_geneve.c
@@ -138,7 +138,6 @@ static int geneve_parse_opt(struct link_util *lu, int argc, char **argv,
 static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
 	__u32 vni;
-	char s1[1024];
 	__u8 tos;
 
 	if (!tb)
@@ -156,7 +155,7 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 
 		if (addr)
 			fprintf(f, "remote %s ",
-				format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
+				format_host(AF_INET, 4, &addr));
 	} else if (tb[IFLA_GENEVE_REMOTE6]) {
 		struct in6_addr addr;
 
@@ -164,7 +163,7 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
 			if (IN6_IS_ADDR_MULTICAST(&addr))
 				fprintf(f, "remote %s ",
-					format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
+					format_host(AF_INET6, sizeof(struct in6_addr), &addr));
 		}
 	}
 
diff --git a/ip/iplink_vxlan.c b/ip/iplink_vxlan.c
index 5c23cf534ce81..543522e139ff4 100644
--- a/ip/iplink_vxlan.c
+++ b/ip/iplink_vxlan.c
@@ -310,7 +310,6 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 	unsigned int link;
 	__u8 tos;
 	__u32 maxaddr;
-	char s1[1024];
 	char s2[64];
 
 	if (!tb)
@@ -329,10 +328,10 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		if (addr) {
 			if (IN_MULTICAST(ntohl(addr)))
 				fprintf(f, "group %s ",
-					format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
+					format_host(AF_INET, 4, &addr));
 			else
 				fprintf(f, "remote %s ",
-					format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
+					format_host(AF_INET, 4, &addr));
 		}
 	} else if (tb[IFLA_VXLAN_GROUP6]) {
 		struct in6_addr addr;
@@ -341,10 +340,10 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
 			if (IN6_IS_ADDR_MULTICAST(&addr))
 				fprintf(f, "group %s ",
-					format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
+					format_host(AF_INET6, sizeof(struct in6_addr), &addr));
 			else
 				fprintf(f, "remote %s ",
-					format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
+					format_host(AF_INET6, sizeof(struct in6_addr), &addr));
 		}
 	}
 
@@ -353,14 +352,14 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 
 		if (addr)
 			fprintf(f, "local %s ",
-				format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
+				format_host(AF_INET, 4, &addr));
 	} else if (tb[IFLA_VXLAN_LOCAL6]) {
 		struct in6_addr addr;
 
 		memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
 		if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
 			fprintf(f, "local %s ",
-				format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
+				format_host(AF_INET6, sizeof(struct in6_addr), &addr));
 	}
 
 	if (tb[IFLA_VXLAN_LINK] &&
diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
index 347990b5c8f52..c3673979f9761 100644
--- a/ip/ipmaddr.c
+++ b/ip/ipmaddr.c
@@ -204,8 +204,6 @@ static void print_maddr(FILE *fp, struct ma_info *list)
 						    list->addr.bytelen, 0,
 						    b1, sizeof(b1)));
 	} else {
-		char abuf[256];
-
 		switch (list->addr.family) {
 		case AF_INET:
 			fprintf(fp, "inet  ");
@@ -219,9 +217,7 @@ static void print_maddr(FILE *fp, struct ma_info *list)
 		}
 		fprintf(fp, "%s",
 			format_host(list->addr.family,
-				    -1,
-				    list->addr.data,
-				    abuf, sizeof(abuf)));
+				    -1, list->addr.data));
 	}
 	if (list->users != 1)
 		fprintf(fp, " users %d", list->users);
diff --git a/ip/ipneigh.c b/ip/ipneigh.c
index 6458c8eaa6b1e..583aad30dc820 100644
--- a/ip/ipneigh.c
+++ b/ip/ipneigh.c
@@ -198,7 +198,6 @@ int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	struct ndmsg *r = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[NDA_MAX+1];
-	char abuf[256];
 	static int logit = 1;
 
 	if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH &&
@@ -281,8 +280,7 @@ int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, "%s ",
 			format_host(r->ndm_family,
 				    RTA_PAYLOAD(tb[NDA_DST]),
-				    RTA_DATA(tb[NDA_DST]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[NDA_DST])));
 	}
 	if (!filter.index && r->ndm_ifindex)
 		fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex));
diff --git a/ip/iproute.c b/ip/iproute.c
index 19b28701d4da1..8315ad3e6c6ac 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -380,8 +380,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		} else {
 			fprintf(fp, "%s ", format_host(r->rtm_family,
 						       RTA_PAYLOAD(tb[RTA_DST]),
-						       RTA_DATA(tb[RTA_DST]),
-						       abuf, sizeof(abuf))
+						       RTA_DATA(tb[RTA_DST]))
 				);
 		}
 	} else if (r->rtm_dst_len) {
@@ -400,8 +399,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		} else {
 			fprintf(fp, "from %s ", format_host(r->rtm_family,
 						       RTA_PAYLOAD(tb[RTA_SRC]),
-						       RTA_DATA(tb[RTA_SRC]),
-						       abuf, sizeof(abuf))
+						       RTA_DATA(tb[RTA_SRC]))
 				);
 		}
 	} else if (r->rtm_src_len) {
@@ -410,8 +408,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	if (tb[RTA_NEWDST]) {
 		fprintf(fp, "as to %s ", format_host(r->rtm_family,
 						  RTA_PAYLOAD(tb[RTA_NEWDST]),
-						  RTA_DATA(tb[RTA_NEWDST]),
-						  abuf, sizeof(abuf))
+						  RTA_DATA(tb[RTA_NEWDST]))
 			);
 	}
 
@@ -427,8 +424,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, "via %s ",
 			format_host(r->rtm_family,
 				    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-				    RTA_DATA(tb[RTA_GATEWAY]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[RTA_GATEWAY])));
 	}
 	if (tb[RTA_VIA]) {
 		size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
@@ -436,8 +432,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 		fprintf(fp, "via %s %s ",
 			family_name(via->rtvia_family),
-			format_host(via->rtvia_family, len, via->rtvia_addr,
-				    abuf, sizeof(abuf)));
+			format_host(via->rtvia_family, len, via->rtvia_addr));
 	}
 	if (tb[RTA_OIF] && filter.oifmask != -1)
 		fprintf(fp, "dev %s ", ll_index_to_name(*(int *)RTA_DATA(tb[RTA_OIF])));
@@ -671,8 +666,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 					fprintf(fp, " via %s ",
 						format_host(r->rtm_family,
 							    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-							    RTA_DATA(tb[RTA_GATEWAY]),
-							    abuf, sizeof(abuf)));
+							    RTA_DATA(tb[RTA_GATEWAY])));
 				}
 				if (tb[RTA_VIA]) {
 					size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
@@ -680,8 +674,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 					fprintf(fp, "via %s %s ",
 						family_name(via->rtvia_family),
-						format_host(via->rtvia_family, len, via->rtvia_addr,
-							    abuf, sizeof(abuf)));
+						format_host(via->rtvia_family, len, via->rtvia_addr));
 				}
 				if (tb[RTA_FLOW]) {
 					__u32 to = rta_getattr_u32(tb[RTA_FLOW]);
diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
index a53e11d9478e9..42abe3753ac6a 100644
--- a/ip/iproute_lwtunnel.c
+++ b/ip/iproute_lwtunnel.c
@@ -58,15 +58,13 @@ static const char *format_encap_type(int type)
 static void print_encap_mpls(FILE *fp, struct rtattr *encap)
 {
 	struct rtattr *tb[MPLS_IPTUNNEL_MAX+1];
-	char abuf[256];
 
 	parse_rtattr_nested(tb, MPLS_IPTUNNEL_MAX, encap);
 
 	if (tb[MPLS_IPTUNNEL_DST])
 		fprintf(fp, " %s ", format_host(AF_MPLS,
 			RTA_PAYLOAD(tb[MPLS_IPTUNNEL_DST]),
-			RTA_DATA(tb[MPLS_IPTUNNEL_DST]),
-			abuf, sizeof(abuf)));
+			RTA_DATA(tb[MPLS_IPTUNNEL_DST])));
 }
 
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
diff --git a/ip/iprule.c b/ip/iprule.c
index dc45c6b43e606..345b3b042307d 100644
--- a/ip/iprule.c
+++ b/ip/iprule.c
@@ -94,8 +94,7 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		} else {
 			fprintf(fp, "from %s ", format_host(r->rtm_family,
 						       RTA_PAYLOAD(tb[FRA_SRC]),
-						       RTA_DATA(tb[FRA_SRC]),
-						       abuf, sizeof(abuf))
+						       RTA_DATA(tb[FRA_SRC]))
 				);
 		}
 	} else if (r->rtm_src_len) {
@@ -115,8 +114,7 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		} else {
 			fprintf(fp, "to %s ", format_host(r->rtm_family,
 						       RTA_PAYLOAD(tb[FRA_DST]),
-						       RTA_DATA(tb[FRA_DST]),
-						       abuf, sizeof(abuf)));
+						       RTA_DATA(tb[FRA_DST])));
 		}
 	} else if (r->rtm_dst_len) {
 		fprintf(fp, "to 0/%d ", r->rtm_dst_len);
@@ -191,8 +189,7 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 			fprintf(fp, "map-to %s ",
 				format_host(r->rtm_family,
 					    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-					    RTA_DATA(tb[RTA_GATEWAY]),
-					    abuf, sizeof(abuf)));
+					    RTA_DATA(tb[RTA_GATEWAY])));
 		} else
 			fprintf(fp, "masquerade");
 	} else if (r->rtm_type == FR_ACT_GOTO) {
diff --git a/ip/iptoken.c b/ip/iptoken.c
index 1d22c03c63ab7..02fe98e249526 100644
--- a/ip/iptoken.c
+++ b/ip/iptoken.c
@@ -51,7 +51,6 @@ static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void *
 	int len = n->nlmsg_len;
 	struct rtattr *tb[IFLA_MAX + 1];
 	struct rtattr *ltb[IFLA_INET6_MAX + 1];
-	char abuf[256];
 
 	if (n->nlmsg_type != RTM_NEWLINK)
 		return -1;
@@ -82,8 +81,7 @@ static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void *
 	fprintf(fp, "token %s ",
 		format_host(ifi->ifi_family,
 			    RTA_PAYLOAD(ltb[IFLA_INET6_TOKEN]),
-			    RTA_DATA(ltb[IFLA_INET6_TOKEN]),
-			    abuf, sizeof(abuf)));
+			    RTA_DATA(ltb[IFLA_INET6_TOKEN])));
 	fprintf(fp, "dev %s ", ll_index_to_name(ifi->ifi_index));
 	fprintf(fp, "\n");
 	fflush(fp);
diff --git a/ip/iptunnel.c b/ip/iptunnel.c
index 65a4e6e9c1a5a..bfee99561615a 100644
--- a/ip/iptunnel.c
+++ b/ip/iptunnel.c
@@ -308,7 +308,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
 	printf("%s: %s/ip remote %s local %s",
 	       p->name,
 	       tnl_strproto(p->iph.protocol),
-	       p->iph.daddr ? format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
+	       p->iph.daddr ? format_host_r(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
 	       p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
 
 	if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
@@ -324,7 +324,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
 				if (prl[i].addr != htonl(INADDR_ANY)) {
 					printf(" %s %s ",
 					       (prl[i].flags & PRL_DEFAULT) ? "pdr" : "pr",
-					       format_host(AF_INET, 4, &prl[i].addr, s1, sizeof(s1)));
+					       format_host(AF_INET, 4, &prl[i].addr));
 				}
 			}
 	}
@@ -360,7 +360,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
 		       ip6rd.prefixlen);
 		if (ip6rd.relay_prefix) {
 			printf(" 6rd-relay_prefix %s/%u",
-			       format_host(AF_INET, 4, &ip6rd.relay_prefix, s1, sizeof(s1)),
+			       format_host(AF_INET, 4, &ip6rd.relay_prefix),
 			       ip6rd.relay_prefixlen);
 		}
 	}
diff --git a/ip/link_gre.c b/ip/link_gre.c
index c50963d79d3c5..bcf003aaa5d7a 100644
--- a/ip/link_gre.c
+++ b/ip/link_gre.c
@@ -339,7 +339,6 @@ get_failed:
 
 static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-	char s1[1024];
 	char s2[64];
 	const char *local = "any";
 	const char *remote = "any";
@@ -353,7 +352,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
 
 		if (addr)
-			remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			remote = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "remote %s ", remote);
@@ -362,7 +361,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
 
 		if (addr)
-			local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			local = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "local %s ", local);
diff --git a/ip/link_gre6.c b/ip/link_gre6.c
index a48ea8b1ebae3..bddfc0ff97aa0 100644
--- a/ip/link_gre6.c
+++ b/ip/link_gre6.c
@@ -302,7 +302,6 @@ get_failed:
 
 static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-	char s1[1024];
 	char s2[64];
 	const char *local = "any";
 	const char *remote = "any";
@@ -327,7 +326,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		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, s1, sizeof(s1));
+			remote = format_host(AF_INET6, sizeof(addr), &addr);
 	}
 
 	fprintf(f, "remote %s ", remote);
@@ -338,7 +337,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		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, s1, sizeof(s1));
+			local = format_host(AF_INET6, sizeof(addr), &addr);
 	}
 
 	fprintf(f, "local %s ", local);
diff --git a/ip/link_iptnl.c b/ip/link_iptnl.c
index 04568ffbd81bf..8411a6a00a1b4 100644
--- a/ip/link_iptnl.c
+++ b/ip/link_iptnl.c
@@ -339,7 +339,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
 		unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
 
 		if (addr)
-			remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			remote = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "remote %s ", remote);
@@ -348,7 +348,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
 		unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
 
 		if (addr)
-			local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			local = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "local %s ", local);
@@ -404,8 +404,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
 		       prefixlen);
 		if (relayprefix) {
 			printf("6rd-relay_prefix %s/%u ",
-			       format_host(AF_INET, 4, &relayprefix, s1,
-					   sizeof(s1)),
+			       format_host(AF_INET, 4, &relayprefix),
 			       relayprefixlen);
 		}
 	}
diff --git a/ip/link_vti.c b/ip/link_vti.c
index 587993035e8d5..8052e7514989a 100644
--- a/ip/link_vti.c
+++ b/ip/link_vti.c
@@ -198,7 +198,6 @@ get_failed:
 
 static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-	char s1[1024];
 	char s2[64];
 	const char *local = "any";
 	const char *remote = "any";
@@ -210,7 +209,7 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		unsigned int addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_REMOTE]);
 
 		if (addr)
-			remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			remote = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "remote %s ", remote);
@@ -219,7 +218,7 @@ static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		unsigned int addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LOCAL]);
 
 		if (addr)
-			local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
+			local = format_host(AF_INET, 4, &addr);
 	}
 
 	fprintf(f, "local %s ", local);
diff --git a/ip/link_vti6.c b/ip/link_vti6.c
index 9a62eb5aa370d..9bcf7fe9dea46 100644
--- a/ip/link_vti6.c
+++ b/ip/link_vti6.c
@@ -197,7 +197,6 @@ get_failed:
 
 static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-	char s1[1024];
 	char s2[64];
 	const char *local = "any";
 	const char *remote = "any";
@@ -210,7 +209,7 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 	if (tb[IFLA_VTI_REMOTE]) {
 		memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr));
 
-		remote = format_host(AF_INET6, 16, &daddr, s1, sizeof(s1));
+		remote = format_host(AF_INET6, 16, &daddr);
 	}
 
 	fprintf(f, "remote %s ", remote);
@@ -218,7 +217,7 @@ static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 	if (tb[IFLA_VTI_LOCAL]) {
 		memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr));
 
-		local = format_host(AF_INET6, 16, &saddr, s1, sizeof(s1));
+		local = format_host(AF_INET6, 16, &saddr);
 	}
 
 	fprintf(f, "local %s ", local);
diff --git a/ip/tcp_metrics.c b/ip/tcp_metrics.c
index 3a649917191a8..f82604f458ada 100644
--- a/ip/tcp_metrics.c
+++ b/ip/tcp_metrics.c
@@ -95,7 +95,6 @@ static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
 	struct genlmsghdr *ghdr;
 	struct rtattr *attrs[TCP_METRICS_ATTR_MAX + 1], *a;
 	int len = n->nlmsg_len;
-	char abuf[256];
 	inet_prefix daddr, saddr;
 	int family, i, atype, stype, dlen = 0, slen = 0;
 
@@ -194,7 +193,7 @@ static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
 		fprintf(fp, "Deleted ");
 
 	fprintf(fp, "%s",
-		format_host(family, dlen, &daddr.data, abuf, sizeof(abuf)));
+		format_host(family, dlen, &daddr.data));
 
 	a = attrs[TCP_METRICS_ATTR_AGE];
 	if (a) {
@@ -298,8 +297,7 @@ static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	if (slen) {
 		fprintf(fp, " source %s",
-			format_host(family, slen, &saddr.data, abuf,
-				    sizeof(abuf)));
+			format_host(family, slen, &saddr.data));
 	}
 
 	fprintf(fp, "\n");
diff --git a/lib/utils.c b/lib/utils.c
index fa35f4d044064..22a5ef8fa8213 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -818,7 +818,7 @@ static const char *resolve_address(const void *addr, int len, int af)
 }
 #endif
 
-const char *format_host(int af, int len, const void *addr,
+const char *format_host_r(int af, int len, const void *addr,
 			char *buf, int buflen)
 {
 #ifdef RESOLVE_HOSTNAMES
@@ -835,6 +835,13 @@ const char *format_host(int af, int len, const void *addr,
 	return rt_addr_n2a(af, len, addr, buf, buflen);
 }
 
+const char *format_host(int af, int len, const void *addr)
+{
+	static char buf[256];
+
+	return format_host_r(af, len, addr, buf, 256);
+}
+
 
 char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen)
 {
diff --git a/misc/ss.c b/misc/ss.c
index 192389cc82371..38cf3312a746e 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -1016,10 +1016,10 @@ static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex
 			buf[0] = '*';
 			buf[1] = 0;
 		} else {
-			ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
+			ap = format_host(AF_INET, 4, a->data);
 		}
 	} else {
-		ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
+		ap = format_host(a->family, 16, a->data);
 		est_len = strlen(ap);
 		if (est_len <= addr_width)
 			est_len = addr_width;
diff --git a/tc/m_nat.c b/tc/m_nat.c
index deb071da90daf..4b90121c4b99e 100644
--- a/tc/m_nat.c
+++ b/tc/m_nat.c
@@ -191,9 +191,9 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
 
 	fprintf(f, " nat %s %s/%d %s %s", sel->flags & TCA_NAT_FLAG_EGRESS ?
 					  "egress" : "ingress",
-		format_host(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
+		format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
 		len,
-		format_host(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)),
+		format_host_r(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)),
 		action_n2a(sel->action, buf3, sizeof(buf3)));
 
 	if (show_stats) {
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 4/7] utils: make rt_addr_n2a() non-reentrant by default
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
                   ` (2 preceding siblings ...)
  2016-03-22 18:35 ` [iproute PATCH 3/7] make format_host non-reentrant by default Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 5/7] lib/utils: introduce format_host_rta() Phil Sutter
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

There is only a single user who needs it to be reentrant (not really,
but it's safer like this), add rt_addr_n2a_r() for it to use.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/utils.h       |  3 ++-
 ip/ip6tunnel.c        |  2 +-
 ip/iplink_bond.c      |  5 +----
 ip/ipmroute.c         |  7 ++-----
 ip/ipprefix.c         |  5 +----
 ip/iproute.c          | 10 +++-------
 ip/iproute_lwtunnel.c |  7 ++-----
 ip/iprule.c           |  8 ++------
 ip/iptunnel.c         |  2 +-
 ip/ipxfrm.c           | 29 ++++++-----------------------
 ip/link_ip6tnl.c      |  7 ++-----
 ip/xfrm_monitor.c     | 16 +++-------------
 lib/utils.c           | 11 +++++++++--
 tc/f_flower.c         |  7 ++-----
 14 files changed, 37 insertions(+), 82 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 84083b0dbba71..bc2cbce0cc303 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -125,8 +125,9 @@ int af_byte_len(int af);
 const char *format_host_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
 const char *format_host(int af, int lne, const void *addr);
-const char *rt_addr_n2a(int af, int len, const void *addr,
+const char *rt_addr_n2a_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
+const char *rt_addr_n2a(int af, int len, const void *addr);
 
 int read_family(const char *name);
 const char *family_name(int family);
diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c
index d588645eb942a..c02fa0746ab69 100644
--- a/ip/ip6tunnel.c
+++ b/ip/ip6tunnel.c
@@ -78,7 +78,7 @@ static void print_tunnel(struct ip6_tnl_parm2 *p)
 	       p->name,
 	       tnl_strproto(p->proto),
 	       format_host_r(AF_INET6, 16, &p->raddr, s1, sizeof(s1)),
-	       rt_addr_n2a(AF_INET6, 16, &p->laddr, s2, sizeof(s2)));
+	       rt_addr_n2a_r(AF_INET6, 16, &p->laddr, s2, sizeof(s2)));
 	if (p->link) {
 		const char *n = ll_index_to_name(p->link);
 
diff --git a/ip/iplink_bond.c b/ip/iplink_bond.c
index 45473f66da065..7da58e4556c07 100644
--- a/ip/iplink_bond.c
+++ b/ip/iplink_bond.c
@@ -411,7 +411,6 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 
 	if (tb[IFLA_BOND_ARP_IP_TARGET]) {
 		struct rtattr *iptb[BOND_MAX_ARP_TARGETS + 1];
-		char buf[INET_ADDRSTRLEN];
 		int i;
 
 		parse_rtattr_nested(iptb, BOND_MAX_ARP_TARGETS,
@@ -425,9 +424,7 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 				fprintf(f, "%s",
 					rt_addr_n2a(AF_INET,
 						    RTA_PAYLOAD(iptb[i]),
-						    RTA_DATA(iptb[i]),
-						    buf,
-						    INET_ADDRSTRLEN));
+						    RTA_DATA(iptb[i])));
 			if (i < BOND_MAX_ARP_TARGETS-1 && iptb[i+1])
 				fprintf(f, ",");
 		}
diff --git a/ip/ipmroute.c b/ip/ipmroute.c
index 34543c00ecb75..2b9f892a62630 100644
--- a/ip/ipmroute.c
+++ b/ip/ipmroute.c
@@ -58,7 +58,6 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	struct rtmsg *r = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[RTA_MAX+1];
-	char abuf[256];
 	char obuf[256];
 
 	SPRINT_BUF(b1);
@@ -126,16 +125,14 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		len = snprintf(obuf, sizeof(obuf),
 			       "(%s, ", rt_addr_n2a(family,
 						    RTA_PAYLOAD(tb[RTA_SRC]),
-						    RTA_DATA(tb[RTA_SRC]),
-						    abuf, sizeof(abuf)));
+						    RTA_DATA(tb[RTA_SRC])));
 	else
 		len = sprintf(obuf, "(unknown, ");
 	if (tb[RTA_DST])
 		snprintf(obuf + len, sizeof(obuf) - len,
 			 "%s)", rt_addr_n2a(family,
 					    RTA_PAYLOAD(tb[RTA_DST]),
-					    RTA_DATA(tb[RTA_DST]),
-					    abuf, sizeof(abuf)));
+					    RTA_DATA(tb[RTA_DST])));
 	else
 		snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
 
diff --git a/ip/ipprefix.c b/ip/ipprefix.c
index 2524f784965b5..4d986dbc1a5d1 100644
--- a/ip/ipprefix.c
+++ b/ip/ipprefix.c
@@ -75,15 +75,12 @@ int print_prefix(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[PREFIX_ADDRESS]) {
 		struct in6_addr *pfx;
-		char abuf[256];
 
 		pfx = (struct in6_addr *)RTA_DATA(tb[PREFIX_ADDRESS]);
 
-		memset(abuf, '\0', sizeof(abuf));
 		fprintf(fp, "%s", rt_addr_n2a(family,
 					      RTA_PAYLOAD(tb[PREFIX_ADDRESS]),
-					      pfx,
-					      abuf, sizeof(abuf)));
+					      pfx));
 	}
 	fprintf(fp, "/%u ", prefix->prefix_len);
 
diff --git a/ip/iproute.c b/ip/iproute.c
index 8315ad3e6c6ac..1ec9294290b2a 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -318,7 +318,6 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	struct rtmsg *r = NLMSG_DATA(n);
 	int len = n->nlmsg_len;
 	struct rtattr *tb[RTA_MAX+1];
-	char abuf[256];
 	int host_len;
 	__u32 table;
 
@@ -373,8 +372,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		if (r->rtm_dst_len != host_len) {
 			fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
 						       RTA_PAYLOAD(tb[RTA_DST]),
-						       RTA_DATA(tb[RTA_DST]),
-						       abuf, sizeof(abuf)),
+						       RTA_DATA(tb[RTA_DST])),
 				r->rtm_dst_len
 				);
 		} else {
@@ -392,8 +390,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		if (r->rtm_src_len != host_len) {
 			fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
 						       RTA_PAYLOAD(tb[RTA_SRC]),
-						       RTA_DATA(tb[RTA_SRC]),
-						       abuf, sizeof(abuf)),
+						       RTA_DATA(tb[RTA_SRC])),
 				r->rtm_src_len
 				);
 		} else {
@@ -452,8 +449,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, " src %s ",
 			rt_addr_n2a(r->rtm_family,
 				    RTA_PAYLOAD(tb[RTA_PREFSRC]),
-				    RTA_DATA(tb[RTA_PREFSRC]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[RTA_PREFSRC])));
 	}
 	if (tb[RTA_PRIORITY])
 		fprintf(fp, " metric %u ", rta_getattr_u32(tb[RTA_PRIORITY]));
diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
index 42abe3753ac6a..53d3ad4e6ea8e 100644
--- a/ip/iproute_lwtunnel.c
+++ b/ip/iproute_lwtunnel.c
@@ -70,7 +70,6 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap)
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
 {
 	struct rtattr *tb[LWTUNNEL_IP_MAX+1];
-	char abuf[256];
 
 	parse_rtattr_nested(tb, LWTUNNEL_IP_MAX, encap);
 
@@ -81,15 +80,13 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
 		fprintf(fp, "src %s ",
 			rt_addr_n2a(AF_INET,
 				    RTA_PAYLOAD(tb[LWTUNNEL_IP_SRC]),
-				    RTA_DATA(tb[LWTUNNEL_IP_SRC]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[LWTUNNEL_IP_SRC])));
 
 	if (tb[LWTUNNEL_IP_DST])
 		fprintf(fp, "dst %s ",
 			rt_addr_n2a(AF_INET,
 				    RTA_PAYLOAD(tb[LWTUNNEL_IP_DST]),
-				    RTA_DATA(tb[LWTUNNEL_IP_DST]),
-				    abuf, sizeof(abuf)));
+				    RTA_DATA(tb[LWTUNNEL_IP_DST])));
 
 	if (tb[LWTUNNEL_IP_TTL])
 		fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
diff --git a/ip/iprule.c b/ip/iprule.c
index 345b3b042307d..3fd510efe5e1b 100644
--- a/ip/iprule.c
+++ b/ip/iprule.c
@@ -57,8 +57,6 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	int host_len = -1;
 	__u32 table;
 	struct rtattr *tb[FRA_MAX+1];
-	char abuf[256];
-
 	SPRINT_BUF(b1);
 
 	if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
@@ -87,8 +85,7 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		if (r->rtm_src_len != host_len) {
 			fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
 						       RTA_PAYLOAD(tb[FRA_SRC]),
-						       RTA_DATA(tb[FRA_SRC]),
-						       abuf, sizeof(abuf)),
+						       RTA_DATA(tb[FRA_SRC])),
 				r->rtm_src_len
 				);
 		} else {
@@ -107,8 +104,7 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		if (r->rtm_dst_len != host_len) {
 			fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
 						       RTA_PAYLOAD(tb[FRA_DST]),
-						       RTA_DATA(tb[FRA_DST]),
-						       abuf, sizeof(abuf)),
+						       RTA_DATA(tb[FRA_DST])),
 				r->rtm_dst_len
 				);
 		} else {
diff --git a/ip/iptunnel.c b/ip/iptunnel.c
index bfee99561615a..e3161d81beded 100644
--- a/ip/iptunnel.c
+++ b/ip/iptunnel.c
@@ -309,7 +309,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
 	       p->name,
 	       tnl_strproto(p->iph.protocol),
 	       p->iph.daddr ? format_host_r(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1)) : "any",
-	       p->iph.saddr ? rt_addr_n2a(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
+	       p->iph.saddr ? rt_addr_n2a_r(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2)) : "any");
 
 	if (p->iph.protocol == IPPROTO_IPV6 && (p->i_flags & SIT_ISATAP)) {
 		struct ip_tunnel_prl prl[16];
diff --git a/ip/ipxfrm.c b/ip/ipxfrm.c
index 860414d377b47..8741ff3b302a9 100644
--- a/ip/ipxfrm.c
+++ b/ip/ipxfrm.c
@@ -285,17 +285,11 @@ void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
 			__u8 mode, __u32 reqid, __u16 family, int force_spi,
 			FILE *fp, const char *prefix, const char *title)
 {
-	char abuf[256];
-
 	if (title)
 		fputs(title, fp);
 
-	memset(abuf, '\0', sizeof(abuf));
-	fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
-					   saddr, abuf, sizeof(abuf)));
-	memset(abuf, '\0', sizeof(abuf));
-	fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
-					  &id->daddr, abuf, sizeof(abuf)));
+	fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), saddr));
+	fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), &id->daddr));
 	fprintf(fp, "%s", _SL_);
 
 	if (prefix)
@@ -447,7 +441,6 @@ void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
 			 FILE *fp, const char *prefix)
 {
-	char abuf[256];
 	__u16 f;
 
 	f = sel->family;
@@ -459,16 +452,12 @@ void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
 	if (prefix)
 		fputs(prefix, fp);
 
-	memset(abuf, '\0', sizeof(abuf));
 	fprintf(fp, "src %s/%u ",
-		rt_addr_n2a(f, sizeof(sel->saddr), &sel->saddr,
-			    abuf, sizeof(abuf)),
+		rt_addr_n2a(f, sizeof(sel->saddr), &sel->saddr),
 		sel->prefixlen_s);
 
-	memset(abuf, '\0', sizeof(abuf));
 	fprintf(fp, "dst %s/%u ",
-		rt_addr_n2a(f, sizeof(sel->daddr), &sel->daddr,
-			    abuf, sizeof(abuf)),
+		rt_addr_n2a(f, sizeof(sel->daddr), &sel->daddr),
 		sel->prefixlen_d);
 
 	if (sel->proto)
@@ -740,7 +729,6 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
 
 	if (tb[XFRMA_ENCAP]) {
 		struct xfrm_encap_tmpl *e;
-		char abuf[256];
 
 		if (prefix)
 			fputs(prefix, fp);
@@ -768,10 +756,8 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
 		fprintf(fp, "sport %u ", ntohs(e->encap_sport));
 		fprintf(fp, "dport %u ", ntohs(e->encap_dport));
 
-		memset(abuf, '\0', sizeof(abuf));
 		fprintf(fp, "addr %s",
-			rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa,
-				    abuf, sizeof(abuf)));
+			rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa));
 		fprintf(fp, "%s", _SL_);
 	}
 
@@ -783,7 +769,6 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
 	}
 
 	if (tb[XFRMA_COADDR]) {
-		char abuf[256];
 		xfrm_address_t *coa;
 
 		if (prefix)
@@ -798,10 +783,8 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
 			return;
 		}
 
-		memset(abuf, '\0', sizeof(abuf));
 		fprintf(fp, "%s",
-			rt_addr_n2a(family, sizeof(*coa), coa,
-				    abuf, sizeof(abuf)));
+			rt_addr_n2a(family, sizeof(*coa), coa));
 		fprintf(fp, "%s", _SL_);
 	}
 
diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index ab5e50a205983..4b32fe5aa8088 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -260,7 +260,6 @@ get_failed:
 
 static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-	char s1[256];
 	char s2[64];
 	int flags = 0;
 	__u32 flowinfo = 0;
@@ -292,16 +291,14 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
 		fprintf(f, "remote %s ",
 			rt_addr_n2a(AF_INET6,
 				    RTA_PAYLOAD(tb[IFLA_IPTUN_REMOTE]),
-				    RTA_DATA(tb[IFLA_IPTUN_REMOTE]),
-				    s1, sizeof(s1)));
+				    RTA_DATA(tb[IFLA_IPTUN_REMOTE])));
 	}
 
 	if (tb[IFLA_IPTUN_LOCAL]) {
 		fprintf(f, "local %s ",
 			rt_addr_n2a(AF_INET6,
 				    RTA_PAYLOAD(tb[IFLA_IPTUN_LOCAL]),
-				    RTA_DATA(tb[IFLA_IPTUN_LOCAL]),
-				    s1, sizeof(s1)));
+				    RTA_DATA(tb[IFLA_IPTUN_LOCAL])));
 	}
 
 	if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
diff --git a/ip/xfrm_monitor.c b/ip/xfrm_monitor.c
index 96b52a44b8a85..9d01a2aab5018 100644
--- a/ip/xfrm_monitor.c
+++ b/ip/xfrm_monitor.c
@@ -227,12 +227,8 @@ static void xfrm_ae_flags_print(__u32 flags, void *arg)
 
 static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
 {
-	char buf[256];
-
-	buf[0] = 0;
 	fprintf(fp, "dst %s ",
-		rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr,
-			    buf, sizeof(buf)));
+		rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
 
 	fprintf(fp, " reqid 0x%x", reqid);
 
@@ -245,15 +241,12 @@ static int xfrm_ae_print(const struct sockaddr_nl *who,
 {
 	FILE *fp = (FILE *)arg;
 	struct xfrm_aevent_id *id = NLMSG_DATA(n);
-	char abuf[256];
 
 	fprintf(fp, "Async event ");
 	xfrm_ae_flags_print(id->flags, arg);
 	fprintf(fp, "\n\t");
-	memset(abuf, '\0', sizeof(abuf));
 	fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
-					   sizeof(id->saddr), &id->saddr,
-					   abuf, sizeof(abuf)));
+					   sizeof(id->saddr), &id->saddr));
 
 	xfrm_usersa_print(&id->sa_id, id->reqid, fp);
 
@@ -265,10 +258,7 @@ static int xfrm_ae_print(const struct sockaddr_nl *who,
 
 static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
 {
-	char buf[256];
-
-	buf[0] = 0;
-	fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a, buf, sizeof(buf)));
+	fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
 }
 
 static int xfrm_mapping_print(const struct sockaddr_nl *who,
diff --git a/lib/utils.c b/lib/utils.c
index 22a5ef8fa8213..9b303a6b096bc 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -702,7 +702,7 @@ int __get_user_hz(void)
 	return sysconf(_SC_CLK_TCK);
 }
 
-const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen)
+const char *rt_addr_n2a_r(int af, int len, const void *addr, char *buf, int buflen)
 {
 	switch (af) {
 	case AF_INET:
@@ -725,6 +725,13 @@ const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen
 	}
 }
 
+const char *rt_addr_n2a(int af, int len, const void *addr)
+{
+	static char buf[256];
+
+	return rt_addr_n2a_r(af, len, addr, buf, 256);
+}
+
 int read_family(const char *name)
 {
 	int family = AF_UNSPEC;
@@ -832,7 +839,7 @@ const char *format_host_r(int af, int len, const void *addr,
 			return n;
 	}
 #endif
-	return rt_addr_n2a(af, len, addr, buf, buflen);
+	return rt_addr_n2a_r(af, len, addr, buf, buflen);
 }
 
 const char *format_host(int af, int len, const void *addr)
diff --git a/tc/f_flower.c b/tc/f_flower.c
index 6139fe08651a4..40f8c595b2eec 100644
--- a/tc/f_flower.c
+++ b/tc/f_flower.c
@@ -394,7 +394,6 @@ static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
 				 struct rtattr *addr6_attr,
 				 struct rtattr *mask6_attr)
 {
-	SPRINT_BUF(b1);
 	struct rtattr *addr_attr;
 	struct rtattr *mask_attr;
 	int family;
@@ -418,16 +417,14 @@ static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
 		return;
 	fprintf(f, "\n  %s %s", name, rt_addr_n2a(family,
 						  RTA_PAYLOAD(addr_attr),
-						  RTA_DATA(addr_attr),
-						  b1, sizeof(b1)));
+						  RTA_DATA(addr_attr)));
 	if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
 		return;
 	bits = __mask_bits(RTA_DATA(mask_attr), len);
 	if (bits < 0)
 		fprintf(f, "/%s", rt_addr_n2a(family,
 					      RTA_PAYLOAD(mask_attr),
-					      RTA_DATA(mask_attr),
-					      b1, sizeof(b1)));
+					      RTA_DATA(mask_attr)));
 	else if (bits < len * 8)
 		fprintf(f, "/%d", bits);
 }
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 5/7] lib/utils: introduce format_host_rta()
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
                   ` (3 preceding siblings ...)
  2016-03-22 18:35 ` [iproute PATCH 4/7] utils: make rt_addr_n2a() " Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta() Phil Sutter
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

This simple macro eases calling format_host() with data from an rt_attr
pointer.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/utils.h       |  2 ++
 ip/ipaddress.c        | 20 ++++++++------------
 ip/ipaddrlabel.c      |  5 ++---
 ip/ipneigh.c          |  4 +---
 ip/iproute.c          | 33 +++++++++++----------------------
 ip/iproute_lwtunnel.c |  5 ++---
 ip/iprule.c           | 16 ++++++----------
 ip/iptoken.c          |  9 +++------
 8 files changed, 35 insertions(+), 59 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index bc2cbce0cc303..ebb80c9c20b6d 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -125,6 +125,8 @@ int af_byte_len(int af);
 const char *format_host_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
 const char *format_host(int af, int lne, const void *addr);
+#define format_host_rta(af, rta) \
+	format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
 const char *rt_addr_n2a_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
 const char *rt_addr_n2a(int af, int len, const void *addr);
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 03c8c03cd4a17..3998d8cec4ab2 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -1058,18 +1058,16 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 
 	if (rta_tb[IFA_LOCAL]) {
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
-		              format_host(ifa->ifa_family,
-		                          RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-		                          RTA_DATA(rta_tb[IFA_LOCAL])));
+		              format_host_rta(ifa->ifa_family,
+		                              rta_tb[IFA_LOCAL]));
 		if (rta_tb[IFA_ADDRESS] &&
 		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
 		           RTA_DATA(rta_tb[IFA_LOCAL]),
 		           ifa->ifa_family == AF_INET ? 4 : 16)) {
 			fprintf(fp, " peer ");
 			color_fprintf(fp, ifa_family_color(ifa->ifa_family),
-			              "%s", format_host(ifa->ifa_family,
-			              RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
-			              RTA_DATA(rta_tb[IFA_ADDRESS])));
+			              "%s", format_host_rta(ifa->ifa_family,
+			              rta_tb[IFA_ADDRESS]));
 		}
 		fprintf(fp, "/%d ", ifa->ifa_prefixlen);
 	}
@@ -1080,16 +1078,14 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 	if (rta_tb[IFA_BROADCAST]) {
 		fprintf(fp, "brd ");
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
-			format_host(ifa->ifa_family,
-				    RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
-				    RTA_DATA(rta_tb[IFA_BROADCAST])));
+		              format_host_rta(ifa->ifa_family,
+		                              rta_tb[IFA_BROADCAST]));
 	}
 	if (rta_tb[IFA_ANYCAST]) {
 		fprintf(fp, "any ");
 		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
-			format_host(ifa->ifa_family,
-				    RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
-				    RTA_DATA(rta_tb[IFA_ANYCAST])));
+		              format_host_rta(ifa->ifa_family,
+		                              rta_tb[IFA_ANYCAST]));
 	}
 	fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
 	if (ifa_flags & IFA_F_SECONDARY) {
diff --git a/ip/ipaddrlabel.c b/ip/ipaddrlabel.c
index 6076bb952297f..b4cd784094719 100644
--- a/ip/ipaddrlabel.c
+++ b/ip/ipaddrlabel.c
@@ -75,9 +75,8 @@ int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
 
 	if (tb[IFAL_ADDRESS]) {
 		fprintf(fp, "prefix %s/%u ",
-			format_host(ifal->ifal_family,
-				    RTA_PAYLOAD(tb[IFAL_ADDRESS]),
-				    RTA_DATA(tb[IFAL_ADDRESS])),
+			format_host_rta(ifal->ifal_family,
+		                        tb[IFAL_ADDRESS]),
 			ifal->ifal_prefixlen);
 	}
 
diff --git a/ip/ipneigh.c b/ip/ipneigh.c
index 583aad30dc820..c49fb4e7f7b58 100644
--- a/ip/ipneigh.c
+++ b/ip/ipneigh.c
@@ -278,9 +278,7 @@ int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		fprintf(fp, "miss ");
 	if (tb[NDA_DST]) {
 		fprintf(fp, "%s ",
-			format_host(r->ndm_family,
-				    RTA_PAYLOAD(tb[NDA_DST]),
-				    RTA_DATA(tb[NDA_DST])));
+			format_host_rta(r->ndm_family, tb[NDA_DST]));
 	}
 	if (!filter.index && r->ndm_ifindex)
 		fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex));
diff --git a/ip/iproute.c b/ip/iproute.c
index 1ec9294290b2a..67d551b54d00c 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -376,10 +376,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 				r->rtm_dst_len
 				);
 		} else {
-			fprintf(fp, "%s ", format_host(r->rtm_family,
-						       RTA_PAYLOAD(tb[RTA_DST]),
-						       RTA_DATA(tb[RTA_DST]))
-				);
+			fprintf(fp, "%s ",
+			        format_host_rta(r->rtm_family, tb[RTA_DST]));
 		}
 	} else if (r->rtm_dst_len) {
 		fprintf(fp, "0/%d ", r->rtm_dst_len);
@@ -394,19 +392,15 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 				r->rtm_src_len
 				);
 		} else {
-			fprintf(fp, "from %s ", format_host(r->rtm_family,
-						       RTA_PAYLOAD(tb[RTA_SRC]),
-						       RTA_DATA(tb[RTA_SRC]))
-				);
+			fprintf(fp, "from %s ",
+			        format_host_rta(r->rtm_family, tb[RTA_SRC]));
 		}
 	} 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(r->rtm_family,
-						  RTA_PAYLOAD(tb[RTA_NEWDST]),
-						  RTA_DATA(tb[RTA_NEWDST]))
-			);
+		fprintf(fp, "as to %s ",
+		        format_host_rta(r->rtm_family, tb[RTA_NEWDST]));
 	}
 
 	if (tb[RTA_ENCAP])
@@ -419,9 +413,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
 		fprintf(fp, "via %s ",
-			format_host(r->rtm_family,
-				    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-				    RTA_DATA(tb[RTA_GATEWAY])));
+		        format_host_rta(r->rtm_family, tb[RTA_GATEWAY]));
 	}
 	if (tb[RTA_VIA]) {
 		size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
@@ -653,16 +645,13 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 							tb[RTA_ENCAP]);
 				if (tb[RTA_NEWDST]) {
 					fprintf(fp, " as to %s ",
-						format_host(r->rtm_family,
-						RTA_PAYLOAD(tb[RTA_NEWDST]),
-						RTA_DATA(tb[RTA_NEWDST]),
-						abuf, sizeof(abuf)));
+						format_host_rta(r->rtm_family,
+								tb[RTA_NEWDST]));
 				}
 				if (tb[RTA_GATEWAY]) {
 					fprintf(fp, " via %s ",
-						format_host(r->rtm_family,
-							    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-							    RTA_DATA(tb[RTA_GATEWAY])));
+						format_host_rta(r->rtm_family,
+								tb[RTA_GATEWAY]));
 				}
 				if (tb[RTA_VIA]) {
 					size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
index 53d3ad4e6ea8e..56af9e4e92ecd 100644
--- a/ip/iproute_lwtunnel.c
+++ b/ip/iproute_lwtunnel.c
@@ -62,9 +62,8 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap)
 	parse_rtattr_nested(tb, MPLS_IPTUNNEL_MAX, encap);
 
 	if (tb[MPLS_IPTUNNEL_DST])
-		fprintf(fp, " %s ", format_host(AF_MPLS,
-			RTA_PAYLOAD(tb[MPLS_IPTUNNEL_DST]),
-			RTA_DATA(tb[MPLS_IPTUNNEL_DST])));
+		fprintf(fp, " %s ",
+		        format_host_rta(AF_MPLS, tb[MPLS_IPTUNNEL_DST]));
 }
 
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
diff --git a/ip/iprule.c b/ip/iprule.c
index 3fd510efe5e1b..ac570440d6633 100644
--- a/ip/iprule.c
+++ b/ip/iprule.c
@@ -89,10 +89,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 				r->rtm_src_len
 				);
 		} else {
-			fprintf(fp, "from %s ", format_host(r->rtm_family,
-						       RTA_PAYLOAD(tb[FRA_SRC]),
-						       RTA_DATA(tb[FRA_SRC]))
-				);
+			fprintf(fp, "from %s ",
+			        format_host_rta(r->rtm_family, tb[FRA_SRC]));
 		}
 	} else if (r->rtm_src_len) {
 		fprintf(fp, "from 0/%d ", r->rtm_src_len);
@@ -108,9 +106,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 				r->rtm_dst_len
 				);
 		} else {
-			fprintf(fp, "to %s ", format_host(r->rtm_family,
-						       RTA_PAYLOAD(tb[FRA_DST]),
-						       RTA_DATA(tb[FRA_DST])));
+			fprintf(fp, "to %s ",
+			        format_host_rta(r->rtm_family, tb[FRA_DST]));
 		}
 	} else if (r->rtm_dst_len) {
 		fprintf(fp, "to 0/%d ", r->rtm_dst_len);
@@ -183,9 +180,8 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	if (r->rtm_type == RTN_NAT) {
 		if (tb[RTA_GATEWAY]) {
 			fprintf(fp, "map-to %s ",
-				format_host(r->rtm_family,
-					    RTA_PAYLOAD(tb[RTA_GATEWAY]),
-					    RTA_DATA(tb[RTA_GATEWAY])));
+			        format_host_rta(r->rtm_family,
+			                        tb[RTA_GATEWAY]));
 		} else
 			fprintf(fp, "masquerade");
 	} else if (r->rtm_type == FR_ACT_GOTO) {
diff --git a/ip/iptoken.c b/ip/iptoken.c
index 02fe98e249526..6e1a1ab7f36e7 100644
--- a/ip/iptoken.c
+++ b/ip/iptoken.c
@@ -78,12 +78,9 @@ static int print_token(const struct sockaddr_nl *who, struct nlmsghdr *n, void *
 		return -1;
 	}
 
-	fprintf(fp, "token %s ",
-		format_host(ifi->ifi_family,
-			    RTA_PAYLOAD(ltb[IFLA_INET6_TOKEN]),
-			    RTA_DATA(ltb[IFLA_INET6_TOKEN])));
-	fprintf(fp, "dev %s ", ll_index_to_name(ifi->ifi_index));
-	fprintf(fp, "\n");
+	fprintf(fp, "token %s dev %s\n",
+	        format_host_rta(ifi->ifi_family, ltb[IFLA_INET6_TOKEN]),
+	        ll_index_to_name(ifi->ifi_index));
 	fflush(fp);
 
 	return 0;
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta()
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
                   ` (4 preceding siblings ...)
  2016-03-22 18:35 ` [iproute PATCH 5/7] lib/utils: introduce format_host_rta() Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-22 18:35 ` [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit Phil Sutter
  2016-03-27 17:38 ` [iproute PATCH 0/7] Refactor some internal library functions Stephen Hemminger
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

This simple macro eases calling rt_addr_n2a() with data from an rt_attr
pointer.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/utils.h       |  2 ++
 ip/iplink_bond.c      |  4 +---
 ip/ipmroute.c         |  8 ++------
 ip/ipprefix.c         | 14 +++-----------
 ip/iproute.c          | 20 +++++++-------------
 ip/iproute_lwtunnel.c | 19 ++++---------------
 ip/iprule.c           | 16 ++++++----------
 ip/link_ip6tnl.c      |  8 ++------
 tc/f_flower.c         |  8 ++------
 9 files changed, 29 insertions(+), 70 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index ebb80c9c20b6d..ef81d00f3d70d 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -130,6 +130,8 @@ const char *format_host(int af, int lne, const void *addr);
 const char *rt_addr_n2a_r(int af, int len, const void *addr,
 			       char *buf, int buflen);
 const char *rt_addr_n2a(int af, int len, const void *addr);
+#define rt_addr_n2a_rta(af, rta) \
+	rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
 
 int read_family(const char *name);
 const char *family_name(int family);
diff --git a/ip/iplink_bond.c b/ip/iplink_bond.c
index 7da58e4556c07..fe83479a091a8 100644
--- a/ip/iplink_bond.c
+++ b/ip/iplink_bond.c
@@ -422,9 +422,7 @@ static void bond_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 		for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
 			if (iptb[i])
 				fprintf(f, "%s",
-					rt_addr_n2a(AF_INET,
-						    RTA_PAYLOAD(iptb[i]),
-						    RTA_DATA(iptb[i])));
+					rt_addr_n2a_rta(AF_INET, iptb[i]));
 			if (i < BOND_MAX_ARP_TARGETS-1 && iptb[i+1])
 				fprintf(f, ",");
 		}
diff --git a/ip/ipmroute.c b/ip/ipmroute.c
index 2b9f892a62630..c33cdcbbde21b 100644
--- a/ip/ipmroute.c
+++ b/ip/ipmroute.c
@@ -123,16 +123,12 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[RTA_SRC])
 		len = snprintf(obuf, sizeof(obuf),
-			       "(%s, ", rt_addr_n2a(family,
-						    RTA_PAYLOAD(tb[RTA_SRC]),
-						    RTA_DATA(tb[RTA_SRC])));
+			       "(%s, ", rt_addr_n2a_rta(family, tb[RTA_SRC]));
 	else
 		len = sprintf(obuf, "(unknown, ");
 	if (tb[RTA_DST])
 		snprintf(obuf + len, sizeof(obuf) - len,
-			 "%s)", rt_addr_n2a(family,
-					    RTA_PAYLOAD(tb[RTA_DST]),
-					    RTA_DATA(tb[RTA_DST])));
+			 "%s)", rt_addr_n2a_rta(family, tb[RTA_DST]));
 	else
 		snprintf(obuf + len, sizeof(obuf) - len, "unknown) ");
 
diff --git a/ip/ipprefix.c b/ip/ipprefix.c
index 4d986dbc1a5d1..a833efcf67c4a 100644
--- a/ip/ipprefix.c
+++ b/ip/ipprefix.c
@@ -71,19 +71,11 @@ int print_prefix(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	parse_rtattr(tb, RTA_MAX, RTM_RTA(prefix), len);
 
-	fprintf(fp, "prefix ");
-
 	if (tb[PREFIX_ADDRESS]) {
-		struct in6_addr *pfx;
-
-		pfx = (struct in6_addr *)RTA_DATA(tb[PREFIX_ADDRESS]);
-
-		fprintf(fp, "%s", rt_addr_n2a(family,
-					      RTA_PAYLOAD(tb[PREFIX_ADDRESS]),
-					      pfx));
+		fprintf(fp, "prefix %s/%u",
+		        rt_addr_n2a_rta(family, tb[PREFIX_ADDRESS]),
+			prefix->prefix_len);
 	}
-	fprintf(fp, "/%u ", prefix->prefix_len);
-
 	fprintf(fp, "dev %s ", ll_index_to_name(prefix->prefix_ifindex));
 
 	if (prefix->prefix_flags & IF_PREFIX_ONLINK)
diff --git a/ip/iproute.c b/ip/iproute.c
index 67d551b54d00c..8224d7ffa94bf 100644
--- a/ip/iproute.c
+++ b/ip/iproute.c
@@ -370,11 +370,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[RTA_DST]) {
 		if (r->rtm_dst_len != host_len) {
-			fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
-						       RTA_PAYLOAD(tb[RTA_DST]),
-						       RTA_DATA(tb[RTA_DST])),
-				r->rtm_dst_len
-				);
+			fprintf(fp, "%s/%u ",
+			        rt_addr_n2a_rta(r->rtm_family, tb[RTA_DST]),
+			        r->rtm_dst_len);
 		} else {
 			fprintf(fp, "%s ",
 			        format_host_rta(r->rtm_family, tb[RTA_DST]));
@@ -386,11 +384,9 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 	}
 	if (tb[RTA_SRC]) {
 		if (r->rtm_src_len != host_len) {
-			fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
-						       RTA_PAYLOAD(tb[RTA_SRC]),
-						       RTA_DATA(tb[RTA_SRC])),
-				r->rtm_src_len
-				);
+			fprintf(fp, "from %s/%u ",
+			        rt_addr_n2a_rta(r->rtm_family, tb[RTA_SRC]),
+			        r->rtm_src_len);
 		} else {
 			fprintf(fp, "from %s ",
 			        format_host_rta(r->rtm_family, tb[RTA_SRC]));
@@ -439,9 +435,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		   and symbolic name will not be useful.
 		 */
 		fprintf(fp, " src %s ",
-			rt_addr_n2a(r->rtm_family,
-				    RTA_PAYLOAD(tb[RTA_PREFSRC]),
-				    RTA_DATA(tb[RTA_PREFSRC])));
+			rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]));
 	}
 	if (tb[RTA_PRIORITY])
 		fprintf(fp, " metric %u ", rta_getattr_u32(tb[RTA_PRIORITY]));
diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
index 56af9e4e92ecd..3baac77208168 100644
--- a/ip/iproute_lwtunnel.c
+++ b/ip/iproute_lwtunnel.c
@@ -77,15 +77,11 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
 
 	if (tb[LWTUNNEL_IP_SRC])
 		fprintf(fp, "src %s ",
-			rt_addr_n2a(AF_INET,
-				    RTA_PAYLOAD(tb[LWTUNNEL_IP_SRC]),
-				    RTA_DATA(tb[LWTUNNEL_IP_SRC])));
+			rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_SRC]));
 
 	if (tb[LWTUNNEL_IP_DST])
 		fprintf(fp, "dst %s ",
-			rt_addr_n2a(AF_INET,
-				    RTA_PAYLOAD(tb[LWTUNNEL_IP_DST]),
-				    RTA_DATA(tb[LWTUNNEL_IP_DST])));
+			rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_DST]));
 
 	if (tb[LWTUNNEL_IP_TTL])
 		fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
@@ -112,7 +108,6 @@ static void print_encap_ila(FILE *fp, struct rtattr *encap)
 static void print_encap_ip6(FILE *fp, struct rtattr *encap)
 {
 	struct rtattr *tb[LWTUNNEL_IP6_MAX+1];
-	char abuf[256];
 
 	parse_rtattr_nested(tb, LWTUNNEL_IP6_MAX, encap);
 
@@ -121,17 +116,11 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap)
 
 	if (tb[LWTUNNEL_IP6_SRC])
 		fprintf(fp, "src %s ",
-			rt_addr_n2a(AF_INET6,
-				    RTA_PAYLOAD(tb[LWTUNNEL_IP6_SRC]),
-				    RTA_DATA(tb[LWTUNNEL_IP6_SRC]),
-				    abuf, sizeof(abuf)));
+			rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_SRC]));
 
 	if (tb[LWTUNNEL_IP6_DST])
 		fprintf(fp, "dst %s ",
-			rt_addr_n2a(AF_INET6,
-				    RTA_PAYLOAD(tb[LWTUNNEL_IP6_DST]),
-				    RTA_DATA(tb[LWTUNNEL_IP6_DST]),
-				    abuf, sizeof(abuf)));
+			rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_DST]));
 
 	if (tb[LWTUNNEL_IP6_HOPLIMIT])
 		fprintf(fp, "hoplimit %d ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT]));
diff --git a/ip/iprule.c b/ip/iprule.c
index ac570440d6633..7cb19e4d5ebd0 100644
--- a/ip/iprule.c
+++ b/ip/iprule.c
@@ -83,11 +83,9 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[FRA_SRC]) {
 		if (r->rtm_src_len != host_len) {
-			fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
-						       RTA_PAYLOAD(tb[FRA_SRC]),
-						       RTA_DATA(tb[FRA_SRC])),
-				r->rtm_src_len
-				);
+			fprintf(fp, "from %s/%u ",
+			        rt_addr_n2a_rta(r->rtm_family, tb[FRA_SRC]),
+			        r->rtm_src_len);
 		} else {
 			fprintf(fp, "from %s ",
 			        format_host_rta(r->rtm_family, tb[FRA_SRC]));
@@ -100,11 +98,9 @@ int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 
 	if (tb[FRA_DST]) {
 		if (r->rtm_dst_len != host_len) {
-			fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
-						       RTA_PAYLOAD(tb[FRA_DST]),
-						       RTA_DATA(tb[FRA_DST])),
-				r->rtm_dst_len
-				);
+			fprintf(fp, "to %s/%u ",
+			        rt_addr_n2a_rta(r->rtm_family, tb[FRA_DST]),
+			        r->rtm_dst_len);
 		} else {
 			fprintf(fp, "to %s ",
 			        format_host_rta(r->rtm_family, tb[FRA_DST]));
diff --git a/ip/link_ip6tnl.c b/ip/link_ip6tnl.c
index 4b32fe5aa8088..8a31d0dcd1883 100644
--- a/ip/link_ip6tnl.c
+++ b/ip/link_ip6tnl.c
@@ -289,16 +289,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
 
 	if (tb[IFLA_IPTUN_REMOTE]) {
 		fprintf(f, "remote %s ",
-			rt_addr_n2a(AF_INET6,
-				    RTA_PAYLOAD(tb[IFLA_IPTUN_REMOTE]),
-				    RTA_DATA(tb[IFLA_IPTUN_REMOTE])));
+			rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
 	}
 
 	if (tb[IFLA_IPTUN_LOCAL]) {
 		fprintf(f, "local %s ",
-			rt_addr_n2a(AF_INET6,
-				    RTA_PAYLOAD(tb[IFLA_IPTUN_LOCAL]),
-				    RTA_DATA(tb[IFLA_IPTUN_LOCAL])));
+			rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
 	}
 
 	if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
diff --git a/tc/f_flower.c b/tc/f_flower.c
index 40f8c595b2eec..306f056c1b662 100644
--- a/tc/f_flower.c
+++ b/tc/f_flower.c
@@ -415,16 +415,12 @@ static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
 	}
 	if (!addr_attr || RTA_PAYLOAD(addr_attr) != len)
 		return;
-	fprintf(f, "\n  %s %s", name, rt_addr_n2a(family,
-						  RTA_PAYLOAD(addr_attr),
-						  RTA_DATA(addr_attr)));
+	fprintf(f, "\n  %s %s", name, rt_addr_n2a_rta(family, addr_attr));
 	if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
 		return;
 	bits = __mask_bits(RTA_DATA(mask_attr), len);
 	if (bits < 0)
-		fprintf(f, "/%s", rt_addr_n2a(family,
-					      RTA_PAYLOAD(mask_attr),
-					      RTA_DATA(mask_attr)));
+		fprintf(f, "/%s", rt_addr_n2a_rta(family, mask_attr));
 	else if (bits < len * 8)
 		fprintf(f, "/%d", bits);
 }
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
                   ` (5 preceding siblings ...)
  2016-03-22 18:35 ` [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta() Phil Sutter
@ 2016-03-22 18:35 ` Phil Sutter
  2016-03-27 17:38 ` [iproute PATCH 0/7] Refactor some internal library functions Stephen Hemminger
  7 siblings, 0 replies; 9+ messages in thread
From: Phil Sutter @ 2016-03-22 18:35 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Apart from making the code a bit more compact and efficient, this also
prevents a potential buffer overflow if the passed buffer is really too
small: Although correctly decrementing the size parameter passed to
snprintf, it could become negative which would then wrap since snprintf
uses (unsigned) size_t for the parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 lib/ll_addr.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/lib/ll_addr.c b/lib/ll_addr.c
index 2ce9abfbb8c69..465ed6fa4d9a2 100644
--- a/lib/ll_addr.c
+++ b/lib/ll_addr.c
@@ -41,18 +41,9 @@ const char *ll_addr_n2a(const unsigned char *addr, int alen, int type, char *buf
 	if (alen == 16 && type == ARPHRD_TUNNEL6) {
 		return inet_ntop(AF_INET6, addr, buf, blen);
 	}
-	l = 0;
-	for (i=0; i<alen; i++) {
-		if (i==0) {
-			snprintf(buf+l, blen, "%02x", addr[i]);
-			blen -= 2;
-			l += 2;
-		} else {
-			snprintf(buf+l, blen, ":%02x", addr[i]);
-			blen -= 3;
-			l += 3;
-		}
-	}
+	snprintf(buf, blen, "%02x", addr[0]);
+	for (i = 1, l = 2; i < alen && l < blen; i++, l += 3)
+		snprintf(buf + l, blen - l, ":%02x", addr[i]);
 	return buf;
 }
 
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [iproute PATCH 0/7] Refactor some internal library functions
  2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
                   ` (6 preceding siblings ...)
  2016-03-22 18:35 ` [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit Phil Sutter
@ 2016-03-27 17:38 ` Stephen Hemminger
  7 siblings, 0 replies; 9+ messages in thread
From: Stephen Hemminger @ 2016-03-27 17:38 UTC (permalink / raw)
  To: Phil Sutter; +Cc: netdev

On Tue, 22 Mar 2016 19:35:12 +0100
Phil Sutter <phil@nwl.cc> wrote:

> The following series is a result of reviewing color output support and
> some formatting helpers usually used with struct rtattr fields.
> 
> Phil Sutter (7):
>   color: introduce color helpers and COLOR_CLEAR
>   ipaddress: colorize peer, broadcast and anycast addresses as well
>   make format_host non-reentrant by default
>   utils: make rt_addr_n2a() non-reentrant by default
>   lib/utils: introduce format_host_rta()
>   lib/utils: introduce rt_addr_n2a_rta()
>   lib/ll_addr: improve ll_addr_n2a() a bit
> 
>  bridge/fdb.c          |  4 +--
>  include/color.h       |  5 ++-
>  include/utils.h       | 10 ++++--
>  ip/ip6tunnel.c        |  4 +--
>  ip/ipaddress.c        | 87 ++++++++++++++++++---------------------------------
>  ip/ipaddrlabel.c      |  7 ++---
>  ip/iplink_bond.c      |  7 +----
>  ip/iplink_geneve.c    |  5 ++-
>  ip/iplink_vxlan.c     | 13 ++++----
>  ip/ipmaddr.c          |  6 +---
>  ip/ipmroute.c         | 11 ++-----
>  ip/ipneigh.c          |  6 +---
>  ip/ipprefix.c         | 17 ++--------
>  ip/iproute.c          | 68 ++++++++++++----------------------------
>  ip/iproute_lwtunnel.c | 29 ++++-------------
>  ip/iprule.c           | 39 +++++++----------------
>  ip/iptoken.c          | 11 ++-----
>  ip/iptunnel.c         |  8 ++---
>  ip/ipxfrm.c           | 29 ++++-------------
>  ip/link_gre.c         |  5 ++-
>  ip/link_gre6.c        |  5 ++-
>  ip/link_ip6tnl.c      | 11 ++-----
>  ip/link_iptnl.c       |  7 ++---
>  ip/link_vti.c         |  5 ++-
>  ip/link_vti6.c        |  5 ++-
>  ip/tcp_metrics.c      |  6 ++--
>  ip/xfrm_monitor.c     | 16 ++--------
>  lib/color.c           | 30 +++++++++++++++++-
>  lib/ll_addr.c         | 15 ++-------
>  lib/utils.c           | 20 ++++++++++--
>  misc/ss.c             |  4 +--
>  tc/f_flower.c         | 11 ++-----
>  tc/m_nat.c            |  4 +--
>  33 files changed, 187 insertions(+), 323 deletions(-)
> 

Applied thanks

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-03-27 17:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 3/7] make format_host non-reentrant by default Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 4/7] utils: make rt_addr_n2a() " Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 5/7] lib/utils: introduce format_host_rta() Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta() Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit Phil Sutter
2016-03-27 17:38 ` [iproute PATCH 0/7] Refactor some internal library functions Stephen Hemminger

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.