Finding IP address of a website through DNS Server

This is a sample code for getting IP addresses of website from their domain name. Like what an internet browser does, web address is used (for example www.google.com) to request for information from its server. Instead, these sample code allows you to get the IP address of the corresponding web address through DNS server only.


Before burning the bin file into your EVB:

    1. Connect W5500 EVB to the router/NAT that you are using, or any other device that allows you to connect to internet.
    2. Manually change the Network configuration:
        • Go to the definition of function set_default() (Should be inside device.c), make sure the subnet mask, local IP address, gateway and DNS are set according to the internet setting you are using with your router/NAT
    3. Rebuild the sample code and burn it into W5500EVB
    4. When you are sending your request, please be reminded to add “$0D$0A” at the end of your web address
 

Code Explanation

int main(void)
{


After booting or rebooting W5500EVB, all necessary configurations are initialized.

uint8 len,dns_flag=0;

Systick_Init(72);	

NVIC_Configuration();
GPIO_Configuration();
WIZ_SPI_Init();
USART1_Init();
at24c16_init();
Reset_W5500();
set_default();
set_network();

printf("W5500 Init Complete!\r\n");
printf("Start DNS Test!\r\n");
printf("\r\nInput the domain name(eg>>www.baidu.com):");
/******************

These are all the necessary
 initialization








These are messages shown in 
serial window for debugging

********************/

Receiving Packet

After initialization, W5500EVB detects whether there is any data stored in USART_RX_STA from Serial Port, which means checking whether there is packet received

while(1){
  if(USART_RX_STA&0x8000)
  {
    len=USART_RX_STA&0x3fff;
    memcpy(buffer,USART_RX_BUF,len);
    USART_RX_STA=0;
    memset(USART_RX_BUF,0,len+1);
    printf("\r\n[ %s ]'s IP Address is:\r\n",buffer);
    dns_flag=1;
  }
/***************
Remarks:

  USART_RX_STA automatically stores data whenever there 
  are data from Serial Port. This is handled by USART1_IRQHandler.

  Measure the length of received data, store the data into buffer for later use and clear USART_RX_STA for next round’s data.

  0x8000 is used to detect whether data is received from Serial
  port. For details please refer to packet format of Usart.
****************/

Flag Clearing

the loop would generally be run for 6 times to see if any correctly identified domain name is received. Else it stops requesting the DNS server and asks user to make a new request.

if(dns_flag==1)
{
  if(dns_num>=6)
  {
    dns_flag=0;
    dns_num=0;
    printf("\r\nInput the domain name(eg>>www.baidu.com):");
  }

Do DNS Query

If there is no packet received, the function do_dns() starts.

  else
    do_dns(buffer);
  }
 }
}


To know more about how it works, let’s analyse the function do_dns().

uint16 do_dns(uint8 *domain_name)
{
  uint8 dns_retry_cnt=0;
  uint8 dns_ok=0;
  if( (dns_ok==1) ||  (dns_retry_cnt > DNS_RETRY))
  {
    return;
  }

  else if(memcmp(ConfigMsg.dns,"\x00\x00\x00\x00",4))
/****************



Initialized the counters 
needed



****************/




Domain Name Analysis

Send a DNS query to see if the web address matches any IP address in DNS server: There are three possible results:

      • Query succeeded and IP address is found
      • Query fails since no corresponding IP address is found in DNS server
      • Query fails due to incorrect network configuration or any other network problem
switch(dns_query(SOCK_DNS,domain_name))
    {

      case DNS_RET_SUCCESS:
        dns_ok=1;
        memcpy(ConfigMsg.rip,DNS_GET_IP,4);
        dns_retry_cnt=0;
        printf("%d.%d.%d.%d\r\n",ConfigMsg.rip[0],ConfigMsg.rip[1],ConfigMsg.rip[2],ConfigMsg.rip[3]);
        break;

      case DNS_RET_FAIL:
        dns_ok=0;
        dns_retry_cnt++;
        printf("Fail! Please check your network configuration or DNS server.\r\n");
        break;
      default:
        break;
    }
    
  }

  else
     printf("Invalid DNS server [%d.%d.%d.%d]\r\n",ConfigMsg.dns[0],ConfigMsg.dns[1],ConfigMsg.dns[2],ConfigMsg.dns[3]);
			dns_num++;
}



Remarks