Module Name:    src
Committed By:   nat
Date:           Tue Aug 16 10:47:11 UTC 2022

Modified Files:
        src/sbin/ifconfig: af_inetany.c

Log Message:
No need address every time to specify broadcast and netmask.

Previously ifconfig would fail silently.

Addresses PR bin/10911.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sbin/ifconfig/af_inetany.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/ifconfig/af_inetany.c
diff -u src/sbin/ifconfig/af_inetany.c:1.19 src/sbin/ifconfig/af_inetany.c:1.20
--- src/sbin/ifconfig/af_inetany.c:1.19	Sun Jun  7 06:02:58 2020
+++ src/sbin/ifconfig/af_inetany.c	Tue Aug 16 10:47:10 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: af_inetany.c,v 1.19 2020/06/07 06:02:58 thorpej Exp $	*/
+/*	$NetBSD: af_inetany.c,v 1.20 2022/08/16 10:47:10 nat Exp $	*/
 
 /*-
  * Copyright (c) 2008 David Young.  All rights reserved.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: af_inetany.c,v 1.19 2020/06/07 06:02:58 thorpej Exp $");
+__RCSID("$NetBSD: af_inetany.c,v 1.20 2022/08/16 10:47:10 nat Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -68,12 +68,15 @@ commit_address(prop_dictionary_t env, pr
     const struct afparam *param)
 {
 	const char *ifname;
+	struct ifreq ifr;
 	int af, rc, s;
 	bool alias, delete, replace;
 	prop_data_t d;
 	const struct paddr_prefix *addr, *brd, *dst, *mask;
 	unsigned short flags;
 
+	addr = NULL;
+
 	if ((af = getaf(env)) == -1)
 		af = AF_INET;
 
@@ -89,8 +92,23 @@ commit_address(prop_dictionary_t env, pr
 	if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
 		addr = prop_data_value(d);
 	else if (!prop_dictionary_get_bool(env, "alias", &alias) || alias ||
-	    param->gifaddr.cmd == 0)
-		return;
+	    param->gifaddr.cmd == 0) {
+		if (addr == NULL) {
+			static struct paddr_prefix existingaddr;
+
+			memset(&ifr, 0, sizeof(ifr));
+			estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+			if (prog_ioctl(s, SIOCGIFADDR, &ifr) == -1) {
+				if (errno == EADDRNOTAVAIL ||
+				    errno == EAFNOSUPPORT)
+					return;
+				err(EXIT_FAILURE,"SIOCGIFADDR");
+			}
+
+			existingaddr.pfx_addr = ifr.ifr_addr;
+			addr = &existingaddr;
+		}
+	}
 	else if (prog_ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == -1)
 		err(EXIT_FAILURE, "%s", param->gifaddr.desc);
 	else if (prog_ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)

Reply via email to