netpkt/telnet.rs#
Source: lib/netpkt/telnet.rs
telnet -- the bytes of the Telnet protocol (TCP port 23, RFC 854): commands after IAC, option
negotiation (RFC 855) without loops (the Q method of RFC 1143), ECHO (RFC 857) and
SUPPRESS-GO-AHEAD (RFC 858), IAC escaping, the line ends of the network virtual terminal, a
reader for bytes that arrive in pieces
IAC, DONT, DO, WONT, WILL, SB, GA, EL, EC, AYT, AO, IP, BRK, DM, NOP, SE, EOR commands
OPT_* (BINARY 0, ECHO 1, SGA 3, STATUS 5, TM 6, TTYPE 24, EOR 25, NAWS 31, TSPEED 32,
LFLOW 33, LINEMODE 34, XDISPLOC 35, ENVIRON 36, NEW_ENVIRON 39) options
cmd_name(c), opt_name(o) "Do", "Will", "Interrupt Process" ...; "Echo",
"Suppress Go Ahead" ... (as capture tools name them)
escape(data) IAC doubled (the bytes of a data stream)
to_nvt(text) "\n" as CR LF, a lone CR as CR NUL, IAC doubled
cmd(c) / opt(verb, o) the bytes IAC c, IAC verb o
Item Data(bytes) | Cmd(c) | Opt { verb, opt } | Sub { opt, data }
| Partial(bytes) (a sequence cut off at the end, parse only)
Reader::new(nvt) / push(piece) the items of a stream; a sequence split between two pieces
waits for the next one; nvt: CR LF becomes "\n" and CR NUL
"\r" in the data
parse(b) the items of one buffer with their offsets and lengths,
the data as it is (a dissector)
Options::new(us, him) the state of every option on both sides (RFC 1143: No, Yes,
WantNo, WantYes): received(verb, o) gives the answer to send,
if any (a request is accepted when its bit is in `us` for
what this side does, `him` for what the peer does; every
other option is refused), enable_us / disable_us /
enable_him / disable_him ask for a change, us(o) / him(o)
tell what is in effect
A data byte 255 travels as IAC IAC. An answer is only sent for a request that would change
something, so two sides that both follow these rules never negotiate in a circle.
/// interpret as command
IAC: u8 = 255
DONT: u8 = 254
DO: u8 = 253
WONT: u8 = 252
WILL: u8 = 251
/// subnegotiation begins (ends with IAC SE)
SB: u8 = 250
/// go ahead
GA: u8 = 249
/// erase line, erase character, are you there, abort output, interrupt process, break
EL: u8 = 248
EC: u8 = 247
AYT: u8 = 246
AO: u8 = 245
IP: u8 = 244
BRK: u8 = 243
/// data mark (the end of urgent data)
DM: u8 = 242
NOP: u8 = 241
/// subnegotiation end
SE: u8 = 240
/// end of record (RFC 885)
EOR: u8 = 239
OPT_BINARY: u8 = 0
OPT_ECHO: u8 = 1
OPT_SGA: u8 = 3
OPT_STATUS: u8 = 5
OPT_TM: u8 = 6
OPT_TTYPE: u8 = 24
OPT_EOR: u8 = 25
OPT_NAWS: u8 = 31
OPT_TSPEED: u8 = 32
OPT_LFLOW: u8 = 33
OPT_LINEMODE: u8 = 34
OPT_XDISPLOC: u8 = 35
OPT_ENVIRON: u8 = 36
OPT_NEW_ENVIRON: u8 = 39
/// the well-known port
PORT: u16 = 23
fn cmd_name(c: u8) -> &'static strthe name of a command bytefn opt_name(o: u8) -> Stringthe name of an optionfn escape(data: &[u8]) -> Vec<u8>data with every IAC doubledfn to_nvt(text: &[u8]) -> Vec<u8>text for the network virtual terminal: "\n" becomes CR LF, a lone CR becomes CR NUL, an IAC is doubledfn cmd(c: u8) -> [u8; 2]IAC c (GA, IP, AYT ...)fn opt(verb: u8, o: u8) -> [u8; 3]IAC verb option (WILL, WONT, DO, DONT)enum Itema piece of a Telnet streamData(Vec<u8>)-- data bytes (IAC IAC already one byte)Cmd(u8)-- a command without an option (GA, IP, AYT, NOP ...)Opt { verb: u8, opt: u8 }-- a negotiation: WILL, WONT, DO or DONT and the optionSub { opt: u8, data: Vec<u8> }-- a subnegotiation: IAC SB opt data IAC SEPartial(Vec<u8>)-- the start of a sequence cut off at the end of the buffer (parse only)
impl Item
fn describe(&self) -> String"Do Echo", "Interrupt Process", "Suboption Terminal Type" (a negotiation or a command)struct Readerreads a Telnet stream that arrives in pieces
impl Reader
fn new(nvt: bool) -> Readernvt: CR LF becomes "\n" and CR NUL "\r" (a lone CR stays one)fn push(&mut self, piece: &[u8]) -> Vec<Item>the items of the next piece; what ends inside a sequence waits for the next piecefn parse(b: &[u8]) -> Vec<(usize, usize, Item)>the items of one buffer with their offset and length, the data as it isenum Qthe state of one side of an option (RFC 1143)NoYesWantNo-- we asked to switch it off and wait for the answerWantYes-- we asked to switch it on and wait for the answer
struct Optionsthe options of one end of a connection: what this side does (us) and what the peer does (him)allow_us: u64-- the options this side agrees to do / lets the peer do (bit o for option o < 64)allow_him: u64
impl Options
fn new(us: &[u8], him: &[u8]) -> Optionswhat this side agrees to do (us) and to let the peer do (him); the others are refusedfn us(&self, o: u8) -> boolthis side does option ofn him(&self, o: u8) -> boolthe peer does option ofn state(&self, o: u8) -> (Q, Q)the state of both sides (us, him)fn received(&mut self, verb: u8, o: u8) -> Option<[u8; 3]>a negotiation from the peer: the answer to send, if any (RFC 1143 2.6, 2.7)fn enable_us(&mut self, o: u8) -> Option<[u8; 3]>this side offers to do option o (WILL), unless it does or asked alreadyfn disable_us(&mut self, o: u8) -> Option<[u8; 3]>this side stops doing option o (WONT)fn enable_him(&mut self, o: u8) -> Option<[u8; 3]>this side asks the peer to do option o (DO)fn disable_him(&mut self, o: u8) -> Option<[u8; 3]>this side asks the peer to stop option o (DONT)