8. W5100S/W5500+RP2040 Raspberry Pi Pico<DNS>

Article directory

  • 1 Introduction
  • 2 Introduction to the protocol
    • 2.1 What is DNS
    • 2.2 Advantages of DNS
    • 2.3 How DNS works
    • 2.4 Application scenarios
  • 3 WIZnet Ethernet chip
  • 4 DNS Network Settings Example Overview and Usage
    • 4.1 Flowchart
    • 4.2 Core preparation work
    • 4.3 Connection method
    • 4.4 Main code overview
    • 4.5 Burning Verification
  • 5 things to note
  • 6 related links

1 Preface

To better support application performance and availability, DNS optimization technology will be further developed. These technologies improve user experience by helping users access the websites or applications they need faster.

W5100S/W5500 is an embedded Ethernet controller integrating a full hardware TCP/IP protocol stack. It is also an industrial-grade Ethernet control chip. This tutorial will introduce the basic principles, usage steps, application examples and precautions of W5100S/W5500 Ethernet DNS application to help readers better master this technology.

2 Protocol Introduction

2.1 What is DNS

DNS is the abbreviation of Domain Name System (Domain Name System), which is composed of domain name resolution server and domain name server.

The domain name resolution server relies on it to find the URL to be accessed and then sends the information to the computer. A domain name server refers to a server that stores the domain names and corresponding IP addresses of all hosts in the network and has the function of converting domain names into IP addresses. The domain name must correspond to an IP address, and the IP address does not necessarily have a domain name. The domain name server is the server side in the client/server model. It mainly comes in two forms: main server and forwarding server. The process of mapping domain names to IP addresses is called “domain name resolution”.

On the Internet, domain names and IP addresses are one-to-one (or many-to-one). Although domain names are easy for people to remember, machines can only recognize each other’s IP addresses. The conversion between them is called domain name resolution. , Domain name resolution needs to be completed by a specialized domain name resolution server, and DNS is the server that performs domain name resolution. DNS naming is used in TCP/IP networks such as the Internet to find computers and services by user-friendly names.

2.2 Advantages of DNS

The advantages of DNS mainly include:

  1. Domain names are easy to remember: DNS can convert difficult-to-remember IP addresses into easy-to-remember domain names, making it possible to use domain names in network access, making it convenient for users.
  2. Convenient supervision: DNS can cooperate with the server to perform domain name resolution, provide supervision and identification, and facilitate website administrators to manage and maintain the website.
  3. Does not rely on a single IP: DNS technology allows users to only access the website domain name without knowing the website’s IP address, which makes network access more flexible.
  4. Easy to implement load balancing and disaster recovery backup: DNS can resolve the same domain name to different IP addresses, thereby achieving load balancing. At the same time, different DNS servers can implement disaster recovery backup to ensure network reliability and stability.
  5. Can resolve other services: In addition to resolving domain names into IP addresses, DNS can also resolve into other services, such as MX records (mail exchange records), CNAME records (alias records), etc., which makes network management more convenient and flexible.

2.3 How DNS works

  1. The client makes a domain name resolution request and sends the request to the local domain name server.
  2. When the local domain name server receives the request, it first queries the local cache. If the record entry exists, the local domain name server directly returns the query result.
  3. If there is no such record in the local cache, the local domain name server will directly send the request to the root domain name server, and then the root domain name server will return to the local domain name server the address of the primary domain name server of the queried domain (subdomain of the root).
  4. The local server then sends a request to the domain name server returned in the previous step, and then the server that accepts the request queries its own cache.
  5. If there is no such record, the address of the relevant subordinate domain name server is returned.
  6. Repeat the previous operation until the correct record is found.
  7. The local domain name server saves the returned results to the cache for next time use.
  8. The results are returned to the client.

2.4 Application scenarios

1. Disaster recovery switching-global traffic management

(1) Multi-line intelligent optimization analysis service

Use DNS routing technologies such as weighted polling, GeoDNS, and application health checks to implement automatic fault screening and failover of services

(2) Problems solved:

