1 18 package net.sf.drftpd.slave; 19 20 import java.io.IOException ; 21 import java.net.InetSocketAddress ; 22 import java.net.ServerSocket ; 23 import java.net.Socket ; 24 25 import javax.net.ServerSocketFactory; 26 import javax.net.ssl.SSLContext; 27 import javax.net.ssl.SSLServerSocket; 28 import javax.net.ssl.SSLSocket; 29 30 import net.sf.drftpd.util.PortRange; 31 32 import org.apache.log4j.Logger; 33 34 38 public class PassiveConnection extends Connection { 39 private PortRange _portRange; 40 private static final Logger logger = Logger.getLogger(PassiveConnection.class); 41 private ServerSocket _server; 42 43 public PassiveConnection(SSLContext ctx, PortRange portRange, InetSocketAddress bindAddr) 44 throws IOException { 45 if (ctx != null) { 46 SSLServerSocket sslserver; 47 sslserver = 48 (SSLServerSocket) ctx 49 .getServerSocketFactory() 50 .createServerSocket(); 51 _server = sslserver; 52 } else { 53 _server = ServerSocketFactory.getDefault().createServerSocket(); 54 } 55 if(bindAddr.getPort() == 0) { 56 _portRange = portRange; 57 _server.bind(new InetSocketAddress (bindAddr.getAddress(), portRange.getPort())); 58 } else { 59 _server.bind(bindAddr, 1); 60 } 61 _server.setSoTimeout(TIMEOUT); 62 } 63 64 public Socket connect() throws IOException { 65 Socket sock = _server.accept(); 66 _server.close(); 67 _portRange.releasePort(_server.getLocalPort()); 68 _server = null; 69 _portRange = null; 70 71 setSockOpts(sock); 72 if (sock instanceof SSLSocket) { 73 SSLSocket sslsock = (SSLSocket) sock; 74 sslsock.setUseClientMode(false); 75 sslsock.startHandshake(); 76 } 77 return sock; 78 } 79 80 public int getLocalPort() { 81 return _server.getLocalPort(); 82 } 83 84 public void abort() { 85 try { 86 _server.close(); 87 } catch (IOException e) { 88 logger.warn("failed to close() server socket", e); 89 } 90 } 91 92 } 93 | Popular Tags |