Overview
DNS (Domain Name System) is a distributed database service that maps domain names that people can easily remember with IP addresses used for addressing and positioning in computer networks. On the Internet, every device has a unique IP address. However, since IP addresses are usually a string of difficult-to-remember numbers, the DNS system came into being, providing a service that converts human-readable domain names with certain logical structures into IP addresses.
When the user enters a URL in the browser, a DNS resolution process is actually triggered, which includes the following steps.
Query the local DNS cache: First check whether the machine has an IP address record corresponding to the domain name.
Recursive query: If there is no hit cache, the request is sent to the local DNS server provided by the user's ISP. The local DNS server is responsible for initiating the recursive query process, asking the root domain name server, top-level domain name server and authoritative domain name server upward step by step until the IP address of the target domain name is found.
Response and cache: Once the authoritative domain name server returns the IP address corresponding to the domain name, this information will be returned to the user along the query path and cached by the DNS server along the way to speed up the subsequent query of the same domain name.
CHP_DNS class
In C++, getting the IP address corresponding to the DNS domain name is usually not implemented directly by writing the original DNS query protocol (although it can be done theoretically), but instead calls the network library or API provided by the operating system for parsing. This is because handling DNS protocol details directly is quite complex and error-prone, and the operating system and standard library have encapsulated these features.
gethostbyname and getaddrinfo functions are used to get the IP address corresponding to the domain name, but both functions are blocking. In non-blocking usage scenarios, it is not appropriate to use these two functions directly. In order to facilitate application layer use, we encapsulate the asynchronous DNS resolution class CHP_DNS. The CHP_DNS class is a single instance class derived from the CHP_BaseThread class. For the header file of the CHP_DNS class, please refer to the following example code.
#pragma once #include <string> #include <map> #include "HP_BaseThread.h" #include "HP_Mutex.h" class CHP_DNS : public CHP_BaseThread { public: static void Open(); static CHP_DNS *&Singleton(); static void Close(); unsigned int FetchIP(const char *pszIP); void DefetchIP(const char *pszIP); static int GetIP(const char *pszIP, unsigned int &uiIP); protected: CHP_DNS(); virtual ~CHP_DNS(); virtual unsigned int Run(); private: typedef struct _TDNSInfo { _TDNSInfo() { uiIP = 0; uiLastGotTick = 0; uiLastActiveTick = 0; } unsigned int uiIP; unsigned int uiLastGotTick; unsigned int uiLastActiveTick; }TDNSInfo; typedef std::map<std::string, TDNSInfo> IPToDNSInfoMap; static CHP_DNS *m_pThis; IPToDNSInfoMap m_mapIPToDNSInfo; bool m_bMapChanged; CHP_Mutex m_mutexMap; };
The CHP_DNS class exports two instance member functions and a static member function, which are introduced below.
FetchIP: non-blocking to obtain DNS. The parameter pszIP is an IP address or domain name; when the return value is 0, it means that the resolution is not successful, and this function needs to be continued to be called, and other IP addresses that are successfully resolved.
DefetchIP: Cancel non-blocking acquisition of DNS. When FetchIP keeps returning 0 and the upper layer judges that the timeout, it is best to call this interface for release. Otherwise, the underlying layer will keep trying to parse, which will affect the parsing process of other addresses. The parameter pszIP is an IP address or domain name.
GetIP: Blocking to get DNS, static functions. The parameter pszIP is the IP address or domain name, the parameter uiIP is the IP address that was successfully resolved, the return value is 0 to indicate success, and other means error code.
Summarize
DNS resolution is a process of converting a domain name (such as:) into a corresponding IP address (such as: 192.168.1.100), which is completed by a DNS server. DNS resolution is a key service on the internet, allowing people to access websites through easy-to-remember domain names without having to remember complex IP addresses.
Overall, DNS resolution is a key service on the internet, allowing people to access websites through easy-to-remember domain names without having to remember complex IP addresses. At the same time, DNS resolution is also stored in distributed ways. Through collaboration between multiple servers, rapid and accurate domain name-to-IP address conversion is achieved.
This is the end of this article about the implementation of DNS resolution in C++ practical library. For more related C++ DNS resolution content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!