Merge branch 'bugfix/CID-116538'

This commit is contained in:
Hugo Trippaers 2014-09-17 11:40:55 +02:00
commit 577a2f40b3
3 changed files with 31 additions and 30 deletions

View File

@ -31,6 +31,7 @@ import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -159,7 +160,7 @@ public class Link {
pkgBuf.clear();
engResult = sslEngine.wrap(buffers, pkgBuf);
if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
engResult.getStatus() != SSLEngineResult.Status.OK) {
engResult.getStatus() != SSLEngineResult.Status.OK) {
throw new IOException("SSL: SSLEngine return bad result! " + engResult);
}
@ -285,7 +286,7 @@ public class Link {
appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40);
engResult = _sslEngine.unwrap(_readBuffer, appBuf);
if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
engResult.getStatus() != SSLEngineResult.Status.OK) {
engResult.getStatus() != SSLEngineResult.Status.OK) {
throw new IOException("SSL: SSLEngine return bad result! " + engResult);
}
if (remaining == _readBuffer.remaining()) {
@ -405,7 +406,7 @@ public class Link {
_connection.scheduleTask(task);
}
public static SSLContext initSSLContext(boolean isClient) throws Exception {
public static SSLContext initSSLContext(boolean isClient) throws GeneralSecurityException, IOException {
InputStream stream;
SSLContext sslContext = null;
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");

View File

@ -24,6 +24,7 @@ import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
@ -48,30 +49,23 @@ public class NioClient extends NioConnection {
@Override
protected void init() throws IOException {
_selector = Selector.open();
SocketChannel sch = null;
InetSocketAddress addr = null;
Task task = null;
try {
sch = SocketChannel.open();
try (SocketChannel sch = SocketChannel.open()) {
sch.configureBlocking(true);
s_logger.info("Connecting to " + _host + ":" + _port);
if (_bindAddress != null) {
s_logger.info("Binding outbound interface at " + _bindAddress);
addr = new InetSocketAddress(_bindAddress, 0);
sch.socket().bind(addr);
InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
sch.socket().bind(bindAddr);
}
addr = new InetSocketAddress(_host, _port);
sch.connect(addr);
} catch (IOException e) {
_selector.close();
throw e;
}
InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
sch.connect(peerAddr);
SSLEngine sslEngine = null;
try {
SSLEngine sslEngine = null;
// Begin SSL handshake in BLOCKING mode
sch.configureBlocking(true);
@ -82,15 +76,10 @@ public class NioClient extends NioConnection {
Link.doHandshake(sch, sslEngine, true);
s_logger.info("SSL: Handshake done");
s_logger.info("Connected to " + _host + ":" + _port);
} catch (Exception e) {
_selector.close();
throw new IOException("SSL: Fail to init SSL! " + e);
}
Task task = null;
try {
sch.configureBlocking(false);
Link link = new Link(addr, this);
Link link = new Link(peerAddr, this);
link.setSSLEngine(sslEngine);
SelectionKey key = sch.register(_selector, SelectionKey.OP_READ);
link.setKey(key);
@ -98,9 +87,10 @@ public class NioClient extends NioConnection {
// Notice we've already connected due to the handshake, so let's get the
// remaining task done
task = _factory.create(Task.Type.CONNECT, link, null);
} catch (Exception e) {
} catch (GeneralSecurityException e) {
throw new IOException("Failed to initialise security", e);
} finally {
_selector.close();
throw new IOException("Fail to init NioClient! " + e);
}
_executor.execute(task);
}

View File

@ -33,6 +33,7 @@ public class NioServer extends NioConnection {
private final static Logger s_logger = Logger.getLogger(NioServer.class);
protected InetSocketAddress _localAddr;
private ServerSocketChannel _serverSocket;
protected WeakHashMap<InetSocketAddress, Link> _links;
@ -46,17 +47,26 @@ public class NioServer extends NioConnection {
protected void init() throws IOException {
_selector = SelectorProvider.provider().openSelector();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
_serverSocket = ServerSocketChannel.open();
_serverSocket.configureBlocking(false);
_localAddr = new InetSocketAddress(_port);
ssc.socket().bind(_localAddr);
_serverSocket.socket().bind(_localAddr);
ssc.register(_selector, SelectionKey.OP_ACCEPT, null);
_serverSocket.register(_selector, SelectionKey.OP_ACCEPT, null);
s_logger.info("NioConnection started and listening on " + _localAddr.toString());
}
@Override
public void cleanUp() throws IOException {
super.cleanUp();
if (_serverSocket != null) {
_serverSocket.close();
}
s_logger.info("NioConnection stopped on " + _localAddr.toString());
}
@Override
protected void registerLink(InetSocketAddress addr, Link link) {
_links.put(addr, link);