Module Name:    src
Committed By:   dyoung
Date:           Tue Apr 21 22:46:40 UTC 2009

Modified Files:
        src/sbin/ifconfig: af_link.c ifconfig.c util.c util.h

Log Message:
To make sure that we always print the active link-layer address in the
'address: ' field, don't treat the first address as the active address,
but search the link-layer addresses for the ones flagged IFLR_ACTIVE,
and print those.  Extract a subroutine, print_link_addresses(), for
printing link-layer addresses.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sbin/ifconfig/af_link.c
cvs rdiff -u -r1.217 -r1.218 src/sbin/ifconfig/ifconfig.c
cvs rdiff -u -r1.9 -r1.10 src/sbin/ifconfig/util.c
cvs rdiff -u -r1.6 -r1.7 src/sbin/ifconfig/util.h

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_link.c
diff -u src/sbin/ifconfig/af_link.c:1.5 src/sbin/ifconfig/af_link.c:1.6
--- src/sbin/ifconfig/af_link.c:1.5	Tue Apr 21 21:42:35 2009
+++ src/sbin/ifconfig/af_link.c	Tue Apr 21 22:46:39 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: af_link.c,v 1.5 2009/04/21 21:42:35 dyoung Exp $	*/
+/*	$NetBSD: af_link.c,v 1.6 2009/04/21 22:46:39 dyoung Exp $	*/
 
 /*-
  * Copyright (c) 2008 David Young.  All rights reserved.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: af_link.c,v 1.5 2009/04/21 21:42:35 dyoung Exp $");
+__RCSID("$NetBSD: af_link.c,v 1.6 2009/04/21 22:46:39 dyoung Exp $");
 #endif /* not lint */
 
 #include <sys/param.h> 
@@ -74,53 +74,7 @@
 static void
 link_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
 {
-	char hbuf[NI_MAXHOST];
-	const char *ifname;
-	int s;
-	struct ifaddrs *ifa, *ifap;
-	const struct sockaddr_dl *sdl;
-	struct if_laddrreq iflr;
-
-	if ((ifname = getifname(env)) == NULL)
-		err(EXIT_FAILURE, "%s: getifname", __func__);
-
-	if ((s = getsock(AF_LINK)) == -1)
-		err(EXIT_FAILURE, "%s: getsock", __func__);
-
-	if (getifaddrs(&ifap) == -1)
-		err(EXIT_FAILURE, "%s: getifaddrs", __func__);
-
-	memset(&iflr, 0, sizeof(iflr));
-
-	strlcpy(iflr.iflr_name, ifname, sizeof(iflr.iflr_name));
-
-	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
-		if (strcmp(ifname, ifa->ifa_name) != 0)
-			continue;
-		if (ifa->ifa_addr->sa_family != AF_LINK)
-			continue;
-		if (ifa->ifa_data != NULL)
-			continue;
-
-		sdl = satocsdl(ifa->ifa_addr);
-
-		memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
-		    sizeof(iflr.addr)));
-		iflr.flags = IFLR_PREFIX;
-		iflr.prefixlen = sdl->sdl_alen * NBBY;
-
-		if (ioctl(s, SIOCGLIFADDR, &iflr) == -1)
-			err(EXIT_FAILURE, "%s: ioctl", __func__);
-
-		if ((iflr.flags & IFLR_ACTIVE) != 0)
-			continue;
-
-		if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
-			hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
-		    hbuf[0] != '\0')
-			printf("\tlink %s\n", hbuf);
-	}
-	freeifaddrs(ifap);
+	print_link_addresses(env, false);
 }
 
 static int

