On Fri, 25 Aug 2000, Mike A. Harris wrote:

> I am trying to write a small network application in C, however it
> needs to determine wether or not a given network interface is up
> and running or not.
> 
> Since it is in _C_, I want to talk _directly_ to the kernel.  How
> exactly does one poll the kernel in C for interface information?
> 
> I do _not_ want to open and read files in the /proc filesystem,
> nor shell out to some script, etc..  I want to read the interface
> info from the kernel directly in C.  No interest in doing it any
> other way.
> 
> I'm specifying that because I asked this before a year or so ago,
> and got back all kinds of answers on how to do it in perl, bash,
> etc.. reading /proc/net/route, and other info.  All great and
> dandy, except it didn't answer my question.
> 
> Someone told me I have to "walk the interface list", while
> someone else recommended reading the source for "route" or
> "ifconfig".
> 
> I've got the ifconfig/route, etc.. source, but it is more complex
> than I need.  I need something like:
> 
> 
> if( !isnetdevup("ppp0"))
> {
>       fprintf(stderr, "ppp0 is not up\n");
>       exit 1;
> }
> 
> 
> What i need now is the isneetdevup() function.  I've looked at
> interface.c, if.h, and other files both in the net-tools source,
> and in /usr/include/net, etc.. but am not sure how to proceed.
> 
> A small sniglet of code that does a simple interface "walk" would
> be very handy to look at if someone can point one out to me.  The
> existing apps like ifconfig span many files, etc.. and it is hard
> to follow clearly just to find the code I need.
> 
> Please do not respond with perl/bash scripts to do this, or /proc
> reading methods, I have plenty of those allready.  I'm wanting
> this so I can learn kernel interface functions, and doing it via
> other methods does not teach what I'm wanting to learn.

I believe that I saw a code example for this in one of "the Stevens"
books, but I can't remember which one.  "TCP/IP Illustrated, Vol. 2"
(Wright/Stevens) however describes the basic mechanism in Chapter 3.

Basically, I think there's an ioctl to read a "struct ifreq", and the
particular field you want is "ifr_ifru.ifr_flags" where the "IFF_UP" flag
is set.

I think all of this is defined in "/usr/src/linux/include/linux/if.h" and
"/usr/src/linux/net/core/dev.c".

Not exactly a code sample, and not exactly portable across UN*X machines,
sorry. :)

If you can't figure it out I'll try to dig up the exact book I saw this
in...


Jim



_______________________________________________
Redhat-devel-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

Reply via email to