@@ -918,8 +918,8 @@ pub enum SocketAddressParseError {
918
918
InvalidInput ,
919
919
/// Invalid port
920
920
InvalidPort ,
921
- /// Invalid onion address
922
- InvalidOnion ,
921
+ /// Invalid onion v3 address
922
+ InvalidOnionV3 ,
923
923
}
924
924
925
925
impl fmt:: Display for SocketAddressParseError {
@@ -929,7 +929,7 @@ impl fmt::Display for SocketAddressParseError {
929
929
SocketAddressParseError :: InvalidInput => write ! ( f, "Invalid input format. \
930
930
Expected: \" <ipv4>:<port>\" , \" [<ipv6>]:<port>\" , \" <onion address>.onion:<port>\" or \" <hostname>:<port>\" ") ,
931
931
SocketAddressParseError :: InvalidPort => write ! ( f, "Invalid port" ) ,
932
- SocketAddressParseError :: InvalidOnion => write ! ( f, "Invalid onion address" ) ,
932
+ SocketAddressParseError :: InvalidOnionV3 => write ! ( f, "Invalid onion v3 address" ) ,
933
933
}
934
934
}
935
935
}
@@ -958,24 +958,18 @@ impl From<std::net::SocketAddr> for SocketAddress {
958
958
}
959
959
}
960
960
961
- /// Parses an Onion host and port into a [`SocketAddress::OnionV3`] or [`SocketAddress::OnionV2 `].
961
+ /// Parses an OnionV3 host and port into a [`SocketAddress::OnionV3`].
962
962
///
963
963
/// The host part must end with ".onion".
964
964
pub fn parse_onion_address ( host : & str , port : u16 ) -> Result < SocketAddress , SocketAddressParseError > {
965
965
if host. ends_with ( ".onion" ) {
966
966
let domain = & host[ ..host. len ( ) - ".onion" . len ( ) ] ;
967
- if domain. len ( ) != 56 && domain. len ( ) != 16 {
968
- return Err ( SocketAddressParseError :: InvalidOnion ) ;
969
- }
970
- let mut onion = base32:: Alphabet :: RFC4648 { padding : false } . decode ( & domain) . map_err ( |_| SocketAddressParseError :: InvalidOnion ) ?;
971
- if onion. len ( ) == 10 {
972
- onion. extend_from_slice ( & port. to_be_bytes ( ) ) ;
973
- let mut bytes = [ 0 ; 12 ] ;
974
- bytes. copy_from_slice ( & onion) ;
975
- return Ok ( SocketAddress :: OnionV2 ( bytes) ) ;
967
+ if domain. len ( ) != 56 {
968
+ return Err ( SocketAddressParseError :: InvalidOnionV3 ) ;
976
969
}
970
+ let onion = base32:: Alphabet :: RFC4648 { padding : false } . decode ( & domain) . map_err ( |_| SocketAddressParseError :: InvalidOnionV3 ) ?;
977
971
if onion. len ( ) != 35 {
978
- return Err ( SocketAddressParseError :: InvalidOnion ) ;
972
+ return Err ( SocketAddressParseError :: InvalidOnionV3 ) ;
979
973
}
980
974
let version = onion[ 0 ] ;
981
975
let first_checksum_flag = onion[ 1 ] ;
@@ -4129,8 +4123,8 @@ mod tests {
4129
4123
assert_eq ! ( hostname, SocketAddress :: from_str( & hostname. to_string( ) ) . unwrap( ) ) ;
4130
4124
4131
4125
let onion_v2 = SocketAddress :: OnionV2 ( [ 40 , 4 , 64 , 185 , 202 , 19 , 162 , 75 , 90 , 200 , 38 , 7 ] , ) ;
4132
- assert_eq ! ( onion_v2 , SocketAddress :: from_str ( "facebookcorewwwi .onion:9735") . unwrap ( ) ) ;
4133
- assert_eq ! ( onion_v2 , SocketAddress :: from_str( & onion_v2 . to_string ( ) ) . unwrap ( ) ) ;
4126
+ assert_eq ! ( "FACEBOOKCOREWWWI .onion:9735", & onion_v2 . to_string ( ) ) ;
4127
+ assert_eq ! ( Err ( SocketAddressParseError :: InvalidOnionV3 ) , SocketAddress :: from_str( "FACEBOOKCOREWWWI.onion:9735" ) ) ;
4134
4128
4135
4129
let onion_v3 = SocketAddress :: OnionV3 {
4136
4130
ed25519_pubkey : [ 37 , 24 , 75 , 5 , 25 , 73 , 117 , 194 , 139 , 102 , 182 , 107 , 4 , 105 , 247 , 246 , 85 ,
@@ -4142,7 +4136,7 @@ mod tests {
4142
4136
assert_eq ! ( onion_v3, SocketAddress :: from_str( "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:1234" ) . unwrap( ) ) ;
4143
4137
assert_eq ! ( onion_v3, SocketAddress :: from_str( & onion_v3. to_string( ) ) . unwrap( ) ) ;
4144
4138
4145
- assert_eq ! ( Err ( SocketAddressParseError :: InvalidOnion ) , SocketAddress :: from_str( "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6.onion:1234" ) ) ;
4139
+ assert_eq ! ( Err ( SocketAddressParseError :: InvalidOnionV3 ) , SocketAddress :: from_str( "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6.onion:1234" ) ) ;
4146
4140
assert_eq ! ( Err ( SocketAddressParseError :: InvalidInput ) , SocketAddress :: from_str( "127.0.0.1@1234" ) ) ;
4147
4141
assert_eq ! ( Err ( SocketAddressParseError :: InvalidInput ) , "" . parse:: <SocketAddress >( ) ) ;
4148
4142
assert ! ( SocketAddress :: from_str( "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.onion:9735:94" ) . is_err( ) ) ;
0 commit comments