1 16 package org.apache.commons.net.bsd; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.BindException ; 21 import java.net.InetAddress ; 22 import java.net.ServerSocket ; 23 import java.net.Socket ; 24 import java.net.SocketException ; 25 26 import org.apache.commons.net.io.SocketInputStream; 27 28 86 87 public class RCommandClient extends RExecClient 88 { 89 92 public static final int DEFAULT_PORT = 514; 93 94 98 public static final int MIN_CLIENT_PORT = 512; 99 100 104 public static final int MAX_CLIENT_PORT = 1023; 105 106 InputStream _createErrorStream() throws IOException 109 { 110 int localPort; 111 ServerSocket server; 112 Socket socket; 113 114 localPort = MAX_CLIENT_PORT; 115 server = null; 117 for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; --localPort) 118 { 119 try 120 { 121 server = _socketFactory_.createServerSocket(localPort, 1, 122 getLocalAddress()); 123 } 124 catch (SocketException e) 125 { 126 continue; 127 } 128 break; 129 } 130 131 if (localPort < MIN_CLIENT_PORT) 132 throw new BindException ("All ports in use."); 133 134 _output_.write(Integer.toString(server.getLocalPort()).getBytes()); 135 _output_.write('\0'); 136 _output_.flush(); 137 138 socket = server.accept(); 139 server.close(); 140 141 if (isRemoteVerificationEnabled() && !verifyRemote(socket)) 142 { 143 socket.close(); 144 throw new IOException ( 145 "Security violation: unexpected connection attempt by " + 146 socket.getInetAddress().getHostAddress()); 147 } 148 149 return (new SocketInputStream(socket, socket.getInputStream())); 150 } 151 152 156 public RCommandClient() 157 { 158 setDefaultPort(DEFAULT_PORT); 159 } 160 161 162 178 public void connect(InetAddress host, int port, InetAddress localAddr) 179 throws SocketException , BindException , IOException 180 { 181 int localPort; 182 183 localPort = MAX_CLIENT_PORT; 184 185 for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; --localPort) 186 { 187 try 188 { 189 _socket_ = 190 _socketFactory_.createSocket(host, port, localAddr, localPort); 191 } 192 catch (SocketException e) 193 { 194 continue; 195 } 196 break; 197 } 198 199 if (localPort < MIN_CLIENT_PORT) 200 throw new BindException ("All ports in use or insufficient permssion."); 201 202 _connectAction_(); 203 } 204 205 206 207 222 public void connect(InetAddress host, int port) 223 throws SocketException , IOException 224 { 225 connect(host, port, InetAddress.getLocalHost()); 226 } 227 228 229 245 public void connect(String hostname, int port) 246 throws SocketException , IOException 247 { 248 connect(InetAddress.getByName(hostname), port, InetAddress.getLocalHost()); 249 } 250 251 252 268 public void connect(String hostname, int port, InetAddress localAddr) 269 throws SocketException , IOException 270 { 271 connect(InetAddress.getByName(hostname), port, localAddr); 272 } 273 274 275 295 public void connect(InetAddress host, int port, 296 InetAddress localAddr, int localPort) 297 throws SocketException , IOException , IllegalArgumentException 298 { 299 if (localPort < MIN_CLIENT_PORT || localPort > MAX_CLIENT_PORT) 300 throw new IllegalArgumentException ("Invalid port number " + localPort); 301 super.connect(host, port, localAddr, localPort); 302 } 303 304 305 326 public void connect(String hostname, int port, 327 InetAddress localAddr, int localPort) 328 throws SocketException , IOException , IllegalArgumentException 329 { 330 if (localPort < MIN_CLIENT_PORT || localPort > MAX_CLIENT_PORT) 331 throw new IllegalArgumentException ("Invalid port number " + localPort); 332 super.connect(hostname, port, localAddr, localPort); 333 } 334 335 336 372 public void rcommand(String localUsername, String remoteUsername, 373 String command, boolean separateErrorStream) 374 throws IOException 375 { 376 rexec(localUsername, remoteUsername, command, separateErrorStream); 377 } 378 379 380 384 public void rcommand(String localUsername, String remoteUsername, 385 String command) 386 throws IOException 387 { 388 rcommand(localUsername, remoteUsername, command, false); 389 } 390 391 } 392 393 | Popular Tags |