Multicast DNS (mDNS) for libp2p service discovery

Multicast DNS (mDNS) for libp2p service discovery

After initialization, libp2p will continue to "get to know" more nodes through various methods, including using Bootstrap list, mDNS, DHT, etc.
libP2P defines the routing interface. There are currently 2 implementations, namely KAD routing and MDNS routing. Extended It's easy, just implement the corresponding method according to the interface. This article focuses on mDNS.

In libp2p, mDNS and Kademlia DHT are both technologies used for node discovery and connection management. When a node needs to join the libp2p network, it can find and connect to other nodes through mDNS and Kademlia DHT to establish a peer-to-peer network. Specifically, mDNS is typically used to find other nodes in the local area network, while Kademlia DHT is used to find other nodes in the wide area network.

一、Multicast DNS(mDNS)

multicast DNS, specification document address: http://www.ietf.org/rfc/rfc6762.txt

mDNS was developed based on Apple's Bonjour protocol and eventually became a formal standard (RFC 6762). This protocol is supported by Windows 10, Mac, Linux and Raspberry Pi.

Multicast DNS (mDNS) is a method of resolving network hostnames in small networks (such as a single subnet) without the need for a central site to assign names. It is part of the zero-configuration networking (Zeroconf) suite of protocols designed to allow network devices to discover and communicate with each other without additional configuration.

How mDNS works

How mDNS works
First, some reserved addresses are specified in the IP protocol, one of which is 224.0.0.251, and the corresponding IPv6 address is [FF02::FB].
The mDNS protocol specifies a port, 5353.
mDNS is based on UDP protocol.
Every host that enters the LAN, if the mDNS service is turned on, will multicast a message to all hosts in the LAN, who am I and what is my IP address. Then other hosts that also have the service will respond and tell you who it is and what its IP address is.

Here’s how mDNS works:

When a device wants to resolve a hostname, it will broadcast an mDNS request on the local network containing the hostname it wants to resolve.

All devices receiving the request will check if they correspond to the hostname in the request. If a device finds that it corresponds to the hostname in the request, it will respond to the request with its IP address.

After the request-initiating device receives the reply, it knows the IP address of the device corresponding to the host name, and can then communicate directly with that device.

mDNS is mainly used in small networks, such as home networks or single office networks. In these networks, the number of devices is small, and communication between devices is often required, so mDNS is very suitable. Using mDNS in these networks allows devices to discover and communicate with each other without a central site assigning names, greatly simplifying network configuration.

You can use mDNS to discover nodes in the same LAN. As long as the nodes have the mDNS service started, they can discover each other and start communicating.

The difference between Multicast DNS (mDNS) and DNS (Domain Name System)

Multicast DNS (mDNS) and DNS (Domain Name System) are both services used in the network to resolve host names to IP addresses.

  1. Service Scope
    The DNS service is a core part of the Internet, resolving hostnames to IP addresses on a global scale. mDNS is mainly used for a single local area network, such as a home or small office network.

  2. Centralized vs distributed
    DNS is centralized and relies on DNS servers distributed around the world. When you want to resolve a hostname, your device sends a request to the DNS server, and the DNS server replies with the corresponding IP address. mDNS is distributed and does not rely on any central server. When a device wants to resolve a hostname, it broadcasts the request on the LAN, and the device with the corresponding hostname responds to the request.

  3. Configuring
    DNS usually requires manual configuration. For example, you need to purchase a domain name and then associate the domain name with your IP address. And mDNS is zero-configuration, and the device can automatically broadcast its host name and IP address on the network.

  4. Security
    DNS has a complex set of security mechanisms, such as DNSSEC, to protect DNS requests and replies. The security of mDNS is weak, and it mainly relies on the physical isolation of the network to ensure security.

2. The relationship between mDNS and libp2p

Libp2p is a modular networking stack that allows you to choose the features you need, including transport, multiplexing, encryption, peer discovery, and more. Peer Discovery is an important part of Libp2p. It has many implementation methods, mDNS is only one of them.

Libp2p's mDNS discovery module allows peer nodes in the same local network to discover each other, but Libp2p also supports other peer discovery methods, such as Kademlia Distributed Hash Table (DHT) and Bootstrap List. This means you can use Libp2p's service discovery mechanism without using mDNS.

If your application requires peer discovery within a larger network (such as the Internet), or your network environment does not support mDNS, you can choose to use Libp2p's other service discovery modules.

