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() {
|
|
133 try {
|
13
|
134 connected = handler.connect(host.getHostname(), host.getPort());
|
|
135 is = handler.bin;
|
|
136 os = handler.bout;
|
11
|
137 bridge.onConnected();
|
|
138 }
|
|
139 catch (UnknownHostException e) {
|
|
140 Log.d(TAG, "IO Exception connecting to host", e);
|
|
141 }
|
|
142 catch (IOException e) {
|
|
143 Log.d(TAG, "IO Exception connecting to host", e);
|
|
144 }
|
|
145 }
|
|
146
|
|
147
|
|
148 /**
|
|
149 * Checks if read() will block. If there are no bytes remaining in
|
|
150 * the underlying transport, return true.
|
|
151 */
|
|
152 @Override
|
|
153 public boolean willBlock() {
|
|
154 if (is == null) return true;
|
|
155 try {
|
|
156 return is.available() == 0;
|
|
157 } catch (Exception e) {
|
|
158 return true;
|
|
159 }
|
|
160 }
|
|
161
|
|
162 /**
|
|
163 * Reads from the transport. Transport must support reading into a byte array
|
|
164 * <code>buffer</code> at the start of <code>offset</code> and a maximum of
|
|
165 * <code>length</code> bytes. If the remote host disconnects, throw an
|
|
166 * {@link IOException}.
|
|
167 * @param buffer byte buffer to store read bytes into
|
|
168 * @param offset where to start writing in the buffer
|
|
169 * @param length maximum number of bytes to read
|
|
170 * @return number of bytes read
|
|
171 * @throws IOException when remote host disconnects
|
|
172 */
|
|
173 public int read(byte[] buffer, int offset, int length) throws IOException {
|
|
174 return 0;
|
|
175 }
|
|
176
|
|
177
|
|
178 /**
|
|
179 * Writes to the transport. If the host is not yet connected, simply return without
|
|
180 * doing anything. An {@link IOException} should be thrown if there is an error after
|
|
181 * connection.
|
|
182 * @param buffer bytes to write to transport
|
|
183 * @throws IOException when there is a problem writing after connection
|
|
184 */
|
|
185 public void write(byte[] buffer) throws IOException {
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 * Writes to the transport. See {@link #write(byte[])} for behavior details.
|
|
190 * @param c character to write to the transport
|
|
191 * @throws IOException when there is a problem writing after connection
|
|
192 */
|
|
193 public void write(int c) throws IOException {
|
|
194 }
|
|
195
|
|
196 /**
|
|
197 * Flushes the write commands to the transport.
|
|
198 * @throws IOException when there is a problem writing after connection
|
|
199 */
|
|
200 public void flush() throws IOException {
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * Closes the connection to the terminal. Note that the resulting failure to read
|
|
205 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
|
|
206 */
|
|
207 public void close() {
|
13
|
208 handler.disconnect();
|
11
|
209 connected = false;
|
|
210 }
|
|
211
|
|
212 /**
|
|
213 * Tells the transport what dimensions the display is currently
|
|
214 * @param columns columns of text
|
|
215 * @param rows rows of text
|
|
216 * @param width width in pixels
|
|
217 * @param height height in pixels
|
|
218 */
|
|
219 @Override
|
|
220 public void setDimensions(int columns, int rows, int width, int height) {
|
|
221 // do nothing
|
|
222 }
|
|
223
|
|
224 public void setOptions(Map<String, String> options) {
|
|
225 // do nothing
|
|
226 }
|
|
227
|
|
228 public Map<String, String> getOptions() {
|
|
229 return null;
|
|
230 }
|
|
231
|
|
232 public void setCompression(boolean compression) {
|
|
233 // do nothing
|
|
234 }
|
|
235
|
|
236 public void setHttpproxy(String httpproxy) {
|
|
237 // do nothing
|
|
238 }
|
|
239
|
|
240 public void setUseAuthAgent(String useAuthAgent) {
|
|
241 // do nothing
|
|
242 }
|
|
243
|
|
244 public void setEmulation(String emulation) {
|
|
245 this.emulation = emulation;
|
|
246 }
|
|
247
|
|
248 public String getEmulation() {
|
|
249 return emulation;
|
|
250 }
|
|
251
|
|
252 public void setHost(HostBean host) {
|
|
253 this.host = host;
|
|
254 }
|
|
255
|
|
256 public void setBridge(TerminalBridge bridge) {
|
|
257 this.bridge = bridge;
|
|
258 }
|
|
259
|
|
260 public void setManager(TerminalManager manager) {
|
|
261 this.manager = manager;
|
|
262 }
|
|
263
|
|
264 /**
|
|
265 * Whether or not this transport type can forward ports.
|
|
266 * @return true on ability to forward ports
|
|
267 */
|
|
268 public boolean canForwardPorts() {
|
|
269 return false;
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Adds the {@link PortForwardBean} to the list.
|
|
274 * @param portForward the port forward bean to add
|
|
275 * @return true on successful addition
|
|
276 */
|
|
277 public boolean addPortForward(PortForwardBean portForward) {
|
|
278 return false;
|
|
279 }
|
|
280
|
|
281 /**
|
|
282 * Enables a port forward member. After calling this method, the port forward should
|
|
283 * be operational iff it could be enabled by the transport.
|
|
284 * @param portForward member of our current port forwards list to enable
|
|
285 * @return true on successful port forward setup
|
|
286 */
|
|
287 public boolean enablePortForward(PortForwardBean portForward) {
|
|
288 return false;
|
|
289 }
|
|
290
|
|
291 /**
|
|
292 * Disables a port forward member. After calling this method, the port forward should
|
|
293 * be non-functioning iff it could be disabled by the transport.
|
|
294 * @param portForward member of our current port forwards list to enable
|
|
295 * @return true on successful port forward tear-down
|
|
296 */
|
|
297 public boolean disablePortForward(PortForwardBean portForward) {
|
|
298 return false;
|
|
299 }
|
|
300
|
|
301 /**
|
|
302 * Removes the {@link PortForwardBean} from the available port forwards.
|
|
303 * @param portForward the port forward bean to remove
|
|
304 * @return true on successful removal
|
|
305 */
|
|
306 public boolean removePortForward(PortForwardBean portForward) {
|
|
307 return false;
|
|
308 }
|
|
309
|
|
310 /**
|
|
311 * Gets a list of the {@link PortForwardBean} currently used by this transport.
|
|
312 * @return the list of port forwards
|
|
313 */
|
|
314 public List<PortForwardBean> getPortForwards() {
|
|
315 return null;
|
|
316 }
|
|
317
|
|
318 /**
|
|
319 * Whether or not this transport type can transfer files.
|
|
320 * @return true on ability to transfer files
|
|
321 */
|
|
322 public boolean canTransferFiles() {
|
|
323 return false;
|
|
324 }
|
|
325
|
|
326 /**
|
|
327 * Downloads the specified remote file to a local folder.
|
|
328 * @param remoteFile The path to the remote file to be downloaded. Must be non-null.
|
|
329 * @param localFolder The path to local folder. Null = default external storage folder.
|
|
330 * @return true on success, false on failure
|
|
331 */
|
|
332 public boolean downloadFile(String remoteFile, String localFolder) {
|
|
333 return false;
|
|
334 }
|
|
335
|
|
336 /**
|
|
337 * Uploads the specified local file to the remote host.
|
|
338 * @param localFile The path to the local file to be uploaded. Must be non-null.
|
|
339 * @param remoteFolder The path to the remote directory. Null == default remote directory.
|
|
340 * @return true on success, false on failure
|
|
341 */
|
|
342 public boolean uploadFile(String localFile, String remoteFile,
|
|
343 String remoteFolder, String mode) {
|
|
344 return false;
|
|
345 }
|
|
346
|
|
347 @Override
|
|
348 public int getDefaultPort() {
|
|
349 return DEFAULT_PORT;
|
|
350 }
|
|
351
|
|
352 @Override
|
|
353 public boolean isConnected() {
|
|
354 return connected;
|
|
355 }
|
|
356
|
|
357 @Override
|
|
358 public boolean isSessionOpen() {
|
|
359 return connected;
|
|
360 }
|
|
361
|
|
362 @Override
|
|
363 public boolean isAuthenticated() {
|
|
364 return connected;
|
|
365 }
|
|
366
|
|
367
|
|
368 @Override
|
|
369 public String getDefaultNickname(String username, String hostname, int port) {
|
|
370 if (port == DEFAULT_PORT) {
|
|
371 return String.format("%s", hostname);
|
|
372 }
|
|
373 else {
|
|
374 return String.format("%s:%d", hostname, port);
|
|
375 }
|
|
376 }
|
|
377
|
|
378 @Override
|
|
379 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
|
|
380 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
|
|
381 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
|
|
382 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
|
|
383 int port = uri.getPort();
|
|
384
|
|
385 if (port < 0)
|
|
386 port = DEFAULT_PORT;
|
|
387
|
|
388 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
|
|
389 }
|
|
390
|
|
391
|
|
392 @Override
|
|
393 public HostBean createHost(Uri uri) {
|
|
394 HostBean host = new HostBean();
|
|
395 host.setProtocol(PROTOCOL);
|
|
396 host.setHostname(uri.getHost());
|
|
397 int port = uri.getPort();
|
|
398
|
|
399 if (port < 0)
|
|
400 port = DEFAULT_PORT;
|
|
401
|
|
402 host.setPort(port);
|
|
403 String nickname = uri.getFragment();
|
|
404
|
|
405 if (nickname == null || nickname.length() == 0) {
|
|
406 host.setNickname(getDefaultNickname(host.getUsername(),
|
|
407 host.getHostname(), host.getPort()));
|
|
408 }
|
|
409 else {
|
|
410 host.setNickname(uri.getFragment());
|
|
411 }
|
|
412
|
|
413 return host;
|
|
414 }
|
|
415
|
|
416
|
12
|
417 public String getFormatHint(Context context) {
|
11
|
418 return String.format("%s:%s",
|
|
419 context.getString(R.string.format_hostname),
|
|
420 context.getString(R.string.format_port));
|
|
421 }
|
|
422
|
|
423
|
|
424 @Override
|
|
425 public boolean usesNetwork() {
|
|
426 return true;
|
|
427 }
|
|
428 }
|