How to get WAN IP Address of Router

Posted on 2007-10-15
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

    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.
    LVL 3

    Accepted Solution

    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


    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    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.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now