KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > slave > PassiveConnection


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.slave;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.net.Socket JavaDoc;
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 /**
35  * @author mog
36  * @version $Id: PassiveConnection.java,v 1.11 2004/02/10 00:03:31 mog Exp $
37  */

38 public class PassiveConnection extends Connection {
39     private PortRange _portRange;
40     private static final Logger logger = Logger.getLogger(PassiveConnection.class);
41     private ServerSocket JavaDoc _server;
42
43     public PassiveConnection(SSLContext ctx, PortRange portRange, InetSocketAddress JavaDoc bindAddr)
44         throws IOException JavaDoc {
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 JavaDoc(bindAddr.getAddress(), portRange.getPort()));
58         } else {
59             _server.bind(bindAddr, 1);
60         }
61         _server.setSoTimeout(TIMEOUT);
62     }
63
64     public Socket JavaDoc connect() throws IOException JavaDoc {
65         Socket JavaDoc 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 JavaDoc e) {
88             logger.warn("failed to close() server socket", e);
89         }
90     }
91
92 }
93
Popular Tags