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

Improve API robustness and remove public dependencies #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 1 addition & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum SnmpError {
#[error("Nom error")]
NomError(ErrorKind),
#[error("BER error")]
BerError(Error),
BerError(#[from] Error),
}

impl<I> ParseError<I> for SnmpError {
Expand All @@ -33,12 +33,6 @@ impl<I> ParseError<I> for SnmpError {
}
}

impl From<Error> for SnmpError {
fn from(e: Error) -> SnmpError {
SnmpError::BerError(e)
}
}

impl From<SnmpError> for nom::Err<SnmpError> {
fn from(e: SnmpError) -> nom::Err<SnmpError> {
nom::Err::Error(e)
Expand Down
9 changes: 7 additions & 2 deletions src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::snmp::*;
use crate::snmpv3::*;
use asn1_rs::{Any, FromBer, Tag};
use nom::combinator::map_res;
use nom::{Err, IResult};
use nom::{Err, Finish, IResult};

#[derive(Debug, PartialEq)]
pub enum SnmpGenericMessage<'a> {
Expand Down Expand Up @@ -47,7 +47,12 @@ fn parse_snmp_v3_pdu_content(i: &[u8]) -> IResult<&[u8], SnmpV3Message, SnmpErro
Ok((i, msg))
}

pub fn parse_snmp_generic_message(i: &[u8]) -> IResult<&[u8], SnmpGenericMessage, SnmpError> {
/// Parse any valid SNMP message.
pub fn parse_snmp_generic_message(i: &[u8]) -> Result<(&[u8], SnmpGenericMessage), SnmpError> {
internal_parse_snmp_generic_message(i).finish()
}

fn internal_parse_snmp_generic_message(i: &[u8]) -> IResult<&[u8], SnmpGenericMessage, SnmpError> {
let (rem, any) = Any::from_ber(i).or(Err(Err::Error(SnmpError::InvalidMessage)))?;
if any.tag() != Tag::Sequence {
return Err(Err::Error(SnmpError::InvalidMessage));
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
unreachable_pub
)]
#![forbid(unsafe_code)]
#![deny(broken_intra_doc_links)]
#![deny(rustdoc::broken_intra_doc_links)]
#![doc(test(
no_crate_inject,
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
Expand All @@ -48,3 +48,6 @@ pub mod snmpv3;
pub use generic::*;
pub use snmp::*;
pub use snmpv3::*;

// re-exports to prevent public dependency on asn1_rs
pub use asn1_rs::{Oid, OidParseError};
19 changes: 13 additions & 6 deletions src/snmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
//! - [RFC3416](https://tools.ietf.org/html/rfc3416): SNMP v2
//! - [RFC2570](https://tools.ietf.org/html/rfc2570): Introduction to SNMP v3

use crate::error::SnmpError;
use crate::{error::SnmpError, Oid};
use asn1_rs::{
Any, BitString, Class, Error, FromBer, Header, Implicit, Integer, Oid, Sequence, Tag,
TaggedValue,
Any, BitString, Class, Error, FromBer, Header, Implicit, Integer, Sequence, Tag, TaggedValue,
};
use nom::combinator::map;
use nom::{Err, IResult};
use nom::{Err, Finish, IResult};
use std::convert::TryFrom;
use std::net::Ipv4Addr;
use std::slice::Iter;
Expand Down Expand Up @@ -535,7 +534,11 @@ fn parse_snmp_v1_trap_pdu(i: &[u8]) -> IResult<&[u8], SnmpPdu, SnmpError> {
/// }
/// # }
/// ```
pub fn parse_snmp_v1(bytes: &[u8]) -> IResult<&[u8], SnmpMessage, SnmpError> {
pub fn parse_snmp_v1(bytes: &[u8]) -> Result<(&[u8], SnmpMessage), SnmpError> {
internal_parse_snmp_v1(bytes).finish()
}

fn internal_parse_snmp_v1(bytes: &[u8]) -> IResult<&[u8], SnmpMessage, SnmpError> {
Sequence::from_der_and_then(bytes, |i| {
let (i, version) = u32::from_ber(i).map_err(Err::convert)?;
if version != 0 {
Expand Down Expand Up @@ -589,7 +592,11 @@ pub(crate) fn parse_snmp_v1_pdu(i: &[u8]) -> IResult<&[u8], SnmpPdu, SnmpError>
/// ANY
/// }
/// </pre>
pub fn parse_snmp_v2c(bytes: &[u8]) -> IResult<&[u8], SnmpMessage, SnmpError> {
pub fn parse_snmp_v2c(bytes: &[u8]) -> Result<(&[u8], SnmpMessage), SnmpError> {
internal_parse_snmp_v2c(bytes).finish()
}

fn internal_parse_snmp_v2c(bytes: &[u8]) -> IResult<&[u8], SnmpMessage, SnmpError> {
Sequence::from_der_and_then(bytes, |i| {
let (i, version) = u32::from_ber(i).map_err(Err::convert)?;
if version != 1 {
Expand Down
8 changes: 6 additions & 2 deletions src/snmpv3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use asn1_rs::{Error, FromBer, Sequence};
use nom::combinator::{map, map_res};
use nom::{Err, IResult};
use nom::{Err, Finish, IResult};
use std::fmt;

use crate::error::SnmpError;
Expand Down Expand Up @@ -167,7 +167,11 @@ pub(crate) fn parse_secp<'a>(
/// }
/// # }
/// ```
pub fn parse_snmp_v3(bytes: &[u8]) -> IResult<&[u8], SnmpV3Message, SnmpError> {
pub fn parse_snmp_v3(bytes: &[u8]) -> Result<(&[u8], SnmpV3Message), SnmpError> {
internal_parse_snmp_v3(bytes).finish()
}

fn internal_parse_snmp_v3(bytes: &[u8]) -> IResult<&[u8], SnmpV3Message, SnmpError> {
Sequence::from_der_and_then(bytes, |i| {
let (i, version) = u32::from_ber(i).map_err(Err::convert)?;
let (i, header_data) = parse_snmp_v3_headerdata(i)?;
Expand Down
12 changes: 10 additions & 2 deletions src/usm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::parse_ber_octetstring_as_str;
use asn1_rs::{Error, FromBer, Sequence};
use nom::IResult;
use nom::{Finish, IResult};

#[derive(Debug, PartialEq)]
pub struct UsmSecurityParameters<'a> {
Expand All @@ -14,7 +14,15 @@ pub struct UsmSecurityParameters<'a> {
pub msg_privacy_parameters: &'a [u8],
}

pub fn parse_usm_security_parameters(bytes: &[u8]) -> IResult<&[u8], UsmSecurityParameters, Error> {
pub fn parse_usm_security_parameters(
bytes: &[u8],
) -> Result<(&[u8], UsmSecurityParameters), Error> {
internal_parse_usm_security_parameters(bytes).finish()
}

fn internal_parse_usm_security_parameters(
bytes: &[u8],
) -> IResult<&[u8], UsmSecurityParameters, Error> {
Sequence::from_der_and_then(bytes, |i| {
let (i, msg_authoritative_engine_id) = <&[u8]>::from_ber(i)?;
let (i, msg_authoritative_engine_boots) = u32::from_ber(i)?;
Expand Down
2 changes: 0 additions & 2 deletions tests/v1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#[macro_use]
extern crate hex_literal;
extern crate nom;
extern crate snmp_parser;

use asn1_rs::Oid;
use snmp_parser::*;
use std::net::Ipv4Addr;

Expand Down
2 changes: 0 additions & 2 deletions tests/v2c.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#[macro_use]
extern crate pretty_assertions;
extern crate nom;
extern crate snmp_parser;

use asn1_rs::Oid;
use snmp_parser::*;

static SNMPV2_GET: &[u8] = include_bytes!("../assets/snmpv2c-get-response.bin");
Expand Down
1 change: 0 additions & 1 deletion tests/v3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[macro_use]
extern crate pretty_assertions;

extern crate nom;
extern crate snmp_parser;

use snmp_parser::*;
Expand Down
Loading