1 2 29 30 package com.jcraft.jsch; 31 32 import java.net.*; 33 import java.io.*; 34 35 class PortWatcher implements Runnable { 36 private static java.util.Vector pool=new java.util.Vector (); 37 private static InetAddress anyLocalAddress=null; 38 static{ 39 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 host; 54 InetAddress boundaddress; 55 Runnable thread; 56 ServerSocket ss; 57 58 static String [] getPortForwarding(Session session){ 59 java.util.Vector foo=new java.util.Vector (); 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 [] bar=new String [foo.size()]; 69 for(int i=0; i<foo.size(); i++){ 70 bar[i]=(String )(foo.elementAt(i)); 71 } 72 return bar; 73 } 74 static PortWatcher getPort(Session session, String 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( 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 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."); 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 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 address, int lport, 130 String 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 e){ 143 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); 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 e){ 179 } 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 e){ 192 } 193 } 194 } 195 | Popular Tags |