Crate sawp_tftp

source ·
Expand description

A TFTP 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). TFTP ignores Direction.

The following protocol references were used to create this module:

TFTP Protocol (Revision 2)

Example

use sawp::parser::{Direction, Parse};
use sawp::error::Error;
use sawp::error::ErrorKind;
use sawp_tftp::{TFTP, Message};

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

    Ok(bytes)
}

Structs

Enums

  • The error code is an integer indicating the nature of the error.
  • The TFTP header of a packet contains the opcode associated with that packet. TFTP supports five types of packets
  • Represents the various types of TFTP Packets