Local Service Discovery (LSD) provides a mechanism similar to SSDP (via UDP multicast over HTTP) to announce its presence in a specific group to local neighbors. This allows other local devices to know what services this device provides, facilitating communication and data transfer between them. This mechanism can serve as a primary peer source for local transfers and can also be used to supplement other sources that only operate on global unicast addresses.
Protocol#
LSD uses the following multicast groups: A) 239.192.152.143:6771 (org-local)
and B) [ff15::efc0:988f]:6771 (site-local)
.
Implementation note: Since multicast groups are more extensive than local LANs, the IP_MULTICAST_TTL socket option may need to be set to a value greater than the default.
The announce message format for the LSD protocol is:
BT-SEARCH * HTTP/1.1\r\n
Host: <host>\r\n
Port: <port>\r\n
Infohash: <hash>\r\n
cookie: <cookie (optional)>\r\n
\r\n
\r\n
host#
The host needs to comply with the specifications of RFC 2616 section 14.23 and RFC 2732, specifying the multicast group address to which the announce message is sent.
RFC 2616 is the specification document for HTTP/1.1, defining a series of information about the HTTP protocol. Among them, section 14.23 describes the format of the Host header. The Host header is used to specify the hostname and port number of the target server for the request. RFC 2732 is a standard for URLs that allows the use of IPv6 addresses to represent hostnames in URLs. This means that in the Host header of the LSD protocol, IPv6 addresses can be used to represent multicast group addresses.
port#
This field specifies the port number that the BitTorrent client is listening on, represented in base-10 ASCII encoding.
hash#
This field is a 40-character hexadecimal encoded infohash value used to uniquely identify a torrent file.
When sending multiple infohashes, care should be taken to avoid packet lengths exceeding 1400 bytes to avoid MTU/fragmentation issues. During network transmission, if the packet length exceeds the size of the Maximum Transmission Unit (MTU), IP fragmentation occurs, leading to reduced network transmission efficiency and increased risk of packet loss.
cookie#
An opaque value that is completely unknown and invisible to the client, so it only needs to be sent to the client without further interpretation. The purpose of this value is to allow the client to filter when receiving its own announce requests to avoid duplicate processing. Any additional header information that a client does not understand should be ignored to ensure forward compatibility.
Implementation#
First, if a client is participating in a swarm, it should send an LSD announce every 5 minutes on each interface to let other clients know of its existence and connect to it. To avoid causing multicast storms in large networks, a client can only send an announcement once per minute at most.
Second, if a client is downloading or uploading more than 5 torrents simultaneously, it can choose to alternate sending announcements on each torrent or include multiple infohashes in a single announcement (distinguishing the hash values for different torrents). This allows other clients to use this information to establish connections with the client.
When a client receives an LSD announcement from another client, it needs to determine the IP address to contact the remote client based on the UDP source address.
Summary#
In the BitTorrent protocol, Local Service Discovery (LSD) is a mechanism for discovering other BitTorrent clients within a local network. When a BitTorrent client starts, it broadcasts its presence through LSD and receives broadcast information from other clients, establishing peer-to-peer connections.
LSD uses UDP multicast to quickly discover other clients within a local network. Additionally, LSD can be used in conjunction with other local service discovery protocols like mDNS to further improve the efficiency of discovering devices within a local network.
LSD is an important component of the BitTorrent protocol, accelerating node discovery in P2P downloads and improving the speed and stability of file downloads.