@@ -489,7 +489,7 @@ where
489489/// using a stream isolation parameter sourced from [`EntropySource::get_secure_random_bytes`].
490490///
491491/// The `addr` parameter will be set to the [`PeerDetails::socket_address`] for that peer in
492- /// [`PeerManager::list_peers`], and if it is not a private IPV4 or IPV6 address, it will also
492+ /// [`PeerManager::list_peers`], and if it is not a private IPv4 or IPv6 address, it will also
493493/// reported to the peer in our init message.
494494///
495495/// Returns a future (as the fn is async) that yields another future, see [`connect_outbound`] for
@@ -791,7 +791,7 @@ mod tests {
791791 use lightning:: ln:: types:: ChannelId ;
792792 use lightning:: routing:: gossip:: NodeId ;
793793 use lightning:: types:: features:: * ;
794- use lightning:: util:: test_utils:: TestNodeSigner ;
794+ use lightning:: util:: test_utils:: { TestLogger , TestNodeSigner } ;
795795
796796 use tokio:: sync:: mpsc;
797797
@@ -800,13 +800,6 @@ mod tests {
800800 use std:: sync:: { Arc , Mutex } ;
801801 use std:: time:: Duration ;
802802
803- pub struct TestLogger ( ) ;
804- impl lightning:: util:: logger:: Logger for TestLogger {
805- fn log ( & self , record : lightning:: util:: logger:: Record ) {
806- println ! ( "{}" , record) ;
807- }
808- }
809-
810803 struct MsgHandler {
811804 expected_pubkey : PublicKey ,
812805 pubkey_connected : mpsc:: Sender < ( ) > ,
@@ -1000,7 +993,7 @@ mod tests {
1000993 a_msg_handler,
1001994 0 ,
1002995 & [ 1 ; 32 ] ,
1003- Arc :: new ( TestLogger ( ) ) ,
996+ Arc :: new ( TestLogger :: new ( ) ) ,
1004997 Arc :: new ( TestNodeSigner :: new ( a_key) ) ,
1005998 ) ) ;
1006999
@@ -1024,7 +1017,7 @@ mod tests {
10241017 b_msg_handler,
10251018 0 ,
10261019 & [ 2 ; 32 ] ,
1027- Arc :: new ( TestLogger ( ) ) ,
1020+ Arc :: new ( TestLogger :: new ( ) ) ,
10281021 Arc :: new ( TestNodeSigner :: new ( b_key) ) ,
10291022 ) ) ;
10301023
@@ -1087,7 +1080,7 @@ mod tests {
10871080 a_msg_handler,
10881081 0 ,
10891082 & [ 1 ; 32 ] ,
1090- Arc :: new ( TestLogger ( ) ) ,
1083+ Arc :: new ( TestLogger :: new ( ) ) ,
10911084 Arc :: new ( TestNodeSigner :: new ( a_key) ) ,
10921085 ) ) ;
10931086
@@ -1173,4 +1166,136 @@ mod tests {
11731166 assert ! ( tor_connect( addr, tor_proxy_addr, & entropy_source) . await . is_err( ) ) ;
11741167 }
11751168 }
1169+
1170+ async fn test_remote_address_with_override ( b_addr_override : Option < SocketAddress > ) {
1171+ let secp_ctx = Secp256k1 :: new ( ) ;
1172+ let a_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
1173+ let b_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
1174+ let a_pub = PublicKey :: from_secret_key ( & secp_ctx, & a_key) ;
1175+ let b_pub = PublicKey :: from_secret_key ( & secp_ctx, & b_key) ;
1176+
1177+ let ( a_connected_sender, mut a_connected) = mpsc:: channel ( 1 ) ;
1178+ let ( a_disconnected_sender, _a_disconnected) = mpsc:: channel ( 1 ) ;
1179+ let a_handler = Arc :: new ( MsgHandler {
1180+ expected_pubkey : b_pub,
1181+ pubkey_connected : a_connected_sender,
1182+ pubkey_disconnected : a_disconnected_sender,
1183+ disconnected_flag : AtomicBool :: new ( false ) ,
1184+ msg_events : Mutex :: new ( Vec :: new ( ) ) ,
1185+ } ) ;
1186+ let a_msg_handler = MessageHandler {
1187+ chan_handler : Arc :: clone ( & a_handler) ,
1188+ route_handler : Arc :: clone ( & a_handler) ,
1189+ onion_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1190+ custom_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1191+ send_only_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1192+ } ;
1193+ let a_logger = Arc :: new ( TestLogger :: new ( ) ) ;
1194+ let a_manager = Arc :: new ( PeerManager :: new (
1195+ a_msg_handler,
1196+ 0 ,
1197+ & [ 1 ; 32 ] ,
1198+ Arc :: clone ( & a_logger) ,
1199+ Arc :: new ( TestNodeSigner :: new ( a_key) ) ,
1200+ ) ) ;
1201+
1202+ let ( b_connected_sender, mut b_connected) = mpsc:: channel ( 1 ) ;
1203+ let ( b_disconnected_sender, _b_disconnected) = mpsc:: channel ( 1 ) ;
1204+ let b_handler = Arc :: new ( MsgHandler {
1205+ expected_pubkey : a_pub,
1206+ pubkey_connected : b_connected_sender,
1207+ pubkey_disconnected : b_disconnected_sender,
1208+ disconnected_flag : AtomicBool :: new ( false ) ,
1209+ msg_events : Mutex :: new ( Vec :: new ( ) ) ,
1210+ } ) ;
1211+ let b_msg_handler = MessageHandler {
1212+ chan_handler : Arc :: clone ( & b_handler) ,
1213+ route_handler : Arc :: clone ( & b_handler) ,
1214+ onion_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1215+ custom_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1216+ send_only_message_handler : Arc :: new ( IgnoringMessageHandler { } ) ,
1217+ } ;
1218+ let b_logger = Arc :: new ( TestLogger :: new ( ) ) ;
1219+ let b_manager = Arc :: new ( PeerManager :: new (
1220+ b_msg_handler,
1221+ 0 ,
1222+ & [ 2 ; 32 ] ,
1223+ Arc :: clone ( & b_logger) ,
1224+ Arc :: new ( TestNodeSigner :: new ( b_key) ) ,
1225+ ) ) ;
1226+
1227+ // We bind on localhost, hoping the environment is properly configured with a local
1228+ // address. This may not always be the case in containers and the like, so if this test is
1229+ // failing for you check that you have a loopback interface and it is configured with
1230+ // 127.0.0.1.
1231+ let ( conn_a, conn_b) = make_tcp_connection ( ) ;
1232+
1233+ // Given that `make_tcp_connection` binds the peer to 127.0.0.1,
1234+ // `get_addr_from_stream` always returns a private address, and will not be reported to the peer
1235+ // in the init message.
1236+ let b_addr = b_addr_override
1237+ . clone ( )
1238+ . unwrap_or_else ( || super :: get_addr_from_stream ( & conn_a) . unwrap ( ) ) ;
1239+ let _fut_a = super :: setup_outbound_internal (
1240+ Arc :: clone ( & a_manager) ,
1241+ b_pub,
1242+ conn_a,
1243+ Some ( b_addr. clone ( ) ) ,
1244+ ) ;
1245+ let _fut_b = super :: setup_inbound ( Arc :: clone ( & b_manager) , conn_b) ;
1246+
1247+ tokio:: time:: timeout ( Duration :: from_secs ( 10 ) , a_connected. recv ( ) ) . await . unwrap ( ) ;
1248+ tokio:: time:: timeout ( Duration :: from_secs ( 1 ) , b_connected. recv ( ) ) . await . unwrap ( ) ;
1249+
1250+ // Check `PeerDetails::socket_address`
1251+
1252+ let mut peers = a_manager. list_peers ( ) ;
1253+ assert_eq ! ( peers. len( ) , 1 ) ;
1254+ let peer = peers. pop ( ) . unwrap ( ) ;
1255+ assert_eq ! ( peer. socket_address, Some ( b_addr) ) ;
1256+
1257+ // Check the init message sent to the peer
1258+
1259+ let mainnet_hash = ChainHash :: using_genesis_block ( Network :: Testnet ) ;
1260+ let a_init_msg = Init {
1261+ features : InitFeatures :: empty ( ) ,
1262+ networks : Some ( vec ! [ mainnet_hash] ) ,
1263+ // We set it to the override here because addresses from the stream are private addresses,
1264+ // so they are filtered out and not reported to the peer
1265+ remote_network_address : b_addr_override,
1266+ } ;
1267+ a_logger. assert_log (
1268+ "lightning::ln::peer_handler" ,
1269+ format ! ( "Enqueueing message Init({:?})" , a_init_msg) ,
1270+ 1 ,
1271+ ) ;
1272+ }
1273+
1274+ #[ tokio:: test]
1275+ async fn test_remote_address ( ) {
1276+ // Test that the remote address of the peer passed to `setup_outbound_internal` is set correctly in the
1277+ // corresponding `PeerDetails::socket_address` returned from `PeerManager::list_peers`, and if it is
1278+ // not a private address, that it is reported to the peer in the init message.
1279+
1280+ // This tests a private address read from `get_addr_from_stream`
1281+ test_remote_address_with_override ( None ) . await ;
1282+ // Make sure these are not private IPv4 or IPv6 addresses; we assert they are present in the init message
1283+ test_remote_address_with_override ( Some ( SocketAddress :: TcpIpV4 {
1284+ addr : [ 0xab ; 4 ] ,
1285+ port : 0xabab ,
1286+ } ) )
1287+ . await ;
1288+ test_remote_address_with_override ( Some ( SocketAddress :: TcpIpV6 {
1289+ addr : [ 0x2a ; 16 ] ,
1290+ port : 0x2a2a ,
1291+ } ) )
1292+ . await ;
1293+ let torproject_onion_addr_str =
1294+ "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion:80" ;
1295+ let torproject_onion_addr: SocketAddress = torproject_onion_addr_str. parse ( ) . unwrap ( ) ;
1296+ test_remote_address_with_override ( Some ( torproject_onion_addr) ) . await ;
1297+ let torproject_addr_str = "torproject.org:80" ;
1298+ let torproject_addr: SocketAddress = torproject_addr_str. parse ( ) . unwrap ( ) ;
1299+ test_remote_address_with_override ( Some ( torproject_addr) ) . await ;
1300+ }
11761301}
0 commit comments