Obtaining the Internet Visible IP Address

Everything else that doesn't fall into one of the other PB categories.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Obtaining the Internet Visible IP Address

Post 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
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Thanks dell_jockey, but that didn't get me anywhere.
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Re: Obtaining the Internet Visible IP Address

Post 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.
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Obtaining the Internet Visible IP Address

Post 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?
Jurgen
User
User
Posts: 37
Joined: Mon Sep 08, 2003 11:53 pm

Post by Jurgen »

I think this should work :

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

The code is in Pascal.
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post 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.
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
Jurgen
User
User
Posts: 37
Joined: Mon Sep 08, 2003 11:53 pm

Post by Jurgen »

It was just a wild guess, I can't test it here.
Excuses my idiotness :oops:
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post 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
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post 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.
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Maybe this will help

Post 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.
BERESHEIT
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Maybe this will help

Post by NoahPhense »

Or you can use my lib.. ;)

check my site..

- np
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post 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
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post by Max. »

TerryHough wrote:Care to share your code?
It is basically what you posted already.
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply