Don't export in_alias(). Pass in_alias() a more useful template.

Misc. cosmetic changes.  Delete dead code.  Treat the address
preference as int16_t, since that is its real type.
This commit is contained in:
dyoung 2008-05-11 23:25:46 +00:00
parent 79c3bfd61b
commit 50a22265be
2 changed files with 30 additions and 45 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: af_inet.c,v 1.9 2008/05/11 22:07:23 dyoung Exp $ */
/* $NetBSD: af_inet.c,v 1.10 2008/05/11 23:25:46 dyoung Exp $ */
/*
* Copyright (c) 1983, 1993
@ -31,7 +31,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: af_inet.c,v 1.9 2008/05/11 22:07:23 dyoung Exp $");
__RCSID("$NetBSD: af_inet.c,v 1.10 2008/05/11 23:25:46 dyoung Exp $");
#endif /* not lint */
#include <sys/param.h>
@ -59,13 +59,14 @@ __RCSID("$NetBSD: af_inet.c,v 1.9 2008/05/11 22:07:23 dyoung Exp $");
#include "af_inet.h"
#include "af_inetany.h"
static void in_alias(const char *, prop_dictionary_t, prop_dictionary_t,
struct in_aliasreq *);
static void in_preference(const char *, const struct sockaddr *);
void
in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
struct ifreq *creq)
struct in_aliasreq *creq)
{
struct sockaddr_in *sin;
struct ifreq ifr;
int alias, s;
unsigned short flags;
@ -85,18 +86,14 @@ in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
memset(&ifr, 0, sizeof(ifr));
estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
return;
} else
warn("SIOCGIFADDR");
warn("SIOCGIFADDR");
}
/* If creq and ifr are the same address, this is not an alias. */
if (memcmp(&ifr.ifr_addr, &creq->ifr_addr, sizeof(creq->ifr_addr)) == 0)
if (memcmp(&ifr.ifr_addr, &creq->ifra_addr, sizeof(ifr.ifr_addr)) == 0)
alias = 0;
memset(&in_addreq, 0, sizeof(in_addreq));
estrlcpy(in_addreq.ifra_name, ifname, sizeof(in_addreq.ifra_name));
memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
sizeof(in_addreq.ifra_addr));
in_addreq = *creq;
if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
return;
@ -104,27 +101,24 @@ in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
warn("SIOCGIFALIAS");
}
sin = &in_addreq.ifra_addr;
printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(sin->sin_addr));
printf("\tinet %s%s", alias ? "alias " : "",
inet_ntoa(in_addreq.ifra_addr.sin_addr));
if (getifflags(env, oenv, &flags) == -1)
err(EXIT_FAILURE, "%s: getifflags", __func__);
if (flags & IFF_POINTOPOINT) {
sin = &in_addreq.ifra_dstaddr;
printf(" -> %s", inet_ntoa(sin->sin_addr));
}
if (flags & IFF_POINTOPOINT)
printf(" -> %s", inet_ntoa(in_addreq.ifra_dstaddr.sin_addr));
sin = &in_addreq.ifra_mask;
printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
printf(" netmask 0x%x", ntohl(in_addreq.ifra_mask.sin_addr.s_addr));
if (flags & IFF_BROADCAST) {
sin = &in_addreq.ifra_broadaddr;
printf(" broadcast %s", inet_ntoa(sin->sin_addr));
printf(" broadcast %s",
inet_ntoa(in_addreq.ifra_broadaddr.sin_addr));
}
}
static uint16_t
static int16_t
in_get_preference(const char *ifname, const struct sockaddr *sa)
{
struct if_addrprefreq ifap;
@ -136,7 +130,7 @@ in_get_preference(const char *ifname, const struct sockaddr *sa)
err(EXIT_FAILURE, "socket");
}
memset(&ifap, 0, sizeof(ifap));
strncpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
estrlcpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
memcpy(&ifap.ifap_addr, sa, MIN(sizeof(ifap.ifap_addr), sa->sa_len));
if (ioctl(s, SIOCGIFADDRPREF, &ifap) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
@ -149,20 +143,20 @@ in_get_preference(const char *ifname, const struct sockaddr *sa)
static void
in_preference(const char *ifname, const struct sockaddr *sa)
{
uint16_t preference;
int16_t preference;
if (lflag)
return;
preference = in_get_preference(ifname, sa);
printf(" preference %" PRIu16, preference);
printf(" preference %" PRId16, preference);
}
void
in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
{
struct ifaddrs *ifap, *ifa;
struct ifreq ifr;
struct in_aliasreq ifra;
int printprefs = 0;
const char *ifname;
@ -171,10 +165,11 @@ in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
/* Print address preference numbers if any address has a non-zero
* preference assigned.
*/
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
@ -184,30 +179,22 @@ in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
break;
}
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
if (sizeof(ifra.ifra_addr) < ifa->ifa_addr->sa_len)
continue;
memset(&ifr, 0, sizeof(ifr));
estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
in_alias(ifa->ifa_name, env, oenv, &ifr);
memset(&ifra, 0, sizeof(ifra));
estrlcpy(ifra.ifra_name, ifa->ifa_name, sizeof(ifra.ifra_name));
memcpy(&ifra.ifra_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
in_alias(ifa->ifa_name, env, oenv, &ifra);
if (printprefs)
in_preference(ifa->ifa_name, ifa->ifa_addr);
printf("\n");
}
if (ifa != NULL) {
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
}
}
freeifaddrs(ifap);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: af_inet.h,v 1.5 2008/05/08 07:13:20 dyoung Exp $ */
/* $NetBSD: af_inet.h,v 1.6 2008/05/11 23:25:46 dyoung Exp $ */
/*
* Copyright (c) 1983, 1993
@ -33,8 +33,6 @@
#include <netinet/in.h>
#include "parse.h"
void in_alias(const char *, prop_dictionary_t, prop_dictionary_t,
struct ifreq *);
void in_status(prop_dictionary_t, prop_dictionary_t, bool);
void in_commit_address(prop_dictionary_t, prop_dictionary_t);