FreeBSD/usr.sbin/ifmcstat/ifmcstat.c
/*
 * Retrieve per-group source filter mode and lists via sysctl.
 */
Retrieve per-group source filter mode and lists via sysctl.
Retrieve per - group source filter mode and lists via sysctl .
static void
inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
{
#define	MAX_SYSCTL_TRY	5
	int mib[7];
	int ntry = 0;
	size_t mibsize;
	size_t len;
	size_t needed;
	size_t cnt;
	int i;
	char *buf;
	struct in_addr *pina;
	uint32_t *p;
	uint32_t fmode;
	const char *modestr;
	mibsize = sizeof(mib) / sizeof(mib[0]);
	if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
		perror("sysctlnametomib");
		return;
	}
	needed = 0;
	mib[5] = ifindex;
	mib[6] = gina.s_addr;	
	mibsize = sizeof(mib) / sizeof(mib[0]);
	do {
		if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
			perror("sysctl net.inet.ip.mcast.filters");
			return;
		}
		if ((buf = malloc(needed)) == NULL) {
			perror("malloc");
			return;
		}
		if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
				perror("sysctl");
				goto out_free;
			}
			free(buf);
			buf = NULL;
		} 
	} while (buf == NULL);
	len = needed;
	if (len < sizeof(uint32_t)) {
		perror("sysctl");
		goto out_free;
	}
	p = (uint32_t *)buf;
	fmode = *p++;
	len -= sizeof(uint32_t);
	modestr = inm_mode(fmode);
	if (modestr)
		printf(" mode %s", modestr);
	else
		printf(" mode (%u)", fmode);
	if (vflag == 0)
		goto out_free;
	cnt = len / sizeof(struct in_addr);
	pina = (struct in_addr *)p;
	for (i = 0; i < cnt; i++) {
		if (i == 0)
			printf(" srcs ");
		fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
		    inet_ntoa(*pina++));
		len -= sizeof(struct in_addr);
	}
	if (len > 0) {
		fprintf(stderr, "warning: %u trailing bytes from %s\n",
		    (unsigned int)len, "net.inet.ip.mcast.filters");
	}
out_free:
	free(buf);
#undef	MAX_SYSCTL_TRY
}
static void inm_print_sources_sysctl ( uint32_t ifindex , struct in_addr gina ) { #define MAX_SYSCTL_TRY 	5 int mib [ 7 ] ; int ntry = 0 ; size_t mibsize ; size_t len ; size_t needed ; size_t cnt ; int i ; char * buf ; struct in_addr * pina ; uint32_t * p ; uint32_t fmode ; const char * modestr ; mibsize = sizeof ( mib ) / sizeof ( mib [ 0 ] ) ; if ( sysctlnametomib ( " " , mib , & mibsize ) == -1 ) { perror ( " " ) ; return ; } needed = 0 ; mib [ 5 ] = ifindex ; mib [ 6 ] = gina . s_addr ; mibsize = sizeof ( mib ) / sizeof ( mib [ 0 ] ) ; do { if ( sysctl ( mib , mibsize , NULL , & needed , NULL , 0 ) == -1 ) { perror ( " " ) ; return ; } if ( ( buf = malloc ( needed ) ) == NULL ) { perror ( " " ) ; return ; } if ( sysctl ( mib , mibsize , buf , & needed , NULL , 0 ) == -1 ) { if ( errno != ENOMEM || ++ ntry >= MAX_SYSCTL_TRY ) { perror ( " " ) ; goto out_free ; } free ( buf ) ; buf = NULL ; } } while ( buf == NULL ) ; len = needed ; if ( len < sizeof ( uint32_t ) ) { perror ( " " ) ; goto out_free ; } p = ( uint32_t * ) buf ; fmode = * p ++ ; len -= sizeof ( uint32_t ) ; modestr = inm_mode ( fmode ) ; if ( modestr ) printf ( " " , modestr ) ; else printf ( " " , fmode ) ; if ( vflag == 0 ) goto out_free ; cnt = len / sizeof ( struct in_addr ) ; pina = ( struct in_addr * ) p ; for ( i = 0 ; i < cnt ; i ++ ) { if ( i == 0 ) printf ( " " ) ; fprintf ( stdout , " " , ( i == 0 ? " " : " " ) , inet_ntoa ( * pina ++ ) ) ; len -= sizeof ( struct in_addr ) ; } if ( len > 0 ) { fprintf ( stderr , " \n " , ( unsigned int ) len , " " ) ; } out_free : free ( buf ) ; #undef 	MAX_SYSCTL_TRY }
Retrieve per-group source filter mode and lists via sysctl.
Retrieve per - group source filter mode and lists via sysctl .
/* 32 bits wide */