Revised Telnet byte macro option
obsoletes
- rfc-729 — Telnet byte macro option
Extracted elements (21)
IAC interpretation is intentionally not overridden during BM subcommand processing so that IAC SE will continue to safely terminate malformed subcommands, preserving the robustness of the framing mechanism.
Macro bytes are recommended to be drawn from the Telnet data space between decimal 128 and decimal 254, since this range is not normally used except by obsolete original Telnet implementations. This minimizes conflicts with normal NVT data.
The BM option is motivated as a simple data compression technique. Standard Telnet subcommands require a minimum of five characters overhead (IAC SB <option> IAC SE). For block-oriented data with blocks as small as five or ten bytes, the majority of cross-network traffic would otherwise be control overhead; BM removes this burden by substituting single bytes for longer strings.
The <count> field in the DEFINE subcommand is included specifically to allow the receiver to pre-allocate storage for the replacement string before receiving it.
A macro byte can be defined to expand into itself (IAC SB BM DEFINE X <1> X IAC SE) to negate its macro effect. Sending IAC SB BM DEFINE X <0> IAC SE causes X to be silently ignored in the data stream.
The sender of WILL BM (the data sender) has exclusive control over macro definitions; the receiver (sender of DO BM) has no control over definition or use except for the ability to refuse particular definitions via REFUSE or PLEASE CANCEL.
An IAC occurring within a replacement string must be doubled so that it will be interpreted later as an IAC; to be later interpreted as data byte 255, it must be quadrupled in the original DEFINE replacement string specification.
Expanded replacement strings are NOT subject to reinterpretation, preventing recursive macro definitions. Telnet commands may be included in replacement strings but must be either totally contained within the macro or begin within it and terminate outside; they may not begin outside and continue inside.
Macro replacement must occur before any other Telnet interpretation, unless the macro byte occurs as part of a Telnet command, in which case no replacement is to be made.
The LITERAL subcommand cannot be used during Telnet subcommands, since Telnet subcommands are defined to end with the next occurrence of IAC SE; including a BM LITERAL subcommand inside another subcommand would prematurely terminate the containing subcommand.
The sender of WILL BM is prohibited from using or redefining a macro byte until it has received an ACCEPT, REFUSE, or DONT BM in reply to the corresponding DEFINE subcommand.
The Telnet IAC character (decimal 255) may appear in a replacement string but is the ONLY character that may NOT be defined as a macro byte.
Upon receipt of WON'T BM or DON'T BM, any existing BM macro byte definitions are discarded (reset to their original data interpretations).
When skipping data due to Telnet SYNCH (INS/DM) processing, BM macro replacement should still take place, since 'IAC DM' is a valid replacement string.
Within any Telnet command sequence beginning with IAC, macro replacement may NOT take place. Characters in such sequences are to be interpreted only as their normal values. This includes the entire extent of a Telnet subnegotiation (IAC SB through IAC SE).
Four BM subnegotiation subcommands are defined: DEFINE (1) to propose a macro mapping; ACCEPT (2) to confirm a definition; REFUSE (3) to reject a definition with a reason code; LITERAL (4) to treat a macro byte as real data rather than its replacement string.
The Byte Macro (BM) Telnet option is assigned option code 19. It allows a sender to define single data bytes that are to be interpreted as if a replacement string of NVT characters or commands had been sent.
The PLEASE CANCEL subcommand (IAC SB BM <PLEASE CANCEL> <macro byte> <REASON> IAC SE) allows the receiver of a defined macro byte to request the sender cancel its definition. The sender should (but is not required to) respond by redefining the macro byte to expand into itself.
Option negotiation follows standard Telnet option mechanics: default state is WON'T BM / DON'T BM (no reinterpretation). Transition to active state requires WILL BM + DO BM exchange. WON'T BM or DON'T BM at any point returns to default and discards all macro definitions. If a receiver insists on canceling a macro it cannot refuse, it may send DONT BM, await WONT BM, then restart with DO BM, resetting all macros.
The DEFINE subcommand has the form: IAC SB BM <DEFINE=1> <macro byte> <count> <replacement string> IAC SE. The <count> field is one 8-bit binary byte indicating how many replacement string characters follow (not counting the trailing IAC SE); doubled IACs in the string count as one character per pair.
The REFUSE subcommand has the form: IAC SB BM <REFUSE=3> <macro byte> <REASON> IAC SE. REASON values are: BAD-CHOICE=1, TOO-LONG=2, WRONG-LENGTH=3, OTHER-REASON=0.