How to get WAN IP Address of Router

Hey everyone I'm new to network and winsock programming so bare with me.

Can anyone show me step by step with source code examples on how to get the WAN IP address of my router using C\C++ and using either Visual C++ 2005 compiler or Borland Command Line Compiler. The source code has to compile please. I've done a search on EE and found this article which is close but I cannot get the code to compile because some of the header files I don't know where to find or where some of the structures are defined.


Who is Participating?
Adrien de CroyCommented:
That other experts-exchange query you referred to is no use to you.  It relates to reverse ARP, which is only usable to get an IP address from a known MAC address on the local subnet.  The WAN interface on your router isn't on your subnet, so you can't use RARP to discover its IP.

there are several methods to get the WAN IP.  One is to connect to a known service which tells you as suggested above.  this has a couple of drawbacks, one being the service you connect to must be always available.

Another is to install a dynamic DNS client on your computer.  When it registers your IP with the dynamic DNS server, that server will register the actual WAN IP of your gateway.  You can then use DNS to look up your own IP address.  This is possibly the easiest method, since all the coding you'll need to do is a gethostbyname() call.

Another is to use SNMP to query the device directly for its WAN IP.  This will only work if the device supports SNMP of course, but most of them do.  There is sample SNMP in MSDev.

Another one again may be to connect to the telnet port on the device and log in and issue the commands necessary to obtain the WAN IP, and scrape the result back off the output.

Check this out

It's a client which connects to a public STUN servers and detects where the request came from.
You will need to extract usefull parts or compile as a whole thing using instructions inside the package.
eh936Author Commented:

I was aware of many of the online websites available for determining your own public IP address but did not want to go that route or rely on their service. Thanks for pointing the part of the WAN IP not being on the same subnet this really helps understand everything more clearer. I'm going to look into the SNMP suggestion you had made but if I can't make heads or tail with the sample source being a newbie to network programming and pretty much a newbie programmer I'll try working on the DNS client you talked about.

Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.