mDNS and Domain Name Resolution
Article Catalog
- mDNS and Domain Name Resolution
- 1. Introduction to mDNS
- 1.1 How does multicast DNS work?
- 1.2 Benefits of mDNS
- 1.3 Disadvantages and Risks of mDNS
- 2. Software preparation
- 3. Hardware preparation
- 4、Code realization
In computer networks, the multicast DNS (mDNS) protocol resolves hostnames to IP addresses in small networks that do not contain local name servers. It is a zero-configuration service that uses essentially the same programming interface, packet format, and operational semantics as the unicast Domain Name System (DNS).
mDNS is a multicast UDP service used to provide local network services and host discovery. This article describes in detail how to use mDNS in theESP32How to use mDNS in.
1. Introduction to mDNS
The Domain Name System (DNS) can be thought of as a very large phone book: a user can type a web address into a browser and the system will determine the correctIP address. Name resolution works by having your own computer query the appropriate DNS server, which stores a list of each hostname (i.e., a list of Web addresses) and assigns it the correct IP address. However, multicast DNS takes a different route. How does the alternative to classic DNS work?
1.1 How does multicast DNS work?
Multicast DNS (mDNS) is a protocol designed to aid name resolution in small networks. To do this, it takes a different approach than the well-known DNS. Instead of querying name servers, all participants in the network address them directly. The appropriate client sends a multicast to the network while asking which network participant matches the hostname. Multicast is a unique form of communication by which a single message is directed to a group of recipients. The group may consist of, for example, an entire network or a subnetwork.
In this way, the request is also sent to the group participant that owns the hostname being searched. The latter responds to the entire network (also via multicast). All participants are informed of the link between the name and the IP address and can create the corresponding entry in their mDNS caches. As long as this representation is valid, no one in the network needs to request a hostname.
Multicast DNS generates relatively large amounts of traffic, but tries to conserve active network resources. To do this, the client making the request sends (based on the current cached entry) what they believe to be the correct reply. The recipient is only required to reply if that information is no longer correct or if the entry is about to expire. Other participants are notified before they receive the reply. With this protocol, traffic within the network can be reduced.
In general, multicast DNS is only available for hostnames ending in .local. This limits this form of name resolution on local networks. mDNS will not process hostnames with other top-level domains (TLDs) such as .de or .com. Therefore, URLs cannot be resolved in this way.
Note: Multicast DNS was developed in the early 2010s and is described in RFC 6762.
MDNS was developed in the context of Zeroconf (Zero Configuration Networking). The idea behind Zero Configuration Networks is that computers can communicate through humans without much prior adaptation. Multicast DNS fits within these constraints. The multicast process isTCPpart of the /IP and can be run without proper configuration.
1.2 Benefits of mDNS
Multicast DNS is designed for small networks and aims to improve their user-friendliness. The idea is that users can connect devices in a secret LAN without any problems. There is no need to create servers or directories as all devices exchange information with each other via their IP addresses. In this way, other devices can be imported quickly and dynamically.
A popular implementation of mDNS is Apple's Bonjour. The service is primarily designed to make it easier to connect networked printers to a PC or Mac. Since the devices exchange information through their IP addresses, users are not allowed to configure the connection independently. In addition to Apple's service, you can now use theexpand one's financial resourcesThe software Avahi acts as an mDNS service. This makes it possible to connect different devices without having to perform prior configuration. FromWindows Starting with 10, mDNS is provided as part of the Microsoft operating system.
1.3 Disadvantages and Risks of mDNS
However, simplicity comes with some drawbacks. One such problem lies in the multicast process itself. While the protocol does attempt to keep network traffic low, the computers involved must constantly monitor the network and process incoming messages. This is a burden on processing power.
In addition, the assignment of host names is problematic. In principle, it is free to assign a name to each device, as long as the name ends in ".local". This could (at least in theory) lead to two network participants being represented by the same hostname. The developers of mDNS have consciously not implemented a solution for such scenarios. On the one hand, they believe that this scenario is rare. On the other hand, the double designation may be intentional.
Another issue is the source of danger. In many cases, mDNS is open. This means that it will also be open to external queries (via thethe Internet) to respond. Cybercriminals can find these types of open services and utilize them for DDoS attacks. Network devices are then abused in order to bombard targeted servers with queries. In addition, sensitive data can be discovered through open multicast DNS. In this way, for example, an attacker can read the Mac address of a connected device and use this information for further attacks.
2、hardwareintend
- Arduino IDE
In the previous article, how to build ESP32'sArduino IDE open environment, main reference:
- ESP32-Arduino-Development Examples-Arduino Development Environment Setup
3. Hardware preparation
- ESP32 Development Board
4、Code realization
This example will use the following open source libraries:
- ESPAsyncWebServer
- AsyncTCP
The sample code is as follows:
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
AsyncWebServer server(80);
const char* ssid = "********"; // Your WiFi SSID
const char* password = "********"; // Your WiFi Password
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
if(!MDNS.begin("esp32")) {
Serial.println("mdns start failed");
while(true);
}
//("IP Address: ");
Serial.println("/");
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/text", "Hello,World");
});
server.begin();
}
void loop() {
delay(10);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
How does the code work?
1) Import dependent library header files:
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
- 1
- 2
- 3
- 4
- 5
- 6
2) Create a server instance
AsyncWebServer server(80);
- 1
3) Define WiFi connection credentials
const char* ssid = "********"; // Your WiFi SSID
const char* password = "********"; // Your WiFi Password
- 1
- 2
4) Insetup
function (math.)Middle.
Initialize the serial port:
Serial.begin(115200);
- 1
Set the WiFi operating mode to STA mode:
WiFi.mode(WIFI_STA);
- 1
Connect to WiFi and wait for the connection to complete:
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
- 1
- 2
- 3
- 4
- 5
Start mDNS:
if(!MDNS.begin("esp32")) {
Serial.println("mdns start failed");
while(true);
}
- 1
- 2
- 3
- 4
parametersesp32
for the domain name to be resolved.
Finally start the server:
//("IP Address: ");
Serial.println("/");
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/text", "Hello,World");
});
server.begin();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5) Inloop
function, calling thedelay
function with a delay of 10 milliseconds. This is done to avoid the watchdog task timing out.
void loop() {
delay(10);
}
- 1
- 2
- 3
After the program is downloaded and the WiFi connection is complete, type in the browser address:
/
Accessible.