@@ -40,13 +40,14 @@ use core::convert::TryFrom;
40
40
use core:: fmt;
41
41
use core:: fmt:: Debug ;
42
42
use core:: str:: FromStr ;
43
- use std:: net:: { AddrParseError , Ipv4Addr , SocketAddrV4 , SocketAddrV6 , Ipv6Addr } ;
43
+ use std:: net:: { AddrParseError , Ipv4Addr , Ipv6Addr , SocketAddr } ;
44
44
use crate :: io:: { self , Read } ;
45
45
use crate :: io_extras:: read_to_end;
46
46
47
47
use crate :: events:: { MessageSendEventsProvider , OnionMessageProvider } ;
48
48
use crate :: util:: logger;
49
49
use crate :: util:: ser:: { LengthReadable , Readable , ReadableArgs , Writeable , Writer , WithoutLength , FixedLengthReader , HighZeroBytesDroppedBigSize , Hostname } ;
50
+ use crate :: util:: base32;
50
51
51
52
use crate :: ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
52
53
@@ -661,42 +662,43 @@ impl Readable for NetAddress {
661
662
}
662
663
}
663
664
664
- fn rem_first_and_last ( value : & str ) -> & str {
665
- let mut chars = value. chars ( ) ;
666
- chars. next ( ) ;
667
- chars. next_back ( ) ;
668
- chars. as_str ( )
669
- }
670
-
671
665
impl FromStr for NetAddress {
672
666
type Err = AddrParseError ;
673
667
674
668
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
675
- let trim_s_by_latest_colon = s. rfind ( ":" ) . unwrap ( ) ;
676
- let host = & s[ ..trim_s_by_latest_colon] ;
677
- let port: u16 = s[ trim_s_by_latest_colon + 1 ..] . parse ( ) . expect ( "Invalid port" ) ;
678
- if let Ok ( addr) = host. parse :: < Ipv4Addr > ( ) {
679
- let addr = SocketAddrV4 :: new ( addr, port) ;
680
- return Ok ( NetAddress :: IPv4 { addr : addr. ip ( ) . octets ( ) , port } ) ;
681
- }
682
- if let Ok ( addr) = rem_first_and_last ( host) . parse :: < Ipv6Addr > ( ) {
683
- let addr = SocketAddrV6 :: new ( addr, port, 0 , 0 ) ;
684
- return Ok ( NetAddress :: IPv6 { addr : addr. ip ( ) . octets ( ) , port } ) ;
685
- } ;
686
- if host. ends_with ( ".onion" ) {
687
- let onion = host. split ( ".onion" ) . collect :: < Vec < & str > > ( ) [ 0 ] ;
688
- let onion = base32:: decode ( base32:: Alphabet :: RFC4648 { padding : false } , & onion) . expect ( "error in onion" ) ;
689
- let version = onion[ 0 ] ;
690
- let checksum = u16:: from_be_bytes ( [ onion[ 1 ] , onion[ 2 ] ] ) ;
691
- let ed25519_pubkey = onion[ 3 ..35 ] . try_into ( ) . expect ( "Invalid onion address" ) ;
692
- return Ok ( NetAddress :: OnionV3 { ed25519_pubkey, checksum, version, port } ) ;
669
+ match SocketAddr :: from_str ( s) {
670
+ Ok ( addr) => {
671
+ let port: u16 = addr. port ( ) ;
672
+ match addr {
673
+ SocketAddr :: V4 ( addr) => {
674
+ let addr = addr. ip ( ) . to_string ( ) . parse :: < Ipv4Addr > ( ) ?;
675
+ return Ok ( NetAddress :: IPv4 { addr : addr. octets ( ) , port } ) ;
676
+ } ,
677
+ SocketAddr :: V6 ( addr) => {
678
+ let addr = addr. ip ( ) . to_string ( ) . parse :: < Ipv6Addr > ( ) ?;
679
+ return Ok ( NetAddress :: IPv6 { addr : addr. octets ( ) , port } ) ;
680
+ } ,
681
+ }
682
+ } ,
683
+ Err ( e) => {
684
+ let trimmed_input = s. rfind ( ":" ) . expect ( "Invalid input, needs to include seperator \" :\" " ) ;
685
+ let host = & s[ ..trimmed_input] ;
686
+ let port: u16 = s[ trimmed_input + 1 ..] . parse ( ) . expect ( "Invalid input, port needs to be u16" ) ;
687
+ if host. ends_with ( ".onion" ) {
688
+ let onion = host. split ( ".onion" ) . collect :: < Vec < & str > > ( ) [ 0 ] ;
689
+ let onion = base32:: decode ( base32:: Alphabet :: RFC4648 { padding : false } , & onion) . expect ( "Error decoding onion address" ) ;
690
+ let version = onion[ 0 ] ;
691
+ let checksum = u16:: from_be_bytes ( [ onion[ 1 ] , onion[ 2 ] ] ) ;
692
+ let ed25519_pubkey = onion[ 3 ..35 ] . try_into ( ) . expect ( "Invalid onion address" ) ;
693
+ return Ok ( NetAddress :: OnionV3 { ed25519_pubkey, checksum, version, port } ) ;
694
+ }
695
+
696
+ if let Ok ( hostname) = Hostname :: try_from ( host. to_string ( ) ) {
697
+ return Ok ( NetAddress :: Hostname { hostname, port } ) ;
698
+ }
699
+ return Err ( e)
700
+ } ,
693
701
}
694
-
695
- if let Ok ( hostname) = Hostname :: try_from ( host. to_string ( ) ) {
696
- return Ok ( NetAddress :: Hostname { hostname, port } ) ;
697
- }
698
-
699
- panic ! ( "cant parse input" ) ;
700
702
}
701
703
}
702
704
@@ -3183,6 +3185,6 @@ mod tests {
3183
3185
assert_eq ! ( ipv4_net_address, result_ipv4) ;
3184
3186
assert_eq ! ( ipv6_net_address, result_ipv6) ;
3185
3187
assert_eq ! ( hostname_net_address, result_hostname) ;
3186
- assert_eq ! ( onionv3_net_address, result_onionv3) ; // failing
3188
+ assert_eq ! ( onionv3_net_address, result_onionv3) ;
3187
3189
}
3188
3190
}
0 commit comments