FreeBSD/sbin/ifconfig/ifconfig.c
/*
 * Print the status of the interface.  If an address family was
 * specified, show only it; otherwise, show them all.
 */
Print the status of the interface.  If an address family was
specified, show only it; otherwise, show them all.
Print the status of the interface . If an address family was specified show only it ; otherwise show them all .
static void
status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
	struct ifaddrs *ifa)
{
	struct ifaddrs *ift;
	int allfamilies, s;
	struct ifstat ifs;
	if (afp == NULL) {
		allfamilies = 1;
		ifr.ifr_addr.sa_family = AF_LOCAL;
	} else {
		allfamilies = 0;
		ifr.ifr_addr.sa_family =
		    afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
	}
	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
	s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
	if (s < 0)
		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
	printf("%s: ", name);
	printb("flags", ifa->ifa_flags, IFFBITS);
	if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
		printf(" metric %d", ifr.ifr_metric);
	if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
		printf(" mtu %d", ifr.ifr_mtu);
	putchar('\n');
	for (;;) {
		if ((descr = reallocf(descr, descrlen)) != NULL) {
			ifr.ifr_buffer.buffer = descr;
			ifr.ifr_buffer.length = descrlen;
			if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
				if (ifr.ifr_buffer.buffer == descr) {
					if (strlen(descr) > 0)
						printf("\tdescription: %s\n",
						    descr);
				} else if (ifr.ifr_buffer.length > descrlen) {
					descrlen = ifr.ifr_buffer.length;
					continue;
				}
			}
		} else
			warn("unable to allocate memory for interface"
			    "description");
		break;
	}
	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
		if (ifr.ifr_curcap != 0) {
			printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
			putchar('\n');
		}
		if (supmedia && ifr.ifr_reqcap != 0) {
			printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
			putchar('\n');
		}
	}
	tunnel_status(s);
	for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
		if (ift->ifa_addr == NULL)
			continue;
		if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
			continue;
		if (allfamilies) {
			const struct afswtch *p;
			p = af_getbyfamily(ift->ifa_addr->sa_family);
			if (p != NULL && p->af_status != NULL)
				p->af_status(s, ift);
		} else if (afp->af_af == ift->ifa_addr->sa_family)
			afp->af_status(s, ift);
	}
#if 0
	if (allfamilies || afp->af_af == AF_LINK) {
		const struct afswtch *lafp;
		lafp = af_getbyname("lladdr");
		if (lafp != NULL) {
			info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
			lafp->af_status(s, &info);
		}
	}
#endif
	if (allfamilies)
		af_other_status(s);
	else if (afp->af_other_status != NULL)
		afp->af_other_status(s);
	strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
	if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
		printf("%s", ifs.ascii);
	if (verbose > 0)
		sfp_status(s, &ifr, verbose);
	close(s);
	return;
}
static void status ( const struct afswtch * afp , const struct sockaddr_dl * sdl , struct ifaddrs * ifa ) { struct ifaddrs * ift ; int allfamilies , s ; struct ifstat ifs ; if ( afp == NULL ) { allfamilies = 1 ; ifr . ifr_addr . sa_family = AF_LOCAL ; } else { allfamilies = 0 ; ifr . ifr_addr . sa_family = afp -> af_af == AF_LINK ? AF_LOCAL : afp -> af_af ; } strlcpy ( ifr . ifr_name , name , sizeof ( ifr . ifr_name ) ) ; s = socket ( ifr . ifr_addr . sa_family , SOCK_DGRAM , 0 ) ; if ( s < 0 ) err ( 1 , " " , ifr . ifr_addr . sa_family ) ; printf ( " " , name ) ; printb ( " " , ifa -> ifa_flags , IFFBITS ) ; if ( ioctl ( s , SIOCGIFMETRIC , & ifr ) != -1 ) printf ( " " , ifr . ifr_metric ) ; if ( ioctl ( s , SIOCGIFMTU , & ifr ) != -1 ) printf ( " " , ifr . ifr_mtu ) ; putchar ( ' \n ' ) ; for ( ; ; ) { if ( ( descr = reallocf ( descr , descrlen ) ) != NULL ) { ifr . ifr_buffer . buffer = descr ; ifr . ifr_buffer . length = descrlen ; if ( ioctl ( s , SIOCGIFDESCR , & ifr ) == 0 ) { if ( ifr . ifr_buffer . buffer == descr ) { if ( strlen ( descr ) > 0 ) printf ( " \t \n " , descr ) ; } else if ( ifr . ifr_buffer . length > descrlen ) { descrlen = ifr . ifr_buffer . length ; continue ; } } } else warn ( " " " " ) ; break ; } if ( ioctl ( s , SIOCGIFCAP , ( caddr_t ) & ifr ) == 0 ) { if ( ifr . ifr_curcap != 0 ) { printb ( " \t " , ifr . ifr_curcap , IFCAPBITS ) ; putchar ( ' \n ' ) ; } if ( supmedia && ifr . ifr_reqcap != 0 ) { printb ( " \t " , ifr . ifr_reqcap , IFCAPBITS ) ; putchar ( ' \n ' ) ; } } tunnel_status ( s ) ; for ( ift = ifa ; ift != NULL ; ift = ift -> ifa_next ) { if ( ift -> ifa_addr == NULL ) continue ; if ( strcmp ( ifa -> ifa_name , ift -> ifa_name ) != 0 ) continue ; if ( allfamilies ) { const struct afswtch * p ; p = af_getbyfamily ( ift -> ifa_addr -> sa_family ) ; if ( p != NULL && p -> af_status != NULL ) p -> af_status ( s , ift ) ; } else if ( afp -> af_af == ift -> ifa_addr -> sa_family ) afp -> af_status ( s , ift ) ; } #if 0 if ( allfamilies || afp -> af_af == AF_LINK ) { const struct afswtch * lafp ; lafp = af_getbyname ( " " ) ; if ( lafp != NULL ) { info . rti_info [ RTAX_IFA ] = ( struct sockaddr * ) sdl ; lafp -> af_status ( s , & info ) ; } } #endif if ( allfamilies ) af_other_status ( s ) ; else if ( afp -> af_other_status != NULL ) afp -> af_other_status ( s ) ; strlcpy ( ifs . ifs_name , name , sizeof ifs . ifs_name ) ; if ( ioctl ( s , SIOCGIFSTATUS , & ifs ) == 0 ) printf ( " " , ifs . ascii ) ; if ( verbose > 0 ) sfp_status ( s , & ifr , verbose ) ; close ( s ) ; return ; }
Print the status of the interface.
Print the status of the interface .
/*
		 * Hack; the link level address is received separately
		 * from the routing information so any address is not
		 * handled above.  Cobble together an entry and invoke
		 * the status method specially.
		 */