Locating MAC and IP address in my C program

Locating MAC and IP address in my C program

Post by andreas_li » Sat, 18 Oct 2003 12:05:09


Hello Everyone

I am writing a application in C code that needs the IP and MAC address
(only have one MAC card) of the machine it is running on. I cannot
hardcode this since each machine where my application will run
might/will have different MAC and IP address.

So my question is if there is anyway for me to in a fast and easy way
retrive the MAC and IP from a 2.4.17 Linux kernel and store the result
in a string/variable for usage in my C program?

Any hints and tips are welcome.

Regards
Andreas
 
 
 

Locating MAC and IP address in my C program

Post by Sybren Stu » Sat, 18 Oct 2003 18:19:53

Andreas enlightened us with:

You can take a look at the ifconfig source or simply use

ifconfig | grep eth0 | awk '{ print $5 }'

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?

 
 
 

Locating MAC and IP address in my C program

Post by cfk » Sun, 19 Oct 2003 05:31:23

Dear Sybren:
Do you know how to query the ioctl's in a running network driver from a
userspace program. This is a little different question then Andreas is
asking, but in my work, I have a need to read some ioctls. If I knew how to
get a pointer to the net_device structure for eth0, 1 or 2 from userspace
then I think I could puzzle the rest of it out.

Charles
 
 
 

Locating MAC and IP address in my C program

Post by Sybren Stu » Sun, 19 Oct 2003 08:45:04

cfk enlightened us with:

Nope, sorry, don't have a clue.


You seem to know about this structure living in kernel space. Perhaps
you can write a kernel module that publishes this structure to /proc or
some /dev entry?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
 
 
 

Locating MAC and IP address in my C program

Post by cfk » Mon, 20 Oct 2003 01:11:28

Dear Sybren:
I know that ifconfig and iwconfig can actually read and write to this
structure in order to control and query the network devices. So, far, it
looks like it will take a few days to slug through the ifconfig or iwconfig
sources. By the way, the ifconfig sources are in net-tools-1.60.tar.bz2 in
case anyone else is interested in how this works.

I did get to ifconfig.c and its cousin, interface.c in those source last
night, but I couldnt quite come to a grok yet.

In any case, back to Rubini's book.

Charles