BASIC User Group > Code Challenges

Retrieve your External IP address

<< < (4/5) > >>

AIR:
MBC:


--- Code: Text ---$EXECON "-lresolv" $HEADER    #include <resolv.h>    #include <arpa/inet.h>    #include <netdb.h>$HEADER Const query = "myip.opendns.com" dim he as hostent PTR, addr_list as in_addr PTR PTR, idim res as __res_state, addr as in_addr, answer[512] as UCHARdim ip$,length,handle as ns_msg, rr as ns_rr he = gethostbyname( "resolver1.opendns.com" )addr_list = (in_addr **) he->h_addr_listip$ = inet_ntoa(*addr_list[0]) res_ninit(&res)inet_aton(ip$, &addr)res.nsaddr_list[0].sin_addr = addrres.nsaddr_list[0].sin_family = AF_INET;res.nsaddr_list[0].sin_port = htons(NS_DEFAULTPORT);res.nscount = 1; length = res_nquery(&res, query, ns_c_in, ns_t_a, answer, sizeof(answer))ns_initparse(answer, length, &handle)if ns_msg_count(handle, ns_s_an) > 0 then    if ns_parserr(&handle, ns_s_an, 0, &rr) = 0 then        ip$ = inet_ntoa(*(in_addr *)ns_rr_rdata(rr))    endifendif print "External IP: ",ip$ 
AIR.

AIR:

--- Quote from: John on March 20, 2019, 09:15:10 am ---I would like to see an example using raw sockets without the support libraries hiding what is really going on.

--- End quote ---

Here's what that would look like in Python:


--- Code: Python ---#!/usr/bin/env python import socket, sys    if __name__ == '__main__':     server = (socket.gethostbyname("resolver1.opendns.com"), 53)     prefix = "\x41\x41\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00"    suffix = "\x00\x00\x01\x00\x01"    message = "\x04\x6d\x79\x69\x70\x07\x6f\x70\x65\x6e\x64\x6e\x73\x03\x63\x6f\x6d"     query = ''.join( (prefix,message,suffix) )     try:        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)        sock.settimeout(5)        sock.sendto(query, server)                      data, addr = sock.recvfrom(512)    except Exception, e:        print "No response from server", server,e        sock.close()        sys.exit()        # get the response from the server      response = str(data.split(',', 0)[0].encode("hex"))        # check the RCODE value    if int(response[7:8]) == 0: # no error, get the ip address           # get the answer section from the server response        answer = response[2*len(query)+24:]              # get the RDATA from the answer        rdata = answer[:8]         a = int(rdata, 16)               # convert result to human readable IP        ipaddress = '.'.join([ str(x) for x in (a >> 24 & 0xff, a >> 16 & 0xff, a >> 8 & 0xff, a & 0xff)])         # Output the ipaddress        print "External IP: ", ipaddress      else:        # Catch-All error message        print "Error Communicating with Server"    sock.close() 
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.

John:

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

--- End quote ---

 :(

I'll just have to be happy with myip.sb.

AIR:

--- Quote from: John on March 23, 2019, 04:48:12 pm ---
--- Quote from: AIR ---AFAIK, SB Sockets are TCP only, so I don't think using Sockets there will work since this requires a UDP connection.

--- End quote ---

 :(

I'll just have to be happy with myip.sb.

--- End quote ---

Way back around 2002-2004, Peter wrote a SB module called ssockets.  In the old mailing list, someone asked about UDP and Peter said that while this module was TCP only, that it should be fairly simple to modify it to use UDP.

I found the source on one of his old websites, and am looking it over...

AIR.

John:
Your memory is outstanding!

What I like about SB is I feel I only scratched the surface of it's feature set and there is still much to learn.

Peter mentioned on Twitter that the development Cycle for SB was 1997 to 2006.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version