BASIC User Group > Code Challenges

Retrieve your External IP address

<< < (5/5)

jalih:

--- Quote from: AIR on March 23, 2019, 11:33:09 am ---Couple of notes:

I hand-crafted the binary query/request that is sent to the dns server.  The alternative would have required the use of the "structs" or "binascii" modules.

AFAIK, SB Sockets are TCP only, so I don't think using Sockets there will work since this requires a UDP connection.

This works under macOS, Linux, and RasPi at this point.

AIR.

--- End quote ---

Here is updated 8th code that also checks RCODE for errors. Using pack and unpack words makes it really easy to handle buffer of message data.


--- Code: ---

net:INET4 net:DGRAM net:socket var, socket

[ 0xaa, 0xaa, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  "myip", "opendns", "com", 0x00, 0x00, 0x01, 0x00, 0x01 ] "12b1s1c1s1c1s1c5b" pack var, 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." . cr
    net:close
    bye
  else
    drop
  then

  512 b:new 0 net:recvfrom null? if
    drop
    "No response from the server" . cr
    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: " . . cr
  else
    2drop
    "Error communicating with the server." . cr
  then
  drop
  net:close
  bye ;

--- End code ---

John:
SB also supports binary pack/unpack. Just no UDP.  :-\

AIR:
I created a custom SB module to try this.


--- Code: Script BASIC ---include ip.bas print "Public IP: " & IP::Public() & "\n" print "Local IP: " & IP::Local("eth0") & "\n" print "MAC Address for eth0: " & IP::MacAddress("eth0") & "\n"
OUTPUT:
riveraa@dpi:~/sb $ sb iptest.sb
Public IP: 24.188.233.50 <--not my real ip.  LOL.
Local IP: 192.168.1.64
MAC Address for eth0: b8:27:eb:2a:e9:22

All done in code, no calls to external web sites (which wouldn't be able to grab the MAC Address.  I hope!), or executing shell commands.

I used libresolv for the DNS Query, and SOCKET & IOCTL calls to get the local IP and MAC Address of the adapter.

I took a slightly different approach to putting the module together; I wrote the core code in pure C and then called the Functions from within the module after compiling the C file along with the interface.c file (similar to what I did with the JSON and SLRE modules).

This is a much cleaner approach since I didn't have to re-write the core code to work within the BES macro system.  All I had to do was plug in the Function calls and I was done.

Coded start to finish on my RasPi.  8)

I'll push this up later today after some cleanup and sleep, it's 4:20am where I am right now!

AIR.

John:

--- Quote ---it's 4:20am
--- End quote ---

Here on the west coast 4:20 isn't about bedtime.  ;)

I have yet to find a language with an easier to use extension interface.

Looking forward to see your new extension module.

Navigation

[0] Message Index

[*] Previous page

Go to full version