Skip to content

Commit

Permalink
Add RequestCode::EchoRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanbeom kim authored and msk committed Jun 5, 2023
1 parent 17a3a7d commit cf8c1f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Introduce new request code: `RequestCode:EchoRequest`
- `RequestCode::AllowList`: This new request code is for ping testing.
When you make a request to a damon connected with `RequestCode::EchoRequest`,
the receiving daemon respones with an empty tuple.

## [0.8.0] - 2023-05-24

### Changed
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub enum RequestCode {
/// Update the list of block
BlockList = 16,

/// Request Echo (for ping)
EchoRequest = 17,

/// Update the list of trusted domains
TrustedDomainList = 0,

Expand Down
26 changes: 26 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ pub trait Handler: Send {
async fn block_list(&mut self, _list: HostNetworkGroup) -> Result<(), String> {
return Err("not supported".to_string());
}
// This feature is supported for all daemons that use oinq.
async fn echo_request(&mut self) -> Result<(), String> {
return Ok(());
}
}

/// Handles requests to an agent.
Expand Down Expand Up @@ -248,6 +252,9 @@ pub async fn handle<H: Handler>(
let result = handler.block_list(block_list).await;
send_response(send, &mut buf, result).await?;
}
RequestCode::EchoRequest => {
send_response(send, &mut buf, handler.echo_request().await).await?;
}
RequestCode::ReloadFilterRule => {
let rules = codec
.deserialize::<Vec<IpNet>>(body)
Expand Down Expand Up @@ -312,6 +319,7 @@ mod tests {
internal_network_list: usize,
allow_list: usize,
block_list: usize,
echo_request_count: usize,
}

#[async_trait]
Expand Down Expand Up @@ -345,14 +353,21 @@ mod tests {
self.internal_network_list = network_list.hosts.len();
Ok(())
}

async fn allow_list(&mut self, allow_list: HostNetworkGroup) -> Result<(), String> {
self.allow_list = allow_list.networks.len();
Ok(())
}

async fn block_list(&mut self, block_list: HostNetworkGroup) -> Result<(), String> {
self.block_list = block_list.ip_ranges.len();
Ok(())
}

async fn echo_request(&mut self) -> Result<(), String> {
self.echo_request_count += 1;
Ok(())
}
}

let mut handler = TestHandler::default();
Expand Down Expand Up @@ -456,6 +471,15 @@ mod tests {
.await;
assert!(res.is_ok());

let res = message::send_request(
&mut channel.client.send,
&mut buf,
RequestCode::EchoRequest,
(),
)
.await;
assert!(res.is_ok());

channel.client.send.finish().await.unwrap();

assert_eq!(handler.reboot_count, 0);
Expand All @@ -464,6 +488,7 @@ mod tests {
assert_eq!(handler.internal_network_list, 0);
assert_eq!(handler.allow_list, 0);
assert_eq!(handler.block_list, 0);
assert_eq!(handler.echo_request_count, 0);
let res = super::handle(
&mut handler,
&mut channel.server.send,
Expand All @@ -477,6 +502,7 @@ mod tests {
assert_eq!(handler.internal_network_list, 3);
assert_eq!(handler.allow_list, 2);
assert_eq!(handler.block_list, 1);
assert_eq!(handler.echo_request_count, 1);

frame::recv_raw(&mut channel.client.recv, &mut buf)
.await
Expand Down

0 comments on commit cf8c1f8

Please sign in to comment.