How to get WAN IP Address of Router

Posted on 2007-10-15
Medium Priority
Last Modified: 2013-12-04
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.



Question by:eh936
LVL 11

Expert Comment

ID: 20082983
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.

Accepted Solution

Adrien de Croy earned 750 total points
ID: 20090615
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.


Author Comment

ID: 20097529

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question