How can I obtain my internet address?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

UPnP is always enabled by default on the various ones I've seen. UPnP would have to be manually disabled.

Marlin UPnP code isn't ready for primetime yet. I've had to change the port to 49152 from 49000 for Win7 to make it connect. However it still fails to retrieve external IP address.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: How can I obtain my internet address?

Post by Marlin »

Thunder93 wrote:Marlin UPnP code isn't ready for primetime yet. I've had to change the port to 49152 from 49000 for Win7 to make it connect. However it still fails to retrieve external IP address.
I posted it as example code. Yet it should be rather easily adjusted, if needed.

Did you compile it as a unicode executable?

Why would the operating system (you mentioned Win7) have anything to do with what port you need to connect to at the router? (The router itself would not run on Win7, would it?)

How did you get the idea to use port 49152 instead of 49000?

What did the debug output say?
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

Why would I compile using Unicode .. while in the code at the near beginning states 'NOT unicode'? :wink:

I always mention what I'm running with.

Because the port 49152tcp belongs to UPnP SOAP on D-Links. If the UPnP service has been disabled on the Router, the service port is also closed too. The source / local port doesn't need to be specified either, that would eventually get in the way of it working universally.


For the debug response information, first line complains..
'HTTP/1.1 404 Not Found'
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

For my D-Link.., it'll now work with tweaks to your code. For universal use, there need to be a database to maintaine.

Now using;

Code: Select all

strBuffer = "POST /soap.cgi?service=WANIPConn1 HTTP/1.1"+ #CRLF$
and as earlier stated..

Code: Select all