Index: src/sbin/ifconfig/ifconfig.c
diff -u src/sbin/ifconfig/ifconfig.c:1.217 src/sbin/ifconfig/ifconfig.c:1.218
--- src/sbin/ifconfig/ifconfig.c:1.217	Tue Apr 21 22:13:10 2009
+++ src/sbin/ifconfig/ifconfig.c	Tue Apr 21 22:46:39 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifconfig.c,v 1.217 2009/04/21 22:13:10 dyoung Exp $	*/
+/*	$NetBSD: ifconfig.c,v 1.218 2009/04/21 22:46:39 dyoung Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: ifconfig.c,v 1.217 2009/04/21 22:13:10 dyoung Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.218 2009/04/21 22:46:39 dyoung Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -1152,7 +1152,6 @@
 	statistics_func_t *statistics_f;
 	struct ifdatareq ifdr;
 	struct ifreq ifr;
-	char hbuf[NI_MAXHOST];
 	char fbuf[BUFSIZ];
 	int af, s;
 	const char *ifname;
@@ -1202,11 +1201,7 @@
 	SIMPLEQ_FOREACH(status_f, &status_funcs, f_next)
 		(*status_f->f_func)(env, oenv);
 
-	if (sdl != NULL &&
-	    getnameinfo(sdl, sdl->sa_len,
-		hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
-	    hbuf[0] != '\0')
-		printf("\taddress: %s\n", hbuf);
+	print_link_addresses(env, true);
 
 	media_status(env, oenv);
 

Index: src/sbin/ifconfig/util.c
diff -u src/sbin/ifconfig/util.c:1.9 src/sbin/ifconfig/util.c:1.10
--- src/sbin/ifconfig/util.c:1.9	Sun Jan 18 00:24:29 2009
+++ src/sbin/ifconfig/util.c	Tue Apr 21 22:46:39 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: util.c,v 1.9 2009/01/18 00:24:29 lukem Exp $	*/
+/*	$NetBSD: util.c,v 1.10 2009/04/21 22:46:39 dyoung Exp $	*/
 
 /*-
  * Copyright (c) 2008 David Young.  All rights reserved.
@@ -27,12 +27,13 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.9 2009/01/18 00:24:29 lukem Exp $");
+__RCSID("$NetBSD: util.c,v 1.10 2009/04/21 22:46:39 dyoung Exp $");
 #endif /* not lint */
 
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <netdb.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -40,9 +41,14 @@
 #include <unistd.h>
 #include <util.h>
 
+#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/socket.h>
+#include <ifaddrs.h>
+
 #include <sys/ioctl.h>
 #include <net/if.h>
+#include <net/if_dl.h>
 #include <netinet/in.h>		/* XXX */
 
 #include "env.h"
@@ -230,6 +236,60 @@
 	return direct_ioctl(env, cmd, &ifr);
 }
 
+void
+print_link_addresses(prop_dictionary_t env, bool print_active_only)
+{
+	char hbuf[NI_MAXHOST];
+	const char *ifname;
+	int s;
+	struct ifaddrs *ifa, *ifap;
+	const struct sockaddr_dl *sdl;
+	struct if_laddrreq iflr;
+
+	if ((ifname = getifname(env)) == NULL)
+		err(EXIT_FAILURE, "%s: getifname", __func__);
+
+	if ((s = getsock(AF_LINK)) == -1)
+		err(EXIT_FAILURE, "%s: getsock", __func__);
+
+	if (getifaddrs(&ifap) == -1)
+		err(EXIT_FAILURE, "%s: getifaddrs", __func__);
+
+	memset(&iflr, 0, sizeof(iflr));
+
+	strlcpy(iflr.iflr_name, ifname, sizeof(iflr.iflr_name));
+
+	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
+		if (strcmp(ifname, ifa->ifa_name) != 0)
+			continue;
+		if (ifa->ifa_addr->sa_family != AF_LINK)
+			continue;
+		if (ifa->ifa_data != NULL)
+			continue;
+
+		sdl = satocsdl(ifa->ifa_addr);
+
+		memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
+		    sizeof(iflr.addr)));
+		iflr.flags = IFLR_PREFIX;
+		iflr.prefixlen = sdl->sdl_alen * NBBY;
+
+		if (ioctl(s, SIOCGLIFADDR, &iflr) == -1)
+			err(EXIT_FAILURE, "%s: ioctl", __func__);
+
+		if (((iflr.flags & IFLR_ACTIVE) != 0) != print_active_only)
+			continue;
+
+		if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
+			hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
+		    hbuf[0] != '\0') {
+			printf("\t%s %s\n",
+			    print_active_only ? "address:" : "link", hbuf);
+		}
+	}
+	freeifaddrs(ifap);
+}
+
 #ifdef INET6
 /* KAME idiosyncrasy */
 void

Index: src/sbin/ifconfig/util.h
diff -u src/sbin/ifconfig/util.h:1.6 src/sbin/ifconfig/util.h:1.7
--- src/sbin/ifconfig/util.h:1.6	Wed Jul  2 07:44:15 2008
+++ src/sbin/ifconfig/util.h	Tue Apr 21 22:46:39 2009
@@ -13,6 +13,7 @@
 	SIMPLEQ_ENTRY(afswtch)	af_next;
 };
 
+void print_link_addresses(prop_dictionary_t, bool);
 const char *get_string(const char *, const char *, u_int8_t *, int *);
 const struct afswtch *lookup_af_byname(const char *);
 const struct afswtch *lookup_af_bynum(int);

Reply via email to