Skip to content

Commit

Permalink
refactor: use now-stable stdlib to_canonical (#1097)
Browse files Browse the repository at this point in the history
No need to implement ourselves anymore
  • Loading branch information
howardjohn authored Jun 3, 2024
1 parent c6f1231 commit 6f42530
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use std::io::Error;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::net::SocketAddr;

use tokio::io;

Expand Down Expand Up @@ -46,16 +46,7 @@ pub fn set_freebind_and_transparent(socket: &TcpSocket) -> io::Result<()> {

pub fn to_canonical(addr: SocketAddr) -> SocketAddr {
// another match has to be used for IPv4 and IPv6 support
// @zhlsunshine TODO: to_canonical() should be used when it becomes stable a function in Rust
let ip = match addr.ip() {
IpAddr::V4(_) => return addr,
IpAddr::V6(i) => match i.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
}
_ => return addr,
},
};
let ip = addr.ip().to_canonical();
SocketAddr::from((ip, addr.port()))
}

Expand Down

0 comments on commit 6f42530

Please sign in to comment.