comparison src/ch/ethz/ssh2/channel/LocalAcceptThread.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.channel;
6
7 import java.io.IOException;
8 import java.net.InetSocketAddress;
9 import java.net.ServerSocket;
10 import java.net.Socket;
11
12 /**
13 * LocalAcceptThread.
14 *
15 * @author Christian Plattner
16 * @version 2.50, 03/15/10
17 */
18 public class LocalAcceptThread extends Thread implements IChannelWorkerThread
19 {
20 ChannelManager cm;
21 String host_to_connect;
22 int port_to_connect;
23
24 final ServerSocket ss;
25
26 public LocalAcceptThread(ChannelManager cm, int local_port, String host_to_connect, int port_to_connect)
27 throws IOException
28 {
29 this.cm = cm;
30 this.host_to_connect = host_to_connect;
31 this.port_to_connect = port_to_connect;
32
33 ss = new ServerSocket(local_port);
34 }
35
36 public LocalAcceptThread(ChannelManager cm, InetSocketAddress localAddress, String host_to_connect,
37 int port_to_connect) throws IOException
38 {
39 this.cm = cm;
40 this.host_to_connect = host_to_connect;
41 this.port_to_connect = port_to_connect;
42
43 ss = new ServerSocket();
44 ss.bind(localAddress);
45 }
46
47 public ServerSocket getServerSocket()
48 {
49 return ss;
50 }
51
52 @Override
53 public void run()
54 {
55 try
56 {
57 cm.registerThread(this);
58 }
59 catch (IOException e)
60 {
61 stopWorking();
62 return;
63 }
64
65 while (true)
66 {
67 Socket s = null;
68
69 try
70 {
71 s = ss.accept();
72 }
73 catch (IOException e)
74 {
75 stopWorking();
76 return;
77 }
78
79 Channel cn = null;
80 StreamForwarder r2l = null;
81 StreamForwarder l2r = null;
82
83 try
84 {
85 /* This may fail, e.g., if the remote port is closed (in optimistic terms: not open yet) */
86
87 cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, s.getInetAddress().getHostAddress(), s
88 .getPort());
89
90 }
91 catch (IOException e)
92 {
93 /* Simply close the local socket and wait for the next incoming connection */
94
95 try
96 {
97 s.close();
98 }
99 catch (IOException ignore)
100 {
101 }
102
103 continue;
104 }
105
106 try
107 {
108 r2l = new StreamForwarder(cn, null, null, cn.stdoutStream, s.getOutputStream(), "RemoteToLocal");
109 l2r = new StreamForwarder(cn, r2l, s, s.getInputStream(), cn.stdinStream, "LocalToRemote");
110 }
111 catch (IOException e)
112 {
113 try
114 {
115 /* This message is only visible during debugging, since we discard the channel immediatelly */
116 cn.cm.closeChannel(cn, e, true);
117 }
118 catch (IOException ignore)
119 {
120 }
121
122 continue;
123 }
124
125 r2l.setDaemon(true);
126 l2r.setDaemon(true);
127 r2l.start();
128 l2r.start();
129 }
130 }
131
132 public void stopWorking()
133 {
134 try
135 {
136 /* This will lead to an IOException in the ss.accept() call */
137 ss.close();
138 }
139 catch (IOException ignored)
140 {
141 }
142 }
143 }