External Ip Address?

Find quick help here to get you started with Hollywood
Post Reply
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

External Ip Address?

Post by Allanon »

Hello all :)
I was looking for a function that returns the external ip address but or it does not exists or I was not able to find it, any hints?
Flinx
Posts: 188
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: External Ip Address?

Post by Flinx »

The IP protocol does not have a functionality to tell you if any devices made NAT translations of your packets. So you would need to ask a server for the IP address of your incoming packets and read it for example in form of an HTML page. I would not expect such things in a programming language.
But let's see what Andreas has to say about it.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: External Ip Address?

Post by Allanon »

I'm using this free service right now:

https://www.ifconfig.me/ip

which returns the external ip address in plain text, seems a reliable service from what I've read on Internet.

I'm a really noob at network styff :D btw, thanks for the answer!
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: External Ip Address?

Post by jalih »

Allanon wrote: Mon Apr 26, 2021 11:06 am I'm using this free service right now:

https://www.ifconfig.me/ip

which returns the external ip address in plain text, seems a reliable service from what I've read on Internet.

I'm a really noob at network styff :D btw, thanks for the answer!
Using http request is typically used to retrieve external IP address programmatically. There is a faster way, you can query DNS server rather than http server.

I have not been programming with Hollywood for a while, so can't give example but here is my old 8th source (Can be a bit hard to read, if you are not used to Forth style programming languages.).

Code: Select all



net:INET4 net:DGRAM net:socket constant socket

[ 0xaa, 0xaa, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  "myip", "opendns", "com", 0x00, 0x00, 0x01, 0x00, 0x01 ] "12b1s1c1s1c1s1c5b" pack constant message

: address-info  \  -- ai
  "resolver1.opendns.com" 53 net:getaddrinfo ;

: app:main
  address-info null? if
    drop
    "Server address information lookup failed." . cr
    bye
  then

  socket swap message 0 net:sendto null? if
    drop
    "Error sending message.\n" .
    net:close
    bye
  else
    drop
  then

  512 b:new 0 net:recvfrom null? if
    drop
    "No response from the server\n" .
    net:close
    bye
  then

  \ Test RCODE
  over 3 b:@ nip 0xf n:band not if
    \ Last four bytes from the response are the IP address.
    dup 4 n:- swap b:slice "4:1B" unpack drop
    ' >s a:map
    "." a:join
    "External IP: %s\n" s:strfmt .
  else
    2drop
    "Error communicating with the server.\n" . 
  then
  drop
  net:close
  bye ;
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: External Ip Address?

Post by Allanon »

Thanks for the hint, I'm not a Forth guy but I've got the idea :)
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: External Ip Address?

Post by lazi »

This is what I use for get WAN IP of the network:

Code: Select all

Function p_getip()
	Local err=?OpenConnection (1,"checkip.dyndns.org",80)
	If err=0
		SendData(1,"GET index.html HTTP/1.0\r\n\r\n")
		Local a$=ReceiveData(1,#RECEIVEALL)
		a$=UnrightStr(a$,FindStr(a$,"<body>")+6)
		a$=LeftStr(a$,FindStr(a$,"</body>"))
		Return(a$)
	Else
		Return("IP can't identify.")
	EndIf
EndFunction       
It returns the IP address in a string, or the "IP can't identify." message if failed to obtain the address.
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: External Ip Address?

Post by airsoftsoftwair »

I'm afraid there currently is no other way to get this than using one of the methods described above. Bsdsocket doesn't really have that functionality AFAICS.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: External Ip Address?

Post by Allanon »

Thank you all, after reading a bit about this topic I've found some ways to obtain the public ip address :)
Post Reply