From owner-freebsd-hackers Wed Jul 18 9:45: 3 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from falcon.mail.pas.earthlink.net (falcon.mail.pas.earthlink.net [207.217.120.74]) by hub.freebsd.org (Postfix) with ESMTP id 9F71537B405; Wed, 18 Jul 2001 09:44:56 -0700 (PDT) (envelope-from tlambert2@mindspring.com) Received: from mindspring.com (dialup-209.247.138.210.Dial1.SanJose1.Level3.net [209.247.138.210]) by falcon.mail.pas.earthlink.net (EL-8_9_3_3/8.9.3) with ESMTP id JAA02119; Wed, 18 Jul 2001 09:44:54 -0700 (PDT) Message-ID: <3B55BD2B.7CB85DF1@mindspring.com> Date: Wed, 18 Jul 2001 09:45:31 -0700 From: Terry Lambert Reply-To: tlambert2@mindspring.com X-Mailer: Mozilla 4.7 [en]C-CCK-MCD {Sony} (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Anand Franklin J Cc: freebsd-net@FreeBSD.ORG, freebsd-hackers@FreeBSD.ORG Subject: Re: Hi(some clarifcation about sysctl usage in ifconfig) References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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-hackers" in the body of the message