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 strthe name of an opcode as capture tools show itfn error_name(code: u16) -> &'static strthe meaning of an error code (RFC 1350 appendix, RFC 2347)fn mode_known(mode: &str) -> boola 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 CRenum Badwhy bytes are no TFTP packetShort-- shorter than its opcode and fixed fieldsOpcode(u16)-- an opcode RFC 1350 and 2347 do not defineUnterminated-- a string field without its NULOddOptions-- an option name without a value
type Span = (usize, usize)a NUL-terminated string field: its offset, its length without the NULstruct 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 hasfn bytes(&self) -> &'a [u8]fn opcode(&self) -> u16fn block(&self) -> u16the block number of DATA and ACKfn data(&self) -> &'a [u8]the data of a DATA packetfn error_code(&self) -> u16the code of an ERRORfn error_msg(&self) -> Stringthe 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 packetfn text(&self, s: Span) -> Stringa field as textenum Packeta 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) -> Packetfn parse(b: &[u8]) -> Result<Packet, Bad>bytes as a packetfn error_of(code: u16) -> Packetan ERROR with the meaning of its code as the messagefn opcode(&self) -> u16fn 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]) -> boolthe 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