@@ -40,7 +40,7 @@ use io_extras::read_to_end;
40
40
41
41
use util:: events:: MessageSendEventsProvider ;
42
42
use util:: logger;
43
- use util:: ser:: { Readable , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt } ;
43
+ use util:: ser:: { Readable , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt , Hostname } ;
44
44
45
45
use ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
46
46
@@ -442,6 +442,13 @@ pub enum NetAddress {
442
442
/// The port on which the node is listening
443
443
port : u16 ,
444
444
} ,
445
+ /// A hostname/port on which the peer is listening.
446
+ Hostname {
447
+ /// The hostname on which the node is listening.
448
+ hostname : Hostname ,
449
+ /// The port on which the node is listening.
450
+ port : u16 ,
451
+ } ,
445
452
}
446
453
impl NetAddress {
447
454
/// Gets the ID of this address type. Addresses in node_announcement messages should be sorted
@@ -452,6 +459,7 @@ impl NetAddress {
452
459
& NetAddress :: IPv6 { ..} => { 2 } ,
453
460
& NetAddress :: OnionV2 ( _) => { 3 } ,
454
461
& NetAddress :: OnionV3 { ..} => { 4 } ,
462
+ & NetAddress :: Hostname { ..} => { 5 } ,
455
463
}
456
464
}
457
465
@@ -462,11 +470,15 @@ impl NetAddress {
462
470
& NetAddress :: IPv6 { .. } => { 18 } ,
463
471
& NetAddress :: OnionV2 ( _) => { 12 } ,
464
472
& NetAddress :: OnionV3 { .. } => { 37 } ,
473
+ // Consists of 1-byte hostname length, hostname bytes, and 2-byte port.
474
+ & NetAddress :: Hostname { ref hostname, .. } => { u16:: from ( hostname. len ( ) ) + 3 } ,
465
475
}
466
476
}
467
477
468
- /// The maximum length of any address descriptor, not including the 1-byte type
469
- pub ( crate ) const MAX_LEN : u16 = 37 ;
478
+ /// The maximum length of any address descriptor, not including the 1-byte type.
479
+ /// This maximum length is reached by a hostname address descriptor:
480
+ /// a hostname with a maximum length of 255, its 1-byte length and a 2-byte port.
481
+ pub ( crate ) const MAX_LEN : u16 = 258 ;
470
482
}
471
483
472
484
impl Writeable for NetAddress {
@@ -492,7 +504,12 @@ impl Writeable for NetAddress {
492
504
checksum. write ( writer) ?;
493
505
version. write ( writer) ?;
494
506
port. write ( writer) ?;
495
- }
507
+ } ,
508
+ & NetAddress :: Hostname { ref hostname, ref port } => {
509
+ 5u8 . write ( writer) ?;
510
+ hostname. write ( writer) ?;
511
+ port. write ( writer) ?;
512
+ } ,
496
513
}
497
514
Ok ( ( ) )
498
515
}
@@ -523,6 +540,12 @@ impl Readable for Result<NetAddress, u8> {
523
540
port : Readable :: read ( reader) ?,
524
541
} ) )
525
542
} ,
543
+ 5 => {
544
+ Ok ( Ok ( NetAddress :: Hostname {
545
+ hostname : Readable :: read ( reader) ?,
546
+ port : Readable :: read ( reader) ?,
547
+ } ) )
548
+ } ,
526
549
_ => return Ok ( Err ( byte) ) ,
527
550
}
528
551
}
@@ -1829,7 +1852,7 @@ mod tests {
1829
1852
use ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures , NodeFeatures } ;
1830
1853
use ln:: msgs;
1831
1854
use ln:: msgs:: { FinalOnionHopData , OptionalField , OnionErrorPacket , OnionHopDataFormat } ;
1832
- use util:: ser:: { Writeable , Readable } ;
1855
+ use util:: ser:: { Writeable , Readable , Hostname } ;
1833
1856
1834
1857
use bitcoin:: hashes:: hex:: FromHex ;
1835
1858
use bitcoin:: util:: address:: Address ;
@@ -1843,6 +1866,7 @@ mod tests {
1843
1866
1844
1867
use io:: Cursor ;
1845
1868
use prelude:: * ;
1869
+ use core:: convert:: TryFrom ;
1846
1870
1847
1871
#[ test]
1848
1872
fn encoding_channel_reestablish_no_secret ( ) {
@@ -1971,7 +1995,7 @@ mod tests {
1971
1995
do_encoding_channel_announcement ( true , true ) ;
1972
1996
}
1973
1997
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 ) {
1998
+ 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 ) {
1975
1999
let secp_ctx = Secp256k1 :: new ( ) ;
1976
2000
let ( privkey_1, pubkey_1) = get_keys_from ! ( "0101010101010101010101010101010101010101010101010101010101010101" , secp_ctx) ;
1977
2001
let sig_1 = get_sig_on ! ( privkey_1, secp_ctx, String :: from( "01010101010101010101010101010101" ) ) ;
@@ -2007,6 +2031,12 @@ mod tests {
2007
2031
port : 9735
2008
2032
} ) ;
2009
2033
}
2034
+ if hostname {
2035
+ addresses. push ( msgs:: NetAddress :: Hostname {
2036
+ hostname : Hostname :: try_from ( String :: from ( "host" ) ) . unwrap ( ) ,
2037
+ port : 9735 ,
2038
+ } ) ;
2039
+ }
2010
2040
let mut addr_len = 0 ;
2011
2041
for addr in & addresses {
2012
2042
addr_len += addr. len ( ) + 1 ;
@@ -2047,6 +2077,9 @@ mod tests {
2047
2077
if onionv3 {
2048
2078
target_value. append ( & mut hex:: decode ( "04fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e00020102607" ) . unwrap ( ) ) ;
2049
2079
}
2080
+ if hostname {
2081
+ target_value. append ( & mut hex:: decode ( "0504686f73742607" ) . unwrap ( ) ) ;
2082
+ }
2050
2083
if excess_address_data {
2051
2084
target_value. append ( & mut hex:: decode ( "216c280b5395a2546e7e4b2663e04f811622f15a4f92e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d269" ) . unwrap ( ) ) ;
2052
2085
}
@@ -2058,15 +2091,16 @@ mod tests {
2058
2091
2059
2092
#[ test]
2060
2093
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 ) ;
2094
+ do_encoding_node_announcement ( true , true , true , true , true , true , true , true ) ;
2095
+ do_encoding_node_announcement ( false , false , false , false , false , false , false , false ) ;
2096
+ do_encoding_node_announcement ( false , true , false , false , false , false , false , false ) ;
2097
+ do_encoding_node_announcement ( false , false , true , false , false , false , false , false ) ;
2098
+ do_encoding_node_announcement ( false , false , false , true , false , false , false , false ) ;
2099
+ do_encoding_node_announcement ( false , false , false , false , true , false , false , false ) ;
2100
+ do_encoding_node_announcement ( false , false , false , false , false , true , false , false ) ;
2101
+ do_encoding_node_announcement ( false , false , false , false , false , false , true , false ) ;
2102
+ do_encoding_node_announcement ( false , true , false , true , false , false , true , false ) ;
2103
+ do_encoding_node_announcement ( false , false , true , false , true , false , false , false ) ;
2070
2104
}
2071
2105
2072
2106
fn do_encoding_channel_update ( direction : bool , disable : bool , htlc_maximum_msat : bool , excess_data : bool ) {
0 commit comments