Skip to content

Commit

Permalink
Add dnsaddr (#1356)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Krieger <[email protected]>
  • Loading branch information
ackintosh and tomaka authored Feb 6, 2020
1 parent b200728 commit 69852a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions misc/multiaddr/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::onion_addr::Onion3Addr;
const DCCP: u32 = 33;
const DNS4: u32 = 54;
const DNS6: u32 = 55;
const DNSADDR: u32 = 56;
const HTTP: u32 = 480;
const HTTPS: u32 = 443;
const IP4: u32 = 4;
Expand Down Expand Up @@ -67,6 +68,7 @@ pub enum Protocol<'a> {
Dccp(u16),
Dns4(Cow<'a, str>),
Dns6(Cow<'a, str>),
Dnsaddr(Cow<'a, str>),
Http,
Https,
Ip4(Ipv4Addr),
Expand Down Expand Up @@ -131,6 +133,10 @@ impl<'a> Protocol<'a> {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Dns6(Cow::Borrowed(s)))
}
"dnsaddr" => {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Dnsaddr(Cow::Borrowed(s)))
}
"sctp" => {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Sctp(s.parse()?))
Expand Down Expand Up @@ -210,6 +216,11 @@ impl<'a> Protocol<'a> {
let (data, rest) = split_at(n, input)?;
Ok((Protocol::Dns6(Cow::Borrowed(str::from_utf8(data)?)), rest))
}
DNSADDR => {
let (n, input) = decode::usize(input)?;
let (data, rest) = split_at(n, input)?;
Ok((Protocol::Dnsaddr(Cow::Borrowed(str::from_utf8(data)?)), rest))
}
HTTP => Ok((Protocol::Http, input)),
HTTPS => Ok((Protocol::Https, input)),
IP4 => {
Expand Down Expand Up @@ -346,6 +357,12 @@ impl<'a> Protocol<'a> {
w.write_all(encode::usize(bytes.len(), &mut encode::usize_buffer()))?;
w.write_all(&bytes)?
}
Protocol::Dnsaddr(s) => {
w.write_all(encode::u32(DNSADDR, &mut buf))?;
let bytes = s.as_bytes();
w.write_all(encode::usize(bytes.len(), &mut encode::usize_buffer()))?;
w.write_all(&bytes)?
}
Protocol::Unix(s) => {
w.write_all(encode::u32(UNIX, &mut buf))?;
let bytes = s.as_bytes();
Expand Down Expand Up @@ -406,6 +423,7 @@ impl<'a> Protocol<'a> {
Dccp(a) => Dccp(a),
Dns4(cow) => Dns4(Cow::Owned(cow.into_owned())),
Dns6(cow) => Dns6(Cow::Owned(cow.into_owned())),
Dnsaddr(cow) => Dnsaddr(Cow::Owned(cow.into_owned())),
Http => Http,
Https => Https,
Ip4(a) => Ip4(a),
Expand Down Expand Up @@ -438,6 +456,7 @@ impl<'a> fmt::Display for Protocol<'a> {
Dccp(port) => write!(f, "/dccp/{}", port),
Dns4(s) => write!(f, "/dns4/{}", s),
Dns6(s) => write!(f, "/dns6/{}", s),
Dnsaddr(s) => write!(f, "/dnsaddr/{}", s),
Http => f.write_str("/http"),
Https => f.write_str("/https"),
Ip4(addr) => write!(f, "/ip4/{}", addr),
Expand Down
10 changes: 10 additions & 0 deletions misc/multiaddr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ fn construct_success() {
"BD03ADADEC040BE047F9658668B11A504F3155001F231A37F54C4476C07FB4CC139ED7E30304D2",
vec![Onion3(([173, 173, 236, 4, 11, 224, 71, 249, 101, 134, 104, 177, 26, 80, 79, 49, 85, 0, 31, 35, 26, 55, 245, 76, 68, 118, 192, 127, 180, 204, 19, 158, 215, 227, 3], 1234).into())],
);
ma_valid(
"/dnsaddr/sjc-1.bootstrap.libp2p.io",
"3819736A632D312E626F6F7473747261702E6C69627032702E696F",
vec![Dnsaddr(Cow::Borrowed("sjc-1.bootstrap.libp2p.io"))]
);
ma_valid(
"/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/1234/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"3819736A632D312E626F6F7473747261702E6C69627032702E696F0604D2A50322122006B3608AA000274049EB28AD8E793A26FF6FAB281A7D3BD77CD18EB745DFAABB",
vec![Dnsaddr(Cow::Borrowed("sjc-1.bootstrap.libp2p.io")), Tcp(1234), P2p(multihash("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN"))]
);
}

#[test]
Expand Down

0 comments on commit 69852a5

Please sign in to comment.