Network Component  Version 7.3
MDK Middleware for IPv4 and IPv6 Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Starting DNS

Functions to start the DNS Client. More...

Typedefs

typedef void(* netDNSc_cb_t )(netDNSc_Event event, const NET_ADDR *addr)
 DNS Client Event callback function. More...
 

Functions

netStatus netDNSc_GetHostByName (const char *name, int16_t addr_type, netDNSc_cb_t cb_func)
 Resolve IP address of a host from a hostname. [thread-safe]. More...
 

Description

Functions to start the DNS Client.

Start the DNS Client by calling the function netDNSc_GetHostByName. 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 interface, you must enable the "Use default Gateway on remote Network" option in the configuration file (Net_Config_PPP.h or Net_Config_SLIP.h).

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

static void dns_cbfunc (netDNSc_Event event, const NET_ADDR *addr) {
char ip_ascii[40];
switch (event) {
// Host Address successfully resolved.
netIP_ntoa (addr->addr_type, addr->addr, ip_ascii, sizeof (ip_ascii));
printf ("IP Address: %s\n", ip_ascii)
break;
// All DNS Resolver retries used up and timeouts expired.
printf ("DNS Resolver Timeout expired, Host Address not resolved.\n");
break;
// Host Name does not exist in DNS record database.
printf ("Host name does not exist.\n");
break;
// DNS Protocol Error, invalid or corrupted reply received.
printf ("DNS Resolver Protocol Error, Host Address not resolved.\n");
break;
}
}

When the required host is found in the local DNS Cache, the callback function is called immediately with the result code netDNSc_EventSuccess 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.

Note
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.

Typedef Documentation

void(* netDNSc_cb_t)(netDNSc_Event event, const NET_ADDR *addr)

DNS Client Event callback function.

Parameters
[in]eventDNS client event type as defined in netDNSc_Event.
[in]addrPointer to the structure containing resolved IP address of the host name.
Returns
none.

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 netDNSc_EventSuccess) when calling the function.

Parameter for:

Code Example

static void dns_cbfunc (netDNSc_Event event, const NET_ADDR *addr) {
char ip_ascii[40];
switch (event) {
// Host Address successfully resolved
netIP_ntoa (addr->addr_type, adr->addr, ip_ascii, sizeof(ip_ascii));
printf("IP Address: %s\n", ip_ascii);
break;
// Error, host name does not exist in DNS record database
break;
// Error, DNS resolver timeout expired
break;
// Error, DNS protocol error occurred
break;
}
}

Function Documentation

netStatus netDNSc_GetHostByName ( const char *  name,
int16_t  addr_type,
netDNSc_cb_t  cb_func 
)

Resolve IP address of a host from a hostname. [thread-safe].

Parameters
[in]namehostname, a null-terminated string.
[in]addr_typenetwork address type to resolve:
  • NET_ADDR_IP4 = IPv4 address.
  • NET_ADDR_IP6 = IPv6 address.
[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 non-blocking function netDNSc_GetHostByName 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 name can also point to the dotted decimal IPv4 address in string format (for example "192.168.0.100"), or to the hexadecimal IPv6 address in string format (for example "2001:4860:4860::8888"). In this case, the DNS client calls the cb_func function immediately with the IP address.

The argument addr_type specifies the type of address to resolve. It is NET_ADDR_IP4 when you want to resolve an IPv4 address, and NET_ADDR_IP6 when you want to resolve an IPv6 address.

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

Code Example

if (netDNSc_GetHostByName ("www.arm.com", NET_ADDR_IP4, dns_cbfunc) == netOK) {
// Started, will complete on callback notification
}