11
|
1 /*
|
|
2 * 510ConnectBot
|
|
3 * Copyright 2014 Carl Byington
|
|
4 *
|
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
6 * you may not use this file except in compliance with the License.
|
|
7 * You may obtain a copy of the License at
|
|
8 *
|
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
|
10 *
|
|
11 * Unless required by applicable law or agreed to in writing, software
|
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14 * See the License for the specific language governing permissions and
|
|
15 * limitations under the License.
|
|
16 */
|
|
17
|
|
18 package com.five_ten_sg.connectbot.transport;
|
|
19
|
|
20 import java.io.IOException;
|
|
21 import java.io.InputStream;
|
|
22 import java.io.OutputStream;
|
|
23 import java.net.Socket;
|
|
24 import java.net.SocketException;
|
13
|
25 import java.net.UnknownHostException;
|
11
|
26 import java.util.List;
|
|
27 import java.util.Map;
|
13
|
28 import java.util.regex.Matcher;
|
|
29 import java.util.regex.Pattern;
|
11
|
30
|
12
|
31 import org.tn5250j.framework.tn5250.Screen5250;
|
|
32 import org.tn5250j.framework.tn5250.tnvt;
|
|
33
|
13
|
34 import com.five_ten_sg.connectbot.R;
|
11
|
35 import com.five_ten_sg.connectbot.bean.HostBean;
|
|
36 import com.five_ten_sg.connectbot.bean.PortForwardBean;
|
|
37 import com.five_ten_sg.connectbot.service.TerminalBridge;
|
|
38 import com.five_ten_sg.connectbot.service.TerminalManager;
|
|
39 import com.five_ten_sg.connectbot.util.HostDatabase;
|
|
40
|
|
41 import android.content.Context;
|
|
42 import android.net.Uri;
|
13
|
43 import android.util.Log;
|
11
|
44
|
|
45
|
|
46 /**
|
|
47 * @author Carl Byington
|
|
48 *
|
|
49 */
|
|
50 public class TN5250 extends AbsTransport {
|
|
51 private static final String PROTOCOL = "tn5250";
|
|
52 private static final String TAG = "ConnectBot.tn5250";
|
|
53 private static final int DEFAULT_PORT = 23;
|
|
54 private Screen5250 screen52;
|
|
55
|
|
56 private tnvt handler = null;
|
|
57 private Socket socket;
|
|
58
|
|
59 private InputStream is;
|
|
60 private OutputStream os;
|
|
61 private int width;
|
|
62 private int height;
|
|
63
|
|
64 private boolean connected = false;
|
|
65
|
12
|
66 static final Pattern hostmask;
|
|
67 static {
|
|
68 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE);
|
|
69 }
|
|
70
|
|
71
|
11
|
72 public TN5250() {
|
|
73 super();
|
|
74 }
|
|
75
|
|
76 public TN5250(HostBean host, TerminalBridge bridge, TerminalManager manager) {
|
|
77 super(host, bridge, manager);
|
|
78 handler = new tnvt(screen52, true, false, bridge, manager);
|
|
79 }
|
|
80
|
|
81
|
|
82 /**
|
|
83 * @return protocol part of the URI
|
|
84 */
|
|
85 public static String getProtocolName() {
|
|
86 return PROTOCOL;
|
|
87 }
|
|
88
|
12
|
89 public Uri getUri(String input) {
|
|
90 Matcher matcher = hostmask.matcher(input);
|
|
91
|
|
92 if (!matcher.matches())
|
|
93 return null;
|
|
94
|
|
95 StringBuilder sb = new StringBuilder();
|
|
96 sb.append(PROTOCOL)
|
|
97 .append("://")
|
|
98 .append(matcher.group(1));
|
|
99 String portString = matcher.group(3);
|
|
100 int port = DEFAULT_PORT;
|
|
101
|
|
102 if (portString != null) {
|
|
103 try {
|
|
104 port = Integer.parseInt(portString);
|
|
105
|
|
106 if (port < 1 || port > 65535) {
|
|
107 port = DEFAULT_PORT;
|
|
108 }
|
|
109 }
|
|
110 catch (NumberFormatException nfe) {
|
|
111 // Keep the default port
|
|
112 }
|
|
113 }
|
|
114
|
|
115 if (port != DEFAULT_PORT) {
|
|
116 sb.append(':');
|
|
117 sb.append(port);
|
|
118 }
|
|
119
|
|
120 sb.append("/#")
|
|
121 .append(Uri.encode(input));
|
|
122 Uri uri = Uri.parse(sb.toString());
|
|
123 return uri;
|
11
|
124 }
|
|
125
|
|
126 /**
|
|
127 * Causes transport to connect to the target host. After connecting but before a
|
|
128 * session is started, must call back to {@link TerminalBridge#onConnected()}.
|
|
129 * After that call a session may be opened.
|
|
130 */
|
|
131 @Override
|
|
132 public void connect() {
|
23
|
133 connected = handler.connect(host.getHostname(), host.getPort());
|
|
134 if (connected) {
|
13
|
135 is = handler.bin;
|
|
136 os = handler.bout;
|
11
|
137 bridge.onConnected();
|
|
138 }
|
|
139 }
|
|
140
|
|
141
|
|
142 /**
|
|
143 * Checks if read() will block. If there are no bytes remaining in
|
|
144 * the underlying transport, return true.
|
|
145 */
|
|
146 @Override
|
|
147 public boolean willBlock() {
|
|
148 if (is == null) return true;
|
|
149 try {
|
|
150 return is.available() == 0;
|
|
151 } catch (Exception e) {
|
|
152 return true;
|
|
153 }
|
|
154 }
|
|
155
|
|
156 /**
|
|
157 * Reads from the transport. Transport must support reading into a byte array
|
|
158 * <code>buffer</code> at the start of <code>offset</code> and a maximum of
|
|
159 * <code>length</code> bytes. If the remote host disconnects, throw an
|
|
160 * {@link IOException}.
|
|
161 * @param buffer byte buffer to store read bytes into
|
|
162 * @param offset where to start writing in the buffer
|
|
163 * @param length maximum number of bytes to read
|
|
164 * @return number of bytes read
|
|
165 * @throws IOException when remote host disconnects
|
|
166 */
|
|
167 public int read(byte[] buffer, int offset, int length) throws IOException {
|
|
168 return 0;
|
|
169 }
|
|
170
|
|
171
|
|
172 /**
|
|
173 * Writes to the transport. If the host is not yet connected, simply return without
|
|
174 * doing anything. An {@link IOException} should be thrown if there is an error after
|
|
175 * connection.
|
|
176 * @param buffer bytes to write to transport
|
|
177 * @throws IOException when there is a problem writing after connection
|
|
178 */
|
|
179 public void write(byte[] buffer) throws IOException {
|
|
180 }
|
|
181
|
|
182 /**
|
|
183 * Writes to the transport. See {@link #write(byte[])} for behavior details.
|
|
184 * @param c character to write to the transport
|
|
185 * @throws IOException when there is a problem writing after connection
|
|
186 */
|
|
187 public void write(int c) throws IOException {
|
|
188 }
|
|
189
|
|
190 /**
|
|
191 * Flushes the write commands to the transport.
|
|
192 * @throws IOException when there is a problem writing after connection
|
|
193 */
|
|
194 public void flush() throws IOException {
|
|
195 }
|
|
196
|
|
197 /**
|
|
198 * Closes the connection to the terminal. Note that the resulting failure to read
|
|
199 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
|
|
200 */
|
|
201 public void close() {
|
13
|
202 handler.disconnect();
|
11
|
203 connected = false;
|
|
204 }
|
|
205
|
|
206 /**
|
|
207 * Tells the transport what dimensions the display is currently
|
|
208 * @param columns columns of text
|
|
209 * @param rows rows of text
|
|
210 * @param width width in pixels
|
|
211 * @param height height in pixels
|
|
212 */
|
|
213 @Override
|
|
214 public void setDimensions(int columns, int rows, int width, int height) {
|
|
215 // do nothing
|
|
216 }
|
|
217
|
|
218 public void setOptions(Map<String, String> options) {
|
|
219 // do nothing
|
|
220 }
|
|
221
|
|
222 public Map<String, String> getOptions() {
|
|
223 return null;
|
|
224 }
|
|
225
|
|
226 public void setCompression(boolean compression) {
|
|
227 // do nothing
|
|
228 }
|
|
229
|
|
230 public void setHttpproxy(String httpproxy) {
|
|
231 // do nothing
|
|
232 }
|
|
233
|
|
234 public void setUseAuthAgent(String useAuthAgent) {
|
|
235 // do nothing
|
|
236 }
|
|
237
|
|
238 public void setEmulation(String emulation) {
|
|
239 this.emulation = emulation;
|
|
240 }
|
|
241
|
|
242 public String getEmulation() {
|
|
243 return emulation;
|
|
244 }
|
|
245
|
|
246 public void setHost(HostBean host) {
|
|
247 this.host = host;
|
|
248 }
|
|
249
|
|
250 public void setBridge(TerminalBridge bridge) {
|
|
251 this.bridge = bridge;
|
|
252 }
|
|
253
|
|
254 public void setManager(TerminalManager manager) {
|
|
255 this.manager = manager;
|
|
256 }
|
|
257
|
|
258 /**
|
|
259 * Whether or not this transport type can forward ports.
|
|
260 * @return true on ability to forward ports
|
|
261 */
|
|
262 public boolean canForwardPorts() {
|
|
263 return false;
|
|
264 }
|
|
265
|
|
266 /**
|
|
267 * Adds the {@link PortForwardBean} to the list.
|
|
268 * @param portForward the port forward bean to add
|
|
269 * @return true on successful addition
|
|
270 */
|
|
271 public boolean addPortForward(PortForwardBean portForward) {
|
|
272 return false;
|
|
273 }
|
|
274
|
|
275 /**
|
|
276 * Enables a port forward member. After calling this method, the port forward should
|
|
277 * be operational iff it could be enabled by the transport.
|
|
278 * @param portForward member of our current port forwards list to enable
|
|
279 * @return true on successful port forward setup
|
|
280 */
|
|
281 public boolean enablePortForward(PortForwardBean portForward) {
|
|
282 return false;
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Disables a port forward member. After calling this method, the port forward should
|
|
287 * be non-functioning iff it could be disabled by the transport.
|
|
288 * @param portForward member of our current port forwards list to enable
|
|
289 * @return true on successful port forward tear-down
|
|
290 */
|
|
291 public boolean disablePortForward(PortForwardBean portForward) {
|
|
292 return false;
|
|
293 }
|
|
294
|
|
295 /**
|
|
296 * Removes the {@link PortForwardBean} from the available port forwards.
|
|
297 * @param portForward the port forward bean to remove
|
|
298 * @return true on successful removal
|
|
299 */
|
|
300 public boolean removePortForward(PortForwardBean portForward) {
|
|
301 return false;
|
|
302 }
|
|
303
|
|
304 /**
|
|
305 * Gets a list of the {@link PortForwardBean} currently used by this transport.
|
|
306 * @return the list of port forwards
|
|
307 */
|
|
308 public List<PortForwardBean> getPortForwards() {
|
|
309 return null;
|
|
310 }
|
|
311
|
|
312 /**
|
|
313 * Whether or not this transport type can transfer files.
|
|
314 * @return true on ability to transfer files
|
|
315 */
|
|
316 public boolean canTransferFiles() {
|
|
317 return false;
|
|
318 }
|
|
319
|
|
320 /**
|
|
321 * Downloads the specified remote file to a local folder.
|
|
322 * @param remoteFile The path to the remote file to be downloaded. Must be non-null.
|
|
323 * @param localFolder The path to local folder. Null = default external storage folder.
|
|
324 * @return true on success, false on failure
|
|
325 */
|
|
326 public boolean downloadFile(String remoteFile, String localFolder) {
|
|
327 return false;
|
|
328 }
|
|
329
|
|
330 /**
|
|
331 * Uploads the specified local file to the remote host.
|
|
332 * @param localFile The path to the local file to be uploaded. Must be non-null.
|
|
333 * @param remoteFolder The path to the remote directory. Null == default remote directory.
|
|
334 * @return true on success, false on failure
|
|
335 */
|
|
336 public boolean uploadFile(String localFile, String remoteFile,
|
|
337 String remoteFolder, String mode) {
|
|
338 return false;
|
|
339 }
|
|
340
|
|
341 @Override
|
|
342 public int getDefaultPort() {
|
|
343 return DEFAULT_PORT;
|
|
344 }
|
|
345
|
|
346 @Override
|
|
347 public boolean isConnected() {
|
|
348 return connected;
|
|
349 }
|
|
350
|
|
351 @Override
|
|
352 public boolean isSessionOpen() {
|
|
353 return connected;
|
|
354 }
|
|
355
|
|
356 @Override
|
|
357 public boolean isAuthenticated() {
|
|
358 return connected;
|
|
359 }
|
|
360
|
|
361
|
|
362 @Override
|
|
363 public String getDefaultNickname(String username, String hostname, int port) {
|
|
364 if (port == DEFAULT_PORT) {
|
|
365 return String.format("%s", hostname);
|
|
366 }
|
|
367 else {
|
|
368 return String.format("%s:%d", hostname, port);
|
|
369 }
|
|
370 }
|
|
371
|
|
372 @Override
|
|
373 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
|
|
374 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
|
|
375 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
|
|
376 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
|
|
377 int port = uri.getPort();
|
|
378
|
|
379 if (port < 0)
|
|
380 port = DEFAULT_PORT;
|
|
381
|
|
382 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
|
|
383 }
|
|
384
|
|
385
|
|
386 @Override
|
|
387 public HostBean createHost(Uri uri) {
|
|
388 HostBean host = new HostBean();
|
|
389 host.setProtocol(PROTOCOL);
|
|
390 host.setHostname(uri.getHost());
|
|
391 int port = uri.getPort();
|
|
392
|
|
393 if (port < 0)
|
|
394 port = DEFAULT_PORT;
|
|
395
|
|
396 host.setPort(port);
|
|
397 String nickname = uri.getFragment();
|
|
398
|
|
399 if (nickname == null || nickname.length() == 0) {
|
|
400 host.setNickname(getDefaultNickname(host.getUsername(),
|
|
401 host.getHostname(), host.getPort()));
|
|
402 }
|
|
403 else {
|
|
404 host.setNickname(uri.getFragment());
|
|
405 }
|
|
406
|
|
407 return host;
|
|
408 }
|
|
409
|
|
410
|
12
|
411 public String getFormatHint(Context context) {
|
11
|
412 return String.format("%s:%s",
|
|
413 context.getString(R.string.format_hostname),
|
|
414 context.getString(R.string.format_port));
|
|
415 }
|
|
416
|
|
417
|
|
418 @Override
|
|
419 public boolean usesNetwork() {
|
|
420 return true;
|
|
421 }
|
|
422 }
|