Wiznet makers

WIZnet

Published May 17, 2023 ©

758 UCC

232 VAR

0 Contests

0 Followers

0 Following

Original Link

DNS transplantation based on STM32+W5500

Then DNS can solve this problem by converting domain names into IP addresses.

COMPONENTS Hardware components

WIZnet - W5500

x 1


PROJECT DESCRIPTION

DNS: Domain Name System, domain name system. Simply understand, we usually enter domain names on the browser, such as www.baidu.com, which is easy for people to remember and communicate. You can easily memorize 10 domain names, but if you need to memorize 10 purely numeric ip addresses, it will be more laborious, but the computer communicates through ip. Then DNS can solve this problem by converting domain names into IP addresses. For details of specific DNS, please refer to this article:http://blog.51cto.com/369369/812889

W5500 DNS transplantation process

Following the previous article, the transplantation of DNS is relatively simple. First of all, add the driver file.

Query the dns.h header file. The definition is as follows. The port number of the DNS server is 53, the communication protocol is UDP, and the default domain name length is 16.

#define  MAX_DOMAIN_NAME   16       // for example "www.google.com"
#define IPPORT_DOMAIN     53       ///< DNS server port number

 

The DNS configuration mainly calls the DNS_init and DNS_run functions. Next, write the DNS Test function, enter the domain name into the DNS, and check whether the DNS returns the IP address of this domain name. The specific code is as follows:

#define SOCKET_DNS         2//Define a SOCKET port, W5500 has8A,0-7All do
wiz_NetInfo gWIZNETINFO = { .mac = {0x78, 0x83, 0x68, 0x88, 0x56, 0x38},
                            .ip =  {192, 168, 4,17},
                            .sn =  {255,255,255,0},
                            .gw =  {192, 168,4,1},
                            .dns = {180,76,76,76},
                            .dhcp = NETINFO_DHCP};
uint8_t DNS_2nd[4]={168,126,63,1};//Define an alternate DNS
uint8_t Domain_name[]="www.baidu.com";//Domain name
uint8_t Domain_IP[4]={0,};//Store the IP address corresponding to the domain name returned by DNS

 

void DNS_Test(void)
{
    int8_t ret;
    //Print out two DNS
    printf("rn=== DNS Client Example ===============rn");
    printf("> DNS 1st : %d.%d.%d.%drn", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]);
    printf("> DNS 2nd : %d.%d.%d.%drn", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]);
    printf("======================================rn");
    printf("> Example Domain Name : %srn", Domain_name);

    /* DNS client initialization */
    DNS_init(SOCKET_DNS, gDATABUF);//Initialization
    /* DNS procssing */
    if ((ret = DNS_run(gWIZNETINFO.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS, query the DNS server gWIZNETINFO.dns for the IP address of the domain name Domain_name, and store the returned IP address in Domain_IP
    {
        printf("> 1st DNS Reponsedrn");
    }
    else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0))     // retry to 2nd DNS
    {
        printf("> 2nd DNS Reponsedrn");
    }
    else if(ret == -1)
    {
        printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.rn");
    }
    else
    {
        printf("> DNS Failedrn");
    }
    if(ret > 0)
    {
     printf("> Translated %s to %d.%d.%d.%drn",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
    }
}

 

There are three DNS return values: -1, 0, and 1, as follows:

/*
 * @brief DNS process
 * @details Send DNS query and receive DNS response
 * @param dns_ip        : DNS server ip
 * @param name          : Domain name to be queryed
 * @param ip_from_dns   : IP address from DNS server
 * @return  -1 : failed. @ref  MAX_DOMIN_NAME is too small n //Return-1, The entered domain name is too long, and the maximum defined domain name is too small
 *           0 : failed  (Timeout or  Parse error)n//Return-1, Timed out
 *           1  : success//success
 * @note This funtion blocks until success or fail. max time = @ref MAX_DNS_RETRY * @ref DNS_WAIT_TIME
 */
int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns);

 

Call the DNS_Test() function to perform DNS test and query the IP address of www.baidu.com from the two DNSs.

void W5500_ChipInit(void)
{
    W5500_RESET();
    RegisterFunction();
    ChipParametersConfiguration();
    NetworkParameterConfiguration();
    DHCP_SET();
    DNS_Test();//DNS test
}

 

The download program is executed, and the output of the serial port is as follows:

You can see that when querying the DNS for the ip of the domain name www.baidu.com, the first DNS responded, and the IP corresponding to the domain name was 61.135.169.125. Enter the address in the browser, open Baidu successfully, and DNS resolution is successful.

See the link for the transplanted source program:https://download.csdn.net/download/u014470361/10234803

Documents
Comments Write