upnpConnection = OpenNetworkConnection(#Router, 49152, #PB_Network_TCP)


Another problem I originally had was with the #Router constant using Named address. While I'm able to resolve properly my Router name, it'll not work unless I specify IP address instead.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: How can I obtain my internet address?

Post by Marlin »

Quite interesting.
So upnp is not as "universal" as the name may imply.

So you open your connection like:

Code: Select all

upnpConnection = OpenNetworkConnection(#Router_IP, 49152, #PB_Network_TCP)
?

If OpenNetworkConnection() could resolve the given router name to it's ip address, shouldn't it work with the name as such as well?

Perhaps it is using another way of name resolving then your browser?
If liberty means anything at all, it means the right to tell people what they do not want to hear.
- George Orwell
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

It was failing on OpenNetworkConnection(), If I used Router IP instead for only with OpenNetworkConnection() .. all is good. The problem comes from using NetBIOS name resolution. This method is denied with my firewall, and since I never had the need for this, I'll continue to keep it denied. :mrgreen:
Last edited by Thunder93 on Mon Dec 22, 2014 1:09 am, edited 2 times in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I obtain my internet address?

Post by heartbone »

jassing wrote:anytime you use a 3rd party website (perhaps, even websites in general) you run the risk of things changing or being temporarily down or permanently done.

I would consider using something that probably won't go away, like google and just use their search... (https://www.google.com/search?q=my+ip) and parse the result

Code: Select all

StringField(StringField(HTML$,2,"<div class="+#DQUOTE$+"_h4c _rGd vk_h"+#DQUOTE$+">"),1,"</div>"))
Of course, the format could change w/o notice, but chances are, it'll be there far into the future.
Not too far, because I held my nose and looked into that Google page.
That linked page which gets displayed on my computer using Firefox,
when examined in the 'View Page Source' browser option,
will show my IP on the line that contains the text "Your public IP address".
Whatever they are doing, the page that gets downloaded to my computer using ReceiveHTTPFile() does not contain the IP information.

But no problem, I wasn't going to actually include Google within that procedure anyhow. :P

After I added a few more sites to the procedure, AVIRA stopped bitching! :o

Code: Select all

; WHICH=7 EXTRACT IP ADDRESS FROM http://www.realip.info/api/p/realip.php
; WHICH=8 EXTRACT IP ADDRESS FROM http://www.iplocation.net/
; WHICH=9 EXTRACT IP ADDRESS FROM http://www.whatsmyip.net/
         Case 7
            If ReceiveHTTPFile("http://www.realip.info/api/p/realip.php","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 8
            If ReceiveHTTPFile("http://www.iplocation.net/","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                  If FindString(EXTADDR$,"Your IP Address is",1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
                  EndIf
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
         Case 9
            If ReceiveHTTPFile("http://www.whatsmyip.net/","IPADDRESS")
               ReadFile(1,"IPADDRESS")
               Dim sIP.s(0) : sIP(0) = ""
               While Eof(1) = 0 And sIP(0) = ""
                  EXTADDR$= ReadString(1)
                  If FindString(EXTADDR$," Address is ",1)
                     CreateRegularExpression(0,"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
                     If ExtractRegularExpression(0,EXTADDR$,sIP()) > 0 : EXTADDR$= sIP(0) : EndIf
                     FreeRegularExpression(0)
                  EndIf
               Wend
               CloseFile(1) : DeleteFile("IPADDRESS")
            EndIf
Keep it BASIC.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8422
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How can I obtain my internet address?

Post by netmaestro »

Anyone who wants to use mine is welcome (now that my security issue is fixed :oops: ), here's my completed code for accessing it:

Code: Select all

Procedure.s GetWanIP()
  Protected connect,limit,completed,expired,request$,result$,ipaddress$
  request$ = "GET /whatsmyip.php HTTP/1.1"        + #CRLF$ +
             "host: www.lloydsplace.com"          + #CRLF$ +
             "accept: text/html, text/plain"      + #CRLF$ +
             "accept-Language: en-us, en"         + #CRLF$ +
             "accept-charset:  ISO-8859-1, utf-8" + #CRLF$ +
             "Content-Length: 0" + #CRLF$         + #CRLF$ 
  result$  = Space(1024)
  If InitNetwork()
    connect = OpenNetworkConnection("www.lloydsplace.com", 80)
    If connect
      SendNetworkString(connect, request$)
      limit     = ElapsedMilliseconds()+2000
      expired   = #False 
      completed = #False
      Repeat
        If NetworkClientEvent(connect) <> #PB_NetworkEvent_Data
          If ElapsedMilliseconds()<limit
            Delay(1)
          Else
            expired=#True
          EndIf
        Else
          completed=#True
        EndIf
      Until completed Or expired
      If completed      
        If ReceiveNetworkData(connect, @result$, 1024)
          result$ = Trim(PeekS(@result$, -1, #PB_Ascii))
          ipaddress$ = StringField(StringField(result$, 2, "["), 1, "]")
        Else
          ipaddress$ = "no data received"
        EndIf
      Else
        ipaddress$ = "request timed out"
      EndIf
      CloseNetworkConnection(connect) 
    Else
      ipaddress$ = "could not connect to lloydsplace.com"
    EndIf
  Else
    ipaddress$ = "could not initialize the network"
  EndIf
  ProcedureReturn ipaddress$
EndProcedure

Debug GetWanIP()
BERESHEIT
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: How can I obtain my internet address?

Post by Vera »

Hi heartbone, thx for the additions.

Now, as I don't intend to use this check in an application, I'm fine to just debug the results. But I'm too interested to see, who had been chosen and so debug the address as well.
This way I also have the chance to notice, if something might have changed ... which is the case with the 'already broken' netmaestro's service ;-)
It now only returns the IP in a single bracket-embraced string and your procedure could be shortened like this:

Code: Select all

If ReceiveHTTPFile("http://lloydsplace.com/whatsmyip.php", "IPADDRESS")
  ReadFile(1, "IPADDRESS")
    EXTADDR$ = StringField(StringField(ReadString(1), 2, "["), 1, "]") 
  CloseFile(1) : DeleteFile("IPADDRESS")
EndIf
Thanks netmaestro
for your enhanced example :-)
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: How can I obtain my internet address?

Post by heartbone »

Vera wrote:Hi heartbone, thx for the additions.

Now, as I don't intend to use this check in an application, I'm fine to just debug the results. But I'm too interested to see, who had been chosen and so debug the address as well.
This way I also have the chance to notice, if something might have changed ... which is the case with the 'already broken' netmaestro's service ;-)
It now only returns the IP in a single bracket-embraced string and your procedure could be shortened like this:

Code: Select all

If ReceiveHTTPFile("http://lloydsplace.com/whatsmyip.php", "IPADDRESS")
  ReadFile(1, "IPADDRESS")
    EXTADDR$ = StringField(StringField(ReadString(1), 2, "["), 1, "]") 
  CloseFile(1) : DeleteFile("IPADDRESS")
EndIf
Thanks for the code, but what if he later decides to drop those superfluous brackets?
It's probably best to leave the procedure as is.
Keep it BASIC.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: How can I obtain my internet address?

Post by Zebuddi123 »

Hi to All.

This just returns the ip address as text

Uncheck --- unicode executable.

Code: Select all

Procedure.s Get_Wan_IP()
	Protected url.s = "http://www.icanhazip.com/"
	Protected tf.s = GetTemporaryDirectory()+"pinawym"
	Protected wan_ip.s, *mem, buffer.i 
	CompilerIf #PB_Compiler_Unicode
		Debug "Not unicode compaptible."
		Debug "Uncheck unicode executable in Compiler Settings"
	CompilerElse   
		InitNetwork()
		If ReceiveHTTPFile(url, tf)
			If ReadFile(0,tf)
				*mem=AllocateMemory(Lof(0))
				buffer=ReadData(0,*mem,Lof(0))
				wan_ip=PeekS(*mem)
				FreeMemory(*mem)
				CloseFile(0)
				DeleteFile(tf, #PB_FileSystem_Force)
				ProcedureReturn wan_ip
			EndIf
		Else
			ProcedureReturn "Download File Error "+Str(GetLastError_())
		EndIf
	CompilerEndIf
EndProcedure
Debug Get_Wan_IP()
Zebuddi. :D
Merry Xmas & Happy year
Last edited by Zebuddi123 on Wed Dec 31, 2014 4:52 pm, edited 2 times in total.
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8422
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How can I obtain my internet address?

Post by netmaestro »

Thanks for the code, but what if he later decides to drop those superfluous brackets?
The brackets aren't superfluous. If you load the page in a browser you see only brackets and numbers. If you view the page source you see only brackets and numbers. However, accessed via code you get more:
HTTP/1.1 200 OK
Date: Tue, 23 Dec 2014 14:17:32 GMT
Server: Apache
X-Powered-By: PHP/5.4.25
Content-Length: 15
Content-Type: text/html

[75.156.21.118]
Which is the reason for the brackets and StringFields. Bottom line, he's not going to later decide to drop those superfluous brackets.
BERESHEIT
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

ReceiveHTTPFile() strips the HTTP header. Therefore makes the use of brackets superfluous for in this case.


netmaestro method could be improved upon. Knowing where HTTP header section ends and when actual page contents begins. Then would make the use of brackets superfluous still.

Here's how to read properly beyond HTTP header.

Code: Select all

Result$ = "HTTP/1.1 200 OK" + #CRLF$ +
          "Date: Tue, 23 Dec 2014 14:17:32 GMT" + #CRLF$ +
          "Server: Apache" + #CRLF$ +
          "X-Powered-By: PHP/5.4.25" + #CRLF$ +
          "Content-Length: 17" + #CRLF$ +
          "Content-Type: text/html" + #CRLF$ + #CRLF$ +
          "123.123.123.123"


Debug Mid(Result$, FindString(Result$, #CRLF$+#CRLF$)+4)
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: How can I obtain my internet address?

Post by Thunder93 »

"The end of the header section is indicated by an empty field, resulting in the transmission of two consecutive CR-LF pairs." - http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8422
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How can I obtain my internet address?

Post by netmaestro »

Okay, that's perfect then. I was going to leave the brackets there in case future versions of the host's software changed what was sent but it looks like whatever it is is going to be followed by a known string. So I took the brackets out and adjusted the stringfield code to:

Code: Select all

ipaddress$ = StringField(result$, 2, #CRLF$+#CRLF$)
and that works fine.
BERESHEIT
Post Reply