How read out own IP-address?

R

R. Grasbon

Hi,

What is the best way to read out the obtained (DHCP) or
setted (manually) IP-address of eth0 (or eth1) from out
a C or C++ program in a Linux system?

Has someone a good hint or example?

Thanks,
Reimar
 
M

Mark Wooding

R. Grasbon said:
What is the best way to read out the obtained (DHCP) or setted
(manually) IP-address of eth0 (or eth1) from out a C or C++ program in
a Linux system?

You're probably best off asking this in a newsgroup specific to Unix or
Linux programming. I've taken the liberty of crossposting this message
and setting follow-ups to comp.unix.programmer.

The `obvious' approach is to use the SIOCGIFADDR ioctl on a socket
(doesn't matter which). This will come unstuck if you have multiple
addresses on an interface, which is perfectly possible on Linux. If you
care, then it might be best to use a NETLINK socket -- I don't know the
details.

However, it strikes me that it's actually rather unusual to want to know
this information. If you have a connection (even a connected UDP
socket), you can read your local address using the getsockaddr call.

-- [mdw]
 
U

user923005

Hi,

What is the best way to read out the obtained (DHCP) or
setted (manually) IP-address of eth0 (or eth1) from out
a C or C++ program in a Linux system?

Has someone a good hint or example?

Guessing linux,
system("ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print
$1}' > ip.dat");
then open and read ip.dat.

What's wrong with gethostbyname(), giving the name of your
workstation?

Anyway, since the answer is not really covered by the C language, no
answer will be fully portable.
I guess that the *most* portable solution would be to use gethostbyname
().
There are networking groups that may give you a more sensible answer.
 
M

Mark Wooding

user923005 said:
What is the best way to read out the obtained (DHCP) or setted
(manually) IP-address of eth0 (or eth1) from out a C or C++ program
in a Linux system?
[...]
What's wrong with gethostbyname(), giving the name of your
workstation?

For a start, it's quite likely not to resolve to the desired address,
particularly in an environment with DHCP (unless there's a dynamic-DNS
tie-in or similar).
Anyway, since the answer is not really covered by the C language, no
answer will be fully portable. I guess that the *most* portable
solution would be to use gethostbyname ().

I suspect that the closest approximation is to choose some arbitrary IP
address, create a UDP socket, connect the socket to the IP address, and
call getsockname to find which address the kernel has chosen for the
local end. (This won't actually send any network packets.)

Crossposted to comp.unix.programmer and followups set.

-- [mdw]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top