JFLib

netpkt/tftp.rs#

Source: lib/netpkt/tftp.rs

tftp -- Trivial File Transfer Protocol packets (UDP port 69, RFC 1350) with the option
extension of RFC 2347 (OACK, error 8) and the options blksize (RFC 2348), timeout and tsize
(RFC 2349)

  TftpView::parse(b)        the opcode and the fields of each kind: file / mode / options
                            (RRQ, WRQ, OACK as NUL-terminated strings with their offsets),
                            block and data (DATA), block (ACK), code and message (ERROR)
  Packet                    Rrq / Wrq { file, mode, opts } | Data { block, data } |
                            Ack { block } | Error { code, msg } | Oack { opts }:
                            read(view) / parse(b) / to_bytes / opcode / opt(name)
  Packet::error_of(code)    an ERROR with the RFC's text of the code
  OP_* (1 .. 6), ERR_* (0 .. 8), op_name, error_name, BLOCK (512), BLKSIZE_MIN / _MAX,
  mode_known ("octet", "netascii", "mail"), to_netascii / from_netascii (RFC 764 line ends)
  looks_like(b)             the shape of a packet of a running transfer (DATA, ACK, ERROR,
                            OACK): RFC 1350 moves a transfer to new ports at once, so a
                            dissector without state knows it only by its bytes

A transfer is lock-step: every DATA block waits for its ACK, a block shorter than the block
size ends it (an empty one when the size is a multiple), block numbers count from 1 and roll
over after 65535.  Option names compare case-insensitively; values are decimal text.  Errors
of parse name what is wrong for a dissector (Bad).
OP_RRQ: u16 = 1
OP_WRQ: u16 = 2
OP_DATA: u16 = 3
OP_ACK: u16 = 4
OP_ERROR: u16 = 5
/// option acknowledgment (RFC 2347)
OP_OACK: u16 = 6
/// opcode and block number (DATA, ACK) or error code (ERROR)
TFTP_HDR: usize = 4
/// data bytes in a block without the blksize option
BLOCK: usize = 512
/// the block sizes RFC 2348 allows
BLKSIZE_MIN: u16 = 8
BLKSIZE_MAX: u16 = 65464
/// "Not defined, see error message (if any)."
ERR_UNDEFINED: u16 = 0
ERR_NOT_FOUND: u16 = 1
ERR_ACCESS: u16 = 2
ERR_DISK_FULL: u16 = 3
ERR_ILLEGAL: u16 = 4
ERR_UNKNOWN_TID: u16 = 5
ERR_EXISTS: u16 = 6
ERR_NO_USER: u16 = 7
/// option negotiation failed (RFC 2347)
ERR_OPTION: u16 = 8
  • fn op_name(op: u16) -> &'static str the name of an opcode as capture tools show it

  • fn error_name(code: u16) -> &'static str the meaning of an error code (RFC 1350 appendix, RFC 2347)

  • fn mode_known(mode: &str) -> bool a transfer mode RFC 1350 defines (case-insensitive)

  • fn to_netascii(data: &[u8]) -> Vec<u8> bytes as netascii: LF becomes CR LF, a lone CR becomes CR NUL (RFC 764)

  • fn from_netascii(data: &[u8]) -> Vec<u8> netascii back to bytes: CR LF is LF, CR NUL is CR

  • enum Bad why bytes are no TFTP packet

    • Short -- shorter than its opcode and fixed fields
    • Opcode(u16) -- an opcode RFC 1350 and 2347 do not define
    • Unterminated -- a string field without its NUL
    • OddOptions -- an option name without a value
  • type Span = (usize, usize) a NUL-terminated string field: its offset, its length without the NUL

  • struct TftpView<'a> a received TFTP packet

impl<'a> TftpView<'a>

  • fn parse(b: &'a [u8]) -> Result<TftpView<'a>, Bad> at least an opcode RFC 1350 or 2347 defines and the fields its kind has

  • fn bytes(&self) -> &'a [u8]

  • fn opcode(&self) -> u16

  • fn block(&self) -> u16 the block number of DATA and ACK

  • fn data(&self) -> &'a [u8] the data of a DATA packet

  • fn error_code(&self) -> u16 the code of an ERROR

  • fn error_msg(&self) -> String the message of an ERROR (up to its NUL)

  • fn fields(&self) -> Vec<Span> the string fields after the opcode (RRQ, WRQ: file, mode, then option pairs; OACK: the pairs), offsets relative to the packet

  • fn text(&self, s: Span) -> String a field as text

  • enum Packet a TFTP packet to write (or one read)

    • Rrq { file: String, mode: String, opts: Vec<(String, String)> }
    • Wrq { file: String, mode: String, opts: Vec<(String, String)> }
    • Data { block: u16, data: Vec<u8> }
    • Ack { block: u16 }
    • Error { code: u16, msg: String }
    • Oack { opts: Vec<(String, String)> }

impl Packet

  • fn read(v: &TftpView) -> Packet

  • fn parse(b: &[u8]) -> Result<Packet, Bad> bytes as a packet

  • fn error_of(code: u16) -> Packet an ERROR with the meaning of its code as the message

  • fn opcode(&self) -> u16

  • fn opt(&self, name: &str) -> Option<&str> an option of a request or an OACK (the name ignores case)

  • fn to_bytes(&self) -> Vec<u8>

  • fn looks_like(b: &[u8]) -> bool the bytes of a packet of a running transfer: a DATA block (at most BLKSIZE_MAX bytes), an ACK of exactly four bytes, an ERROR with a known code and a printable message, an OACK of option names and values

Source: docs/API.md in the JFLib repository.