The following examples illustrate the sysctl() call.
This example uses the sysctl() call to get the protocol driver version.
#include <stdio.h>
#include <types.h>
#include <netinet\in.h>
#include <sys\socket.h>
#include <netinet\ip_var.h>
#include <sys\sysctl.h>
int main(void)
{
int mib[4],i;
unsigned int oldenp=0, newlen=0;
struct inetvers_ctl uap_old, *uap_new;
mib[0]= CTL_KERN; /* cmd */
mib[1]= KERN_HOSTID; /* Protocol Family.*/
mib[2]= IPPROTO_IP; /* Protocol */
mib[3]= KERNCTL_INETVER; /* Control command for sysctl */
uap_new = NULL;
oldenp = sizeof(struct inetvers_ctl);
if (sysctl(mib,4,(void *)&uap_old,&oldenp,(void *)uap_new,newlen) > 0)
printf (" SOCKETS.SYS: %s\n",uap_old.versionstr);
}
The next example shows an inetcfg sysctl() call that uses a fifth mib argument to specify the actual inet config command. This example sets then gets the value.
#include <stdio.h>
#include <types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp_var.h>
#include <sys/sysctl.h>
void main(void)
{
int mib[5];
unsigned int oldenp=0, newlen;
struct inetcfg_ctl uap_old, uap_new;
mib[0]= CTL_NET; /* Top level idetifier */
mib[1]= PF_INET; /* Protocol Family */
mib[2]= IPPROTO_TCP; /* Protocol */
mib[3]= TCPCTL_INETCFG; /* Control command for tcp_sysctl */
mib[4]= TCPCTL_KEEPCNT; /* Particular Inetcfg cmd for sysctl_inetcfg */
/* Set the Value in stack */
uap_new.var_cur_val = 4; /* Send 4 Keepalive probes, rather than 8 */
newlen = sizeof(struct inetcfg_ctl);
if (sysctl(mib,5,(void *)NULL, &oldenp, (void *)&uap_new, newlen) < 0)
printf("sysctl failed for requested parameter\n");
/* Get the Value from stack */
oldenp = sizeof(struct inetcfg_ctl);
if (sysctl(mib,5,(void *)&uap_old, &oldenp, (void *)NULL, 0) < 0)
printf("sysctl failed for requested parameter\n");
else
printf("Current stack parameter value is %d\n",uap_old.var_cur_val);
}
This example illustrates a sysctl() call that uses the route mib.
#include <stdio.h>
#include <string.h>
#include <types.h>
#include <netinet\in.h>
#include <sys\socket.h>
#include <netinet\ip_var.h>
#include <sys\sysctl.h>
#include <net\route.h>
#include <net\if.h>
void main(void)
{
size_t needed;
int mib[6];
char *buf, *next, *lim;
struct rt_msghdr *rtm;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0; /* Wildcard Protocol */
mib[3] = 0; /* Wildcard Address Family */
mib[4] = NET_RT_IFLIST;
mib[5] = 0; /* All interfaces */
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
psock_errno("route-sysctl-estimate");
if (needed == 0) {
printf("no routes defined\n");
return 0;
}
buf = malloc(needed);
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
psock_errno("sysctl if table");
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
switch(rtm->rtm_type){
case RTM_IFINFO:
{ struct if_msghdr *ifm = (struct if_msghdr *) rtm;
printf("if# %d ,flags 0x%x\n",ifm->ifm_index,ifm->ifm_flags);
}
break;
case RTM_NEWADDR:
/* Add code for this and for RTM_DELADDR etc.....*/
break;
} /* switch */
} /* for */
}