@@ -46,6 +46,8 @@ use core::fmt::Debug;
46
46
use core:: ops:: Deref ;
47
47
#[ cfg( feature = "std" ) ]
48
48
use core:: str:: FromStr ;
49
+ #[ cfg( feature = "std" ) ]
50
+ use std:: net:: SocketAddr ;
49
51
use crate :: io:: { self , Cursor , Read } ;
50
52
use crate :: io_extras:: read_to_end;
51
53
@@ -958,6 +960,37 @@ impl From<std::net::SocketAddr> for SocketAddress {
958
960
}
959
961
}
960
962
963
+ #[ cfg( feature = "std" ) ]
964
+ impl std:: net:: ToSocketAddrs for SocketAddress {
965
+ type Iter = std:: vec:: IntoIter < std:: net:: SocketAddr > ;
966
+
967
+ fn to_socket_addrs ( & self ) -> std:: io:: Result < Self :: Iter > {
968
+ match self {
969
+ SocketAddress :: TcpIpV4 { addr, port } => {
970
+ let ip_addr = std:: net:: Ipv4Addr :: from ( * addr) ;
971
+ let socket_addr = SocketAddr :: new ( ip_addr. into ( ) , * port) ;
972
+ Ok ( vec ! [ socket_addr] . into_iter ( ) )
973
+ }
974
+ SocketAddress :: TcpIpV6 { addr, port } => {
975
+ let ip_addr = std:: net:: Ipv6Addr :: from ( * addr) ;
976
+ let socket_addr = SocketAddr :: new ( ip_addr. into ( ) , * port) ;
977
+ Ok ( vec ! [ socket_addr] . into_iter ( ) )
978
+ }
979
+ SocketAddress :: Hostname { ref hostname, port } => {
980
+ ( hostname. as_str ( ) , * port) . to_socket_addrs ( )
981
+ }
982
+ SocketAddress :: OnionV2 ( ..) => {
983
+ Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Resolution of OnionV2 \
984
+ addresses is currently unsupported.") )
985
+ }
986
+ SocketAddress :: OnionV3 { .. } => {
987
+ Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Resolution of OnionV3 \
988
+ addresses is currently unsupported.") )
989
+ }
990
+ }
991
+ }
992
+ }
993
+
961
994
/// Parses an OnionV3 host and port into a [`SocketAddress::OnionV3`].
962
995
///
963
996
/// The host part must end with ".onion".
@@ -2676,7 +2709,7 @@ mod tests {
2676
2709
use crate :: chain:: transaction:: OutPoint ;
2677
2710
2678
2711
#[ cfg( feature = "std" ) ]
2679
- use std:: net:: { Ipv4Addr , Ipv6Addr } ;
2712
+ use std:: net:: { Ipv4Addr , Ipv6Addr , SocketAddr , SocketAddrV4 , SocketAddrV6 , ToSocketAddrs } ;
2680
2713
use crate :: ln:: msgs:: SocketAddressParseError ;
2681
2714
2682
2715
#[ test]
@@ -4105,4 +4138,22 @@ mod tests {
4105
4138
assert ! ( "invalid-address" . parse:: <SocketAddress >( ) . is_err( ) ) ;
4106
4139
assert ! ( SocketAddress :: from_str( "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.onion:1234" ) . is_err( ) ) ;
4107
4140
}
4141
+
4142
+ #[ test]
4143
+ #[ cfg( feature = "std" ) ]
4144
+ fn test_socket_address_to_socket_addrs ( ) {
4145
+ assert_eq ! ( SocketAddress :: TcpIpV4 { addr: [ 0u8 ; 4 ] , port: 1337 , } . to_socket_addrs( ) . unwrap( ) . next( ) . unwrap( ) ,
4146
+ SocketAddr :: V4 ( SocketAddrV4 :: new( Ipv4Addr :: new( 0 , 0 , 0 , 0 ) , 1337 ) ) ) ;
4147
+ assert_eq ! ( SocketAddress :: TcpIpV6 { addr: [ 0u8 ; 16 ] , port: 1337 , } . to_socket_addrs( ) . unwrap( ) . next( ) . unwrap( ) ,
4148
+ SocketAddr :: V6 ( SocketAddrV6 :: new( Ipv6Addr :: from( [ 0u8 ; 16 ] ) , 1337 , 0 , 0 ) ) ) ;
4149
+ assert_eq ! ( SocketAddress :: Hostname { hostname: Hostname :: try_from( "0.0.0.0" . to_string( ) ) . unwrap( ) , port: 0 }
4150
+ . to_socket_addrs( ) . unwrap( ) . next( ) . unwrap( ) , SocketAddr :: V4 ( SocketAddrV4 :: new( Ipv4Addr :: from( [ 0u8 ; 4 ] ) , 0 ) ) ) ;
4151
+ assert ! ( SocketAddress :: OnionV2 ( [ 0u8 ; 12 ] ) . to_socket_addrs( ) . is_err( ) ) ;
4152
+ assert ! ( SocketAddress :: OnionV3 { ed25519_pubkey: [ 37 , 24 , 75 , 5 , 25 , 73 , 117 , 194 , 139 , 102 ,
4153
+ 182 , 107 , 4 , 105 , 247 , 246 , 85 , 111 , 177 , 172 , 49 , 137 , 167 , 155 , 64 , 221 , 163 , 47 , 31 ,
4154
+ 33 , 71 , 3 ] ,
4155
+ checksum: 48326 ,
4156
+ version: 121 ,
4157
+ port: 1234 } . to_socket_addrs( ) . is_err( ) ) ;
4158
+ }
4108
4159
}
0 commit comments