正しい実装の例
TELNET 実装を書く負担を軽減するため、著者はここに、本 RFC に適合する TELNET 実装が考えうる各状況で返しうる応答の正確な例を示す。本 RFC に適合するすべての TELNET 実装は、ここに示す手順に従うべきである(SHOULD)。
EXAMPLE STATE MACHINE
FOR THE Q METHOD OF IMPLEMENTING TELNET OPTION NEGOTIATION
There are two sides, we (us) and he (him). We keep four
variables:
us: state of option on our side (NO/WANTNO/WANTYES/YES)
usq: a queue bit (EMPTY/OPPOSITE) if us is WANTNO or WANTYES
him: state of option on his side
himq: a queue bit if him is WANTNO or WANTYES
An option is enabled if and only if its state is YES. Note that
us/usq and him/himq could be combined into two six-choice states.
"Error" below means that producing diagnostic information may be a
good idea, though it isn't required.
Upon receipt of WILL, we choose based upon him and himq:
NO If we agree that he should enable, him=YES, send
DO; otherwise, send DONT.
YES Ignore.
WANTNO EMPTY Error: DONT answered by WILL. him=NO.
OPPOSITE Error: DONT answered by WILL. him=YES*,
himq=EMPTY.
WANTYES EMPTY him=YES.
OPPOSITE him=WANTNO, himq=EMPTY, send DONT.
* This behavior is debatable; DONT will never be answered by WILL
over a reliable connection between TELNETs compliant with this
RFC, so this was chosen (1) not to generate further messages,
because if we know we're dealing with a noncompliant TELNET we
shouldn't trust it to be sensible; (2) to empty the queue
sensibly.
Upon receipt of WONT, we choose based upon him and himq:
NO Ignore.
YES him=NO, send DONT.
WANTNO EMPTY him=NO.
OPPOSITE him=WANTYES, himq=NONE, send DO.
WANTYES EMPTY him=NO.*
OPPOSITE him=NO, himq=NONE.**
* Here is the only spot a length-two queue could be useful; after
a WILL negotiation was refused, a queue of WONT WILL would mean
to request the option again. This seems of too little utility
and too much potential waste; there is little chance that the
other side will change its mind immediately.
** Here we don't have to generate another request because we've
been "refused into" the correct state anyway.
If we decide to ask him to enable:
NO him=WANTYES, send DO.
YES Error: Already enabled.
WANTNO EMPTY If we are queueing requests, himq=OPPOSITE;
otherwise, Error: Cannot initiate new request
in the middle of negotiation.
OPPOSITE Error: Already queued an enable request.
WANTYES EMPTY Error: Already negotiating for enable.
OPPOSITE himq=EMPTY.
If we decide to ask him to disable:
NO Error: Already disabled.
YES him=WANTNO, send DONT.
WANTNO EMPTY Error: Already negotiating for disable.
OPPOSITE himq=EMPTY.
WANTYES EMPTY If we are queueing requests, himq=OPPOSITE;
otherwise, Error: Cannot initiate new request
in the middle of negotiation.
OPPOSITE Error: Already queued a disable request.
We handle the option on our side by the same procedures, with DO-
WILL, DONT-WONT, him-us, himq-usq swapped.