mDNS is a member of the libp2p protocol family and plays an important role in building distributed applications and service discovery. In libp2p, mDNS is used for service discovery between nodes in the same LAN. It allows nodes to broadcast their presence and discover other nodes, which is useful for creating peer-to-peer applications within a local area network.

3. Kademlia Distributed Hash Table (DHT) and mDNS

mDNS is primarily used for discovering services in local area networks, such as home networks or small office networks. In this environment, the number of devices is relatively small, all on the same network, so mDNS can discover services quickly and efficiently. However, mDNS is not suitable for use in large networks or the Internet because it works by broadcasting service information, which will cause a lot of network traffic if there are too many devices in the network.

In contrast, Kademlia DHT is suitable for large networks and the Internet. It is a distributed key-value storage system. Each node stores a part of the data and knows how to find the data stored by other nodes. This allows Kademlia DHT to efficiently discover services in large-scale networks without generating excessive network traffic. However, the disadvantage of Kademlia DHT is its higher complexity and the need for more computing resources.

3. mdns example

Official code: https://github.com/libp2p/go-libp2p-examples/tree/master/chat-with-mdns

Using mDNS in libp2p

Official demo: chat-with-mdns

P2P Network Programming-3-Case Practice: PubSub
Reference URL: https://blog.csdn.net/weixin_43988498/article/details/120488890

As a standard, mDNS is implemented on almost any latest device. Mastering the message format of mDNS is of great significance in development and application.

To use these two service discovery protocols in Go, libp2p provides corresponding modules. Here are some simple usage examples:


	// To construct a simple host with all the default settings, just use `New`
	h, err := libp2p.New()
	if err != nil {
		panic(err)
	}
	defer h.Close()

	fmt.Printf("Hello World, my p2p hosts ID is %s\n", h.ID())

	// 设置mDNS服务
	if common.EnableMdns {
		fmt.Printf("hosts ID is %s enable MDNS for discory node!\n", h.ID())
		peerChan := p2p.InitMDNS(h, common.RendezvousString)
		for { // allows multiple peers to join
			peer := <-peerChan // will block until we discover a peer
			//fmt.Println("Found peer:", peer, ", connecting")
			fmt.Println("Found peer:")
			fmt.Println("ID:", peer.ID)
			fmt.Println("Address:", peer.Addrs[0].String())
			for _, addr := range peer.Addrs {
				fmt.Println("Address:", addr.String())
			}

		}
		// todo other
	}

Summary: You will find that it will print a lot of addresses, all of which are various addresses of the interface, indicating that it will try various IP discovery.

We can create a host and specify the listening port to solve this problem, such as:

h, err := libp2p.New(
	libp2p.ListenAddrStrings(
		"/ip4/0.0.0.0/tcp/9000",      // regular tcp connections
		"/ip4/0.0.0.0/udp/9000/quic", // a UDP endpoint for the QUIC transport
	),
)

0.0.0.0 can be changed to the IP of a specific interface. In this way, all nodes in the LAN that can be connected to this IP will be found, and the address list will be relatively simple and clear.

Using Kademlia DHT for service discovery:

import (
    "github.com/libp2p/go-libp2p"
    "github.com/libp2p/go-libp2p-core/host"
    "github.com/libp2p/go-libp2p-kad-dht"
)

h, _ := libp2p.New()
dht, _ := dht.New(ctx, h)
Custom Notifee structure

What is the difference between type discoveryNotifee struct { PeerChan chan peer.AddrInfo } and type discoveryNotifee struct { h host.Host }




The difference between these two discoveryNotifee types lies in the fields they contain.

The first type discoveryNotifee contains a field PeerChan, which is of type chan peer.AddrInfo. This channel is used to receive a peer.AddrInfo structure, which may contain information about the discovered peer. By reading the PeerChan channel, the address information of the discovered peer node can be obtained.

The second type discoveryNotifee contains a field h, which is of type host.Host. host.Host represents the host interface of the node, which provides the function of communicating with other nodes. Through this field, you can access the host object of the node to perform more operations, such as establishing connections, sending messages, etc.

In general, the difference between these two types lies in the differences in their internal fields. The first type uses a channel to receive the address information of the discovered peer node, and the second type uses the host object as a field to provide more node communication functions. Which type to use depends on the needs and design of your code.

type Notifee interface { HandlePeerFound(peer.AddrInfo) } The official Notifee interface only stipulates the methods to be implemented. Our customized xxxNotifee structure can be used freely according to our needs.


おすすめ

転載: blog.csdn.net/inthat/article/details/134536583
おすすめ