Table of Contents

24.2.2 DNSClient

The class DNSClient (gb.net) implements a simple DNS client (resolver) for determining the host name to a given IP address (reverse lookup) and for determining the IP address to a given host name (forward lookup). It is said that host names are resolved into IP addresses and vice versa. The DNS client directs the queries to a DNS server that is either entered in the TCP/IP settings for your system or can be requested via DHCP.

The DNS client can work in asynchronous or synchronous mode. You can set the mode via the 'Async' property. In synchronous mode, the programme will not react to events during the resolution of host names and IP addresses. The default is synchronous mode.

For an interesting overview of DNS (Domain Name System), see the website http://de.wikipedia.org/wiki/Domain_Name_System.

24.2.2.1 Properties

The DNSClient class has these properties:

PropertyDataTypeDefaultDescription
AsyncBooleanFalseDetermines or sets the asynchronous mode of the DNS client. If this property is set to TRUE, the DNS client will operate in asynchronous mode so that the programme is not stopped during the resolution process. The status property then has the value Net.Active and the 'Finished' event is triggered after the resolution process has finished.
HostIPString-This property can be used in two ways: Read the IP address after which the host name was resolved or specify the IP address from which you want to determine the host name.
HostNameString-This property can be used in two ways: Read the host name by which the IP address was resolved or specify the host name from which you want to determine the IP address.
StatusInteger0This property reflects the current status of the DNS client: Net.Active - it is trying to resolve host names to IP address or vice versa and Net.Inactive (DNS client is inactive).

Table 24.2.2.1.1 : Properties of the DNSClient class

24.2.2.2 Methods

The DNSClient class has these three methods:

MethodDescription
GetHostIP()It tries to get (resolve) the IP address to the specified host name - stored in the property 'HostName'. The result of the resolution is stored in the property 'HostIP'. The property 'HostIP' has the value NULL if the resolution failed.
GetHostName()Attempts to determine (resolve) the host name for the specified IP address - stored in the 'HostIP' property. The result of the resolution is stored in the property 'HostName'. If the resolution failed, then property 'HostName' has the value NULL.
Stop()Cancels only an asynchronous request. If the DNS client is working in synchronous mode or is inactive, then the 'Stop()' method does nothing.

Table 24.2.2.1 : Methods of the class DNSClient

24.2.2.3 Event

The DNSClient class has only the Finished property. This event is triggered when an (asynchronous) resolution request has finished. Within the event handling routine, you can read the values stored in the HostIP and HostName properties and process them further in your programme.

24.2.2.4 Project


The DNS client from the Gambas sample collection has been adapted to use all properties, methods and the 'Finished' event. You can find the adapted project as project source archive in the download area. The next pictures should give you an impression of working with the DNS client:


Figure 24.2.2.4.1: Programme interface


Figure 24.2.2.4.2: Resolution Host Name → IP Address


Figure 24.2.2.4.3: Resolution IP address → host name


Figure 24.2.2.4.4: Resolution host name → IP address


Figure 24.2.2.4.5: Failed to resolve gambas-book.net.


Figure 24.2.2.4.6: Failed to resolve IP address 46.30.56.205

Comments:

24.2.2.5 Excursus

For the use of the EXEC and SHELL instructions, you can successfully use the presented commands to determine the host name to a given IP address or the IP address to a given host name:

hans@linux:~$ nslookup gambas-buch.de
Server:		127.0.0.1
Address:	127.0.0.1#53
Non-authoritative answer:
Name:	gambas-buch.de
Address: 109.237.140.40

hans@linux:~$ nslookup 109.237.140.40
Server:		127.0.0.1
Address:	127.0.0.1#53
Non-authoritative answer:
40.140.237.109.in-addr.arpa	name = alfa3035.alfahosting-server.de.

hans@linux:~$ resolveip -s gambas-buch.de
109.237.140.40
hans@linux:~$ dig +short gambas-buch.de
109.237.140.40

hans@linux:~$ host gambas-buch.de
gambas-buch.de has address 109.237.140.40

hans@linux:~$ ping -a -c 1 gambas-buch.de
PING gambas-buch.de (109.237.140.40) 56(84) bytes of data.
64 bytes from alfa3035.alfahosting-server.de (109.237.140.40): icmp_req=1 ttl=58 time=59.3 ms

Often it is of interest which IP address has been assigned to you by the provider. In this case, you will reach your goal with one of the four instructions:

hans@linux:~$ curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
hans@linux:~$ wget -qO - http://cfaj.freeshell.org/ipaddr.cgi
hans@linux:~$ curl "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"
$ wget -qO - www.meineip.de 2>&1 | grep 'title="Meine IP:' | sed -e 's/.*"ip">//' -e 's/<.p>.*//'

Download

Project

Download