1/*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5package ch.ethz.ssh2;
6
7import java.io.IOException;
8import java.net.InetSocketAddress;
9
10import ch.ethz.ssh2.channel.ChannelManager;
11import ch.ethz.ssh2.channel.LocalAcceptThread;
12
13/**
14 * A <code>LocalPortForwarder</code> forwards TCP/IP connections to a local
15 * port via the secure tunnel to another host (which may or may not be identical
16 * to the remote SSH-2 server). Checkout {@link Connection#createLocalPortForwarder(int, String, int)}
17 * on how to create one.
18 *
19 * @author Christian Plattner
20 * @version 2.50, 03/15/10
21 */
22public class LocalPortForwarder
23{
24	ChannelManager cm;
25
26	String host_to_connect;
27
28	int port_to_connect;
29
30	LocalAcceptThread lat;
31
32	LocalPortForwarder(ChannelManager cm, int local_port, String host_to_connect, int port_to_connect)
33			throws IOException
34	{
35		this.cm = cm;
36		this.host_to_connect = host_to_connect;
37		this.port_to_connect = port_to_connect;
38
39		lat = new LocalAcceptThread(cm, local_port, host_to_connect, port_to_connect);
40		lat.setDaemon(true);
41		lat.start();
42	}
43
44	LocalPortForwarder(ChannelManager cm, InetSocketAddress addr, String host_to_connect, int port_to_connect)
45			throws IOException
46	{
47		this.cm = cm;
48		this.host_to_connect = host_to_connect;
49		this.port_to_connect = port_to_connect;
50
51		lat = new LocalAcceptThread(cm, addr, host_to_connect, port_to_connect);
52		lat.setDaemon(true);
53		lat.start();
54	}
55
56	/**
57	 * Stop TCP/IP forwarding of newly arriving connections.
58	 *
59	 * @throws IOException
60	 */
61	public void close() throws IOException
62	{
63		lat.stopWorking();
64	}
65}
66