From bf56b5724165bedee83f047be956c4de1e6fc4b0 Mon Sep 17 00:00:00 2001 From: Hanbeom kim Date: Mon, 5 Jun 2023 10:11:28 +0900 Subject: [PATCH] Add `RequestCode::EchoRequest` --- CHANGELOG.md | 9 +++++++++ src/lib.rs | 3 +++ src/request.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3902b9..c062b09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index ca1fc69..da449e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/request.rs b/src/request.rs index 11a79d5..bf90399 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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. @@ -248,6 +252,9 @@ pub async fn handle( 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::>(body) @@ -312,6 +319,7 @@ mod tests { internal_network_list: usize, allow_list: usize, block_list: usize, + echo_request_count: usize, } #[async_trait] @@ -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(); @@ -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); @@ -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, @@ -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