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

Add support for tls protocol #48

Merged
merged 10 commits into from Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/protocol.rs
Expand Up @@ -14,6 +14,8 @@ use std::{
use unsigned_varint::{encode, decode};
use crate::onion_addr::Onion3Addr;

// All these values are obtained by converting hexadecimal protocol codes to u32.
// The protocol codes are present in multiformats/multicodec repository.
Copy link
Member

Choose a reason for hiding this comment

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

👍 for documenting this.

I am not exactly sure what the source of truth is, though I think we should link to multiformats/multiaddr instead.

Suggested change
// All these values are obtained by converting hexadecimal protocol codes to u32.
// The protocol codes are present in multiformats/multicodec repository.
// Protocols as well as their corresponding codes are defined in
// https://github.com/multiformats/multiaddr/blob/master/protocols.csv .

const DCCP: u32 = 33;
const DNS: u32 = 53;
const DNS4: u32 = 54;
Expand All @@ -34,6 +36,7 @@ const P2P_CIRCUIT: u32 = 290;
const QUIC: u32 = 460;
const SCTP: u32 = 132;
const TCP: u32 = 6;
const TLS: u32 = 448;
const UDP: u32 = 273;
const UDT: u32 = 301;
const UNIX: u32 = 400;
Expand Down Expand Up @@ -85,6 +88,7 @@ pub enum Protocol<'a> {
Quic,
Sctp(u16),
Tcp(u16),
Tls,
Udp(u16),
Udt,
Unix(Cow<'a, str>),
Expand Down Expand Up @@ -113,6 +117,7 @@ impl<'a> Protocol<'a> {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Tcp(s.parse()?))
}
"tls" => Ok(Protocol::Tls),
"udp" => {
let s = iter.next().ok_or(Error::InvalidProtocolString)?;
Ok(Protocol::Udp(s.parse()?))
Expand Down Expand Up @@ -294,6 +299,7 @@ impl<'a> Protocol<'a> {
let num = rdr.read_u16::<BigEndian>()?;
Ok((Protocol::Tcp(num), rest))
}
TLS => Ok((Protocol::Tls, input)),
UDP => {
let (data, rest) = split_at(2, input)?;
let mut rdr = Cursor::new(data);
Expand Down Expand Up @@ -342,6 +348,7 @@ impl<'a> Protocol<'a> {
w.write_all(encode::u32(TCP, &mut buf))?;
w.write_u16::<BigEndian>(*port)?
}
Protocol::Tls => w.write_all(encode::u32(TLS, &mut buf))?,
Protocol::Udp(port) => {
w.write_all(encode::u32(UDP, &mut buf))?;
w.write_u16::<BigEndian>(*port)?
Expand Down Expand Up @@ -455,6 +462,7 @@ impl<'a> Protocol<'a> {
Quic => Quic,
Sctp(a) => Sctp(a),
Tcp(a) => Tcp(a),
Tls => Tls,
Udp(a) => Udp(a),
Udt => Udt,
Unix(cow) => Unix(Cow::Owned(cow.into_owned())),
Expand Down Expand Up @@ -495,6 +503,7 @@ impl<'a> fmt::Display for Protocol<'a> {
Quic => f.write_str("/quic"),
Sctp(port) => write!(f, "/sctp/{}", port),
Tcp(port) => write!(f, "/tcp/{}", port),
Tls => write!(f, "/tls"),
Udp(port) => write!(f, "/udp/{}", port),
Udt => f.write_str("/udt"),
Unix(s) => write!(f, "/unix/{}", s),
Expand Down
9 changes: 5 additions & 4 deletions tests/lib.rs
Expand Up @@ -200,6 +200,7 @@ fn construct_success() {
ma_valid("/udp/1234/udt", "910204D2AD02", vec![Udp(1234), Udt]);
ma_valid("/udp/1234/utp", "910204D2AE02", vec![Udp(1234), Utp]);
ma_valid("/tcp/1234/http", "0604D2E003", vec![Tcp(1234), Http]);
ma_valid("/tcp/1234/tls/http", "0604D2C003E003", vec![Tcp(1234), Tls, Http]);
ma_valid("/tcp/1234/https", "0604D2BB03", vec![Tcp(1234), Https]);
ma_valid("/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B0604D2",
Expand Down Expand Up @@ -320,20 +321,20 @@ fn from_bytes_fail() {

#[test]
fn ser_and_deser_json() {
let addr : Multiaddr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
let addr : Multiaddr = "/ip4/0.0.0.0/tcp/0/tls".parse::<Multiaddr>().unwrap();
let serialized = serde_json::to_string(&addr).unwrap();
assert_eq!(serialized, "\"/ip4/0.0.0.0/tcp/0\"");
assert_eq!(serialized, "\"/ip4/0.0.0.0/tcp/0/tls\"");
let deserialized: Multiaddr = serde_json::from_str(&serialized).unwrap();
assert_eq!(addr, deserialized);
}


#[test]
fn ser_and_deser_bincode() {
let addr : Multiaddr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>().unwrap();
let addr : Multiaddr = "/ip4/0.0.0.0/tcp/0/tls".parse::<Multiaddr>().unwrap();
let serialized = bincode::serialize(&addr).unwrap();
// compact addressing
assert_eq!(serialized, vec![8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 6, 0, 0]);
assert_eq!(serialized, vec![10, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 6, 0, 0, 192, 3]);
let deserialized: Multiaddr = bincode::deserialize(&serialized).unwrap();
assert_eq!(addr, deserialized);
}
Expand Down