sysctl for route table dump

Is sysctl supported by the big stack to return a dump of the routing
table? I have a sysctl name of CTL_NET,AF_ROUTE,0,AF_INET,NET_RT_DUMP,0
which should return a number of RTM_GET messages relating to the route
entries. When I call sysctl it tells me that it didn’t error and it has 814
bytes of data for me but they are all zeros. Adding a route to the table
increases the number of bytes in the return message by 167 but it is still
all zeros. If sysctl is supported then what am I doing wrong?

Although the code is very trivial, I’ll paste it for clarity:

int result;
int name[6];
unsigned char oldp[2000];
size_t oldlenp;

name[0] = CTL_NET;
name[1] = AF_ROUTE;
name[2] = 0;
name[3] = AF_INET;
name[4] = NET_RT_DUMP;
name[5] = 0;

memset(oldp, 0x00, sizeof(oldp)); // zero stucture
oldlenp = 0;
result = sysctl(name, 6, oldp, &oldlenp, NULL, 0);
printf(“size: %d, result: %d\n”,oldlenp, result);
for (i=0; i < oldlenp; i++) {
printf("%x:",oldp_);
if ((i%8) == 7) { // just to make dump clearer
printf("\n");
}
}

Cheers

Poseidon_

Poseidon <paul.ryan2@nospam.virgin.net> wrote:
: Is sysctl supported by the big stack to return a dump of the routing
: table? I have a sysctl name of CTL_NET,AF_ROUTE,0,AF_INET,NET_RT_DUMP,0
: which should return a number of RTM_GET messages relating to the route
: entries. When I call sysctl it tells me that it didn’t error and it has 814
: bytes of data for me but they are all zeros. Adding a route to the table
: increases the number of bytes in the return message by 167 but it is still
: all zeros. If sysctl is supported then what am I doing wrong?

: Although the code is very trivial, I’ll paste it for clarity:

: int result;
: int name[6];
: unsigned char oldp[2000];
: size_t oldlenp;

: name[0] = CTL_NET;
: name[1] = AF_ROUTE;
: name[2] = 0;
: name[3] = AF_INET;
: name[4] = NET_RT_DUMP;
: name[5] = 0;

: memset(oldp, 0x00, sizeof(oldp)); // zero stucture
: oldlenp = 0;
: result = sysctl(name, 6, oldp, &oldlenp, NULL, 0);
: printf(“size: %d, result: %d\n”,oldlenp, result);
: for (i=0; i < oldlenp; i++) {
: printf("%x:",oldp_);
: if ((i%8) == 7) { // just to make dump clearer
: printf("\n");
: }
: }

Problem above is oldlenp is passed in as 0. Change to something like:

int name[6];
unsigned char *oldp, *next, *lim;
size_t oldlen;
struct rt_msghdr *rtm;

name[0] = CTL_NET;
name[1] = AF_ROUTE;
name[2] = 0;
name[3] = AF_INET;
name[4] = NET_RT_DUMP;
name[5] = 0;

if(sysctl(name, 6, NULL, &oldlen, NULL, 0) < 0 ||
(oldp = malloc(oldlen)) == NULL ||
sysctl(name, 6, oldp, &oldlen, NULL, 0) < 0)
return 1;

lim = buf + oldlen;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr )next;
/
Print it out */
}
free(oldp);

-seanb_

Cheers Sean, knew I could rely on you for the answer.