Blame SOURCES/0053-nft-arp-Make-use-of-ipv4_addr_to_string.patch

082114
From 201fd565a1ce44b4af11ce9f245b2fa77c026fed Mon Sep 17 00:00:00 2001
082114
From: Phil Sutter <phil@nwl.cc>
082114
Date: Tue, 27 Apr 2021 10:02:34 +0200
082114
Subject: [PATCH] nft-arp: Make use of ipv4_addr_to_string()
082114
082114
This eliminates quite a bit of redundant code apart from also dropping
082114
use of obsolete function gethostbyaddr().
082114
082114
Signed-off-by: Phil Sutter <phil@nwl.cc>
082114
(cherry picked from commit 1e984079817a3c804eae25dea937d63d18c57a6c)
082114
---
082114
 iptables/nft-arp.c | 99 ++++------------------------------------------
082114
 iptables/xshared.c |  6 +--
082114
 iptables/xshared.h |  3 ++
082114
 3 files changed, 14 insertions(+), 94 deletions(-)
082114
082114
diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
082114
index ec8147dd58c0d..7c61c31a13c40 100644
082114
--- a/iptables/nft-arp.c
082114
+++ b/iptables/nft-arp.c
082114
@@ -42,78 +42,6 @@ char *arp_opcodes[] =
082114
 	"ARP_NAK",
082114
 };
082114
 
082114
-static char *
082114
-addr_to_dotted(const struct in_addr *addrp)
082114
-{
082114
-	static char buf[20];
082114
-	const unsigned char *bytep;
082114
-
082114
-	bytep = (const unsigned char *) &(addrp->s_addr);
082114
-	sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
082114
-	return buf;
082114
-}
082114
-
082114
-static char *
082114
-addr_to_host(const struct in_addr *addr)
082114
-{
082114
-	struct hostent *host;
082114
-
082114
-	if ((host = gethostbyaddr((char *) addr,
082114
-					sizeof(struct in_addr), AF_INET)) != NULL)
082114
-		return (char *) host->h_name;
082114
-
082114
-	return (char *) NULL;
082114
-}
082114
-
082114
-static char *
082114
-addr_to_network(const struct in_addr *addr)
082114
-{
082114
-	struct netent *net;
082114
-
082114
-	if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
082114
-		return (char *) net->n_name;
082114
-
082114
-	return (char *) NULL;
082114
-}
082114
-
082114
-static char *
082114
-addr_to_anyname(const struct in_addr *addr)
082114
-{
082114
-	char *name;
082114
-
082114
-	if ((name = addr_to_host(addr)) != NULL ||
082114
-		(name = addr_to_network(addr)) != NULL)
082114
-		return name;
082114
-
082114
-	return addr_to_dotted(addr);
082114
-}
082114
-
082114
-static char *
082114
-mask_to_dotted(const struct in_addr *mask)
082114
-{
082114
-	int i;
082114
-	static char buf[22];
082114
-	u_int32_t maskaddr, bits;
082114
-
082114
-	maskaddr = ntohl(mask->s_addr);
082114
-
082114
-	if (maskaddr == 0xFFFFFFFFL)
082114
-		/* we don't want to see "/32" */
082114
-		return "";
082114
-
082114
-	i = 32;
082114
-	bits = 0xFFFFFFFEL;
082114
-	while (--i >= 0 && maskaddr != bits)
082114
-		bits <<= 1;
082114
-	if (i >= 0)
082114
-		sprintf(buf, "/%d", i);
082114
-	else
082114
-		/* mask was not a decent combination of 1's and 0's */
082114
-		snprintf(buf, sizeof(buf), "/%s", addr_to_dotted(mask));
082114
-
082114
-	return buf;
082114
-}
082114
-
082114
 static bool need_devaddr(struct arpt_devaddr_info *info)
