0

I'm writing a C++ program on a linux box (DHCP client) that depends on the connectivity of the network. I need to pragmatically verify that my system has a IP address. I know this is a general and open ended question, so any quick and dirty solution will work for me, but ideally I would like to check/read a system file to ensure the DHCP client has received an IP address from the DHCP sever.

Thanks in advance.

Shad Reese
  • 119
  • 1
  • 4
  • 10
  • 2
    http://stackoverflow.com/questions/2021549/get-ip-address-in-c-language – Gabriel Southern Jun 09 '12 at 16:13
  • Brendan, I have only conducted research at this point. Gabriel, I'm familiar with that thread, thx, I hoping that there's a system file that read to get the info I need. – Shad Reese Jun 09 '12 at 16:19

2 Answers2

2

Just try any operation that requires an IP address and that should work if there is one. A DNS lookup comes to mind.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Maybe have a look at the source of say ifconfig, since that gets the ip address if one is assigned. A quick strace suggests that it might be an ioctl such as

ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("<my ip>")}})

and a grep of /proc/net for my IP address suggests that if you know how to parse /proc/net/fib_trie you might be able to get it from there.

lxop
  • 7,596
  • 3
  • 27
  • 42