1 package org.apache.commons.net.telnet;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedInputStream;
58 import java.io.BufferedOutputStream;
59 import java.io.IOException;
60 import org.apache.commons.net.SocketClient;
61
62 /***
63 * @author Daniel F. Savarese
64 */
65
66 class Telnet extends SocketClient
67 {
68 static final boolean debug = /*true;*/ false;
69
70 static final byte[] _COMMAND_DO = {
71 (byte)TelnetCommand.IAC, (byte)TelnetCommand.DO
72 };
73
74 static final byte[] _COMMAND_DONT = {
75 (byte)TelnetCommand.IAC, (byte)TelnetCommand.DONT
76 };
77
78 static final byte[] _COMMAND_WILL = {
79 (byte)TelnetCommand.IAC, (byte)TelnetCommand.WILL
80 };
81
82 static final byte[] _COMMAND_WONT = {
83 (byte)TelnetCommand.IAC, (byte)TelnetCommand.WONT
84 };
85
86 static final byte[] _COMMAND_SB = {
87 (byte)TelnetCommand.IAC, (byte)TelnetCommand.SB
88 };
89
90 static final byte[] _COMMAND_SE = {
91 (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE
92 };
93
94 static final int _WILL_MASK = 0x01, _DO_MASK = 0x02,
95 _REQUESTED_WILL_MASK = 0x04, _REQUESTED_DO_MASK = 0x08;
96
97 /* public */
98 static final int DEFAULT_PORT = 23;
99
100 int[] _doResponse, _willResponse, _options;
101
102 /* public */
103 Telnet()
104 {
105 setDefaultPort(DEFAULT_PORT);
106 _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
107 _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
108 _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];
109 }
110
111
112 boolean _stateIsWill(int option)
113 {
114 return ((_options[option] & _WILL_MASK) != 0);
115 }
116
117 boolean _stateIsWont(int option)
118 {
119 return !_stateIsWill(option);
120 }
121
122 boolean _stateIsDo(int option)
123 {
124 return ((_options[option] & _DO_MASK) != 0);
125 }
126
127 boolean _stateIsDont(int option)
128 {
129 return !_stateIsDo(option);
130 }
131
132 boolean _requestedWill(int option)
133 {
134 return ((_options[option] & _REQUESTED_WILL_MASK) != 0);
135 }
136
137 boolean _requestedWont(int option)
138 {
139 return !_requestedWill(option);
140 }
141
142 boolean _requestedDo(int option)
143 {
144 return ((_options[option] & _REQUESTED_DO_MASK) != 0);
145 }
146
147 boolean _requestedDont(int option)
148 {
149 return !_requestedDo(option);
150 }
151
152 void _setWill(int option)
153 {
154 _options[option] |= _WILL_MASK;
155 }
156 void _setDo(int option)
157 {
158 _options[option] |= _DO_MASK;
159 }
160 void _setWantWill(int option)
161 {
162 _options[option] |= _REQUESTED_WILL_MASK;
163 }
164 void _setWantDo(int option)
165 {
166 _options[option] |= _REQUESTED_DO_MASK;
167 }
168
169 void _setWont(int option)
170 {
171 _options[option] &= ~_WILL_MASK;
172 }
173 void _setDont(int option)
174 {
175 _options[option] &= ~_DO_MASK;
176 }
177 void _setWantWont(int option)
178 {
179 _options[option] &= ~_REQUESTED_WILL_MASK;
180 }
181 void _setWantDont(int option)
182 {
183 _options[option] &= ~_REQUESTED_DO_MASK;
184 }
185
186 void _processDo(int option) throws IOException
187 {
188 boolean acceptNewState = false;
189
190 if (_willResponse[option] > 0)
191 {
192 --_willResponse[option];
193 if (_willResponse[option] > 0 && _stateIsWill(option))
194 --_willResponse[option];
195 }
196
197 if (_willResponse[option] == 0)
198 {
199 if (_requestedWont(option))
200 {
201
202 switch (option)
203 {
204
205 default:
206 break;
207
208 }
209
210
211 if (acceptNewState)
212 {
213 _setWantWill(option);
214 _sendWill(option);
215 }
216 else
217 {
218 ++_willResponse[option];
219 _sendWont(option);
220 }
221 }
222 else
223 {
224 // Other end has acknowledged option.
225
226 switch (option)
227 {
228
229 default:
230 break;
231
232 }
233
234 }
235 }
236
237 _setWill(option);
238 }
239
240
241 void _processDont(int option) throws IOException
242 {
243 if (_willResponse[option] > 0)
244 {
245 --_willResponse[option];
246 if (_willResponse[option] > 0 && _stateIsWont(option))
247 --_willResponse[option];
248 }
249
250 if (_willResponse[option] == 0 && _requestedWill(option))
251 {
252
253 switch (option)
254 {
255
256 default:
257 break;
258
259 }
260
261 _setWantWont(option);
262
263 if (_stateIsWill(option))
264 _sendWont(option);
265 }
266
267 _setWont(option);
268 }
269
270
271 void _processWill(int option) throws IOException
272 {
273 boolean acceptNewState = false;
274
275 if (_doResponse[option] > 0)
276 {
277 --_doResponse[option];
278 if (_doResponse[option] > 0 && _stateIsDo(option))
279 --_doResponse[option];
280 }
281
282 if (_doResponse[option] == 0 && _requestedDont(option))
283 {
284
285 switch (option)
286 {
287
288 default:
289 break;
290
291 }
292
293
294 if (acceptNewState)
295 {
296 _setWantDo(option);
297 _sendDo(option);
298 }
299 else
300 {
301 ++_doResponse[option];
302 _sendDont(option);
303 }
304 }
305
306 _setDo(option);
307 }
308
309
310 void _processWont(int option) throws IOException
311 {
312 if (_doResponse[option] > 0)
313 {
314 --_doResponse[option];
315 if (_doResponse[option] > 0 && _stateIsDont(option))
316 --_doResponse[option];
317 }
318
319 if (_doResponse[option] == 0 && _requestedDo(option))
320 {
321
322 switch (option)
323 {
324
325 default:
326 break;
327
328 }
329
330 _setWantDont(option);
331
332 if (_stateIsDo(option))
333 _sendDont(option);
334 }
335
336 _setDont(option);
337 }
338
339
340 protected void _connectAction_() throws IOException
341 {
342 super._connectAction_();
343 _input_ = new BufferedInputStream(_input_);
344 _output_ = new BufferedOutputStream(_output_);
345 }
346
347
348 final synchronized void _sendDo(int option)
349 throws IOException
350 {
351 if (debug)
352 System.err.println("DO: " + TelnetOption.getOption(option));
353 _output_.write(_COMMAND_DO);
354 _output_.write(option);
355 }
356
357 final synchronized void _requestDo(int option)
358 throws IOException
359 {
360 if ((_doResponse[option] == 0 && _stateIsDo(option)) ||
361 _requestedDo(option))
362 return ;
363 _setWantDo(option);
364 ++_doResponse[option];
365 _sendDo(option);
366 }
367
368 final synchronized void _sendDont(int option)
369 throws IOException
370 {
371 if (debug)
372 System.err.println("DONT: " + TelnetOption.getOption(option));
373 _output_.write(_COMMAND_DONT);
374 _output_.write(option);
375 }
376
377 final synchronized void _requestDont(int option)
378 throws IOException
379 {
380 if ((_doResponse[option] == 0 && _stateIsDont(option)) ||
381 _requestedDont(option))
382 return ;
383 _setWantDont(option);
384 ++_doResponse[option];
385 _sendDont(option);
386 }
387
388
389 final synchronized void _sendWill(int option)
390 throws IOException
391 {
392 if (debug)
393 System.err.println("WILL: " + TelnetOption.getOption(option));
394 _output_.write(_COMMAND_WILL);
395 _output_.write(option);
396 }
397
398 final synchronized void _requestWill(int option)
399 throws IOException
400 {
401 if ((_willResponse[option] == 0 && _stateIsWill(option)) ||
402 _requestedWill(option))
403 return ;
404 _setWantWill(option);
405 ++_doResponse[option];
406 _sendWill(option);
407 }
408
409 final synchronized void _sendWont(int option)
410 throws IOException
411 {
412 if (debug)
413 System.err.println("WONT: " + TelnetOption.getOption(option));
414 _output_.write(_COMMAND_WONT);
415 _output_.write(option);
416 }
417
418 final synchronized void _requestWont(int option)
419 throws IOException
420 {
421 if ((_willResponse[option] == 0 && _stateIsWont(option)) ||
422 _requestedWont(option))
423 return ;
424 _setWantWont(option);
425 ++_doResponse[option];
426 _sendWont(option);
427 }
428
429 final synchronized void _sendByte(int b)
430 throws IOException
431 {
432 _output_.write(b);
433 }
434 }
This page was automatically generated by Maven