DNS or other Internet functions

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
n9cqs
Posts: 5
Joined: May 13, 2007 15:04
Location: Urbana, IL

DNS or other Internet functions

Postby n9cqs » Oct 04, 2008 19:23

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
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Oct 07, 2008 2:20

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 )
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Postby DrV » Oct 07, 2008 16:02

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):

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

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 4 guests