• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
6 
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.net.InetAddress;
11 
12 import ch.ethz.ssh2.channel.Channel;
13 import ch.ethz.ssh2.channel.ChannelManager;
14 
15 /**
16  * A <code>LocalStreamForwarder</code> forwards an Input- and Outputstream
17  * pair via the secure tunnel to another host (which may or may not be identical
18  * to the remote SSH-2 server).
19  *
20  * @author Christian Plattner
21  * @version 2.50, 03/15/10
22  */
23 public class LocalStreamForwarder
24 {
25 	private ChannelManager cm;
26 
27 	private Channel cn;
28 
LocalStreamForwarder(ChannelManager cm, String host_to_connect, int port_to_connect)29 	LocalStreamForwarder(ChannelManager cm, String host_to_connect, int port_to_connect) throws IOException
30 	{
31 		this.cm = cm;
32 		cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect,
33 				InetAddress.getLocalHost().getHostAddress(), 0);
34 	}
35 
36 	/**
37 	 * @return An <code>InputStream</code> object.
38 	 * @throws IOException
39 	 */
getInputStream()40 	public InputStream getInputStream() throws IOException
41 	{
42 		return cn.getStdoutStream();
43 	}
44 
45 	/**
46 	 * Get the OutputStream. Please be aware that the implementation MAY use an
47 	 * internal buffer. To make sure that the buffered data is sent over the
48 	 * tunnel, you have to call the <code>flush</code> method of the
49 	 * <code>OutputStream</code>. To signal EOF, please use the
50 	 * <code>close</code> method of the <code>OutputStream</code>.
51 	 *
52 	 * @return An <code>OutputStream</code> object.
53 	 * @throws IOException
54 	 */
getOutputStream()55 	public OutputStream getOutputStream() throws IOException
56 	{
57 		return cn.getStdinStream();
58 	}
59 
60 	/**
61 	 * Close the underlying SSH forwarding channel and free up resources.
62 	 * You can also use this method to force the shutdown of the underlying
63 	 * forwarding channel. Pending output (OutputStream not flushed) will NOT
64 	 * be sent. Pending input (InputStream) can still be read. If the shutdown
65 	 * operation is already in progress (initiated from either side), then this
66 	 * call is a no-op.
67 	 *
68 	 * @throws IOException
69 	 */
close()70 	public void close() throws IOException
71 	{
72 		cm.closeChannel(cn, "Closed due to user request.", true);
73 	}
74 }
75