-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2193 from input-output-hk/trusted-peers-dns-1
DNS for trusted peers
- Loading branch information
Showing
5 changed files
with
87 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,67 @@ | ||
use multiaddr::{AddrComponent, Multiaddr}; | ||
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; | ||
use multiaddr::{AddrComponent, Multiaddr, ToMultiaddr}; | ||
use std::{ | ||
io, | ||
net::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}, | ||
}; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("Not enough components in multiaddr")] | ||
InvalidMultiaddr, | ||
#[error("Cannot find DNS record")] | ||
NotFound(#[source] io::Error), | ||
#[error("No IPv4 address found")] | ||
NoIP4, | ||
#[error("No IPv6 address found")] | ||
NoIP6, | ||
} | ||
|
||
pub fn multiaddr_to_socket_addr(addr: &Multiaddr) -> Option<SocketAddr> { | ||
let mut components = addr.iter(); | ||
|
||
match components.next()? { | ||
AddrComponent::IP4(ipv4) => { | ||
if let AddrComponent::TCP(port) = components.next()? { | ||
Some(SocketAddr::V4(SocketAddrV4::new(ipv4, port))) | ||
} else { | ||
None | ||
} | ||
} | ||
AddrComponent::IP6(ipv6) => { | ||
if let AddrComponent::TCP(port) = components.next()? { | ||
Some(SocketAddr::V6(SocketAddrV6::new(ipv6, port, 0, 0))) | ||
let ip = components.next()?; | ||
let port = if let AddrComponent::TCP(port) = components.next()? { | ||
port | ||
} else { | ||
return None; | ||
}; | ||
|
||
match ip { | ||
AddrComponent::IP4(ipv4) => Some(SocketAddr::V4(SocketAddrV4::new(ipv4, port))), | ||
AddrComponent::IP6(ipv6) => Some(SocketAddr::V6(SocketAddrV6::new(ipv6, port, 0, 0))), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn multiaddr_resolve_dns(addr: &Multiaddr) -> Result<Option<Multiaddr>, Error> { | ||
let mut components = addr.iter(); | ||
|
||
let ip_or_fqdn = components.next().ok_or(Error::InvalidMultiaddr)?; | ||
let port = components | ||
.next() | ||
.and_then(|ac| { | ||
if let AddrComponent::TCP(port) = ac { | ||
Some(port) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
} | ||
}) | ||
.ok_or(Error::InvalidMultiaddr)?; | ||
|
||
let socket_addr = match ip_or_fqdn { | ||
AddrComponent::DNS4(fqdn) => (fqdn.as_str(), port) | ||
.to_socket_addrs() | ||
.map_err(Error::NotFound)? | ||
.find(|addr| matches!(addr, SocketAddr::V4(_))) | ||
.ok_or(Error::NoIP4)?, | ||
AddrComponent::DNS6(fqdn) => (fqdn.as_str(), port) | ||
.to_socket_addrs() | ||
.map_err(Error::NotFound)? | ||
.find(|addr| matches!(addr, SocketAddr::V6(_))) | ||
.ok_or(Error::NoIP6)?, | ||
_ => return Ok(None), | ||
}; | ||
|
||
Ok(Some(socket_addr.to_multiaddr().unwrap())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters