git: 2c1296a3c547 - main - setkey(8): extract prefixlen calculation info helper

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 29 May 2023 22:10:08 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2c1296a3c5473651aa87eefea25e7b7475e9e16c

commit 2c1296a3c5473651aa87eefea25e7b7475e9e16c
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-05-25 12:20:00 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-05-29 22:10:02 +0000

    setkey(8): extract prefixlen calculation info helper
    
    While there, hide AF_INET case under #ifdef INET.
    
    Reviewed by:    ae
    Discussed with: bz
    Sponsored by:   NVidia networking
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D40300
---
 sbin/setkey/parse.y | 56 ++++++++++++++++++++++-------------------------------
 1 file changed, 23 insertions(+), 33 deletions(-)

diff --git a/sbin/setkey/parse.y b/sbin/setkey/parse.y
index 3d3ed123d4f0..d279546fada6 100644
--- a/sbin/setkey/parse.y
+++ b/sbin/setkey/parse.y
@@ -787,6 +787,23 @@ setkeymsg0(struct sadb_msg *msg, unsigned type, unsigned satype, size_t l)
 	return 0;
 }
 
+static int
+setkeymsg_plen(struct addrinfo *s)
+{
+	switch (s->ai_addr->sa_family) {
+#ifdef INET
+	case AF_INET:
+		return (sizeof(struct in_addr) << 3);
+#endif
+#ifdef INET6
+	case AF_INET6:
+		return (sizeof(struct in6_addr) << 3);
+#endif
+	default:
+		return (-1);
+	}
+}
+
 /* XXX NO BUFFER OVERRUN CHECK! BAD BAD! */
 static int
 setkeymsg_spdaddr(unsigned type, unsigned upper, vchar_t *policy,
@@ -825,18 +842,9 @@ setkeymsg_spdaddr(unsigned type, unsigned upper, vchar_t *policy,
 
 			if (s->ai_addr->sa_family != d->ai_addr->sa_family)
 				continue;
-			switch (s->ai_addr->sa_family) {
-			case AF_INET:
-				plen = sizeof(struct in_addr) << 3;
-				break;
-#ifdef INET6
-			case AF_INET6:
-				plen = sizeof(struct in6_addr) << 3;
-				break;
-#endif
-			default:
+			plen = setkeymsg_plen(s);
+			if (plen == -1)
 				continue;
-			}
 
 			/* set src */
 			sa = s->ai_addr;
@@ -954,18 +962,9 @@ setkeymsg_addr(unsigned type, unsigned satype, struct addrinfo *srcs,
 
 			if (s->ai_addr->sa_family != d->ai_addr->sa_family)
 				continue;
-			switch (s->ai_addr->sa_family) {
-			case AF_INET:
-				plen = sizeof(struct in_addr) << 3;
-				break;
-#ifdef INET6
-			case AF_INET6:
-				plen = sizeof(struct in6_addr) << 3;
-				break;
-#endif
-			default:
+			plen = setkeymsg_plen(s);
+			if (plen == -1)
 				continue;
-			}
 
 			/* set src */
 			sa = s->ai_addr;
@@ -1140,18 +1139,9 @@ setkeymsg_add(unsigned type, unsigned satype, struct addrinfo *srcs,
 
 			if (s->ai_addr->sa_family != d->ai_addr->sa_family)
 				continue;
-			switch (s->ai_addr->sa_family) {
-			case AF_INET:
-				plen = sizeof(struct in_addr) << 3;
-				break;
-#ifdef INET6
-			case AF_INET6:
-				plen = sizeof(struct in6_addr) << 3;
-				break;
-#endif
-			default:
+			plen = setkeymsg_plen(s);
+			if (plen == -1)
 				continue;
-			}
 
 			/* set src */
 			sa = s->ai_addr;