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

DNS Client routines help to resolve DNS requests using an external DNS server. More...

Functions

netStatus get_host_by_name (const char *name, net_dns_client_cb_t cb_func)
 Resolve IP address of a host from a hostname.
 
enum  dnsClientEvent {
  dnsClientSuccess = 0,
  dnsClientTimeout,
  dnsClientNotResolved,
  dnsClientError
}
 DNS Client Callback Events. More...
 
typedef void(* net_dns_client_cb_t )(dnsClientEvent event, const uint8_t *ip_addr)
 DNS Client Event callback function.
 

Description

DNS Client routines help to resolve DNS requests using an external DNS server.

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.

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.

Note
The page DNS Client gives you more information on the actual usage of the functions and how to work with them in a project.

Code Examples

DNS Resolution Example

#include <rl_net.h>
#include <stm32f2xx.h>
char const *hosts[] = {
"www.google.com",
"www.keil.com",
"www.microsoft.com",
"www.yahoo.com",
"www.notexisting.site",
"192.168.0.253"
};
unsigned int index;
//--------------------------- next_host -------------------------------------
static void next_host () {
// Select next host from the table.
if (++index == sizeof(hosts)/sizeof(hosts[0])) {
index = 0;
}
}
//--------------------------- dns_cbfunc ------------------------------------
static void dns_cbfunc (uint8_t event, uint8_t *ip) {
/* This function is called by the DNS Client when dns event occurs. */
switch (event) {
case DNS_EVT_SUCCESS:
// Host Address successfully resolved. When IP address is already
// cached, there is no DNS Request sent to remote DNS Server.
printf("IP Address : %d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
break;
case DNS_EVT_NONAME:
// Host Name does not exist in DNS record database.
printf("Host name does not exist.\n");
break;
case DNS_EVT_TIMEOUT:
// All DNS Resolver retries used up and timeouts expired.
printf("DNS Resolver Timeout expired, Host Address not resolved.\n");
break;
case DNS_EVT_ERROR:
// DNS Protocol Error, invalid or corrupted reply received.
printf("DNS Resolver Protocol Error, Host Address not resolved.\n");
delay = 0;
return;
}
next_host();
delay = 30;
}
//---------------------------------------------------------------------------
int main (void) {
uint8_t res;
index = 0;
while (1) {
...
res = get_host_by_name ((uint8_t *)hosts[index],dns_cbfunc);
switch (res) {
case DNS_RES_OK:
// Resolver started, wait for callback event.
break;
case DNS_ERROR_BUSY:
// Busy, retry on next tick.
break;
case DNS_ERROR_LABEL:
printf ("Invalid label specified!\n");
next_host();
break;
case DNS_ERROR_NAME:
printf ("Hostname is not valid!\n");
next_host();
break;
case DNS_ERROR_NOSRV:
printf ("DNS Server unknown!\n");
break;
case DNS_ERROR_PARAM:
printf ("Invalid parameters!\n");
break;
}
}
}

Typedef Documentation

void(* net_dns_client_cb_t)(dnsClientEvent event, const uint8_t *ip_addr)

DNS Client Event callback function.

Parameters
[in]eventDNS client event type as defined in dnsClientEvent.
[in]ip_addrIP address of the host server.

Is the type definition for the DNS callback function. The function is invoked by the DNS client when an event ends the DNS session. The DNS client specifies the event and the host IP address (in case of dnsClientSuccess) when calling the function.

Parameter for:

Enumeration Type Documentation

DNS Client Callback Events.

Parameter for:

Enumerator:
dnsClientSuccess 

Host name successfully resolved.

dnsClientTimeout 

Timeout resolving host.

dnsClientNotResolved 

DNS Error, no such name.

dnsClientError 

Erroneous response packet.

Function Documentation

netStatus get_host_by_name ( const char *  name,
net_dns_client_cb_t  cb_func 
)

Resolve IP address of a host from a hostname.

Parameters
[in]namehostname, a null-terminated string.
[in]cb_funccallback function to call, when DNS session ends.
Returns
status code that indicates the execution status of the function as defined with netStatus.

The function get_host_by_name resolves the IP address of a host from a host name. It starts the DNS client and sends a request to the DNS server.

The argument name is a pointer to a NULL terminated string that specifies the host name.

The argument cb_func specifies a user-provided callback function. Refer to net_dns_client_cb_t.

Note
The argument name can also point to the dotted decimal IP address in string format (for example "192.168.0.100"). In this case, the DNS client calls the cb_func function immediately with the IP address.

Code Example

//coming soon