Skip to content

Commit

Permalink
Add recv_addr to endpoint_receive_filter (#51)
Browse files Browse the repository at this point in the history
Since packets might not come back from the endpoint address that we
expect it to, we should track this data in our filter.

Now we can!

Work on #1
  • Loading branch information
markmandel authored May 27, 2020
1 parent dcc8a72 commit 7b4f555
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
32 changes: 24 additions & 8 deletions src/extensions/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ impl Filter for FilterChain {
Some(c)
}

fn endpoint_receive_filter(&self, endpoint: &EndPoint, contents: Vec<u8>) -> Option<Vec<u8>> {
fn endpoint_receive_filter(
&self,
endpoint: &EndPoint,
recv_addr: SocketAddr,
contents: Vec<u8>,
) -> Option<Vec<u8>> {
let mut c = contents;
for f in &self.filters {
match f.endpoint_receive_filter(&endpoint, c) {
match f.endpoint_receive_filter(&endpoint, recv_addr, c) {
None => return None,
Some(contents) => {
c = contents;
Expand Down Expand Up @@ -167,11 +172,11 @@ mod tests {
fn endpoint_receive_filter(
&self,
endpoint: &EndPoint,
recv_addr: SocketAddr,
contents: Vec<u8>,
) -> Option<Vec<u8>> {
let mut c = contents;
c.append(&mut ":erf:".as_bytes().to_vec());
c.append(&mut endpoint.name.as_bytes().to_vec());
c.append(&mut format!(":erf:{}:{}", endpoint.name, recv_addr).into_bytes());
Some(c)
}

Expand Down Expand Up @@ -276,9 +281,16 @@ mod tests {
);

let content = chain
.endpoint_receive_filter(&endpoints_fixture[0], "hello".as_bytes().to_vec())
.endpoint_receive_filter(
&endpoints_fixture[0],
endpoints_fixture[0].address,
"hello".as_bytes().to_vec(),
)
.unwrap();
assert_eq!("hello:erf:one", from_utf8(content.as_slice()).unwrap());
assert_eq!(
"hello:erf:one:127.0.0.1:80",
from_utf8(content.as_slice()).unwrap()
);

let content = chain
.endpoint_send_filter(
Expand Down Expand Up @@ -325,10 +337,14 @@ mod tests {
);

let content = chain
.endpoint_receive_filter(&endpoints_fixture[0], "hello".as_bytes().to_vec())
.endpoint_receive_filter(
&endpoints_fixture[0],
endpoints_fixture[0].address,
"hello".as_bytes().to_vec(),
)
.unwrap();
assert_eq!(
"hello:erf:one:erf:one",
"hello:erf:one:127.0.0.1:80:erf:one:127.0.0.1:80",
from_utf8(content.as_slice()).unwrap()
);

Expand Down
22 changes: 17 additions & 5 deletions src/extensions/filter_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ pub trait Filter: Send + Sync {
/// If the packet should be rejected, return None.
fn local_send_filter(&self, to: SocketAddr, contents: Vec<u8>) -> Option<Vec<u8>>;

/// endpoint_receive_filter filters packets received from an endpoint, that is going back to the
/// original sender.
/// endpoint_receive_filter filters packets received from recv_addr, but expected from the given endpoint,
/// that are going back to the original sender.
/// This function should return the packet to be sent (which may be manipulated).
/// If the packet should be rejected, return None.
fn endpoint_receive_filter(&self, endpoint: &EndPoint, contents: Vec<u8>) -> Option<Vec<u8>>;
fn endpoint_receive_filter(
&self,
endpoint: &EndPoint,
recv_addr: SocketAddr,
contents: Vec<u8>,
) -> Option<Vec<u8>>;

/// endpoint_send_filter intercepts packets that are being sent back to the original
/// endpoint sender address
Expand Down Expand Up @@ -104,7 +109,12 @@ mod tests {
None
}

fn endpoint_receive_filter(&self, _: &EndPoint, _: Vec<u8>) -> Option<Vec<u8>> {
fn endpoint_receive_filter(
&self,
_: &EndPoint,
_: SocketAddr,
_: Vec<u8>,
) -> Option<Vec<u8>> {
None
}

Expand All @@ -130,6 +140,8 @@ mod tests {
};

assert!(filter.local_receive_filter(&vec![], addr, vec![]).is_none());
assert!(filter.endpoint_receive_filter(&endpoint, vec![]).is_none());
assert!(filter
.endpoint_receive_filter(&endpoint, addr, vec![])
.is_none());
}
}
13 changes: 10 additions & 3 deletions src/extensions/filters/debug_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ impl Filter for DebugFilter {
Some(contents)
}

fn endpoint_receive_filter(&self, endpoint: &EndPoint, contents: Vec<u8>) -> Option<Vec<u8>> {
info!(self.log, "received endpoint packet"; "endpoint" => endpoint.name.clone(), "contents" => packet_to_string(contents.clone()));
fn endpoint_receive_filter(
&self,
endpoint: &EndPoint,
recv_addr: SocketAddr,
contents: Vec<u8>,
) -> Option<Vec<u8>> {
info!(self.log, "received endpoint packet"; "endpoint" => endpoint.name.clone(),
"recv_addr" => recv_addr,
"contents" => packet_to_string(contents.clone()));
Some(contents)
}

Expand Down Expand Up @@ -131,7 +138,7 @@ mod tests {
};
let contents = "hello".to_string().into_bytes();

match df.endpoint_receive_filter(&endpoint, contents.clone()) {
match df.endpoint_receive_filter(&endpoint, endpoint.address, contents.clone()) {
None => assert!(false, "should return a result"),
Some(result_contents) => assert_eq!(contents, result_contents),
}
Expand Down

0 comments on commit 7b4f555

Please sign in to comment.