Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8f875f3d0063ca639d2ee6a1db22533a11b2826e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
 * Copyright (c) 2010, 2017 SAP AG and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation  
 *     IBM Corporation - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.console.telnet;

import org.eclipse.equinox.console.common.ConsoleInputStream;
import org.eclipse.equinox.console.common.ConsoleOutputStream;
import org.eclipse.equinox.console.common.KEYS;
import org.eclipse.equinox.console.common.Scanner;
import org.eclipse.equinox.console.common.terminal.TerminalTypeMappings;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * This class performs the processing of the telnet commands,
 * and updates respectively what is displayed in the output. Also, it performs 
 * terminal type negotiation with the telnet client. This is important for some of the escape sequences, 
 * which are different for the different terminal types. Without such negotiation,
 * some keys (such as backspace, del, insert, home, etc.) may not be correctly
 * interpreted by the telnet server. Currently the supported terminal types are
 * ANSI, VT100, VT220, VT320, XTERM and SCO. The support is limited to the following 
 * keys - BACKSPACE, DEL, INSERT, HOME, END, PAGEUP, PAGEDOWN, ARROWS.
 */
public class TelnetInputScanner extends Scanner {

    private boolean isCommand = false;
    private boolean isReadingTtype = false; 
    private boolean shouldFinish = false;
    private boolean tTypeNegotiationStarted = false;
    private int lastRead = -1;
    private ArrayList<Integer> currentTerminalType = new ArrayList<>();
    private ArrayList<Integer> lastTerminalType = null;
    private Set<String> supportedTerminalTypes = new HashSet<>();
    private Callback callback;

    public TelnetInputScanner(ConsoleInputStream toShell, ConsoleOutputStream toTelnet, Callback callback) {
        super(toShell, toTelnet);
        initializeSupportedTerminalTypes();
        TerminalTypeMappings currentMapping = supportedEscapeSequences.get(DEFAULT_TTYPE);
    	currentEscapesToKey = currentMapping.getEscapesToKey();
    	escapes = currentMapping.getEscapes();
    	setBackspace(currentMapping.getBackspace());
    	setDel(currentMapping.getDel());
    	this.callback = callback;
    }
    
    private void initializeSupportedTerminalTypes() {
        supportedTerminalTypes.add("ANSI");
        supportedTerminalTypes.add("VT100");
        supportedTerminalTypes.add("VT220");
        supportedTerminalTypes.add("VT320");
        supportedTerminalTypes.add("XTERM");
        supportedTerminalTypes.add("SCO");     
    }

    @Override
	public void scan(int b) throws IOException {
        b &= 0xFF;

        if (isEsc) {
            scanEsc(b);
        } else if (isCommand) {
            scanCommand(b);
        } else if (b == IAC) {
            startCommand();
        } else {
        	switch (b) {
                case ESC:
                    startEsc();
                    toShell.add(new byte[]{(byte) b});
                    break;
                default:
                    if (b >= SPACE && b < MAX_CHAR) {
                        echo((byte) b);
                        flush();
                    }
                    toShell.add(new byte[]{(byte) b});
            }

        }
        lastRead = b;
    }

    /* Telnet command codes are described in RFC 854, TELNET PROTOCOL SPECIFICATION
     * available at http://www.ietf.org/rfc/rfc854.txt
     * 
     * Telnet terminal type negotiation option is described in RFC 1091, Telnet Terminal-Type Option
     * available at http://www.ietf.org/rfc/rfc1091.txt
     */
    private static final int SE = 240;
    private static final int EC = 247;
    private static final int EL = 248;
    private static final int SB = 250;
    private static final int WILL = 251;
    private static final int WILL_NOT = 252;
    private static final int DO = 253;
    private static final int DO_NOT = 254;
    private static final int TTYPE = 24;
    private static final int SEND = 1;
    private static final int IAC = 255;
    private static final int IS = 0;

    private boolean isNegotiation;
    private boolean isWill;
    
    private byte[] tTypeRequest = {(byte)IAC, (byte)SB, (byte)TTYPE, (byte)SEND, (byte)IAC, (byte)SE};