082114
 {
082114
 	int i;
082114
@@ -403,7 +331,6 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 				       unsigned int format)
082114
 {
082114
 	const struct arpt_entry *fw = &cs->arp;
082114
-	char buf[BUFSIZ];
082114
 	char iface[IFNAMSIZ+2];
082114
 	const char *sep = "";
082114
 	int print_iface = 0;
082114
@@ -450,15 +377,10 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 	}
082114
 
082114
 	if (fw->arp.smsk.s_addr != 0L) {
082114
-		printf("%s%s", sep, fw->arp.invflags & IPT_INV_SRCIP
082114
-			? "! " : "");
082114
-		if (format & FMT_NUMERIC)
082114
-			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.src)));
082114
-		else
082114
-			sprintf(buf, "%s", addr_to_anyname(&(fw->arp.src)));
082114
-		strncat(buf, mask_to_dotted(&(fw->arp.smsk)),
082114
-			sizeof(buf) - strlen(buf) - 1);
082114
-		printf("-s %s", buf);
082114
+		printf("%s%s-s %s", sep,
082114
+		       fw->arp.invflags & IPT_INV_SRCIP ? "! " : "",
082114
+		       ipv4_addr_to_string(&fw->arp.src,
082114
+					   &fw->arp.smsk, format));
082114
 		sep = " ";
082114
 	}
082114
 
082114
@@ -476,15 +398,10 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 after_devsrc:
082114
 
082114
 	if (fw->arp.tmsk.s_addr != 0L) {
082114
-		printf("%s%s", sep, fw->arp.invflags & IPT_INV_DSTIP
082114
-			? "! " : "");
082114
-		if (format & FMT_NUMERIC)
082114
-			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.tgt)));
082114
-		else
082114
-			sprintf(buf, "%s", addr_to_anyname(&(fw->arp.tgt)));
082114
-		strncat(buf, mask_to_dotted(&(fw->arp.tmsk)),
082114
-			sizeof(buf) - strlen(buf) - 1);
082114
-		printf("-d %s", buf);
082114
+		printf("%s%s-d %s", sep,
082114
+		       fw->arp.invflags & IPT_INV_DSTIP ? "! " : "",
082114
+		       ipv4_addr_to_string(&fw->arp.tgt,
082114
+					   &fw->arp.tmsk, format));
082114
 		sep = " ";
082114
 	}
082114
 
082114
diff --git a/iptables/xshared.c b/iptables/xshared.c
082114
index 16c58914e59a5..e3c8072b5ca96 100644
082114
--- a/iptables/xshared.c
082114
+++ b/iptables/xshared.c
082114
@@ -546,9 +546,9 @@ void debug_print_argv(struct argv_store *store)
082114
 }
082114
 #endif
082114
 
082114
-static const char *ipv4_addr_to_string(const struct in_addr *addr,
082114
-				       const struct in_addr *mask,
082114
-				       unsigned int format)
082114
+const char *ipv4_addr_to_string(const struct in_addr *addr,
082114
+				const struct in_addr *mask,
082114
+				unsigned int format)
082114
 {
082114
 	static char buf[BUFSIZ];
082114
 
082114
diff --git a/iptables/xshared.h b/iptables/xshared.h
082114
index 490b19ade5106..e4015c00e2a35 100644
082114
--- a/iptables/xshared.h
082114
+++ b/iptables/xshared.h
082114
@@ -200,6 +200,9 @@ void debug_print_argv(struct argv_store *store);
082114
 #  define debug_print_argv(...) /* nothing */
082114
 #endif
082114
 
082114
+const char *ipv4_addr_to_string(const struct in_addr *addr,
082114
+				const struct in_addr *mask,
082114
+				unsigned int format);
082114
 void print_ipv4_addresses(const struct ipt_entry *fw, unsigned int format);
082114
 void print_ipv6_addresses(const struct ip6t_entry *fw6, unsigned int format);
082114
 
082114
-- 
082114
2.31.1
082114