Sample Application

Initialize

This example shows how to initialize µC/DNSc. Note that µC/TCP-IP must be initialized without error before initializing µC/DNSc.


#include  <Source/dns-c.h>
#include  <dns-c_cfg.h>
 
CPU_BOOLEAN  AppDNSc_Init (void)
{
    DNSc_ERR  dns_err;


#if (DNSc_CFG_MODE_ASYNC_EN == DEF_DISABLED)
    DNSc_Init(&DNSc_Cfg, DEF_NULL, &dns_err);          /* See Note #3a.                       */
#else
    DNSc_Init(&DNSc_Cfg, &DNSc_CfgTask, &dns_err);     /* See Note #3b.                       */
#endif
    if (dns_err != DNSc_ERR_NONE) {
        return (DEF_FAIL);
    }
    return (DEF_OK);
}


  1. Prerequisite modules must be initialized before calling any uC/DNSc functions.

  2. Prior to do any call to uC/DNSc, the module must be initialized. This is done by calling DNSc_Init(). If the process is successful, the DNS client internal data structures are initialized.

  3. The DNS client can be configured to have his own task to perform operation asynchronously:

    1. If the application doesn't require non-blocking call, no task is required and the second parameter can be set to DEF_NULL. 

    2. If the application require non-blocking call then the asynchronous mode must be enabled and a task configuration must be passed in the second function argument.

Resolve host name


#include  <Source/dns-c.h>
#include  <Source/net_util.h>
#include  <Source/net_ascii.h>
#include  <Source/net_sock.h>
 
void  AppDNSc_GetHostMicrium (CPU_CHAR  *p_addr_str)
{
    DNSc_ADDR_OBJ  addrs[2u];
    CPU_INT08U     addr_nbr = 2u;
    CPU_INT08U     ix;
    DNSc_ERR       dns_err;


    DNSc_GetHost("micrium.com",           /* See Note #2a. */
                  DEF_NULL,               /* See Note #2b. */
                  0u,                     /* See Note #2c. */
                  addrs,                  /* See Note #2d. */
                 &addr_nbr,               /* See Note #2e. */ 
                  DNSc_FLAG_NONE,         /* See Note #2f. */
                  DEF_NULL,               /* See Note #2g. */
                 &dns_err);
    if (dns_err != DNSc_ERR_NONE) {
        return;
    }

    for (ix = 0u; ix < addr_nbr; ix++) {  /* See Note #3. */
        NET_ERR   net_err;

        if (addrs[ix].Len == NET_IPv4_ADDR_LEN) {
            NET_IPv4_ADDR  *p_addr = (NET_IPv4_ADDR *)addrs[ix].Addr;


            NetASCII_IPv4_to_Str(*p_addr, p_addr_str, NET_ASCII_LEN_MAX_ADDR_IP, &net_err);

        } else {
            NET_IPv6_ADDR *p_addr = (NET_IPv6_ADDR *)addrs[ix].Addr;


            NetASCII_IPv6_to_Str(p_addr, p_addr_str, DEF_NO, DEF_YES, &net_err);
        }
    }
}


  1. Prior to making any call to uC/DNSc, the module must be initialized.

  2. The function DNSc_GetHost() must be called to resolve an host name.
    1. The first parameter is the host name to resolve.
    2. The second parameter is left as DEF_NULL since we intend to issue a forward DNS lookup request.
    3. A value of 0 is passed as the third parameter since we intend to issue a forward DNS lookup request.
    4. The fourth parameter is a pointer to a address structure that will receive the host name addresses.
    5. The fifth parameter is a pointer that must specify how many address can be stored in the address table and that will receive the number of address returned.
    6. The sixth parameter is the flag that allows to control the behavior of the function.
    7. The seventh parameter is an optional configuration, if the default configuration should be used then this parameter can be set to DEF_NULL.

  3. DNSc_GetHost()  returned the address table filled if the host is successfully resolved. It is possible to browse the table to find all host addresses. The Len field of the address structure determines if the address is an IPv4 or IPv6 address.


Issue a reverse lookup

#include  <Source/dns-c.h>
#include  <lib_mem.h>
 
 
int main (void) {
    DNSc_STATUS  status;
    DNSc_ERR     dnsc_err;
    CPU_CHAR     rev_name_str[255];
    
 
    Mem_Clr(rev_name_str, 255);
    status = AppDNSc_GetReverseHost( rev_name_str,
                                     sizeof(rev_name_str),
                                    &dnsc_err);
 
    if (status == DNSc_STATUS_RESOLVED) {             /* See Note #4.                       */ 
        APP_TRACE_INFO(("Reverse lookup name: %s\r\n", 
                        rev_name_str));
    }
 
}
 
DNSc_STATUS  AppDNSc_GetReverseHost (CPU_CHAR    *p_addr_str,
                                     CPU_INT16U   str_size, 
                                     DNSc_ERR    *p_err)
{
    DNSc_ADDR_OBJ  addrs;
    CPU_INT08U     addr_nbr = 1u;
    DNSc_FLAGS     dns_flags;
    DNSc_STATUS    status;
    CPU_CHAR      *p_hostname = "8.8.8.8";            /* Or "2001:4860:4860::8888" for IPv6.*/


    dns_flags = DNSc_FLAG_REVERSE_LOOKUP;

    status    = DNSc_GetHost( p_hostname,             /* See Note #2a.                      */
                              p_addr_str,             /* See Note #2b.                      */
                              str_size,               /* See Note #2c.                      */
                             &addrs,                  /* See Note #2d.                      */
                             &addr_nbr,               /* See Note #2e.                      */
                              dns_flags,              /* See Note #2f.                      */
                              DEF_NULL,               /* See Note #2g.                      */
                              p_err);
        
    return (status);
}


  1. Prior to making any call to uC/DNSc, the module must be initialized.

  2. The function DNSc_GetHost() must be called to resolve an host name.
    1. The first parameter is the host name to resolve.
    2. The second parameter is the string passed by the caller to store the resolved domain name for reverse DNS lookup request.
    3. The third parameter is the size of the string passed as the second parameter, including the null-terminated byte.
    4. The fourth parameter is a pointer to a address structure that will update its .Addr and .Len fields with the numeric IPv4 address and address length, respectively.
    5. A value of 1 is passed as the third parameter since we intend to issue a reverse DNS lookup request.
    6. The sixth parameter is the flag that allows to control the behavior of the function.
    7. The seventh parameter is an optional configuration, if the default configuration should be used then this parameter can be set to DEF_NULL.
  3. Please note that the BSD equivalent to this function is getpeername() found in 'net_bsd.c'
  4. Output of this program should be "Reverse lookup name: dns.google"