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.
This commit is contained in:
dyoung 2009-04-21 22:46:39 +00:00
parent 65b8e3da03
commit 740c7b59d2
4 changed files with 69 additions and 59 deletions

View File

@ -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_constructor(void) __attribute__((constructor));
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

View File

@ -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 @@ status(const struct sockaddr *sdl, prop_dictionary_t env,
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 @@ status(const struct sockaddr *sdl, prop_dictionary_t env,
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);

View File

@ -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 @@ __RCSID("$NetBSD: util.c,v 1.9 2009/01/18 00:24:29 lukem Exp $");
#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 @@ indirect_ioctl(prop_dictionary_t env, unsigned long cmd, void *data)
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

View File

@ -13,6 +13,7 @@ struct afswtch {
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);