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

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