From 5aab76ba9f24af59f87c141e0dc78b922243fb9c Mon Sep 17 00:00:00 2001 From: Tyler Hamilton Date: Tue, 25 Apr 2023 14:52:15 -0700 Subject: [PATCH] Process command sequences before other processing Changes the order of operations in preprocess so the valid sequence IAC DO OPT_BINARY is correctly interpreted as a command sequence with Binmode=false. As suboption negotiation may contain any byte sequence all other processing is done after the command sequences are handled. --- lib/net/telnet.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/net/telnet.rb b/lib/net/telnet.rb index 43764f2..e797454 100644 --- a/lib/net/telnet.rb +++ b/lib/net/telnet.rb @@ -429,15 +429,8 @@ def binmode=(mode) # method yourself if you have read input directly using sysread() # or similar, and even then only if in telnet mode. def preprocess(string) - # combine CR+NULL into CR - string = string.gsub(/#{CR}#{NULL}/no, CR) if @options["Telnetmode"] - - # combine EOL into "\n" - string = string.gsub(/#{EOL}/no, "\n") unless @options["Binmode"] - - # remove NULL - string = string.gsub(/#{NULL}/no, '') unless @options["Binmode"] - + # Handle command sequences first as they may contain characters + # which would otherwise be processed string.gsub(/#{IAC}( [#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]| [#{DO}#{DONT}#{WILL}#{WONT}] @@ -486,6 +479,15 @@ def preprocess(string) '' end end + + # combine CR+NULL into CR + string = string.gsub(/#{CR}#{NULL}/no, CR) if @options["Telnetmode"] + + # combine EOL into "\n" + string = string.gsub(/#{EOL}/no, "\n") unless @options["Binmode"] + + # remove NULL + string = string.gsub(/#{NULL}/no, '') unless @options["Binmode"] end # preprocess # Read data from the host until a certain sequence is matched.