Greetings. Is there a library that FreeBASIC can use to do DNS/Reverse lookups from within the program given either a URL or IP address? I need it for the Linux platform.
I could write a subroutine to shell to NSLOOKUP or DIG and dump the results to a temp file then read the results and parse out what I need, but that seems AWFULLY clumsy.
Thanks.
Jeff
DNS or other Internet functions
Here's a little program showing a couple of different ways, one uses OPEN PIPE to read in from a program, so no need for messy tempfiles, the other uses the functionality in SDL_net. There are likely a bunch of other ways too. I'm sure theres a function for this somewhere in the C library, I just couldn't find it right now :P
Code: Select all
#include once "SDL/SDL_net.bi"
function pipe_dns_lookup _
( _
byref domain as string _
) as string
dim as integer fslot = freefile( )
dim as string s
open pipe "dig " & domain & " A | grep -v '^;' | awk '{print $5}'" for input as #fslot
while not eof( fslot )
dim as string tmp
line input #fslot, tmp
if tmp <> "" then
s += tmp & !"\n"
end if
wend
close #fslot
function = s
end function
function sdl_dns_lookup _
( _
byref domain as string _
) as string
dim as IPAddress IP
dim as ubyte ptr host
SDLNet_ResolveHost( @IP, domain, 0 )
host = cptr( ubyte ptr, @IP.host )
function = host[0] & "." & host[1] & "." & host[2] & "." & host[3]
end function
dim as string domain
domain = "google.com"
print "**** Lookup using a pipe to a program ****"
print domain
print pipe_dns_lookup( domain )
print "**** Lookup using SDL_net ****"
print domain
print sdl_dns_lookup( domain )
The proper modern functions for this are getaddrinfo(), getnameinfo(), etc.
There is a nice article about these functions here. The article title involves IPv6, but these functions are more generic than the old IPv4-only ones and work for (theoretically) any protocol (in practice, IPv4 and IPv6).
Here is some example code from an old socket library (unfinished, but this part at least worked... probably could use some more error checking):
There is a nice article about these functions here. The article title involves IPv6, but these functions are more generic than the old IPv4-only ones and work for (theoretically) any protocol (in practice, IPv4 and IPv6).
Here is some example code from an old socket library (unfinished, but this part at least worked... probably could use some more error checking):
Code: Select all
#include "crt/netdb.bi"
#include "crt/sys/socket.bi"
#include "crt/unistd.bi"
sub Socket.connect( byref addr as string, byref port as string )
dim res as addrinfo ptr
dim hints as addrinfo
hints.ai_socktype = SOCK_STREAM
hints.ai_flags = AI_ADDRCONFIG
var e = ..getaddrinfo( strptr(addr), strptr(port), @hints, @res )
if e <> 0 then
'' fail
print "Net.connect: getaddrinfo failure: " & *..gai_strerror(e)
return '-1
end if
dim sock as integer = -1
dim r as addrinfo ptr = res
do while r <> NULL
dim as zstring * 1024 hostname, hostaddr
getnameinfo( r->ai_addr, r->ai_addrlen, strptr(hostname), sizeof(hostname), NULL, 0, 0 )
getnameinfo( r->ai_addr, r->ai_addrlen, strptr(hostaddr), sizeof(hostaddr), NULL, 0, NI_NUMERICHOST )
print "Net.connect: trying " & hostname & " (" & hostaddr & ")"
sock = ..opensocket( r->ai_family, r->ai_socktype, r->ai_protocol )
if sock <> -1 andalso ..connect( sock, r->ai_addr, r->ai_addrlen ) = 0 then
print "Net.connect: success"
exit do
end if
if sock <> -1 then
print "Net.connect: failure"
..closesocket( sock )
sock = -1
end if
r = r->ai_next
loop
..freeaddrinfo(res)
this.fd = sock
end sub
Who is online
Users browsing this forum: No registered users and 4 guests