FreeBSD/usr.sbin/mtest/mtest.c
/*
 * Internal: Map an interface index to primary IPv4 address.
 * This is somewhat inefficient. This is a useful enough operation
 * that it probably belongs in the C library.
 * Return zero if found, -1 on error, 1 on not found.
 */
Map an interface index to primary IPv4 address.
This is somewhat inefficient. This is a useful enough operation
that it probably belongs in the C library.
Return zero if found, -1 on error, 1 on not found.
Map an interface index to primary IPv4 address . This is somewhat inefficient . This is a useful enough operation that it probably belongs in the C library . Return zero if found - 1 on error 1 on not found .
static int
__ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina)
{
	char		 ifname[IFNAMSIZ];
	struct ifaddrs	*ifa;
	struct ifaddrs	*ifaddrs;
	sockunion_t	*psu;
	int		 retval;
	assert(ifindex != 0);
	retval = -1;
	if (if_indextoname(ifindex, ifname) == NULL)
		return (retval);
	if (getifaddrs(&ifaddrs) < 0)
		return (retval);
	retval = 1;
	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
		if (strcmp(ifa->ifa_name, ifname) != 0)
			continue;
		psu = (sockunion_t *)ifa->ifa_addr;
		if (psu && psu->sa.sa_family == AF_INET) {
			retval = 0;
			memcpy(pina, &psu->sin.sin_addr,
			    sizeof(struct in_addr));
			break;
		}
	}
	if (retval != 0)
		errno = EADDRNOTAVAIL;	
	freeifaddrs(ifaddrs);
	return (retval);
}
static int __ifindex_to_primary_ip ( const uint32_t ifindex , struct in_addr * pina ) { char ifname [ IFNAMSIZ ] ; struct ifaddrs * ifa ; struct ifaddrs * ifaddrs ; sockunion_t * psu ; int retval ; assert ( ifindex != 0 ) ; retval = -1 ; if ( if_indextoname ( ifindex , ifname ) == NULL ) return ( retval ) ; if ( getifaddrs ( & ifaddrs ) < 0 ) return ( retval ) ; retval = 1 ; for ( ifa = ifaddrs ; ifa != NULL ; ifa = ifa -> ifa_next ) { if ( strcmp ( ifa -> ifa_name , ifname ) != 0 ) continue ; psu = ( sockunion_t * ) ifa -> ifa_addr ; if ( psu && psu -> sa . sa_family == AF_INET ) { retval = 0 ; memcpy ( pina , & psu -> sin . sin_addr , sizeof ( struct in_addr ) ) ; break ; } } if ( retval != 0 ) errno = EADDRNOTAVAIL ; freeifaddrs ( ifaddrs ) ; return ( retval ) ; }
Internal: Map an interface index to primary IPv4 address.
Internal : Map an interface index to primary IPv4 address .
/*
	 * Find the ifaddr entry corresponding to the interface name,
	 * and return the first matching IPv4 address.
	 */ /* XXX */