01: /*
02: * This file is part of DrFTPD, Distributed FTP Daemon.
03: *
04: * DrFTPD is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * 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 org.drftpd;
19:
20: import org.apache.log4j.Logger;
21: import org.drftpd.slave.Connection;
22:
23: import java.io.IOException;
24:
25: import java.net.InetSocketAddress;
26: import java.net.Socket;
27:
28: import javax.net.SocketFactory;
29: import javax.net.ssl.SSLContext;
30: import javax.net.ssl.SSLSocket;
31:
32: /**
33: * @author mog
34: * @version $Id: ActiveConnection.java 1593 2007-01-31 22:56:52Z zubov $
35: */
36: public class ActiveConnection extends Connection {
37: private static final Logger logger = Logger
38: .getLogger(ActiveConnection.class);
39: private SSLContext _ctx;
40: private InetSocketAddress _addr;
41: private Socket _sock;
42: private int _bufferSize;
43: private boolean _useSSLClientHandshake;
44:
45: public ActiveConnection(SSLContext ctx, InetSocketAddress addr,
46: boolean useSSLClientHandshake) {
47: _addr = addr;
48: _ctx = ctx;
49: _useSSLClientHandshake = useSSLClientHandshake;
50: }
51:
52: public Socket connect(int bufferSize) throws IOException {
53: _bufferSize = bufferSize;
54: logger.debug("Connecting to "
55: + _addr.getAddress().getHostAddress() + ":"
56: + _addr.getPort());
57:
58: if (_ctx != null) {
59: SSLSocket sslsock;
60: sslsock = (SSLSocket) _ctx.getSocketFactory()
61: .createSocket();
62: if (_bufferSize > 0) {
63: sslsock.setReceiveBufferSize(_bufferSize);
64: }
65: sslsock.connect(_addr, TIMEOUT);
66: setSockOpts(sslsock);
67: sslsock.setUseClientMode(_useSSLClientHandshake);
68: sslsock.startHandshake();
69: _sock = sslsock;
70: } else {
71: _sock = SocketFactory.getDefault().createSocket();
72: if (_bufferSize > 0) {
73: _sock.setReceiveBufferSize(_bufferSize);
74: }
75: _sock.connect(_addr, TIMEOUT);
76: setSockOpts(_sock);
77: }
78:
79: Socket sock = _sock;
80: _sock = null;
81:
82: return sock;
83: }
84:
85: public void abort() {
86: try {
87: if (_sock != null) {
88: _sock.close();
89: }
90: } catch (IOException e) {
91: logger.warn("abort() failed to close() socket", e);
92: }
93: }
94: }
|