package telnet; @commands = qw(xEOF SUSP ABORT EOR SE NOP DM BREAK IP AO AYT EC EL GA SB WILL WONT DO DONT); @options = qw(BINARY ECHO RCP SGA NAMS STATUS TM RCTE NAOL NAOP NAOCRD NAOHTS NAUHTD NAOFFD NAOVTS NAOVTD NAOLFD XASCII LOGOUT BM DET SUPDUP SUPDUPOUTPUT SNDLOC TTYP EOR TUID OUTMRK TTYLOC 3270REGIME X2PAD NAWS TSPEED LFLOW LINEMODE XDISPLIC ENVIRON AUTHENTICATION); $state = "NORM"; sub get_command { if (defined $commands[$_[0] - 255]) { return $commands[$_[0] - 255]; } else { return sprintf("[0x%02X]", $_[0]); } } sub get_option { if (defined $options[$_[0]]) { return $options[$_[0]]; } else { return sprintf("[0x%02X]", $_[0]); } } sub parse { my $c = ord($_[0]); if ($state eq "NORM") { if ($c == 255) { $state = "IAC"; } return undef; } elsif ($state eq "IAC") { $com = get_command($c); if ($com =~ /WILL|WONT|DO|DONT/o) { $state = $com; return undef; } if ($c == 255) { return undef; } else { return "IAC $com\n"; } } elsif ($state =~ /WILL|WONT|DO|DONT/o) { $ret = "IAC $state " . get_option($c) . "\n"; $state = "NORM"; return $ret; } else { warn "unknown state, $state"; } } 1;