Network Component  Version 6.6
MDK-Professional Middleware for IP Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
DNS Client

Domain Name System (DNS) servers store and manage information about domains and respond to resolution requests for clients (in some cases millions of times each day). The DNS database is a distributed name database stored on many DNS servers. DNS uses a hierarchical tree structure for its name space and a hierarchical tree for name authorities and registration.

Since information in DNS is stored in a distributed form, there is no single server that has information about every domain in the system. The process of resolution instead relies on the hierarchy of name servers as described above.

At the top of the DNS hierarchy is the root domain and the root name servers. These are the most important servers because they maintain information about the top-level domains within the root. They also know the servers that can be used to resolve domains one level below them. Those servers can reference servers that are responsible for second-level domains. Thus, a DNS resolution requests might be sent to more than one server.

The DNS Client is capable of resolving the IP address of a host from the host's name. It does this by sending DNS requests to a DNS Server. The IP address of a DNS Server is specified in the network interface configuration file or can be obtained from the DHCP Server for the Local Area Network.

The DNS Client caches the resolved IP addresses. The length of time the resolved host IP address is kept in the local cache depends on the Time to Live (TTL) timeout. This is returned in an answering packet from the DNS Server. The next time a DNS is requested, the cache table is checked first. If a valid host is found, the IP address is resolved from the cache and no actual DNS request is sent to the DNS Server.

You must use the DNS Client when a remote host uses a Dynamic IP, which changes each time the remote host logs on to the Internet.

Starting DNS

Start the DNS Client by calling the function get_host_by_name. DNS requests are routed to the DNS Server IP address of an active network interface. If you are using a PPP or SLIP interface and no Ethernet interface, you must enable the "Use default Gateway on remote Network" option in the configuration file (Net_Config_PPP_0.h or Net_Config_SLIP_0.h).

You must also specify a callback function, which is called from the DNS Client when a DNS event occurs.

static void dns_cbfunc (dnsClientEvent event, const uint8_t *ip_addr) {
switch (event) {
// Host Address successfully resolved.
printf("IP Address : %d.%d.%d.%d\n", ip_addr[0], ip_addr[1],
ip_addr[2], ip_addr[3]);
// Host Name does not exist in DNS record database.
printf("Host name does not exist.\n");
// All DNS Resolver retries used up and timeouts expired.
printf("DNS Resolver Timeout expired, Host Address not resolved.\n");
// DNS Protocol Error, invalid or corrupted reply received.
printf("DNS Resolver Protocol Error, Host Address not resolved.\n");

When the required host is found in the local DNS Cache, the callback function is called immediately with the result code dnsClientSuccess and provides the IP address of the host to the function. In this case, no actual DNS request packet is sent to the remote DNS Server.

You can also provide the IP address in a string format to specify the host name. The DNS Client decodes it and returns the decoded IP address to the callback function.

DNS Client Configuration

The DNS client configuration file Net_Config_DNS_Client.h contains only the setting Cache Table Size which specifies the size of the DNS Cache by defining the number of entries for the DNS Cache table. When the IP address is resolved, it is also stored to the local cache. When a request for resolving an IP address is received, the DNS Client first checks the local cache memory. If a valid entry is found there, the IP address is taken from the cache, and the request is not sent on to the remote DNS Server.

DNS Cache entries expire after a Time to Live (TTL) timeout. This is defined by the DNS Server. The TTL value for resolved IP addresses is received in an answer packet from the DNS Server. The DNS Client manages the timeouts. When a timeout counter expires, the DNS Cache entry is deleted from the Cache.