Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/mdns: Use a random alphanumeric string for peer name #2311

Merged
merged 11 commits into from Nov 25, 2021

Conversation

jochasinga
Copy link
Contributor

Fixes #2285.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. Thanks a bunch for picking this up @jochasinga!

Would you mind adding a changelog entry as well?

//CC @peat as you have been working on this logic in the past.

protocols/mdns/src/dns.rs Outdated Show resolved Hide resolved
@jochasinga
Copy link
Contributor Author

Very cool. Thanks a bunch for picking this up @jochasinga!

Would you mind adding a changelog entry as well?

//CC @peat as you have been working on this logic in the past.

In the changelog it is currently 0.32.0rc-1. I didn't find a doc explaining the process. Do you mind advise how this change should be reflected in the versioning?

@mxinden
Copy link
Member

mxinden commented Nov 2, 2021

Sorry for the confusion @jochasinga. Could you apply the diff below?

diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md
index f3287379..a2376242 100644
--- a/protocols/mdns/CHANGELOG.md
+++ b/protocols/mdns/CHANGELOG.md
@@ -1,3 +1,9 @@
+# 0.32.1 [unreleased]
+
+- Use a random alphanumeric string for mDNS peer name (see [PR 2311]).
+
+[PR 2311]: https://github.com/libp2p/rust-libp2p/pull/2311/
+
 # 0.32.0 [2021-11-01]
 
 - Make default features of `libp2p-core` optional.
diff --git a/protocols/mdns/Cargo.toml b/protocols/mdns/Cargo.toml
index 43370a9b..0bf1605e 100644
--- a/protocols/mdns/Cargo.toml
+++ b/protocols/mdns/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 name = "libp2p-mdns"
 edition = "2018"
-version = "0.32.0"
+version = "0.32.1"
 description = "Implementation of the libp2p mDNS discovery method"
 authors = ["Parity Technologies <admin@parity.io>"]
 license = "MIT"

@jochasinga jochasinga marked this pull request as ready for review November 2, 2021 20:12
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

One small nit that just now caught my attention: Given that the peer name is no longer based on the peer ID, would you mind renaming the references from peer ID to peer name?

protocols/mdns/src/dns.rs Outdated Show resolved Hide resolved
protocols/mdns/src/dns.rs Outdated Show resolved Hide resolved
protocols/mdns/src/dns.rs Outdated Show resolved Hide resolved
@elenaf9
Copy link
Contributor

elenaf9 commented Nov 3, 2021

Just that I understand it correctly: with this change, the PeerId that is reported in an MdnsEvent (Either in the DiscoveredAddrsIter or ExpiredAddrsIter) is not the actual id of the peer that is listening on that address?

If yes, shouldn't this then also be changed in the examples? E.g. /examples/distributed-key-value-store.rs adds the mdns peer-address pairs to the kademlia DHT, which then wouldn't really make sense.

jochasinga added a commit to jochasinga/rust-libp2p that referenced this pull request Nov 3, 2021
Because the peer name is no longer based on peer ID.

See also: libp2p#2311 (review)
@mxinden
Copy link
Member

mxinden commented Nov 3, 2021

Just that I understand it correctly: with this change, the PeerId that is reported in an MdnsEvent (Either in the DiscoveredAddrsIter or ExpiredAddrsIter) is not the actual id of the peer that is listening on that address?

If yes, shouldn't this then also be changed in the examples? E.g. /examples/distributed-key-value-store.rs adds the mdns peer-address pairs to the kademlia DHT, which then wouldn't really make sense.

You are raising a very important point here @elenaf9 that I wish I didn't miss before writing #2285. Sorry for the trouble this will cause @jochasinga.

I will write up more details on #2285, just a short summary for now:

When receiving an mDNS response, rust-libp2p actually does parse the peer name as a PeerId and discards an address in case it contains a /p2p/QmXX PeerId which doesn't match the one retrieved from the peer name field.

let mut peer_name = match record_value.rsplitn(4, |c| c == '.').last() {
Some(n) => n.to_owned(),
None => return None,
};
// if we have a segmented name, remove the '.'
peer_name.retain(|c| c != '.');
let peer_id = match data_encoding::BASE32_DNSCURVE.decode(peer_name.as_bytes()) {
Ok(bytes) => match PeerId::from_bytes(&bytes) {
Ok(id) => id,
Err(_) => return None,
},
Err(_) => return None,
};

The above implies that peers not running this patch will discard any responses send by peers that are running this patch.

To prevent a network to split, we will need to do a step in between namely to ignore the peer name of a mDNS response and not discard responses in case there is a mismatch. Once that is rolled out on a network, step two would be to roll out this patch setting the peer name to a random string.

