Page 1 of 2

Obtaining the Internet Visible IP Address

Posted: Tue Jun 28, 2005 3:29 pm
by TerryHough
Problem: I need to obtain the visible Internet IP Address of the local
machine that is running behind a router.

Does anyone know a clean way to do that?

I am using the code below, which works, but I am hoping there is a more
appropriate way than depending on the data from a website. Certainly
there should be a way to do it completely locally.

TIA,
Terry

Code: Select all

; Author: ricardo (based on older Forum code)
; Date: 11. October 2003
; -----------------------
; Modified by NoahPhense
; Date: 27. June 2004
; note: Thought I saw something like this somewhere, but couldn't
;       find it, so I modified some old code.  This code is meant
;       to rip pages.  So I'm just parsing the ripped page.
; -----------------------
; Modified by TerryHough
; Data: 27, June 2005

#INTERNET_FLAG_RELOAD = $80000000
Bytes.l = 84
Html.s  = Space(84)

hInet.l = InternetOpen_("http://www.showmyip.com", 1, #NULL, #NULL, 0)
If hInet
  hURL.l  = InternetOpenUrl_(hInet, "http://www.showmyip.com", #NULL, 0, #INTERNET_FLAG_RELOAD, 0)
  If hURL
    If InternetReadFile_(hURL, @Html, Len(Html), @Bytes)
      ; Search the downloaded html code for the IP Address ----------
      SearchString.s = "IP Address properties of your Internet Connection "
      Pos  = FindString(Html, SearchString, 1)
      Html = RemoveString(Trim(Mid(Html, Pos, Len(SearchString) + 15)),SearchString)
      Pos  = FindString(Html," ",1)
      If Pos
        Html = Mid(Html,1,Pos-1)
      EndIf
      MessageRequester("IP Address",Html,#MB_ICONINFORMATION)
      ; -------------------------------------------------------------
      InternetCloseHandle_(hURL)
    Else 
      MessageRequester("Error","Couldn't read the IP Address html page",#MB_ICONERROR)
    EndIf
  Else
    MessageRequester("Error","Couldn't read the designated URL",#MB_ICONERROR)
  EndIf   
  InternetCloseHandle_(hInet)
Else
  MessageRequester("Error","Couldn't connect to the Internet",#MB_ICONERROR)
EndIf
End

Posted: Tue Jun 28, 2005 5:20 pm
by dell_jockey
Hi Terry,

just a method I'd try to implement, I don't have code for it unfortunately.

- read out the gateway address the client is configured with.
- trace a route ('tracert') to an outside address.
- the first address behind the clients configured gateway address should be the public (visible) one.

of course this assumes that the first router is the one between the LAN and the outside world... I wouldn't know what to do with cascaded routers.

Posted: Tue Jun 28, 2005 10:07 pm
by TerryHough
Thanks dell_jockey, but that didn't get me anywhere.

Re: Obtaining the Internet Visible IP Address

Posted: Tue Jun 28, 2005 10:28 pm
by Max.
TerryHough wrote:Problem: I need to obtain the visible Internet IP Address of the local
machine that is running behind a router.
You can try to read out the router's config page via html or telnet or whatever the router supports. I doubt that a "clean" solution that retrieves the IP from just the workstation is possible.

Re: Obtaining the Internet Visible IP Address

Posted: Wed Jun 29, 2005 2:45 pm
by TerryHough
Max. wrote: You can try to read out the router's config page via html or telnet or whatever the router supports.
I thought of that too. Not very easy because it varies with every router
and I wouldn't know the password (except for my own router).
I doubt that a "clean" solution that retrieves the IP from just the workstation is possible.
Beginning to believe that myself. But maybe one of the forum members
has a brilliant idea?

Posted: Wed Jun 29, 2005 4:12 pm
by Jurgen
I think this should work :

http://delphi.about.com/od/networking/l/aa103100a.htm

The code is in Pascal.

Posted: Wed Jun 29, 2005 4:26 pm
by Max.
Jurgen wrote:I think this should work :

http://delphi.about.com/od/networking/l/aa103100a.htm

The code is in Pascal.
Doubt that it does work in a client<>router scenario. Though I'd gladly be proven wrong.

Posted: Wed Jun 29, 2005 4:38 pm
by Jurgen
It was just a wild guess, I can't test it here.
Excuses my idiotness :oops:

Posted: Wed Jun 29, 2005 5:25 pm
by TerryHough
Jurgen wrote:I think this should work :
http://delphi.about.com/od/networking/l/aa103100a.htm
Sorry, this doesn't do the job.

It gets the IP Address of a computer you are connected to on the internet,
but not the visible IP address of your own computer.

BTW, a conversion of this Pascal code exists here on the forum. And I
have a similar function existing in the FTP_Library_Include code already.

Thanks for replying.
Terry

Posted: Thu Jun 30, 2005 8:31 pm
by Max.
Still thinking about that problem as well.

There is one mechanism that would work - arp.
It resolves the MAC address of a device to the corresponding IP address.

As a router consists of 2 parts, the LAN part and the WAN part, it basically has two network addresses thus 2 MAC addresses.

If you do a arp -a on command line, you will get a list of the arp cache. But trouble is, you see only the part of the router, that is in your subnet.

To be able to resolve the WAN part, the router needed to work as Proxy Arp.

Then you could use the (known) MAC address of the WAN interface to resolve it to the IP address. I think. :wink:


Oh, another idea.

Most DSL routers allow the use of dynamic dns services. Usually they send the information to DynDNS, My-IP & Co.

Maybe it'd be feasible to emulate one of those DNS providers and fool the router where he can find the service, aka redirecting it to your client program?

Anyway, no clean solution.

Maybe this will help

Posted: Thu Jul 07, 2005 5:16 am
by netmaestro
I am new to purebasic and I'm afraid I can offer no help in how you might accomplish the mentioned task in this language. However, if you don't mind calling a tiny dll, I can be of some small service. I have a dll that is only 6k in size and I use it in my apps successfully. If you are interested in a 3k zip file with the dll and a small txt on how to use it, click this link:

http://www.networkmaestro.com/getipdll.zip

It's really easy to use if you know how to call a dll from purebasic, which I regret to report I don't know how to do yet.

Re: Maybe this will help

Posted: Sat Jul 09, 2005 1:19 pm
by NoahPhense
Or you can use my lib.. ;)

check my site..

- np

Posted: Sat Jul 09, 2005 3:43 pm
by TerryHough
Thanks NoahPhense.

I really want to know how to do it in PB rather than using a DLL or
library. Care to share your code?

BTW, it worked fine here.

Stay out of Dennis' way over the next few days!

Terry

Posted: Sun Jul 10, 2005 11:09 am
by Max.
TerryHough wrote:Care to share your code?
It is basically what you posted already.

Posted: Sun Jul 10, 2005 11:36 am
by Psychophanta
TerryHough wrote:I really want to know how to do it in PB rather than using a DLL or library.
That's great.
Will you post the solution here in this thread? If so, I'll thank you :)