git: 63ad5ce7335b - main - ifconfig: simplify ifconfig() by factoring out ifa add/del logic

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Sun, 21 May 2023 09:43:02 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=63ad5ce7335be6a0cc4a7bf27fe03d72c28ad401

commit 63ad5ce7335be6a0cc4a7bf27fe03d72c28ad401
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-05-20 11:23:04 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-05-21 09:42:11 +0000

    ifconfig: simplify ifconfig() by factoring out ifa add/del logic
    
    Reviewed by:    kp
    MFC after:      2 weeks
    Differential Revision: https://reviews.freebsd.org/D40180
---
 sbin/ifconfig/ifconfig.c | 83 +++++++++++++++++++++++++++---------------------
 1 file changed, 47 insertions(+), 36 deletions(-)

diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index 16ab651261d1..c5e7b7befc72 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -92,10 +92,10 @@ struct	ifreq ifr;
 char	name[IFNAMSIZ];
 char	*descr = NULL;
 size_t	descrlen = 64;
-int	setaddr;
-int	setmask;
-int	doalias;
-int	clearaddr;
+static int	setaddr;
+static int	setmask;
+static int	doalias;
+static int	clearaddr;
 int	newaddr = 1;
 int	verbose;
 int	printifname = 0;
@@ -968,6 +968,45 @@ static void setifdstaddr(const char *, int, int, const struct afswtch *);
 static const struct cmd setifdstaddr_cmd =
 	DEF_CMD("ifdstaddr", 0, setifdstaddr);
 
+static void
+delifaddr(int s, const struct afswtch *afp)
+{
+	if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
+		warnx("interface %s cannot change %s addresses!",
+		    name, afp->af_name);
+		clearaddr = 0;
+		return;
+	}
+
+	strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
+		sizeof ifr.ifr_name);
+	int ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
+	if (ret < 0) {
+		if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
+			/* means no previous address for interface */
+		} else
+			Perror("ioctl (SIOCDIFADDR)");
+	}
+}
+
+static void
+addifaddr(int s, const struct afswtch *afp)
+{
+	if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
+		warnx("interface %s cannot change %s addresses!",
+		      name, afp->af_name);
+		newaddr = 0;
+		return;
+	}
+
+	if (setaddr || setmask) {
+		strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
+			sizeof ifr.ifr_name);
+		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
+			Perror("ioctl (SIOCAIFADDR)");
+	}
+}
+
 int
 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
 {
@@ -1088,38 +1127,10 @@ top:
 	/*
 	 * Do deferred operations.
 	 */
-	if (clearaddr) {
-		if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
-			warnx("interface %s cannot change %s addresses!",
-			      name, afp->af_name);
-			clearaddr = 0;
-		}
-	}
-	if (clearaddr) {
-		int ret;
-		strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
-			sizeof ifr.ifr_name);
-		ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
-		if (ret < 0) {
-			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
-				/* means no previous address for interface */
-			} else
-				Perror("ioctl (SIOCDIFADDR)");
-		}
-	}
-	if (newaddr) {
-		if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
-			warnx("interface %s cannot change %s addresses!",
-			      name, afp->af_name);
-			newaddr = 0;
-		}
-	}
-	if (newaddr && (setaddr || setmask)) {
-		strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
-			sizeof ifr.ifr_name);
-		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
-			Perror("ioctl (SIOCAIFADDR)");
-	}
+	if (clearaddr)
+		delifaddr(s, afp);
+	if (newaddr)
+		addifaddr(s, afp);
 
 	close(s);
 	return(0);