aboutsummaryrefslogtreecommitdiffhomepage
path: root/libs
diff options
context:
space:
mode:
authorrustdesk <[email protected]>2023-01-10 16:09:25 +0800
committerrustdesk <[email protected]>2023-01-10 16:09:25 +0800
commitfc83fa0a04f1214b47d84d5433f936038151929d (patch)
treea808d178eeabc98a5a046ac36ea79e88132a25a6 /libs
parent338af1af9dd97f69bd078b5c3099728c62a47531 (diff)
downloadrustdesk-server-fc83fa0a04f1214b47d84d5433f936038151929d.tar.gz
rustdesk-server-fc83fa0a04f1214b47d84d5433f936038151929d.zip
try_into_v4
Diffstat (limited to 'libs')
-rw-r--r--libs/hbb_common/src/lib.rs27
1 files changed, 17 insertions, 10 deletions
diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs
index 4874d32..10dd362 100644
--- a/libs/hbb_common/src/lib.rs
+++ b/libs/hbb_common/src/lib.rs
@@ -95,19 +95,24 @@ pub type ResultType<F, E = anyhow::Error> = anyhow::Result<F, E>;
pub struct AddrMangle();
+#[inline]
+pub fn try_into_v4(addr: SocketAddr) -> SocketAddr {
+ match addr {
+ SocketAddr::V6(v6) if !addr.ip().is_loopback() => {
+ if let Some(v4) = v6.ip().to_ipv4() {
+ SocketAddr::new(IpAddr::V4(v4), addr.port())
+ } else {
+ addr
+ }
+ }
+ _ => addr,
+ }
+}
+
impl AddrMangle {
pub fn encode(addr: SocketAddr) -> Vec<u8> {
// not work with [:1]:<port>
- let addr = match addr {
- SocketAddr::V6(v6) => {
- if let Some(v4) = v6.ip().to_ipv4() {
- SocketAddr::new(IpAddr::V4(v4), addr.port())
- } else {
- addr
- }
- }
- _ => addr,
- };
+ let addr = try_into_v4(addr);
match addr {
SocketAddr::V4(addr_v4) => {
let tm = (SystemTime::now()
@@ -353,5 +358,7 @@ mod test {
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v4)), addr_v4);
let addr_v6 = "[ef::fe]:8080".parse().unwrap();
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v6)), addr_v6);
+ let addr_v6 = "[::1]:8080".parse().unwrap();
+ assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v6)), addr_v6);
}
}