Distributed health check, multi-line load balancing, automatic shielding of fault addresses

2. DNS resists DDOS attacks

(1) Ability to defend against large-traffic DDOS attacks

For users of live streaming, video, games, and social networking, you can use the built-in high-defense DNS + high-defense IP through cloud analysis. Protective measures are added to both the parsing request and the website, making the website more secure.

(2) Problems solved:

DDOS attack defense, bandwidth exhaustion DNS rebound denial-of-service attack, 1 billion + QPS resolution requests

3. Mobile analysis

(1) Efficient and smooth APP access experience

Provides mobile low-latency solutions for mobile games, mobile Taobao, and mobile APP businesses, allowing mobile users to enjoy a more efficient and secure parsing request experience.

(2) Problems solved:

Anti-hijacking, parsing delay, local parsing taking effect slowly

3 WIZnet Ethernet chip

? Comparison of WIZnet mainstream hardware protocol stack Ethernet chip parameters

td>

Model Embedded Core Host I/F TX/RX Buffer HW Socket Network Performance
W5100S TCP/IPv4, MAC & PHY 8bit BUS, SPI 16KB 4 Max.25Mbps
W6100 TCP/IPv4/IPv6, MAC & PHY 8bit BUS, Fast SPI 32KB 8 Max.25Mbps
W5500 TCP/IPv4, MAC & PHY Fast SPI 32KB 8 Max 15Mbps
  1. W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.
  2. W6100 supports IPV6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.
  3. W5500 has more Sockets and send and receive buffers than W5100S.

4 DNS network setting example overview and usage

4.1 Flowchart

The running block diagram of the program is as follows:

4.2 Core preparation work

Software

  • Visual Studio Code
  • WIZnet UartTool

Hardware

  • W5100SIO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board
  • Micro USB interface data cable
  • TTL to USB
  • cable

4.3 Connection method

  • Connect the USB port of the PC through the data cable (mainly used for burning programs, but can also be used as a virtual serial port)
  • Convert TTL serial port to USB and connect the default pin of UART0:
    • RP2040 GPIO0 (UART0 TX) <----> USB_TTL_RX
    • RP2040 GPIO1 (UART0 RX) <----> USB_TTL_TX
  • When using module connection RP2040 for wiring
    • RP2040 GPIO16 <----> W5100S MISO
    • RP2040 GPIO17 <----> W5100S CS
    • RP2040 GPIO18 <----> W5100S SCK
    • RP2040 GPIO19 <----> W5100S MOSI
    • RP2040 GPIO20 <----> W5100S RST
  • Connect the PC and device to the router LAN port through network cables

4.4 Main code overview

We are using the official ioLibrary_Driver library of WIZnet. The library supports rich protocols and is easy to operate. The chip integrates the TCP/IP protocol stack on the hardware. The library also encapsulates the protocols above the TCP/IP layer. We only need to simply call the corresponding function to complete the application of the protocol. .

Step 1: Add the corresponding .h file to the dns_client.c file.

Step 2: Define the macros required for DHCP configuration and DNS configuration.

Step 3: Configuration of network information defines the domain name to be resolved.

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dns.h"
#include "dhcp.h"
#include "socket.h"

#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
#define DNS_RET_FAIL 0
#define DNS_RET_SUCCESS 1
#define DNS_RETRY 3 /* 3 times */
#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
#define DHCP_RETRY_COUNT 5 // DHCP retry times

wiz_NetInfo net_info = {<!-- -->
    .mac = {<!-- -->0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e}, //Define MAC variables
    .ip = {<!-- -->192, 168, 1, 10}, // Define IP variables
    .sn = {<!-- -->255, 255, 255, 0}, //Define subnet variables
    .gw = {<!-- -->192, 168, 1, 1}, //Define gateway variables
    .dns = {<!-- -->8, 8, 8, 8}, //Define DNS variables
    .dhcp = NETINFO_DHCP}; //Define the DNCP mode
