Skip to content

Commit e31eff2

Browse files
committed
Adds DNS hostname to NetAddress
1 parent abf6564 commit e31eff2

File tree

3 files changed

+159
-19
lines changed

3 files changed

+159
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,15 +2859,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28592859

28602860
#[allow(dead_code)]
28612861
// Messages of up to 64KB should never end up more than half full with addresses, as that would
2862-
// be absurd. We ensure this by checking that at least 500 (our stated public contract on when
2862+
// be absurd. We ensure this by checking that at least 100 (our stated public contract on when
28632863
// broadcast_node_announcement panics) of the maximum-length addresses would fit in a 64KB
28642864
// message...
28652865
const HALF_MESSAGE_IS_ADDRS: u32 = ::core::u16::MAX as u32 / (NetAddress::MAX_LEN as u32 + 1) / 2;
28662866
#[deny(const_err)]
28672867
#[allow(dead_code)]
28682868
// ...by failing to compile if the number of addresses that would be half of a message is
2869-
// smaller than 500:
2870-
const STATIC_ASSERT: u32 = Self::HALF_MESSAGE_IS_ADDRS - 500;
2869+
// smaller than 100:
2870+
const STATIC_ASSERT: u32 = Self::HALF_MESSAGE_IS_ADDRS - 100;
28712871

28722872
/// Regenerates channel_announcements and generates a signed node_announcement from the given
28732873
/// arguments, providing them in corresponding events via
@@ -2884,13 +2884,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
28842884
/// tying these addresses together and to this node. If you wish to preserve user privacy,
28852885
/// addresses should likely contain only Tor Onion addresses.
28862886
///
2887-
/// Panics if `addresses` is absurdly large (more than 500).
2887+
/// Panics if `addresses` is absurdly large (more than 100).
28882888
///
28892889
/// [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
28902890
pub fn broadcast_node_announcement(&self, rgb: [u8; 3], alias: [u8; 32], mut addresses: Vec<NetAddress>) {
28912891
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
28922892

2893-
if addresses.len() > 500 {
2893+
if addresses.len() > 100 {
28942894
panic!("More than half the message size was taken up by public addresses!");
28952895
}
28962896

lightning/src/ln/msgs.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use io_extras::read_to_end;
4040

4141
use util::events::MessageSendEventsProvider;
4242
use util::logger;
43-
use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt};
43+
use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt, Hostname};
4444

4545
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
4646

@@ -442,6 +442,13 @@ pub enum NetAddress {
442442
/// The port on which the node is listening
443443
port: u16,
444444
},
445+
/// A hostname/port on which the peer is listening.
446+
Hostname {
447+
/// The hostname on which the port is listening.
448+
hostname: Hostname,
449+
/// The port on which the node is listening.
450+
port: u16,
451+
},
445452
}
446453
impl NetAddress {
447454
/// Gets the ID of this address type. Addresses in node_announcement messages should be sorted
@@ -452,6 +459,7 @@ impl NetAddress {
452459
&NetAddress::IPv6 {..} => { 2 },
453460
&NetAddress::OnionV2(_) => { 3 },
454461
&NetAddress::OnionV3 {..} => { 4 },
462+
&NetAddress::Hostname {..} => { 5 },
455463
}
456464
}
457465

@@ -462,11 +470,12 @@ impl NetAddress {
462470
&NetAddress::IPv6 { .. } => { 18 },
463471
&NetAddress::OnionV2(_) => { 12 },
464472
&NetAddress::OnionV3 { .. } => { 37 },
473+
&NetAddress::Hostname { ref hostname, .. } => { u16::from(hostname.len()) + 3 },
465474
}
466475
}
467476

468477
/// The maximum length of any address descriptor, not including the 1-byte type
469-
pub(crate) const MAX_LEN: u16 = 37;
478+
pub(crate) const MAX_LEN: u16 = 258;
470479
}
471480

