Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/version2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::{Buf, BufMut as _, BytesMut};
use snafu::{ensure, Snafu};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

#[derive(Debug, Snafu)]
#[cfg_attr(test, derive(PartialEq, Eq))]
Expand Down Expand Up @@ -51,6 +51,25 @@ pub enum ProxyAddresses {
},
}

impl ProxyAddresses {
/// Construct IPv4 or IPv6 from given source and destination address.
///
/// Returns `None` if the addresses use a different IP version.
pub fn from_socket_addrs(source: SocketAddr, destination: SocketAddr) -> Option<Self> {
match (source, destination) {
(SocketAddr::V4(source), SocketAddr::V4(destination)) => Some(Self::Ipv4 {
source,
destination,
}),
(SocketAddr::V6(source), SocketAddr::V6(destination)) => Some(Self::Ipv6 {
source,
destination,
}),
(_, _) => None,
}
}
}

#[derive(PartialEq, Eq)]
enum ProxyAddressFamily {
Unspec,
Expand Down