1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch;
35 class PortWatcher implements Runnable{
36 private static java.util.Vector pool=new java.util.Vector();
37 private static InetAddress anyLocalAddress=null;
41 try{ anyLocalAddress=InetAddress.getByAddress(new byte[4]); }
42 catch(UnknownHostException e){
45 try{ anyLocalAddress=InetAddress.getByName("0.0.0.0"); }
46 catch(UnknownHostException e){
54 InetAddress boundaddress;
58 static String[] getPortForwarding(Session session){
59 java.util.Vector foo=new java.util.Vector();
61 for(int i=0; i<pool.size(); i++){
62 PortWatcher p=(PortWatcher)(pool.elementAt(i));
63 if(p.session==session){
64 foo.addElement(p.lport+":"+p.host+":"+p.rport);
68 String[] bar=new String[foo.size()];
69 for(int i=0; i<foo.size(); i++){
70 bar[i]=(String)(foo.elementAt(i));
74 static PortWatcher getPort(Session session, String address, int lport) throws JSchException{
77 addr=InetAddress.getByName(address);
79 catch(UnknownHostException uhe){
80 throw new JSchException("PortForwardingL: invalid address "+address+" specified.", uhe);
83 for(int i=0; i<pool.size(); i++){
84 PortWatcher p=(PortWatcher)(pool.elementAt(i));
85 if(p.session==session && p.lport==lport){
86 if(/*p.boundaddress.isAnyLocalAddress() ||*/
87 (anyLocalAddress!=null && p.boundaddress.equals(anyLocalAddress)) ||
88 p.boundaddress.equals(addr))
95 static PortWatcher addPort(Session session, String address, int lport, String host, int rport, ServerSocketFactory ssf) throws JSchException{
96 if(getPort(session, address, lport)!=null){
97 throw new JSchException("PortForwardingL: local port "+ address+":"+lport+" is already registered.");
99 PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
103 static void delPort(Session session, String address, int lport) throws JSchException{
104 PortWatcher pw=getPort(session, address, lport);
106 throw new JSchException("PortForwardingL: local port "+address+":"+lport+" is not registered.");
109 pool.removeElement(pw);
111 static void delPort(Session session){
113 PortWatcher[] foo=new PortWatcher[pool.size()];
115 for(int i=0; i<pool.size(); i++){
116 PortWatcher p=(PortWatcher)(pool.elementAt(i));
117 if(p.session==session) {
122 for(int i=0; i<count; i++){
123 PortWatcher p=foo[i];
124 pool.removeElement(p);
128 PortWatcher(Session session,
129 String address, int lport,
130 String host, int rport,
131 ServerSocketFactory factory) throws JSchException{
132 this.session=session;
137 boundaddress=InetAddress.getByName(address);
139 new ServerSocket(lport, 0, boundaddress) :
140 factory.createServerSocket(lport, 0, boundaddress);
143 //System.err.println(e);
144 String message="PortForwardingL: local port "+address+":"+lport+" cannot be bound.";
145 if(e instanceof Throwable)
146 throw new JSchException(message, (Throwable)e);
147 throw new JSchException(message);
150 int assigned=ss.getLocalPort();
160 Socket socket=ss.accept();
161 socket.setTcpNoDelay(true);
162 InputStream in=socket.getInputStream();
163 OutputStream out=socket.getOutputStream();
164 ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
166 channel.setInputStream(in);
167 channel.setOutputStream(out);
168 session.addChannel(channel);
169 ((ChannelDirectTCPIP)channel).setHost(host);
170 ((ChannelDirectTCPIP)channel).setPort(rport);
171 ((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress());
172 ((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort());
174 if(channel.exitstatus!=-1){
179 //System.err.println("! "+e);
188 if(ss!=null)ss.close();