rama-net 0.3.0

rama network types and utilities
Documentation
use crate::address::SocketAddress;

use rama_core::extensions::Extensions;

#[derive(Debug, Clone)]
/// Matcher based on the [`SocketAddress`] of the peer.
pub struct SocketAddressMatcher {
    addr: SocketAddress,
    optional: bool,
}

impl SocketAddressMatcher {
    /// create a new socket address matcher to match on a socket address
    ///
    /// This matcher will not match in case socket address could not be found,
    /// if you want to match in case socket address could not be found,
    /// use the [`SocketAddressMatcher::optional`] constructor..
    pub fn new(addr: impl Into<SocketAddress>) -> Self {
        Self {
            addr: addr.into(),
            optional: false,
        }
    }

    /// create a new socket address matcher to match on a socket address
    ///
    /// This matcher will match in case socket address could not be found.
    /// Use the [`SocketAddressMatcher::new`] constructor if you want do not want
    /// to match in case socket address could not be found.
    pub fn optional(addr: impl Into<SocketAddress>) -> Self {
        Self {
            addr: addr.into(),
            optional: true,
        }
    }
}

impl<Socket> rama_core::matcher::Matcher<Socket> for SocketAddressMatcher
where
    Socket: crate::stream::Socket,
{
    fn matches(&self, _ext: Option<&Extensions>, stream: &Socket) -> bool {
        stream
            .peer_addr()
            .map(|addr| addr == self.addr)
            .unwrap_or(self.optional)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use rama_core::matcher::Matcher;

    #[test]
    fn test_socket_matcher_socket_trait() {
        let matcher = SocketAddressMatcher::new(([127, 0, 0, 1], 8080));

        struct FakeSocket {
            local_addr: Option<SocketAddress>,
            peer_addr: Option<SocketAddress>,
        }

        impl crate::stream::Socket for FakeSocket {
            fn local_addr(&self) -> std::io::Result<SocketAddress> {
                match &self.local_addr {
                    Some(addr) => Ok(*addr),
                    None => Err(std::io::Error::from(std::io::ErrorKind::AddrNotAvailable)),
                }
            }

            fn peer_addr(&self) -> std::io::Result<SocketAddress> {
                match &self.peer_addr {
                    Some(addr) => Ok(*addr),
                    None => Err(std::io::Error::from(std::io::ErrorKind::AddrNotAvailable)),
                }
            }
        }

        let mut socket = FakeSocket {
            local_addr: None,
            peer_addr: Some(([127, 0, 0, 1], 8081).into()),
        };

        // test #1: no match: test with different socket info (port difference)
        assert!(!matcher.matches(None, &socket));

        // test #2: no match: test with different socket info (ip addr difference)
        socket.peer_addr = Some(([127, 0, 0, 2], 8080).into());
        assert!(!matcher.matches(None, &socket));

        // test #3: match: test with correct address
        socket.peer_addr = Some(([127, 0, 0, 1], 8080).into());
        assert!(matcher.matches(None, &socket));

        // test #5: match: test with missing socket info, but it's seen as optional
        let matcher = SocketAddressMatcher::optional(([127, 0, 0, 1], 8080));
        socket.peer_addr = None;
        assert!(matcher.matches(None, &socket));
    }
}