Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 18 Jul 2001 09:45:31 -0700
From:      Terry Lambert <tlambert2@mindspring.com>
To:        Anand Franklin J <anandfranklin@hotmail.com>
Cc:        freebsd-net@FreeBSD.ORG, freebsd-hackers@FreeBSD.ORG
Subject:   Re: Hi(some clarifcation about sysctl usage in ifconfig)
Message-ID:  <3B55BD2B.7CB85DF1@mindspring.com>
References:  <F197ZDgC81TbdkSmLAz000000d2@hotmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Anand Franklin J wrote:
> The below code is part of ifconfig which use sysctl:
>         1:mib[0] = CTL_NET;
>         2:mib[1] = PF_ROUTE;
>         3:mib[2] = 0;
>         4:mib[3] = 0;   /* address family */
>         5:mib[4] = NET_RT_IFLIST;
>         6:mib[5] = 0;
> 
>         /* if particular family specified, only ask about it */
>         7:if (afp)
>         8:      mib[3] = afp->af_af;
> 
>         9:if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
>         10:     errx(1, "iflist-sysctl-estimate");
>         11:if ((buf = malloc(needed)) == NULL)
>                 errx(1, "malloc");
>         12:if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
>                 errx(1, "actual retrieval of interface table");
>         the doubts are:
> 1) what is the purpose of the above c statements.
> 2)how are the mib values stored before finding its value
>   line from 1->8

In the kernel.  In other words, they are _not_ writable,
they are readable from the kernel.


> 3)line from 9->11 is clear, that is it finds the length of the buffer to be
> allocated.
> 4)what is the significance of line 12 and what will the buf variable will
> contain.

9->11 calculate the space and allocate a region for storing
the interface list.  12 actually retrieves the list into
the allocation space provided (buf).  After it runs, if it
runs successfully, it will contain a list of interfaces.

Probably you want to look at using getifaddrs(3) instead; see
the manual page for this library function.

-- Terry

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-net" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3B55BD2B.7CB85DF1>