JFLib

netpkt/ftp.rs#

Source: lib/netpkt/ftp.rs

ftp -- the control connection of the File Transfer Protocol (TCP port 21, RFC 959): command
lines, replies with their multi-line form, the host-port of PORT and of the 227 reply to PASV,
EPRT and the 229 reply to EPSV (RFC 2428), FEAT's list (RFC 2389)

  Command { verb, arg }     Command::parse(line) (the verb in capitals, the argument as
                            typed), Command::new(verb, arg), to_line() ("USER anna\r\n"; CR,
                            LF and NUL of the fields left out)
  Reply { code, lines }     to_bytes(): one line "220 text", or "211-first" ... "211 last"
                            (the inner lines start with a space when they could look like
                            the end); is_final / is_ok (1xx preliminary, 2xx done, 3xx more)
  Lines                     cuts the bytes of a control connection into lines (CRLF, a lone
                            LF too), in any pieces as they arrive
  ReplyReader               the replies of a server from its bytes: push(bytes) -> Vec<Reply>
                            (a multi-line reply ends with a line of the same code and a space)
  host_port(text) / fmt_host_port(ip, port)   "h1,h2,h3,h4,p1,p2" (PORT's argument, 227's
                            numbers: host_port finds them anywhere in a reply's text)
  eprt(arg) / fmt_eprt(addr, port)            "|1|10.0.0.5|40001|", "|2|2001:db8::5|40001|"
  epsv_port(text) / fmt_epsv(port)            the "(|||port|)" of a 229 reply
  PORT_FTP (21), PORT_FTP_DATA (20), verb_known

Lines are UTF-8 or ASCII; RFC 959 allows Telnet sequences, which nobody sends here.  The
reply texts are the server's own words: clients go by the codes and the numbers of 227 and
229 only.
PORT_FTP: u16 = 21
/// the server's port of active data connections
PORT_FTP_DATA: u16 = 20
  • fn verb_known(v: &str) -> bool the commands of RFC 959, 2389, 2428 and 3659 a dissector names

  • struct Command a command line of a client

    • verb: String -- in capitals
    • arg: String -- everything after the first space (may hold spaces: a file name)

impl Command

  • fn new(verb: &str, arg: &str) -> Command

  • fn parse(line: &str) -> Option<Command> a line without its line end; None for an empty one

  • fn to_line(&self) -> String the line on the wire, CRLF included; CR, LF and NUL of verb and argument are left out (they would end the line and start another command)

  • struct Reply a reply of a server

    • code: u16
    • lines: Vec<String> -- the text of each line (without the code of the first and the last)

impl Reply

  • fn new(code: u16, text: &str) -> Reply

  • fn text(&self) -> &str the text of the first line

  • fn is_preliminary(&self) -> bool 1xx: more replies follow for this command

  • fn is_ok(&self) -> bool 2xx: done

  • fn is_intermediate(&self) -> bool 3xx: the command waits for another (PASS after USER)

  • fn to_bytes(&self) -> Vec<u8> the reply as bytes: "code text\r\n", or the multi-line form of RFC 959 4.2

  • struct Lines cuts a byte stream into lines

impl Lines

  • fn new() -> Lines

  • fn push(&mut self, b: &[u8]) -> Vec<String> the complete lines in b and what came before it, without their line ends

  • fn pending(&self) -> usize bytes waiting for their line end

  • struct ReplyReader the replies of a server, as its bytes arrive

impl ReplyReader

  • fn new() -> ReplyReader

  • fn push(&mut self, b: &[u8]) -> Vec<Reply> the replies that are complete now; a line that is no reply is skipped

  • fn host_port(text: &str) -> Option<(Ipv4, u16)> "h1,h2,h3,h4,p1,p2" anywhere in a text (PORT's argument; a 227 reply, where clients look for the six numbers whatever the words around them)

  • fn fmt_host_port(ip: Ipv4, port: u16) -> String the six numbers of an IPv4 address and a port

  • fn eprt(arg: &str) -> Option<(IpAddr, u16)> EPRT's argument: a delimiter, the family (1 IPv4, 2 IPv6), the address and the port

  • fn fmt_eprt(addr: IpAddr, port: u16) -> String EPRT's argument for an address and a port

  • fn epsv_port(text: &str) -> Option<u16> the port of a 229 reply: "(|||port|)" in its text (any delimiter of RFC 2428)

  • fn fmt_epsv(port: u16) -> String "(|||port|)" for a 229 reply

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