KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > PortWatcher


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
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.
14
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.
17
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.
28 */

29
30 package com.jcraft.jsch;
31
32 import java.net.*;
33 import java.io.*;
34
35 class PortWatcher implements Runnable JavaDoc{
36   private static java.util.Vector JavaDoc pool=new java.util.Vector JavaDoc();
37   private static InetAddress anyLocalAddress=null;
38   static{
39     // 0.0.0.0
40
/*
41     try{ anyLocalAddress=InetAddress.getByAddress(new byte[4]); }
42     catch(UnknownHostException e){
43     }
44 */

45     try{ anyLocalAddress=InetAddress.getByName("0.0.0.0"); }
46     catch(UnknownHostException e){
47     }
48   }
49
50   Session session;
51   int lport;
52   int rport;
53   String JavaDoc host;
54   InetAddress boundaddress;
55   Runnable JavaDoc thread;
56   ServerSocket ss;
57
58   static String JavaDoc[] getPortForwarding(Session session){
59     java.util.Vector JavaDoc foo=new java.util.Vector JavaDoc();
60     synchronized(pool){
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);
65     }
66       }
67     }
68     String JavaDoc[] bar=new String JavaDoc[foo.size()];
69     for(int i=0; i<foo.size(); i++){
70       bar[i]=(String JavaDoc)(foo.elementAt(i));
71     }
72     return bar;
73   }
74   static PortWatcher getPort(Session session, String JavaDoc address, int lport) throws JSchException{
75     InetAddress addr;
76     try{
77       addr=InetAddress.getByName(address);
78     }
79     catch(UnknownHostException uhe){
80       throw new JSchException("PortForwardingL: invalid address "+address+" specified.", uhe);
81     }
82     synchronized(pool){
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))
89       return p;
90     }
91       }
92       return null;
93     }
94   }
95   static PortWatcher addPort(Session session, String JavaDoc address, int lport, String JavaDoc 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.");
98     }
99     PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
100     pool.addElement(pw);
101     return pw;
102   }
103   static void delPort(Session session, String JavaDoc address, int lport) throws JSchException{
104     PortWatcher pw=getPort(session, address, lport);
105     if(pw==null){
106       throw new JSchException("PortForwardingL: local port "+address+":"+lport+" is not registered.");
107     }
108     pw.delete();
109     pool.removeElement(pw);
110   }
111   static void delPort(Session session){
112     synchronized(pool){
113       PortWatcher[] foo=new PortWatcher[pool.size()];
114       int count=0;
115       for(int i=0; i<pool.size(); i++){
116     PortWatcher p=(PortWatcher)(pool.elementAt(i));
117     if(p.session==session) {
118       p.delete();
119       foo[count++]=p;
120     }
121       }
122       for(int i=0; i<count; i++){
123     PortWatcher p=foo[i];
124     pool.removeElement(p);
125       }
126     }
127   }
128   PortWatcher(Session session,
129           String JavaDoc address, int lport,
130           String JavaDoc host, int rport,
131               ServerSocketFactory factory) throws JSchException{
132     this.session=session;
133     this.lport=lport;
134     this.host=host;
135     this.rport=rport;
136     try{
137       boundaddress=InetAddress.getByName(address);
138       ss=(factory==null) ?
139         new ServerSocket(lport, 0, boundaddress) :
140         factory.createServerSocket(lport, 0, boundaddress);
141     }
142     catch(Exception JavaDoc e){
143       //System.err.println(e);
144
String JavaDoc message="PortForwardingL: local port "+address+":"+lport+" cannot be bound.";
145       if(e instanceof Throwable JavaDoc)
146         throw new JSchException(message, (Throwable JavaDoc)e);
147       throw new JSchException(message);
148     }
149     if(lport==0){
150       int assigned=ss.getLocalPort();
151       if(assigned!=-1)
152         this.lport=assigned;
153     }
154   }
155
156   public void run(){
157     thread=this;
158     try{
159       while(thread!=null){
160         Socket socket=ss.accept();
161     socket.setTcpNoDelay(true);
162         InputStream in=socket.getInputStream();
163         OutputStream out=socket.getOutputStream();
164         ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
165         channel.init();
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());
173         channel.connect();
174     if(channel.exitstatus!=-1){
175     }
176       }
177     }
178     catch(Exception JavaDoc e){
179       //System.err.println("! "+e);
180
}
181
182     delete();
183   }
184
185   void delete(){
186     thread=null;
187     try{
188       if(ss!=null)ss.close();
189       ss=null;
190     }
191     catch(Exception JavaDoc e){
192     }
193   }
194 }
195
Popular Tags