472481
impl Writeable for NetAddress {
@@ -492,7 +501,12 @@ impl Writeable for NetAddress {
492501
checksum.write(writer)?;
493502
version.write(writer)?;
494503
port.write(writer)?;
495-
}
504+
},
505+
&NetAddress::Hostname { ref hostname, ref port } => {
506+
5u8.write(writer)?;
507+
hostname.write(writer)?;
508+
port.write(writer)?;
509+
},
496510
}
497511
Ok(())
498512
}
@@ -523,6 +537,12 @@ impl Readable for Result<NetAddress, u8> {
523537
port: Readable::read(reader)?,
524538
}))
525539
},
540+
5 => {
541+
Ok(Ok(NetAddress::Hostname {
542+
hostname: Readable::read(reader)?,
543+
port: Readable::read(reader)?,
544+
}))
545+
},
526546
_ => return Ok(Err(byte)),
527547
}
528548
}
@@ -1829,7 +1849,7 @@ mod tests {
18291849
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
18301850
use ln::msgs;
18311851
use ln::msgs::{FinalOnionHopData, OptionalField, OnionErrorPacket, OnionHopDataFormat};
1832-
use util::ser::{Writeable, Readable};
1852+
use util::ser::{Writeable, Readable, Hostname};
18331853

18341854
use bitcoin::hashes::hex::FromHex;
18351855
use bitcoin::util::address::Address;
@@ -1843,6 +1863,7 @@ mod tests {
18431863

18441864
use io::Cursor;
18451865
use prelude::*;
1866+
use core::convert::TryFrom;
18461867

18471868
#[test]
18481869
fn encoding_channel_reestablish_no_secret() {
@@ -1971,7 +1992,7 @@ mod tests {
19711992
do_encoding_channel_announcement(true, true);
19721993
}
19731994

1974-
fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, excess_address_data: bool, excess_data: bool) {
1995+
fn do_encoding_node_announcement(unknown_features_bits: bool, ipv4: bool, ipv6: bool, onionv2: bool, onionv3: bool, hostname: bool, excess_address_data: bool, excess_data: bool) {
19751996
let secp_ctx = Secp256k1::new();
19761997
let (privkey_1, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
19771998
let sig_1 = get_sig_on!(privkey_1, secp_ctx, String::from("01010101010101010101010101010101"));
@@ -2007,6 +2028,12 @@ mod tests {
20072028
port: 9735
20082029
});
20092030
}
2031+
if hostname {
2032+
addresses.push(msgs::NetAddress::Hostname {
2033+
hostname: Hostname::try_from(String::from("host")).unwrap(),
2034+
port: 9735,
2035+
});
2036+
}
20102037
let mut addr_len = 0;
20112038
for addr in &addresses {
20122039
addr_len += addr.len() + 1;
@@ -2047,6 +2074,9 @@ mod tests {
20472074
if onionv3 {
20482075
target_value.append(&mut hex::decode("04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607").unwrap());
20492076
}
2077+
if hostname {
2078+
target_value.append(&mut hex::decode("0504686f73742607").unwrap());
2079+
}
20502080
if excess_address_data {
20512081
target_value.append(&mut hex::decode("216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269").unwrap());
20522082
}
@@ -2058,15 +2088,16 @@ mod tests {
20582088

20592089
#[test]
20602090
fn encoding_node_announcement() {
2061-
do_encoding_node_announcement(true, true, true, true, true, true, true);
2062-
do_encoding_node_announcement(false, false, false, false, false, false, false);
2063-
do_encoding_node_announcement(false, true, false, false, false, false, false);
2064-
do_encoding_node_announcement(false, false, true, false, false, false, false);
2065-
do_encoding_node_announcement(false, false, false, true, false, false, false);
2066-
do_encoding_node_announcement(false, false, false, false, true, false, false);
2067-
do_encoding_node_announcement(false, false, false, false, false, true, false);
2068-
do_encoding_node_announcement(false, true, false, true, false, true, false);
2069-
do_encoding_node_announcement(false, false, true, false, true, false, false);
2091+
do_encoding_node_announcement(true, true, true, true, true, true, true, true);
2092+
do_encoding_node_announcement(false, false, false, false, false, false, false, false);
2093+
do_encoding_node_announcement(false, true, false, false, false, false, false, false);
2094+
do_encoding_node_announcement(false, false, true, false, false, false, false, false);
2095+
do_encoding_node_announcement(false, false, false, true, false, false, false, false);
2096+
do_encoding_node_announcement(false, false, false, false, true, false, false, false);
2097+
do_encoding_node_announcement(false, false, false, false, false, true, false, false);
2098+
do_encoding_node_announcement(false, false, false, false, false, false, true, false);
2099+
do_encoding_node_announcement(false, true, false, true, false, false, true, false);
2100+
do_encoding_node_announcement(false, false, true, false, true, false, false, false);
20702101
}
20712102

20722103
fn do_encoding_channel_update(direction: bool, disable: bool, htlc_maximum_msat: bool, excess_data: bool) {

lightning/src/util/ser.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use io_extras::{copy, sink};
1616
use core::hash::Hash;
1717
use sync::Mutex;
1818
use core::cmp;
19+
use core::convert::TryFrom;
20+
use core::ops::Deref;
1921

2022
use bitcoin::secp256k1::{PublicKey, SecretKey};
2123
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE};
@@ -913,6 +915,82 @@ impl Readable for String {
913915
}
914916
}
915917

918+
/// Represents a hostname with validation for serialization purposes.
919+
/// The hostname follows the preferred form summarized in e.g. RFC 3696.
920+
/// Its length is guaranteed to be representable by a single byte.
921+
/// This serialization is used by Bolt 7 hostnames.
922+
#[derive(Clone, Debug, PartialEq)]
923+
pub struct Hostname(String);
924+
impl Hostname {
925+
/// Returns the length of the hostname.
926+
pub fn len(&self) -> u8 {
927+
(&self.0).len() as u8
928+
}
929+
}
930+
impl Deref for Hostname {
931+
type Target = String;
932+
933+
fn deref(&self) -> &Self::Target {
934+
&self.0
935+
}
936+
}
937+
impl From<Hostname> for String {
938+
fn from(short_s: Hostname) -> Self {
939+
short_s.0
940+
}
941+
}
942+
impl TryFrom<String> for Hostname {
943+
type Error = ();
944+
945+
fn try_from(s: String) -> Result<Self, Self::Error> {
946+
if s.len() > 255 {
947+
return Err(());
948+
}
949+
let mut labels = s.split('.').enumerate().peekable();
950+
while let Some((label_idx, label)) = labels.next() {
951+
let last_label = labels.peek().is_none();
952+
let bytes = label.as_bytes();
953+
// Trailing period for fully-qualified name.
954+
if bytes.len() == 0 {
955+
if !last_label || label_idx == 0 {
956+
return Err(());
957+
}
958+
}
959+
if bytes.len() > 63 {
960+
return Err(());
961+
}
962+
for byte_idx in 0 .. bytes.len() {
963+
let b = bytes[byte_idx];
964+
if !b.is_ascii_alphanumeric() && b != b'-' {
965+
return Err(());
966+
}
967+
if b == b'-' && (byte_idx == 0 || byte_idx == bytes.len() - 1) {
968+
return Err(());
969+
}
970+
}
971+
};
972+
Ok(Hostname(s))
973+
}
974+
}
975+
impl Writeable for Hostname {
976+
#[inline]
977+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
978+
self.len().write(w)?;
979+
w.write_all(self.as_bytes())
980+
}
981+
}
982+
impl Readable for Hostname {
983+
#[inline]
984+
fn read<R: Read>(r: &mut R) -> Result<Hostname, DecodeError> {
985+
let len: u8 = Readable::read(r)?;
986+
let mut vec = Vec::with_capacity(len as usize);
987+
vec.resize(len as usize, 0);
988+
r.read_exact(&mut vec)?;
989+
let s = String::from_utf8(vec).map_err(|_| DecodeError::InvalidValue)?;
990+
Hostname::try_from(s).map_err(|_| DecodeError::InvalidValue)
991+
}
992+
}
993+
916994
impl Writeable for Duration {
917995
#[inline]
918996
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -928,3 +1006,34 @@ impl Readable for Duration {
9281006
Ok(Duration::new(secs, nanos))
9291007
}
9301008
}
1009+
1010+
#[cfg(test)]
1011+
mod tests {
1012+
use core::convert::TryFrom;
1013+
use util::ser::{Readable, Hostname, Writeable};
1014+
1015+
#[test]
1016+
fn hostname_conversion() {
1017+
assert_eq!(Hostname::try_from(String::from("test")).unwrap().as_str(), "test");
1018+
assert_eq!(Hostname::try_from(String::from("test-1")).unwrap().as_str(), "test-1");
1019+
assert_eq!(Hostname::try_from(String::from("1-test")).unwrap().as_str(), "1-test");
1020+
assert_eq!(Hostname::try_from(String::from("test.com")).unwrap().as_str(), "test.com");
1021+
assert_eq!(Hostname::try_from(String::from("test.com.")).unwrap().as_str(), "test.com.");
1022+
assert_eq!(Hostname::try_from(String::from("a.test.com")).unwrap().as_str(), "a.test.com");
1023+
1024+
assert!(Hostname::try_from(String::from("⚡")).is_err());
1025+
assert!(Hostname::try_from(String::from("-end")).is_err());
1026+
assert!(Hostname::try_from(String::from("start-")).is_err());
1027+
let mut large_vec = Vec::with_capacity(256);
1028+
large_vec.resize(256, b'A');
1029+
assert!(Hostname::try_from(String::from_utf8(large_vec).unwrap()).is_err());
1030+
}
1031+
1032+
#[test]
1033+
fn hostname_serialization() {
1034+
let short_s = Hostname::try_from(String::from("test")).unwrap();
1035+
let mut buf: Vec<u8> = Vec::new();
1036+
short_s.write(&mut buf).unwrap();
1037+
assert_eq!(Hostname::read(&mut buf.as_slice()).unwrap().as_str(), "test");
1038+
}
1039+
}

0 commit comments

Comments
 (0)