Expand description

A DNS protocol parser. Given bytes and a sawp::parser::Direction, it will attempt to parse the bytes and return a Message. The parser will inform the caller about what went wrong if no message is returned (see sawp::parser::Parse for details on possible return types).

The following protocol references were used to create this module:

RFC1035 RFC1123 RFC2065 RFC2505 RFC2535 RFC2845 RFC2930 RFC3655 RFC4255 RFC4408 RFC4635 RFC5001 RFC6742 RFC6891 RFC6975 RFC7314 RFC7828 RFC7830 RFC7871 RFC7873 RFC7901 RFC8145 RFC8764 RFC8914 Cisco - Identifying DNS Traffic Draft DNSOP Zone Digest Draft DNSOP SVCB Draft EDNS Tags Eastlake Kitchen Sink NIMROD DNS Wijngaard’s DNS Parameters


use sawp::parser::{Direction, Parse};
use sawp::error::Error;
use sawp::error::ErrorKind;
use sawp_dns::{Dns, Message};

fn parse_bytes(input: &[u8]) -> std::result::Result<&[u8], Error> {
    let dns = Dns {};
    let mut bytes = input;
    while bytes.len() > 0 {
        // If we know that this is a request or response, change the Direction
        // for a more accurate parsing
        match dns.parse(bytes, Direction::Unknown) {
            // The parser succeeded and returned the remaining bytes and the parsed DNS message
            Ok((rest, Some(message))) => {
                println!("Dns message: {:?}", message);
                bytes = rest;
            // The parser recognized that this might be DNS and made some progress,
            // but more bytes are needed
            Ok((rest, None)) => return Ok(rest),
            // The parser was unable to determine whether this was DNS or not and more
            // bytes are needed
            Err(Error { kind: ErrorKind::Incomplete(_) }) => return Ok(bytes),
            // The parser determined that this was not DNS
            Err(e) => return Err(e)




Breakdown of the parsed dns bytes


Error flags raised while parsing DNS - to be used in the returned Message


Future: replace with nom’s count when we migrate to a version with FnMut combinators.