Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recv_addr to endpoint_receive_filter #51

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
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
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