From 860d14a95e47f234e5ec91042415e7a5f19d7e49 Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Mon, 27 Feb 2023 08:58:28 +0100 Subject: [PATCH 1/4] edns-tcp-keepalive option value is optional --- src/base/opt/rfc7828.rs | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/base/opt/rfc7828.rs b/src/base/opt/rfc7828.rs index b993622d1..ae5cfca8e 100644 --- a/src/base/opt/rfc7828.rs +++ b/src/base/opt/rfc7828.rs @@ -11,22 +11,30 @@ use octseq::parse::Parser; //------------ TcpKeepalive -------------------------------------------------- +// According to RFC 7826, the edns-tcp-keepalive option is empty in +// the client to server direction, and has a 16-bit timeout value in the +// other direction. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct TcpKeepalive(u16); +pub struct TcpKeepalive(Option); impl TcpKeepalive { - pub fn new(timeout: u16) -> Self { + pub fn new(timeout: Option) -> Self { TcpKeepalive(timeout) } - pub fn timeout(self) -> u16 { + pub fn timeout(self) -> Option { self.0 } pub fn parse>( parser: &mut Parser ) -> Result { - u16::parse(parser).map(Self::new) + let len = parser.remaining(); + if len == 0 { + Ok(Self::new(None)) + } else { + u16::parse(parser).map(|v| Self::new(Some(v))) + } } } @@ -54,19 +62,40 @@ impl<'a, Octs: AsRef<[u8]>> ParseOptData<'a, Octs> for TcpKeepalive { impl ComposeOptData for TcpKeepalive { fn compose_len(&self) -> u16 { - u16::COMPOSE_LEN + match self.0 { + Some(_) => { + u16::COMPOSE_LEN + } + None => { + 0 + } + } } fn compose_option( &self, target: &mut Target ) -> Result<(), Target::AppendError> { - self.0.compose(target) + match self.0 { + Some(v) => { + v.compose(target) + } + None => { + Ok(()) + } + } } } impl fmt::Display for TcpKeepalive { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) + match self.0 { + Some(v) => { + write!(f, "{}", v) + } + None => { + write!(f, "") + } + } } } @@ -74,7 +103,7 @@ impl fmt::Display for TcpKeepalive { impl<'a, Target: Composer> OptBuilder<'a, Target> { pub fn tcp_keepalive( - &mut self, timeout: u16 + &mut self, timeout: Option ) -> Result<(), Target::AppendError> { self.push(&TcpKeepalive::new(timeout)) } From 9f7a5b938ba312c6733781744b8164409117c335 Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Mon, 27 Feb 2023 14:57:03 +0100 Subject: [PATCH 2/4] Fix indentation and remove some curlies --- src/base/opt/rfc7828.rs | 48 ++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/src/base/opt/rfc7828.rs b/src/base/opt/rfc7828.rs index ae5cfca8e..f13c53254 100644 --- a/src/base/opt/rfc7828.rs +++ b/src/base/opt/rfc7828.rs @@ -29,12 +29,12 @@ impl TcpKeepalive { pub fn parse>( parser: &mut Parser ) -> Result { - let len = parser.remaining(); - if len == 0 { - Ok(Self::new(None)) - } else { - u16::parse(parser).map(|v| Self::new(Some(v))) - } + let len = parser.remaining(); + if len == 0 { + Ok(Self::new(None)) + } else { + u16::parse(parser).map(|v| Self::new(Some(v))) + } } } @@ -62,40 +62,28 @@ impl<'a, Octs: AsRef<[u8]>> ParseOptData<'a, Octs> for TcpKeepalive { impl ComposeOptData for TcpKeepalive { fn compose_len(&self) -> u16 { - match self.0 { - Some(_) => { - u16::COMPOSE_LEN - } - None => { - 0 - } - } + match self.0 { + Some(_) => u16::COMPOSE_LEN, + None => 0, + } } fn compose_option( &self, target: &mut Target ) -> Result<(), Target::AppendError> { - match self.0 { - Some(v) => { - v.compose(target) - } - None => { - Ok(()) - } - } + match self.0 { + Some(v) => v.compose(target), + None => Ok(()), + } } } impl fmt::Display for TcpKeepalive { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.0 { - Some(v) => { - write!(f, "{}", v) - } - None => { - write!(f, "") - } - } + match self.0 { + Some(v) => write!(f, "{}", v), + None => write!(f, ""), + } } } From eacec8aafa8aa2a3be913770b4e3b61768098115 Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Mon, 27 Feb 2023 15:06:00 +0100 Subject: [PATCH 3/4] Fixed tests --- src/base/opt/rfc7828.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/base/opt/rfc7828.rs b/src/base/opt/rfc7828.rs index f13c53254..c3f84738d 100644 --- a/src/base/opt/rfc7828.rs +++ b/src/base/opt/rfc7828.rs @@ -106,9 +106,16 @@ mod test { use super::super::test::test_option_compose_parse; #[test] - fn tcp_keepalive_compose_parse() { + fn tcp_keepalive_compose_parse_none() { test_option_compose_parse( - &TcpKeepalive::new(12), + &TcpKeepalive::new(None), + |parser| TcpKeepalive::parse(parser) + ); + } + #[test] + fn tcp_keepalive_compose_parse_some() { + test_option_compose_parse( + &TcpKeepalive::new(Some(12)), |parser| TcpKeepalive::parse(parser) ); } From cde554a0a31b2107fd36c2c632c7a5bc6d685d0d Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Mon, 27 Feb 2023 15:29:31 +0100 Subject: [PATCH 4/4] Remove variable that is used only once --- src/base/opt/rfc7828.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/base/opt/rfc7828.rs b/src/base/opt/rfc7828.rs index c3f84738d..36bfb4b02 100644 --- a/src/base/opt/rfc7828.rs +++ b/src/base/opt/rfc7828.rs @@ -29,8 +29,7 @@ impl TcpKeepalive { pub fn parse>( parser: &mut Parser ) -> Result { - let len = parser.remaining(); - if len == 0 { + if parser.remaining() == 0 { Ok(Self::new(None)) } else { u16::parse(parser).map(|v| Self::new(Some(v)))