Merge from NetBSD rev. 1.2 (drochner): Do the address calculations inside

the data delivered by SIOCGIFCONF correctly (this isn't a plain array!),
and sort the checks a bit to avoid duplicates in the interface list.
This commit is contained in:
Tim J. Robbins 2004-01-28 05:55:13 +00:00
parent 66c59e7562
commit 8299402aca
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=125130

View File

@ -30,6 +30,7 @@
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $ * $Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $
* $FreeBSD$
*/ */
#include <sys/param.h> #include <sys/param.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -107,6 +108,7 @@ nb_enum_if(struct nb_ifdesc **iflist, int maxif)
struct in_addr iaddr, imask; struct in_addr iaddr, imask;
char *ifrdata, *iname; char *ifrdata, *iname;
int s, rdlen, ifcnt, error, iflags, i; int s, rdlen, ifcnt, error, iflags, i;
size_t ifrlen;
*iflist = NULL; *iflist = NULL;
s = socket(AF_INET, SOCK_DGRAM, 0); s = socket(AF_INET, SOCK_DGRAM, 0);
@ -124,29 +126,34 @@ nb_enum_if(struct nb_ifdesc **iflist, int maxif)
if (ioctl(s, SIOCGIFCONF, &ifc) != 0) { if (ioctl(s, SIOCGIFCONF, &ifc) != 0) {
error = errno; error = errno;
goto bad; goto bad;
} }
ifrqp = ifc.ifc_req; ifrqp = ifc.ifc_req;
ifcnt = ifc.ifc_len / sizeof(struct ifreq); ifcnt = ifc.ifc_len / sizeof(struct ifreq);
error = 0; error = 0;
for (i = 0; i < ifcnt; i++, ifrqp++) { for (i = 0; i < ifcnt; i++) {
if (ioctl(s, SIOCGIFFLAGS, ifrqp) != 0) ifrlen = sizeof(struct ifreq);
continue; if (ifrqp->ifr_addr.sa_len > sizeof(struct sockaddr))
iflags = ifrqp->ifr_flags; ifrlen += ifrqp->ifr_addr.sa_len
if ((iflags & IFF_UP) == 0 || (iflags & IFF_BROADCAST) == 0) - sizeof(struct sockaddr);
continue;
if (ioctl(s, SIOCGIFADDR, ifrqp) != 0 || if (ifrqp->ifr_addr.sa_family != AF_INET)
ifrqp->ifr_addr.sa_family != AF_INET) goto next;
continue;
iname = ifrqp->ifr_name; iname = ifrqp->ifr_name;
if (strlen(iname) >= sizeof(ifd->id_name)) if (strlen(iname) >= sizeof(ifd->id_name))
continue; goto next;
iaddr = (*(struct sockaddr_in *)&ifrqp->ifr_addr).sin_addr; iaddr = (*(struct sockaddr_in *)&ifrqp->ifr_addr).sin_addr;
if (ioctl(s, SIOCGIFNETMASK, ifrqp) != 0) if (ioctl(s, SIOCGIFNETMASK, ifrqp) != 0)
continue; goto next;
imask = ((struct sockaddr_in *)&ifrqp->ifr_addr)->sin_addr; imask = ((struct sockaddr_in *)&ifrqp->ifr_addr)->sin_addr;
if (ioctl(s, SIOCGIFFLAGS, ifrqp) != 0)
goto next;
iflags = ifrqp->ifr_flags;
if ((iflags & IFF_UP) == 0 || (iflags & IFF_BROADCAST) == 0)
goto next;
ifd = malloc(sizeof(struct nb_ifdesc)); ifd = malloc(sizeof(struct nb_ifdesc));
if (ifd == NULL) if (ifd == NULL)
return ENOMEM; return ENOMEM;
@ -157,6 +164,9 @@ nb_enum_if(struct nb_ifdesc **iflist, int maxif)
ifd->id_mask = imask; ifd->id_mask = imask;
ifd->id_next = *iflist; ifd->id_next = *iflist;
*iflist = ifd; *iflist = ifd;
next:
ifrqp = (struct ifreq *)((caddr_t)ifrqp + ifrlen);
} }
bad: bad:
free(ifrdata); free(ifrdata);
@ -198,4 +208,4 @@ nb_hostlookup(struct nb_name *np, const char *server, const char *hint,
nb_snbfree(snb); nb_snbfree(snb);
return error; return error;
} }
*/ */