    private void scanCommand(final int b) throws IOException {
        if (isNegotiation) {
            scanNegotiation(b);
        } else if (isWill) {
            isWill = false;
            isCommand = false;
            if(b == TTYPE && tTypeNegotiationStarted == false) {
        		sendRequest();
        	}
        } else {
            switch (b) {
                case WILL:
                	isWill = true;
                	break;
                case WILL_NOT:
                	break;
                case DO:
                	break;
                case DO_NOT:
                    break;
                case SB:
                    isNegotiation = true;
                    break;
                case EC:
                    eraseChar();
                    isCommand = false;
                    break;
                case EL:
                default:
                    isCommand = false;
                    break;
            }
        }
    }

    private void scanNegotiation(final int b) {
    	if (lastRead == SB && b == TTYPE) {
    		isReadingTtype = true;
    	} else if (b == IS) {
    		
    	} else if (b == IAC) {
    		
    	} else if (b == SE) {
            isNegotiation = false;
            isCommand = false;
            if (isReadingTtype == true) {
				isReadingTtype = false;
				if (shouldFinish == true) {
					setCurrentTerminalType();
					shouldFinish = false;
					return;
				}
				boolean isMatch = isTerminalTypeSupported();
				boolean isLast = isLast();
				if (isMatch == true) {
					setCurrentTerminalType();
					return;
				}
				lastTerminalType = currentTerminalType;
				currentTerminalType = new ArrayList<>();
				if (isLast == true && isMatch == false) {
					shouldFinish = true;
					sendRequest();
				} else if (isLast == false && isMatch == false) {
					sendRequest();
				}
			}
        } else if (isReadingTtype == true){
        	currentTerminalType.add(b);
        }
    }
    
    private boolean isTerminalTypeSupported() {
    	byte[] tmp = new byte[currentTerminalType.size()];
    	int idx = 0;
    	for(Integer i : currentTerminalType) {
    		tmp[idx] = i.byteValue();
    		idx++;
    	}
    	String tType = new String(tmp);
    	
    	for(String terminal : supportedTerminalTypes) {
    		if(tType.toUpperCase().contains(terminal)) {
    			return true;
    		}
    	}
    	
    	return false;
    }
    
    private boolean isLast() {
    	if(currentTerminalType.equals(lastTerminalType)) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    private void setCurrentTerminalType() {
    	byte[] tmp = new byte[currentTerminalType.size()];
    	int idx = 0;
    	for(Integer i : currentTerminalType) {
    		tmp[idx] = i.byteValue();
    		idx++;
    	}
    	String tType = new String(tmp);
    	String term = null;
    	for(String terminal : supportedTerminalTypes) {
    		if(tType.toUpperCase().contains(terminal)) {
    			term = terminal;
    		}
    	}
    	TerminalTypeMappings currentMapping = supportedEscapeSequences.get(term);
    	if(currentMapping == null) {
    		currentMapping = supportedEscapeSequences.get(DEFAULT_TTYPE);
    	}
    	currentEscapesToKey = currentMapping.getEscapesToKey();
    	escapes = currentMapping.getEscapes();
    	setBackspace(currentMapping.getBackspace());
    	setDel(currentMapping.getDel());
    	if(callback != null) {
    		callback.finished();
    	}
    }
    
    private void sendRequest() {
    	try {
			toTelnet.write(tTypeRequest);
			toTelnet.flush();
			if(tTypeNegotiationStarted == false) {
				tTypeNegotiationStarted = true;
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
    }

    private void startCommand() {
        isCommand = true;
        isNegotiation = false;
        isWill = false;
    }

    private void eraseChar() throws IOException {
        toShell.add(new byte[]{BS});
    }

    @Override
	protected void scanEsc(int b) throws IOException {
        esc += (char) b;
        toShell.add(new byte[]{(byte) b});
        KEYS key = checkEscape(esc);
        if (key == KEYS.UNFINISHED) {
            return;
        }
        if (key == KEYS.UNKNOWN) {
            isEsc = false;
            scan(b);
            return;
        }
        isEsc = false;
    }

}

Back to the top