1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.xbean.terminal.telnet;
18
19 import java.io.FilterInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23
24 public class TelnetInputStream extends FilterInputStream implements TelnetCodes {
25
26 private TelnetOption[] options = new TelnetOption[256];
27 private OutputStream out = null;
28
29
30
31
32
33
34
35
36
37
38 public TelnetInputStream(InputStream in, OutputStream out) throws IOException {
39 super(in);
40 this.out = out;
41 negotiateOption(DONT, 1);
42 negotiateOption(DONT, 6);
43 negotiateOption(DONT, 24);
44 negotiateOption(DONT, 33);
45 negotiateOption(DONT, 34);
46 }
47
48 public int read() throws IOException {
49 int b = super.read();
50 if (b == IAC) {
51
52
53 processCommand();
54
55
56
57 b = this.read();
58 }
59
60 return b;
61 }
62
63
64
65
66
67
68
69 private void processCommand() throws IOException {
70
71 print("C: IAC ");
72 int command = super.read();
73 switch (command) {
74 case WILL:
75 senderWillEnableOption(super.read());
76 break;
77 case DO:
78 pleaseDoEnableOption(super.read());
79 break;
80 case WONT:
81 senderWontEnableOption(super.read());
82 break;
83 case DONT:
84 pleaseDontEnableOption(super.read());
85 break;
86 default:
87 unimplementedCommand(command);
88 break;
89 }
90 }
91
92 private void unimplementedCommand(int command) {
93 println(command + ": command not found");
94 }
95
96
97
98
99
100
101
102
103
104
105 private void senderWillEnableOption(int optionID) throws IOException {
106
107 println("WILL " + optionID);
108 TelnetOption option = getOption(optionID);
109 if (option.hasBeenNegotiated()) return;
110 if (option.isInNegotiation()) {
111 option.enable();
112 } else if (!option.isInNegotiation() && option.isSupported()) {
113 negotiateOption(DO, optionID);
114 option.enable();
115 } else if (!option.isInNegotiation() && !option.isSupported()) {
116 negotiateOption(DONT, optionID);
117 option.disable();
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 private void pleaseDoEnableOption(int optionID) throws IOException {
133
134 println("DO " + optionID);
135 TelnetOption option = getOption(optionID);
136 if (option.hasBeenNegotiated()) return;
137 if (option.isInNegotiation()) {
138 option.enable();
139 } else if (!option.isInNegotiation() && option.isSupported()) {
140 negotiateOption(WILL, optionID);
141 option.enable();
142 } else if (!option.isInNegotiation() && !option.isSupported()) {
143 negotiateOption(WONT, optionID);
144 option.disable();
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 private void senderWontEnableOption(int optionID) throws IOException {
161 println("WONT " + optionID);
162 TelnetOption option = getOption(optionID);
163 if (option.hasBeenNegotiated()) return;
164 if (!option.isInNegotiation()) {
165 negotiateOption(DONT, optionID);
166 }
167 option.disable();
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 private void pleaseDontEnableOption(int optionID) throws IOException {
182
183 println("DONT " + optionID);
184 TelnetOption option = getOption(optionID);
185 if (option.hasBeenNegotiated()) return;
186 if (!option.isInNegotiation()) {
187 negotiateOption(WONT, optionID);
188 }
189 option.disable();
190 }
191
192
193 private void println(String s) {
194
195 }
196
197
198 private void print(String s) {
199
200 }
201
202
203
204
205
206
207
208
209 private void negotiateOption(int negotiate, int optionID)
210 throws IOException {
211 TelnetOption option = getOption(optionID);
212 option.isInNegotiation(true);
213 String n = null;
214 switch (negotiate) {
215 case WILL:
216 n = "WILL ";
217 break;
218 case DO:
219 n = "DO ";
220 break;
221 case WONT:
222 n = "WONT ";
223 break;
224 case DONT:
225 n = "DONT ";
226 break;
227 }
228
229 println("S: IAC " + n + optionID);
230 synchronized (out) {
231 out.write(IAC);
232 out.write(negotiate);
233 out.write(optionID);
234 }
235 }
236
237 private TelnetOption getOption(int optionID) {
238 TelnetOption opt = options[optionID];
239 if (opt == null) {
240 opt = new TelnetOption(optionID);
241 options[optionID] = opt;
242 }
243 return opt;
244 }
245 }