01: package ch.ethz.ssh2.channel;
02:
03: import java.io.IOException;
04: import java.net.Socket;
05:
06: import ch.ethz.ssh2.log.Logger;
07:
08: /**
09: * RemoteAcceptThread.
10: *
11: * @author Christian Plattner, plattner@inf.ethz.ch
12: * @version $Id: RemoteAcceptThread.java,v 1.4 2006/02/13 21:19:25 cplattne Exp $
13: */
14: public class RemoteAcceptThread extends Thread {
15: private static final Logger log = Logger
16: .getLogger(RemoteAcceptThread.class);
17:
18: Channel c;
19:
20: String remoteConnectedAddress;
21: int remoteConnectedPort;
22: String remoteOriginatorAddress;
23: int remoteOriginatorPort;
24: String targetAddress;
25: int targetPort;
26:
27: Socket s;
28:
29: public RemoteAcceptThread(Channel c, String remoteConnectedAddress,
30: int remoteConnectedPort, String remoteOriginatorAddress,
31: int remoteOriginatorPort, String targetAddress,
32: int targetPort) {
33: this .c = c;
34: this .remoteConnectedAddress = remoteConnectedAddress;
35: this .remoteConnectedPort = remoteConnectedPort;
36: this .remoteOriginatorAddress = remoteOriginatorAddress;
37: this .remoteOriginatorPort = remoteOriginatorPort;
38: this .targetAddress = targetAddress;
39: this .targetPort = targetPort;
40:
41: if (log.isEnabled())
42: log.log(20, "RemoteAcceptThread: " + remoteConnectedAddress
43: + "/" + remoteConnectedPort + ", R: "
44: + remoteOriginatorAddress + "/"
45: + remoteOriginatorPort);
46: }
47:
48: public void run() {
49: try {
50: c.cm.sendOpenConfirmation(c);
51:
52: s = new Socket(targetAddress, targetPort);
53:
54: StreamForwarder r2l = new StreamForwarder(c, null, null, c
55: .getStdoutStream(), s.getOutputStream(),
56: "RemoteToLocal");
57: StreamForwarder l2r = new StreamForwarder(c, null, null, s
58: .getInputStream(), c.getStdinStream(),
59: "LocalToRemote");
60:
61: /* No need to start two threads, one can be executed in the current thread */
62:
63: r2l.setDaemon(true);
64: r2l.start();
65: l2r.run();
66:
67: while (r2l.isAlive()) {
68: try {
69: r2l.join();
70: } catch (InterruptedException e) {
71: }
72: }
73:
74: /* If the channel is already closed, then this is a no-op */
75:
76: c.cm.closeChannel(c, "EOF on both streams reached.", true);
77: s.close();
78: } catch (IOException e) {
79: log.log(50, "IOException in proxy code: " + e.getMessage());
80:
81: try {
82: c.cm.closeChannel(c, "IOException in proxy code ("
83: + e.getMessage() + ")", true);
84: } catch (IOException e1) {
85: }
86: try {
87: if (s != null)
88: s.close();
89: } catch (IOException e1) {
90: }
91: }
92: }
93: }
|