1 45 package org.openejb.server.telnet; 46 47 import java.io.FilterInputStream ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.OutputStream ; 51 52 55 public class TelnetInputStream extends FilterInputStream implements TelnetCodes{ 56 57 private TelnetOption[] options = new TelnetOption[256]; 59 60 private OutputStream out = null; 61 62 71 public TelnetInputStream(InputStream in, OutputStream out) throws IOException { 72 super(in); 73 this.out = out; 74 negotiateOption(DONT, 1); 75 negotiateOption(DONT, 6); 76 negotiateOption(DONT, 24); 77 negotiateOption(DONT, 33); 78 negotiateOption(DONT, 34); 79 } 80 81 82 public int read() throws IOException { 83 int b = super.read(); 84 85 if (b == IAC) { 86 processCommand(); 89 b = this.read(); 93 } 94 95 return b; 97 } 98 99 106 private void processCommand() throws IOException { 107 print("C: IAC "); 109 110 int command = super.read(); 111 112 switch ( command ) { 113 case WILL: senderWillEnableOption(super.read()); break; 114 case DO: pleaseDoEnableOption(super.read()); break; 115 case WONT: senderWontEnableOption(super.read()); break; 116 case DONT: pleaseDontEnableOption(super.read()); break; 117 default: unimplementedCommand(command); break; 118 } 119 120 } 121 122 private void unimplementedCommand(int command){ 123 println(command+": command not found"); 124 } 125 126 135 private void senderWillEnableOption(int optionID) throws IOException { 136 println("WILL "+optionID); 138 TelnetOption option = getOption(optionID); 139 140 if ( option.hasBeenNegotiated() ) return; 141 142 if ( option.isInNegotiation() ) { 143 option.enable(); 144 } else if ( !option.isInNegotiation() && option.isSupported() ) { 145 negotiateOption(DO, optionID); 146 option.enable(); 147 } else if ( !option.isInNegotiation() && !option.isSupported() ) { 148 negotiateOption(DONT, optionID); 149 option.disable(); 150 } 151 } 152 153 164 private void pleaseDoEnableOption(int optionID) throws IOException { 165 println("DO "+optionID); 167 TelnetOption option = getOption(optionID); 168 169 if ( option.hasBeenNegotiated() ) return; 170 171 if ( option.isInNegotiation() ) { 172 option.enable(); 173 } else if ( !option.isInNegotiation() && option.isSupported() ) { 174 negotiateOption(WILL, optionID); 175 option.enable(); 176 } else if ( !option.isInNegotiation() && !option.isSupported() ) { 177 negotiateOption(WONT, optionID); 178 option.disable(); 179 } 180 } 181 182 194 private void senderWontEnableOption(int optionID) throws IOException { 195 println("WONT "+optionID); 196 TelnetOption option = getOption(optionID); 197 198 if ( option.hasBeenNegotiated() ) return; 199 200 if ( !option.isInNegotiation() ) { 201 negotiateOption(DONT, optionID); 202 } 203 option.disable(); 204 } 205 206 217 private void pleaseDontEnableOption(int optionID) throws IOException { 218 println("DONT "+optionID); 220 221 TelnetOption option = getOption(optionID); 222 223 if ( option.hasBeenNegotiated() ) return; 224 225 if ( !option.isInNegotiation() ) { 226 negotiateOption(WONT, optionID); 227 } 228 option.disable(); 229 } 230 231 232 233 private void println(String s){ 235 } 237 238 private void print(String s){ 240 } 242 243 250 private void negotiateOption(int negotiate, int optionID) 251 throws IOException { 252 TelnetOption option = getOption(optionID); 253 option.inNegotiation = true; 254 255 String n = null; 256 switch (negotiate) { 257 case WILL : 258 n = "WILL "; 259 break; 260 case DO : 261 n = "DO "; 262 break; 263 case WONT : 264 n = "WONT "; 265 break; 266 case DONT : 267 n = "DONT "; 268 break; 269 } 270 271 println("S: IAC " + n + optionID); 273 274 synchronized (out) { 275 out.write(IAC); 276 out.write(negotiate); 277 out.write(optionID); 278 } 279 } 280 281 private TelnetOption getOption(int optionID) { 282 TelnetOption opt = options[optionID]; 283 if (opt == null) { 284 opt = new TelnetOption(optionID); 285 options[optionID] = opt; 286 } 287 return opt; 288 } 289 290 291 } 292 | Popular Tags |