From 69852a580b01d8f4a5c53599a6c3922afb00e939 Mon Sep 17 00:00:00 2001 From: Akihito Nakano Date: Thu, 6 Feb 2020 21:54:50 +0900 Subject: [PATCH] Add dnsaddr (#1356) Co-authored-by: Pierre Krieger --- misc/multiaddr/src/protocol.rs | 19 +++++++++++++++++++ misc/multiaddr/tests/lib.rs | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/misc/multiaddr/src/protocol.rs b/misc/multiaddr/src/protocol.rs index 0ccfc99f3fd..6491d0d8599 100644 --- a/misc/multiaddr/src/protocol.rs +++ b/misc/multiaddr/src/protocol.rs @@ -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; @@ -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), @@ -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()?)) @@ -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 => { @@ -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(); @@ -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), @@ -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), diff --git a/misc/multiaddr/tests/lib.rs b/misc/multiaddr/tests/lib.rs index 545e0be234d..9770f24c898 100644 --- a/misc/multiaddr/tests/lib.rs +++ b/misc/multiaddr/tests/lib.rs @@ -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]