wiz_NetInfo get_info;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {<!-- -->0,};
uint8_t Domain_name[] = "www.baidu.com"; // The domain name that needs to be resolved
uint8_t Domain_IP[4] = {<!-- -->0,};
static uint8_t dhcp_get_ip_flag = 0; // Define the DHCP acquisition flag

Step 4: Write the timer callback processing function for DHCP and DNS 1s tick timer processing function.

Step 5: The main function first initializes the serial port and SPI, and then writes the network configuration parameters of W5100S. After initializing DHCP, it starts DHCP to obtain the IP. When obtained, it prints the obtained IP. When the number of acquisitions exceeds the maximum number of acquisitions, static is used. IP, and then determine the DNS resolution status. If the resolution is successful, the successfully resolved IP will be printed. If the resolution fails, the resolution will continue. If the number of resolutions is greater than the maximum number of resolutions, the resolution will fail.

int main()
{<!-- -->
    struct repeating_timer timer; // Define the timer structure
    struct repeating_timer timer1; // Define the timer structure
    uint8_t dns_retry_cnt = 0; // Number of parsings
    uint8_t dns_ok = 0; // Parse success Peugeot

    /*mcu init*/
    stdio_init_all(); // Initialize the main control periphera
    wizchip_initialize(); // spi initialization

    /*dhcp init*/
    DHCP_init(SOCKET_ID, ethernet_buf); // DHCP initialization
    add_repeating_timer_ms(1000, repeating_timer_callback_DHCP, NULL, & timer); // Add DHCP 1s Tick Timer handler

    printf("wiznet chip dns client example.\r\
");
    network_init( & amp;net_info); // Configuring Network Information
    print_network_information( & amp;get_info); // Read back the configuration information and print it

    /*dns init*/
    DNS_init(0, ethernet_buf); // DNS client init
    add_repeating_timer_ms(1000, repeating_timer_callback_DNS, NULL, & timer1); // Add DNS 1s Tick Timer handler
    printf("\r\
====== DNS Client Example =====\r\
");
    printf(">Example Domain Name : %s\r\
", Domain_name);

    while(true)
    {<!-- -->

        if ((dns_ok == 0) & amp; & amp; (dns_retry_cnt < DNS_RETRY)) // Determine whether the parsing is successful or whether the parsing exceeds the number of times
        {<!-- -->
            switch (DNS_run(net_info.dns, Domain_name, Domain_IP)) // Read the DNS_run return value
            {<!-- -->
            case DNS_RET_FAIL: // The DNS domain name is successfully resolved
            {<!-- -->
                dns_retry_cnt + + ;
                printf("> DNS Failed\r\
");
            }
            break;
            case DNS_RET_SUCCESS: // DNS domain name resolution failed
            {<!-- -->
                dns_ok = 1;
                printf("> Translated %s to %d.%d.%d.%d\r\
", Domain_name, Domain_IP[0], Domain_IP[1], Domain_IP[2], Domain_IP[3]);
            }
            break;
            }
        }
    }
}

4.5 Burning Verification

  1. Open WIZ UartTool, fill in the parameters: select the COM Port corresponding to the serial port, baud rate 115200, 8 data bits, 1 stop bit, no check bit, no flow control, click open after filling in the parameters.
  2. After pressing the reset button, you can see that after obtaining the IP through DHCP, the dynamic DNS server is obtained.
  3. Use it to resolve Baidu’s domain name.
  4. You can see the IP that successfully resolved the domain name, proving that the DNS resolved the domain name successfully.

5 Notes

  • The resolved domain name may have multiple IP addresses at the same time, so it may not necessarily be the exact same IP address each time it is resolved.
  • If we want to use WIZnet’s W5500 to implement the example in this chapter, we only need to modify two places:

? (1) Find the header file wizchip_conf.h under library/ioLibrary_Driver/Ethernet/, and change the _WIZCHIP_ macro definition to W5500.

? (2) Find the CMakeLists.txt file under library and set COMPILE_SEL to ON. OFF is W5100S and ON is W5500.

6 Related links

WIZnet official website

WIZnet official library link

Routine link in this chapter

If you want to know more, leave a comment!