@jochasinga
Copy link
Contributor Author

@mxinden keep me posted on the update. Would love to get this fixed up.

@mxinden
Copy link
Member

mxinden commented Nov 9, 2021

@mxinden keep me posted on the update. Would love to get this fixed up.

See #2285 (comment).

jochasinga added a commit to jochasinga/rust-libp2p that referenced this pull request Nov 17, 2021
Per @MarcoPolo points that an error isn't useful for the caller
and following MdnsPacket API.

See also: libp2p#2311 (comment)
@jochasinga
Copy link
Contributor Author

@mxinden Do we need to change the examples per @elenaf9 comment?

@MarcoPolo
Copy link
Contributor

Just that I understand it correctly: with this change, the PeerId that is reported in an MdnsEvent (Either in the DiscoveredAddrsIter or ExpiredAddrsIter) is not the actual id of the peer that is listening on that address?

(from @elenaf9)

With the changes in here now, I don't think this is true anymore.

Now the peerid is correct in MdnsResponse, which is used to generate the MdnsEvent. So I don't think we need to change the examples. If this is wrong, then the correct fix is to have the correct PeerId be reported, not changing the examples.

@MarcoPolo
Copy link
Contributor

@jochasinga can you please rebase this branch?

Rename `encode_peer_id` to `generate_peer_id` Because `encode_peer_id`
now does not encode anything.
Update CHANGELOG and Cargo.toml
Because the peer name is no longer based on peer ID.

See also: libp2p#2311 (review)
- Remove the peer ID parsing logic and ignore the peer name.
- Remove `my_peer_id` from `MdnsPeer::new(...)` parameters.
- Ignore addresses with bad or no peer IDs, use the peer ID
of the first address with a peer ID as a reference, and ignore
all addresses whose peer ID does not match the first peer ID.

Note that `MdnsPeer::new(...)` now returns a Result type because
there is no gaurantee that there will be a peer ID to create the peer.
Per @MarcoPolo points that an error isn't useful for the caller
and following MdnsPacket API.

See also: libp2p#2311 (comment)
@jochasinga
Copy link
Contributor Author

@mxinden can you please run checks on this?

protocols/mdns/src/query.rs Outdated Show resolved Hide resolved
protocols/mdns/src/query.rs Outdated Show resolved Hide resolved
Refactor match statement to map since it's more idiomatic and concise.
Use expect in tests for better cue in the test.
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. Thank you for the many iterations!

Just two comments on the release process:

@@ -1,3 +1,9 @@
# 0.33.1 [unreleased]

- Use a random alphanumeric string for mDNS peer name (see [PR 2311]).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Use a random alphanumeric string for mDNS peer name (see [PR 2311]).
- Use a random alphanumeric string instead of the local peer ID for mDNS peer
name (see [PR 2311]).
Note that previous versions of `libp2p-mdns` expect the peer name to be a
valid peer ID. Thus they will be unable to discover nodes running this new
version of `libp2p-mdns`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to correct the protocols/mdns/CHANGELOG to 0.34.0 as well here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please 🙏

protocols/mdns/Cargo.toml Outdated Show resolved Hide resolved
Co-authored-by: Max Inden <mail@max-inden.de>
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge later today unless anyone objects.

Thanks @jochasinga for all the work and thanks @MarcoPolo for the additional help!

@mxinden mxinden changed the title Use a random alphanumeric string for mDNS peer name protocols/mdns: Use a random alphanumeric string for peer name Nov 25, 2021
@mxinden mxinden merged commit f0000d5 into libp2p:master Nov 25, 2021
@jochasinga jochasinga deleted the 2285-random-peername branch November 30, 2021 01:48
vnermolaev pushed a commit to vnermolaev/rust-libp2p that referenced this pull request Nov 30, 2021
…p#2311)

With libp2p/specs#368 the definition of the _peer name_
changed in the mDNS specification.

> peer-name is the case-insensitive unique identifier of the peer, and is less
> than 64 characters.
>
> As the this field doesn't carry any meaning, it is sufficient to ensure the
> uniqueness of this identifier. Peers SHOULD generate a random, lower-case
> alphanumeric string of least 32 characters in length when booting up their
> node. Peers SHOULD NOT use their Peer ID here because a future Peer ID could
> exceed the DNS label limit of 63 characters.

https://github.com/libp2p/specs/blob/master/discovery/mdns.md

This commit adjusts `libp2p-mdns` accordingly.

Also see libp2p/go-libp2p#1222 for the corresponding
change on the Golang side.

Co-authored-by: Max Inden <mail@max-inden.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

protocols/mdns: Use a random string as peer-name
4 participants