-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add conversions between sockets and multiaddrs #99
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider Udp
as well and not just Tcp
.
I think these would be better served as dedicated functions so we can include the tcp
bit in the name to make this clear.
What is also not clear to me is what should happen with the remaining components of the multiaddr? Currently we just drop them. Should we return them instead?
I agree, I'll add the equivalent UDP methods and make the existing TCP ones dedicated methods rather than
Are you referring to the case where we need to produce a pub fn into_tcp_socket(&self) -> Option<(SocketAddr, Multiaddr)> {
/* snip */
Some((sock, remaining))
} Which would yield The issue with this is that it implicitly assumes a left-to-right parsing approach for what it means to convert a multiaddr into a |
Left-to-right is how you are meant to parse multiaddr, see https://github.com/multiformats/multiaddr?tab=readme-ov-file#interpreting-multiaddrs. Perhaps, the functions should be called something like: impl Multiaddr {
pub fn pop_front_tcp_socket(&mut self) -> Option<SocketAddr>;
} This modifies the |
Perhaps what we should add is a You should then be able to implement an efficient extension trait that uses Curious to hear what other maintainers think. |
match socket { | ||
SocketAddr::V4(sock) => Self::empty() | ||
.with(Protocol::Ip4(*sock.ip())) | ||
.with(Protocol::Tcp(sock.port())), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SocketAddr
might as well specify a UDP address, thus this From
implementation is not correct.
This PR allows two type conversions involving both
Multiaddr
andSocketAddr
:SocketAddr
toMultiaddr
Multiaddr
toSocketAddr
(as the space of all possible values of the former is obviously much larger than that for the latter)These are certainly fairly minor changes; however, they enable some niceties such as: