From a217bf39dcb8488173b9598ab3c072a8ea6e215b Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Wed, 6 Oct 2021 15:00:03 +0200 Subject: [PATCH 1/3] Refactor out use of anyhow --- Cargo.toml | 3 +- examples/mdns_query.rs | 4 +-- examples/mdns_server.rs | 4 +-- src/conn/conn_test.rs | 2 +- src/conn/mod.rs | 11 ++++--- src/error.rs | 34 +++++++++++++++++---- src/lib.rs | 4 ++- src/message/builder.rs | 1 - src/message/message_test.rs | 59 +++++++++++++++---------------------- src/message/mod.rs | 1 - src/message/name.rs | 1 - src/message/packer.rs | 2 -- src/message/parser.rs | 18 +++++------ src/message/question.rs | 1 + src/message/resource/mod.rs | 1 - src/message/resource/mx.rs | 1 + src/message/resource/ns.rs | 1 + src/message/resource/opt.rs | 3 +- src/message/resource/ptr.rs | 1 + src/message/resource/soa.rs | 1 + src/message/resource/srv.rs | 1 + src/message/resource/txt.rs | 2 -- 22 files changed, 80 insertions(+), 76 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 84977ea..1011483 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,7 @@ util = { package = "webrtc-util", version = "0.4.3", default-features = false, f tokio = { version = "1.12.0", features = ["full"] } socket2 = { version = "0.4.2", features = ["all"] } log = "0.4" -thiserror = "1.0.29" -anyhow = "1.0.44" +thiserror = "1.0" [dev-dependencies] env_logger = "0.9" diff --git a/examples/mdns_query.rs b/examples/mdns_query.rs index be81d28..3379b4e 100644 --- a/examples/mdns_query.rs +++ b/examples/mdns_query.rs @@ -1,8 +1,8 @@ use webrtc_mdns as mdns; +use mdns::Error; use mdns::{config::*, conn::*}; -use anyhow::Result; use clap::{App, AppSettings, Arg}; use std::net::SocketAddr; use std::str::FromStr; @@ -15,7 +15,7 @@ use tokio::sync::mpsc; // cargo run --color=always --package webrtc-mdns --example mdns_query -- --local-name pion-test.local #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> Result<(), Error> { env_logger::init(); let mut app = App::new("mDNS Query") diff --git a/examples/mdns_server.rs b/examples/mdns_server.rs index 42f0b93..6555d31 100644 --- a/examples/mdns_server.rs +++ b/examples/mdns_server.rs @@ -1,8 +1,8 @@ use webrtc_mdns as mdns; +use mdns::Error; use mdns::{config::*, conn::*}; -use anyhow::Result; use clap::{App, AppSettings, Arg}; use std::net::SocketAddr; use std::str::FromStr; @@ -14,7 +14,7 @@ use std::str::FromStr; // cargo run --color=always --package webrtc-mdns --example mdns_server -- --local-name pion-test.local #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> Result<(), Error> { env_logger::init(); let mut app = App::new("mDNS Sever") diff --git a/src/conn/conn_test.rs b/src/conn/conn_test.rs index 3c1538d..91f10b5 100644 --- a/src/conn/conn_test.rs +++ b/src/conn/conn_test.rs @@ -13,7 +13,7 @@ mod test { server_a.close().await?; if let Err(err) = server_a.close().await { - assert!(Error::ErrConnectionClosed.equal(&err)); + assert_eq!(Error::ErrConnectionClosed, err); } else { assert!(false, "expected error, but got ok"); } diff --git a/src/conn/mod.rs b/src/conn/mod.rs index a118651..9a1b58d 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -7,7 +7,6 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::sync::Arc; use std::time::Duration; -use anyhow::Result; use core::sync::atomic; use socket2::SockAddr; use tokio::net::{ToSocketAddrs, UdpSocket}; @@ -71,7 +70,7 @@ impl DnsConn { Ok(e) => e, Err(e) => { log::error!("Error getting interfaces: {:?}", e); - return Err(Error::new(e.to_string()).into()); + return Err(Error::Other(e.to_string())); } }; @@ -89,7 +88,7 @@ impl DnsConn { } if join_error_count >= interfaces.len() { - return Err(Error::ErrJoiningMulticastGroup.into()); + return Err(Error::ErrJoiningMulticastGroup); } } @@ -321,7 +320,7 @@ async fn run( let q = match p.question() { Ok(q) => q, Err(err) => { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { log::trace!("Parsing has completed"); break; } else { @@ -353,7 +352,7 @@ async fn run( let a = match p.answer_header() { Ok(a) => a, Err(err) => { - if !Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone != err { log::warn!("Failed to parse mDNS packet {}", err); } return; @@ -407,7 +406,7 @@ async fn send_answer( a: match interface_addr.ip() { IpAddr::V4(ip) => ip.octets(), IpAddr::V6(_) => { - return Err(Error::new("Unexpected IpV6 addr".to_owned()).into()) + return Err(Error::Other("Unexpected IpV6 addr".to_owned())) } }, })), diff --git a/src/error.rs b/src/error.rs index c31e73e..32e2646 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,13 @@ use thiserror::Error; +use std::io; +use std::net; +use std::string::FromUtf8Error; + +pub type Result = std::result::Result; + #[derive(Debug, Error, PartialEq)] +#[non_exhaustive] pub enum Error { #[error("mDNS: failed to join multicast group")] ErrJoiningMulticastGroup, @@ -52,14 +59,29 @@ pub enum Error { ErrCompressedSrv, #[error("empty builder msg")] ErrEmptyBuilderMsg, - - #[allow(non_camel_case_types)] #[error("{0}")] - new(String), + Io(#[source] IoError), + #[error("utf-8 error: {0}")] + Utf8(#[from] FromUtf8Error), + #[error("parse addr: {0}")] + ParseIp(#[from] net::AddrParseError), + #[error("{0}")] + Other(String), +} + +#[derive(Debug, Error)] +#[error("io error: {0}")] +pub struct IoError(#[from] pub io::Error); + +// Workaround for wanting PartialEq for io::Error. +impl PartialEq for IoError { + fn eq(&self, other: &Self) -> bool { + self.0.kind() == other.0.kind() + } } -impl Error { - pub fn equal(&self, err: &anyhow::Error) -> bool { - err.downcast_ref::().map_or(false, |e| e == self) +impl From for Error { + fn from(e: io::Error) -> Self { + Error::Io(IoError(e)) } } diff --git a/src/lib.rs b/src/lib.rs index f173c04..eda3a3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,5 +3,7 @@ pub mod config; pub mod conn; -pub mod error; +mod error; pub mod message; + +pub use error::Error; diff --git a/src/message/builder.rs b/src/message/builder.rs index 54e460e..5ea7e01 100644 --- a/src/message/builder.rs +++ b/src/message/builder.rs @@ -4,7 +4,6 @@ use super::resource::*; use super::*; use crate::error::*; -use anyhow::Result; use std::collections::HashMap; // A Builder allows incrementally packing a DNS message. diff --git a/src/message/message_test.rs b/src/message/message_test.rs index 6b5a7b8..e344ba3 100644 --- a/src/message/message_test.rs +++ b/src/message/message_test.rs @@ -9,7 +9,6 @@ use super::resource::{ use super::*; use crate::error::*; -use anyhow::Result; use std::collections::HashMap; fn small_test_msg() -> Result { @@ -320,7 +319,7 @@ fn test_name_pack_unpack() -> Result<()> { let result = input.pack(vec![], &mut Some(HashMap::new()), 0); if let Some(want_err) = want_err { if let Err(actual_err) = result { - assert!(want_err.equal(&actual_err)); + assert_eq!(want_err, actual_err); } else { assert!(false); } @@ -365,8 +364,9 @@ fn test_incompressible_name() -> Result<()> { let mut n2 = Name::default(); let result = n2.unpack_compressed(&buf, off, false /* allowCompression */); if let Err(err) = result { - assert!( - Error::ErrCompressedSrv.equal(&err), + assert_eq!( + Error::ErrCompressedSrv, + err, "unpacking compressed incompressible name with pointers: got {}, want = {}", err, Error::ErrCompressedSrv @@ -507,7 +507,7 @@ fn test_resource_not_started() -> Result<()> { for (name, test_fn) in tests { let mut p = Parser::default(); if let Err(err) = test_fn(&mut p) { - assert!(Error::ErrNotStarted.equal(&err), "{}", name); + assert_eq!(Error::ErrNotStarted, err, "{}", name); } } @@ -623,28 +623,28 @@ fn test_skip_each() -> Result<()> { p.skip_question()?; if let Err(err) = p.skip_question() { - assert!(Error::ErrSectionDone.equal(&err)); + assert_eq!(Error::ErrSectionDone, err); } else { assert!(false, "expected error, but got ok"); } p.skip_answer()?; if let Err(err) = p.skip_answer() { - assert!(Error::ErrSectionDone.equal(&err)); + assert_eq!(Error::ErrSectionDone, err); } else { assert!(false, "expected error, but got ok"); } p.skip_authority()?; if let Err(err) = p.skip_authority() { - assert!(Error::ErrSectionDone.equal(&err)); + assert_eq!(Error::ErrSectionDone, err); } else { assert!(false, "expected error, but got ok"); } p.skip_additional()?; if let Err(err) = p.skip_additional() { - assert!(Error::ErrSectionDone.equal(&err)); + assert_eq!(Error::ErrSectionDone, err); } else { assert!(false, "expected error, but got ok"); } @@ -713,7 +713,7 @@ fn test_skip_after_read() -> Result<()> { }; if let Err(err) = result { - assert!(Error::ErrSectionDone.equal(&err)); + assert_eq!(Error::ErrSectionDone, err); } else { assert!(false, "expected error, but got ok"); } @@ -746,7 +746,7 @@ fn test_skip_not_started() -> Result<()> { let mut p = Parser::default(); for (name, test_fn) in tests { if let Err(err) = test_fn(&mut p) { - assert!(Error::ErrNotStarted.equal(&err)); + assert_eq!(Error::ErrNotStarted, err); } else { assert!(false, "{} expected error, but got ok", name); } @@ -813,12 +813,10 @@ fn test_too_many_records() -> Result<()> { for (name, mut msg, want) in tests { if let Err(got) = msg.pack() { - assert!( - want.equal(&got), + assert_eq!( + want, got, "got Message.Pack() for {} = {}, want = {}", - name, - got, - want + name, got, want ) } else { assert!(false, "expected error, but got ok"); @@ -880,7 +878,7 @@ fn test_too_long_txt() -> Result<()> { } let rb = TxtResource { txt: vec![str256] }; if let Err(err) = rb.pack(vec![], &mut Some(HashMap::new()), 0) { - assert!(Error::ErrStringTooLong.equal(&err)); + assert_eq!(Error::ErrStringTooLong, err); } else { assert!(false, "expected error, but got ok"); } @@ -936,13 +934,10 @@ fn test_start_error() -> Result<()> { for (test_name, test_fn) in &tests { let mut b = env_fn(); if let Err(got_err) = test_fn(&mut b) { - assert!( - env_err.equal(&got_err), + assert_eq!( + *env_err, got_err, "got Builder{}.{} = {}, want = {}", - env_name, - test_name, - got_err, - env_err + env_name, test_name, got_err, env_err ); } else { assert!( @@ -1099,13 +1094,10 @@ fn test_builder_resource_error() -> Result<()> { for (test_name, test_fn) in &tests { let mut b = env_fn(); if let Err(got_err) = test_fn(&mut b) { - assert!( - env_err.equal(&got_err), + assert_eq!( + *env_err, got_err, "got Builder{}.{} = {}, want = {}", - env_name, - test_name, - got_err, - env_err + env_name, test_name, got_err, env_err ); } else { assert!( @@ -1125,12 +1117,7 @@ fn test_finish_error() -> Result<()> { let mut b = Builder::default(); let want = Error::ErrNotStarted; if let Err(got) = b.finish() { - assert!( - want.equal(&got), - "got Builder.Finish() = {}, want = {}", - got, - want - ); + assert_eq!(want, got, "got Builder.Finish() = {}, want = {}", got, want); } else { assert!(false, "expected error, but got ok"); } @@ -1230,7 +1217,7 @@ fn test_resource_pack() -> Result<()> { for (mut m, want_err) in tests { if let Err(err) = m.pack() { - assert!(want_err.equal(&err)); + assert_eq!(want_err, err); } else { assert!(false, "expected error, but got ok"); } diff --git a/src/message/mod.rs b/src/message/mod.rs index efd6ec9..1cfbba6 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -16,7 +16,6 @@ use parser::*; use question::*; use resource::*; -use anyhow::Result; use std::collections::HashMap; use std::fmt; diff --git a/src/message/name.rs b/src/message/name.rs index df699e8..ad55cfb 100644 --- a/src/message/name.rs +++ b/src/message/name.rs @@ -1,6 +1,5 @@ use crate::error::*; -use anyhow::Result; use std::collections::HashMap; use std::fmt; diff --git a/src/message/packer.rs b/src/message/packer.rs index 67e2b91..68cd300 100644 --- a/src/message/packer.rs +++ b/src/message/packer.rs @@ -1,8 +1,6 @@ use super::*; use crate::error::*; -use anyhow::Result; - // pack_bytes appends the wire format of field to msg. pub(crate) fn pack_bytes(mut msg: Vec, field: &[u8]) -> Vec { msg.extend_from_slice(field); diff --git a/src/message/parser.rs b/src/message/parser.rs index 9738c94..591b3cf 100644 --- a/src/message/parser.rs +++ b/src/message/parser.rs @@ -5,8 +5,6 @@ use crate::message::question::Question; use crate::message::resource::{unpack_resource_body, Resource, ResourceBody, ResourceHeader}; use crate::message::{DnsClass, DnsType}; -use anyhow::Result; - // A Parser allows incrementally parsing a DNS message. // // When parsing is started, the Header is parsed. Next, each question can be @@ -130,7 +128,7 @@ impl<'a> Parser<'a> { loop { match self.question() { Err(err) => { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(qs); } else { return Err(err); @@ -156,7 +154,7 @@ impl<'a> Parser<'a> { pub fn skip_all_questions(&mut self) -> Result<()> { loop { if let Err(err) = self.skip_question() { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(()); } else { return Err(err); @@ -190,7 +188,7 @@ impl<'a> Parser<'a> { loop { match self.answer() { Err(err) => { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(a); } else { return Err(err); @@ -210,7 +208,7 @@ impl<'a> Parser<'a> { pub fn skip_all_answers(&mut self) -> Result<()> { loop { if let Err(err) = self.skip_answer() { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(()); } else { return Err(err); @@ -244,7 +242,7 @@ impl<'a> Parser<'a> { loop { match self.authority() { Err(err) => { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(a); } else { return Err(err); @@ -264,7 +262,7 @@ impl<'a> Parser<'a> { pub fn skip_all_authorities(&mut self) -> Result<()> { loop { if let Err(err) = self.skip_authority() { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(()); } else { return Err(err); @@ -298,7 +296,7 @@ impl<'a> Parser<'a> { loop { match self.additional() { Err(err) => { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(a); } else { return Err(err); @@ -318,7 +316,7 @@ impl<'a> Parser<'a> { pub fn skip_all_additionals(&mut self) -> Result<()> { loop { if let Err(err) = self.skip_additional() { - if Error::ErrSectionDone.equal(&err) { + if Error::ErrSectionDone == err { return Ok(()); } else { return Err(err); diff --git a/src/message/question.rs b/src/message/question.rs index 312702d..61f4383 100644 --- a/src/message/question.rs +++ b/src/message/question.rs @@ -1,5 +1,6 @@ use super::name::*; use super::*; +use crate::error::Result; use std::collections::HashMap; use std::fmt; diff --git a/src/message/resource/mod.rs b/src/message/resource/mod.rs index 4fc5c5b..e644ea3 100644 --- a/src/message/resource/mod.rs +++ b/src/message/resource/mod.rs @@ -25,7 +25,6 @@ use soa::*; use srv::*; use txt::*; -use anyhow::Result; use std::collections::HashMap; use std::fmt; diff --git a/src/message/resource/mx.rs b/src/message/resource/mx.rs index 524dcb9..6f20898 100644 --- a/src/message/resource/mx.rs +++ b/src/message/resource/mx.rs @@ -1,4 +1,5 @@ use super::*; +use crate::error::Result; use crate::message::name::*; use crate::message::packer::*; diff --git a/src/message/resource/ns.rs b/src/message/resource/ns.rs index ce8c394..a18b9ec 100644 --- a/src/message/resource/ns.rs +++ b/src/message/resource/ns.rs @@ -1,4 +1,5 @@ use super::*; +use crate::error::Result; use crate::message::name::*; // An NSResource is an NS Resource record. diff --git a/src/message/resource/opt.rs b/src/message/resource/opt.rs index 954de43..211eaf8 100644 --- a/src/message/resource/opt.rs +++ b/src/message/resource/opt.rs @@ -1,9 +1,8 @@ use super::*; +use crate::error::Result; use crate::error::*; use crate::message::packer::*; -use anyhow::Result; - // An OPTResource is an OPT pseudo Resource record. // // The pseudo resource record is part of the extension mechanisms for DNS diff --git a/src/message/resource/ptr.rs b/src/message/resource/ptr.rs index 8618356..fbdf9c2 100644 --- a/src/message/resource/ptr.rs +++ b/src/message/resource/ptr.rs @@ -1,4 +1,5 @@ use super::*; +use crate::error::Result; use crate::message::name::*; // A PTRResource is a PTR Resource record. diff --git a/src/message/resource/soa.rs b/src/message/resource/soa.rs index 1bb2d9b..42bf7fd 100644 --- a/src/message/resource/soa.rs +++ b/src/message/resource/soa.rs @@ -1,4 +1,5 @@ use super::*; +use crate::error::Result; use crate::message::name::*; use crate::message::packer::*; diff --git a/src/message/resource/srv.rs b/src/message/resource/srv.rs index 86f9bd8..2f8dec1 100644 --- a/src/message/resource/srv.rs +++ b/src/message/resource/srv.rs @@ -1,4 +1,5 @@ use super::*; +use crate::error::Result; use crate::message::name::*; use crate::message::packer::*; diff --git a/src/message/resource/txt.rs b/src/message/resource/txt.rs index cd22c0c..9e4cbf1 100644 --- a/src/message/resource/txt.rs +++ b/src/message/resource/txt.rs @@ -2,8 +2,6 @@ use super::*; use crate::error::*; use crate::message::packer::*; -use anyhow::Result; - // A TXTResource is a txt Resource record. #[derive(Default, Debug, Clone, PartialEq)] pub struct TxtResource { From f24c854fd354ae4e3261d7ff8c08b0a2e08daa77 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Fri, 8 Oct 2021 09:31:42 +0200 Subject: [PATCH 2/3] fix clippy warnings --- src/conn/mod.rs | 8 ++++---- src/message/builder.rs | 26 +++++++++++++------------- src/message/message_test.rs | 18 +++++++++--------- src/message/mod.rs | 8 ++++---- src/message/name.rs | 28 ++++++++++++++-------------- src/message/packer.rs | 16 ++++++++-------- src/message/parser.rs | 10 +++++----- src/message/resource/mod.rs | 8 ++++---- src/message/resource/opt.rs | 2 +- src/message/resource/txt.rs | 2 +- 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/conn/mod.rs b/src/conn/mod.rs index 9a1b58d..c234faf 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -142,7 +142,7 @@ impl DnsConn { pub async fn close(&self) -> Result<()> { log::info!("Closing connection"); if self.is_server_closed.load(atomic::Ordering::SeqCst) { - return Err(Error::ErrConnectionClosed.into()); + return Err(Error::ErrConnectionClosed); } log::trace!("Sending close command to server"); @@ -153,7 +153,7 @@ impl DnsConn { } Err(e) => { log::warn!("Error sending close command to server: {:?}", e); - Err(Error::ErrConnectionClosed.into()) + Err(Error::ErrConnectionClosed) } } } @@ -166,7 +166,7 @@ impl DnsConn { mut close_query_signal: mpsc::Receiver<()>, ) -> Result<(ResourceHeader, SocketAddr)> { if self.is_server_closed.load(atomic::Ordering::SeqCst) { - return Err(Error::ErrConnectionClosed.into()); + return Err(Error::ErrConnectionClosed); } let name_with_suffix = name.to_owned() + "."; @@ -192,7 +192,7 @@ impl DnsConn { _ = close_query_signal.recv() => { log::info!("Query close signal received."); - return Err(Error::ErrConnectionClosed.into()) + return Err(Error::ErrConnectionClosed) }, res_opt = query_rx.recv() =>{ diff --git a/src/message/builder.rs b/src/message/builder.rs index 5ea7e01..7930c34 100644 --- a/src/message/builder.rs +++ b/src/message/builder.rs @@ -83,10 +83,10 @@ impl Builder { fn start_check(&self, section: Section) -> Result<()> { if self.section <= Section::NotStarted { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } if self.section > section { - return Err(Error::ErrSectionDone.into()); + return Err(Error::ErrSectionDone); } Ok(()) @@ -127,13 +127,13 @@ impl Builder { Section::Answers => (&mut self.header.answers, Error::ErrTooManyAnswers), Section::Authorities => (&mut self.header.authorities, Error::ErrTooManyAuthorities), Section::Additionals => (&mut self.header.additionals, Error::ErrTooManyAdditionals), - Section::NotStarted => return Err(Error::ErrNotStarted.into()), - Section::Done => return Err(Error::ErrSectionDone.into()), - Section::Header => return Err(Error::ErrSectionHeader.into()), + Section::NotStarted => return Err(Error::ErrNotStarted), + Section::Done => return Err(Error::ErrSectionDone), + Section::Header => return Err(Error::ErrSectionHeader), }; if *count == u16::MAX { - Err(err.into()) + Err(err) } else { *count += 1; Ok(()) @@ -143,10 +143,10 @@ impl Builder { // question adds a single question. pub fn add_question(&mut self, q: &Question) -> Result<()> { if self.section < Section::Questions { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } if self.section > Section::Questions { - return Err(Error::ErrSectionDone.into()); + return Err(Error::ErrSectionDone); } let msg = self.msg.take(); if let Some(mut msg) = msg { @@ -160,10 +160,10 @@ impl Builder { fn check_resource_section(&self) -> Result<()> { if self.section < Section::Answers { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } if self.section > Section::Additionals { - return Err(Error::ErrSectionDone.into()); + return Err(Error::ErrSectionDone); } Ok(()) } @@ -175,7 +175,7 @@ impl Builder { if let Some(body) = &r.body { r.header.typ = body.real_type(); } else { - return Err(Error::ErrNilResourceBody.into()); + return Err(Error::ErrNilResourceBody); } if let Some(msg) = self.msg.take() { @@ -195,7 +195,7 @@ impl Builder { // Finish ends message building and generates a binary message. pub fn finish(&mut self) -> Result> { if self.section < Section::Header { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } self.section = Section::Done; @@ -206,7 +206,7 @@ impl Builder { msg[..HEADER_LEN].copy_from_slice(&buf[..HEADER_LEN]); Ok(msg) } else { - Err(Error::ErrEmptyBuilderMsg.into()) + Err(Error::ErrEmptyBuilderMsg) } } } diff --git a/src/message/message_test.rs b/src/message/message_test.rs index e344ba3..2b5de66 100644 --- a/src/message/message_test.rs +++ b/src/message/message_test.rs @@ -44,7 +44,7 @@ fn small_test_msg() -> Result { }], additionals: vec![Resource { header: ResourceHeader { - name: name.clone(), + name: name, typ: DnsType::A, class: DNSCLASS_INET, ..Default::default() @@ -201,7 +201,7 @@ fn large_test_msg() -> Result { }, Resource { header: ResourceHeader { - name: name.clone(), + name: name, typ: DnsType::Txt, class: DNSCLASS_INET, ..Default::default() @@ -403,7 +403,7 @@ fn test_header_unpack_error() -> Result<()> { #[test] fn test_parser_start() -> Result<()> { let mut p = Parser::default(); - let result = p.start(&vec![]); + let result = p.start(&[]); assert!(result.is_err()); Ok(()) @@ -916,7 +916,7 @@ fn test_start_error() -> Result<()> { ..Default::default() } }), - Error::ErrNotStarted.into(), + Error::ErrNotStarted, ), ( "sectionDone", @@ -926,7 +926,7 @@ fn test_start_error() -> Result<()> { ..Default::default() } }), - Error::ErrSectionDone.into(), + Error::ErrSectionDone, ), ]; @@ -1056,7 +1056,7 @@ fn test_builder_resource_error() -> Result<()> { ..Default::default() } }), - Error::ErrNotStarted.into(), + Error::ErrNotStarted, ), ( "sectionHeader", @@ -1066,7 +1066,7 @@ fn test_builder_resource_error() -> Result<()> { ..Default::default() } }), - Error::ErrNotStarted.into(), + Error::ErrNotStarted, ), ( "sectionQuestions", @@ -1076,7 +1076,7 @@ fn test_builder_resource_error() -> Result<()> { ..Default::default() } }), - Error::ErrNotStarted.into(), + Error::ErrNotStarted, ), ( "sectionDone", @@ -1086,7 +1086,7 @@ fn test_builder_resource_error() -> Result<()> { ..Default::default() } }), - Error::ErrSectionDone.into(), + Error::ErrSectionDone, ), ]; diff --git a/src/message/mod.rs b/src/message/mod.rs index 1cfbba6..0462d78 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -305,16 +305,16 @@ impl Message { // pack more than 65535 of any particular type, but it is possible and // we should fail gracefully. if self.questions.len() > u16::MAX as usize { - return Err(Error::ErrTooManyQuestions.into()); + return Err(Error::ErrTooManyQuestions); } if self.answers.len() > u16::MAX as usize { - return Err(Error::ErrTooManyAnswers.into()); + return Err(Error::ErrTooManyAnswers); } if self.authorities.len() > u16::MAX as usize { - return Err(Error::ErrTooManyAuthorities.into()); + return Err(Error::ErrTooManyAuthorities); } if self.additionals.len() > u16::MAX as usize { - return Err(Error::ErrTooManyAdditionals.into()); + return Err(Error::ErrTooManyAdditionals); } let (id, bits) = self.header.pack(); diff --git a/src/message/name.rs b/src/message/name.rs index ad55cfb..932b90a 100644 --- a/src/message/name.rs +++ b/src/message/name.rs @@ -22,7 +22,7 @@ impl fmt::Display for Name { impl Name { pub fn new(data: &str) -> Result { if data.len() > NAME_LEN { - Err(Error::ErrCalcLen.into()) + Err(Error::ErrCalcLen) } else { Ok(Name { data: data.to_owned(), @@ -47,7 +47,7 @@ impl Name { // Add a trailing dot to canonicalize name. if data.is_empty() || data[data.len() - 1] != b'.' { - return Err(Error::ErrNonCanonicalName.into()); + return Err(Error::ErrNonCanonicalName); } // Allow root domain. @@ -65,12 +65,12 @@ impl Name { // It isn't allowed for segments to be long enough to // need them. if i - begin >= (1 << 6) { - return Err(Error::ErrSegTooLong.into()); + return Err(Error::ErrSegTooLong); } // Segments must have a non-zero length. if i - begin == 0 { - return Err(Error::ErrZeroSegLen.into()); + return Err(Error::ErrZeroSegLen); } msg.push((i - begin) as u8); @@ -134,7 +134,7 @@ impl Name { loop { if curr_off >= msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } let c = msg[curr_off]; curr_off += 1; @@ -147,7 +147,7 @@ impl Name { } let end_off = curr_off + c as usize; if end_off > msg.len() { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } name.push_str(String::from_utf8(msg[curr_off..end_off].to_vec())?.as_str()); name.push('.'); @@ -156,10 +156,10 @@ impl Name { 0xC0 => { // Pointer if !allow_compression { - return Err(Error::ErrCompressedSrv.into()); + return Err(Error::ErrCompressedSrv); } if curr_off >= msg.len() { - return Err(Error::ErrInvalidPtr.into()); + return Err(Error::ErrInvalidPtr); } let c1 = msg[curr_off]; curr_off += 1; @@ -169,13 +169,13 @@ impl Name { // Don't follow too many pointers, maybe there's a loop. ptr += 1; if ptr > 10 { - return Err(Error::ErrTooManyPtr.into()); + return Err(Error::ErrTooManyPtr); } curr_off = ((c ^ 0xC0) as usize) << 8 | (c1 as usize); } _ => { // Prefixes 0x80 and 0x40 are reserved. - return Err(Error::ErrReserved.into()); + return Err(Error::ErrReserved); } } } @@ -183,7 +183,7 @@ impl Name { name.push('.'); } if name.len() > NAME_LEN { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } self.data = name; if ptr == 0 { @@ -200,7 +200,7 @@ impl Name { loop { if new_off >= msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } let c = msg[new_off]; new_off += 1; @@ -213,7 +213,7 @@ impl Name { // literal string new_off += c as usize; if new_off > msg.len() { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } } 0xC0 => { @@ -227,7 +227,7 @@ impl Name { } _ => { // Prefixes 0x80 and 0x40 are reserved. - return Err(Error::ErrReserved.into()); + return Err(Error::ErrReserved); } } } diff --git a/src/message/packer.rs b/src/message/packer.rs index 68cd300..76c617c 100644 --- a/src/message/packer.rs +++ b/src/message/packer.rs @@ -10,7 +10,7 @@ pub(crate) fn pack_bytes(mut msg: Vec, field: &[u8]) -> Vec { pub(crate) fn unpack_bytes(msg: &[u8], off: usize, field: &mut [u8]) -> Result { let new_off = off + field.len(); if new_off > msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } field.copy_from_slice(&msg[off..new_off]); Ok(new_off) @@ -24,7 +24,7 @@ pub(crate) fn pack_uint16(mut msg: Vec, field: u16) -> Vec { pub(crate) fn unpack_uint16(msg: &[u8], off: usize) -> Result<(u16, usize)> { if off + UINT16LEN > msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } Ok(( @@ -35,7 +35,7 @@ pub(crate) fn unpack_uint16(msg: &[u8], off: usize) -> Result<(u16, usize)> { pub(crate) fn skip_uint16(msg: &[u8], off: usize) -> Result { if off + UINT16LEN > msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } Ok(off + UINT16LEN) } @@ -48,7 +48,7 @@ pub(crate) fn pack_uint32(mut msg: Vec, field: u32) -> Vec { pub(crate) fn unpack_uint32(msg: &[u8], off: usize) -> Result<(u32, usize)> { if off + UINT32LEN > msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } let v = (msg[off] as u32) << 24 | (msg[off + 1] as u32) << 16 @@ -59,7 +59,7 @@ pub(crate) fn unpack_uint32(msg: &[u8], off: usize) -> Result<(u32, usize)> { pub(crate) fn skip_uint32(msg: &[u8], off: usize) -> Result { if off + UINT32LEN > msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } Ok(off + UINT32LEN) } @@ -68,7 +68,7 @@ pub(crate) fn skip_uint32(msg: &[u8], off: usize) -> Result { pub(crate) fn pack_str(mut msg: Vec, field: &str) -> Result> { let l = field.len(); if l > 255 { - return Err(Error::ErrStringTooLong.into()); + return Err(Error::ErrStringTooLong); } msg.push(l as u8); msg.extend_from_slice(field.as_bytes()); @@ -77,12 +77,12 @@ pub(crate) fn pack_str(mut msg: Vec, field: &str) -> Result> { pub(crate) fn unpack_str(msg: &[u8], off: usize) -> Result<(String, usize)> { if off >= msg.len() { - return Err(Error::ErrBaseLen.into()); + return Err(Error::ErrBaseLen); } let begin_off = off + 1; let end_off = begin_off + msg[off] as usize; if end_off > msg.len() { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } Ok(( diff --git a/src/message/parser.rs b/src/message/parser.rs index 591b3cf..8b120f8 100644 --- a/src/message/parser.rs +++ b/src/message/parser.rs @@ -43,16 +43,16 @@ impl<'a> Parser<'a> { fn check_advance(&mut self, sec: Section) -> Result<()> { if self.section < sec { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } if self.section > sec { - return Err(Error::ErrSectionDone.into()); + return Err(Error::ErrSectionDone); } self.res_header_valid = false; if self.index == self.header.count(sec) as usize { self.index = 0; self.section = Section::from(1 + self.section as u8); - return Err(Error::ErrSectionDone.into()); + return Err(Error::ErrSectionDone); } Ok(()) } @@ -88,7 +88,7 @@ impl<'a> Parser<'a> { if self.res_header_valid { let new_off = self.off + self.res_header.length as usize; if new_off > self.msg.len() { - return Err(Error::ErrResourceLen.into()); + return Err(Error::ErrResourceLen); } self.off = new_off; self.res_header_valid = false; @@ -331,7 +331,7 @@ impl<'a> Parser<'a> { // method. pub fn resource_body(&mut self) -> Result> { if !self.res_header_valid { - return Err(Error::ErrNotStarted.into()); + return Err(Error::ErrNotStarted); } let (rb, _off) = unpack_resource_body( self.res_header.typ, diff --git a/src/message/resource/mod.rs b/src/message/resource/mod.rs index e644ea3..44afaf4 100644 --- a/src/message/resource/mod.rs +++ b/src/message/resource/mod.rs @@ -68,7 +68,7 @@ impl Resource { if let Some(body) = &self.body { self.header.typ = body.real_type(); } else { - return Err(Error::ErrNilResourceBody.into()); + return Err(Error::ErrNilResourceBody); } let (mut msg, len_off) = self.header.pack(msg, compression, compression_off)?; let pre_len = msg.len(); @@ -95,7 +95,7 @@ impl Resource { let (length, mut new_off) = unpack_uint16(msg, new_off)?; new_off += length as usize; if new_off > msg.len() { - return Err(Error::ErrResourceLen.into()); + return Err(Error::ErrResourceLen); } Ok(new_off) } @@ -178,7 +178,7 @@ impl ResourceHeader { // preLen is the length that msg was before the ResourceBody was packed. pub fn fix_len(&mut self, msg: &mut [u8], len_off: usize, pre_len: usize) -> Result<()> { if msg.len() < pre_len || msg.len() > pre_len + u16::MAX as usize { - return Err(Error::ErrResTooLong.into()); + return Err(Error::ErrResTooLong); } let con_len = msg.len() - pre_len; @@ -264,7 +264,7 @@ pub fn unpack_resource_body( DnsType::Aaaa => Box::new(AaaaResource::default()), DnsType::Srv => Box::new(SrvResource::default()), DnsType::Opt => Box::new(OptResource::default()), - _ => return Err(Error::ErrNilResourceBody.into()), + _ => return Err(Error::ErrNilResourceBody), }; off = rb.unpack(msg, off, length)?; diff --git a/src/message/resource/opt.rs b/src/message/resource/opt.rs index 211eaf8..3618a17 100644 --- a/src/message/resource/opt.rs +++ b/src/message/resource/opt.rs @@ -73,7 +73,7 @@ impl ResourceBody for OptResource { data: vec![0; l as usize], }; if off + l as usize > msg.len() { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } opt.data.copy_from_slice(&msg[off..off + l as usize]); off += l as usize; diff --git a/src/message/resource/txt.rs b/src/message/resource/txt.rs index 9e4cbf1..a3ae9a1 100644 --- a/src/message/resource/txt.rs +++ b/src/message/resource/txt.rs @@ -44,7 +44,7 @@ impl ResourceBody for TxtResource { off = new_off; // Check if we got too many bytes. if length < n + t.as_bytes().len() + 1 { - return Err(Error::ErrCalcLen.into()); + return Err(Error::ErrCalcLen); } n += t.len() + 1; txts.push(t); From 444c890ffd880cf71b89600d42685756427fbd54 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Fri, 8 Oct 2021 10:29:34 +0200 Subject: [PATCH 3/3] Update deps --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1011483..581eb99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ default = [ "reuse_port" ] reuse_port = [] [dependencies] -util = { package = "webrtc-util", version = "0.4.3", default-features = false, features = ["ifaces"] } +util = { package = "webrtc-util", version = "0.5.0", default-features = false, features = ["ifaces"] } tokio = { version = "1.12.0", features = ["full"] } socket2 = { version = "0.4.2", features = ["all"] } log = "0.4"