FreeBSD/contrib/bsnmp/snmp_mibII/mibII.c
/*
 * Fetch the routing table via sysctl
 */
Fetch the routing table via sysctl
Fetch the routing table via sysctl
u_char *
mib_fetch_rtab(int af, int info, int arg, size_t *lenp)
{
	int name[6];
	u_char *buf, *newbuf;
	name[0] = CTL_NET;
	name[1] = PF_ROUTE;
	name[2] = 0;
	name[3] = af;
	name[4] = info;
	name[5] = arg;
	*lenp = 0;
	if (sysctl(name, 6, NULL, lenp, NULL, 0) == -1) {
		syslog(LOG_ERR, "sysctl estimate (%d,%d,%d,%d,%d,%d): %m",
		    name[0], name[1], name[2], name[3], name[4], name[5]);
		return (NULL);
	}
	if (*lenp == 0)
		return (NULL);
	buf = NULL;
	for (;;) {
		if ((newbuf = realloc(buf, *lenp)) == NULL) {
			syslog(LOG_ERR, "sysctl buffer: %m");
			free(buf);
			return (NULL);
		}
		buf = newbuf;
		if (sysctl(name, 6, buf, lenp, NULL, 0) == 0)
			break;
		if (errno != ENOMEM) {
			syslog(LOG_ERR, "sysctl get: %m");
			free(buf);
			return (NULL);
		}
		*lenp += *lenp / 8 + 1;
	}
	return (buf);
}
u_char * mib_fetch_rtab ( int af , int info , int arg , size_t * lenp ) { int name [ 6 ] ; u_char * buf , * newbuf ; name [ 0 ] = CTL_NET ; name [ 1 ] = PF_ROUTE ; name [ 2 ] = 0 ; name [ 3 ] = af ; name [ 4 ] = info ; name [ 5 ] = arg ; * lenp = 0 ; if ( sysctl ( name , 6 , NULL , lenp , NULL , 0 ) == -1 ) { syslog ( LOG_ERR , " " , name [ 0 ] , name [ 1 ] , name [ 2 ] , name [ 3 ] , name [ 4 ] , name [ 5 ] ) ; return ( NULL ) ; } if ( * lenp == 0 ) return ( NULL ) ; buf = NULL ; for ( ; ; ) { if ( ( newbuf = realloc ( buf , * lenp ) ) == NULL ) { syslog ( LOG_ERR , " " ) ; free ( buf ) ; return ( NULL ) ; } buf = newbuf ; if ( sysctl ( name , 6 , buf , lenp , NULL , 0 ) == 0 ) break ; if ( errno != ENOMEM ) { syslog ( LOG_ERR , " " ) ; free ( buf ) ; return ( NULL ) ; } * lenp += * lenp / 8 + 1 ; } return ( buf ) ; }
Fetch the routing table via sysctl
Fetch the routing table via sysctl
/* initial estimate */