Skip to content

Commit 577a2f4

Browse files
committed
Merge branch 'bugfix/CID-116538'
2 parents e1973a2 + de26a72 commit 577a2f4

3 files changed

Lines changed: 31 additions & 30 deletions

File tree

utils/src/com/cloud/utils/nio/Link.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.channels.ReadableByteChannel;
3232
import java.nio.channels.SelectionKey;
3333
import java.nio.channels.SocketChannel;
34+
import java.security.GeneralSecurityException;
3435
import java.security.KeyStore;
3536
import java.util.Properties;
3637
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -159,7 +160,7 @@ private static void doWrite(SocketChannel ch, ByteBuffer[] buffers, SSLEngine ss
159160
pkgBuf.clear();
160161
engResult = sslEngine.wrap(buffers, pkgBuf);
161162
if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
162-
engResult.getStatus() != SSLEngineResult.Status.OK) {
163+
engResult.getStatus() != SSLEngineResult.Status.OK) {
163164
throw new IOException("SSL: SSLEngine return bad result! " + engResult);
164165
}
165166

@@ -285,7 +286,7 @@ public byte[] read(SocketChannel ch) throws IOException {
285286
appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40);
286287
engResult = _sslEngine.unwrap(_readBuffer, appBuf);
287288
if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
288-
engResult.getStatus() != SSLEngineResult.Status.OK) {
289+
engResult.getStatus() != SSLEngineResult.Status.OK) {
289290
throw new IOException("SSL: SSLEngine return bad result! " + engResult);
290291
}
291292
if (remaining == _readBuffer.remaining()) {
@@ -405,7 +406,7 @@ public synchronized void schedule(Task task) throws ClosedChannelException {
405406
_connection.scheduleTask(task);
406407
}
407408

408-
public static SSLContext initSSLContext(boolean isClient) throws Exception {
409+
public static SSLContext initSSLContext(boolean isClient) throws GeneralSecurityException, IOException {
409410
InputStream stream;
410411
SSLContext sslContext = null;
411412
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");

utils/src/com/cloud/utils/nio/NioClient.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.channels.SelectionKey;
2525
import java.nio.channels.Selector;
2626
import java.nio.channels.SocketChannel;
27+
import java.security.GeneralSecurityException;
2728

2829
import javax.net.ssl.SSLContext;
2930
import javax.net.ssl.SSLEngine;
@@ -48,30 +49,23 @@ public void setBindAddress(String ipAddress) {
4849
@Override
4950
protected void init() throws IOException {
5051
_selector = Selector.open();
51-
SocketChannel sch = null;
52-
InetSocketAddress addr = null;
52+
Task task = null;
5353

54-
try {
55-
sch = SocketChannel.open();
54+
try (SocketChannel sch = SocketChannel.open()) {
5655
sch.configureBlocking(true);
5756
s_logger.info("Connecting to " + _host + ":" + _port);
5857

5958
if (_bindAddress != null) {
6059
s_logger.info("Binding outbound interface at " + _bindAddress);
6160

62-
addr = new InetSocketAddress(_bindAddress, 0);
63-
sch.socket().bind(addr);
61+
InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
62+
sch.socket().bind(bindAddr);
6463
}
6564

66-
addr = new InetSocketAddress(_host, _port);
67-
sch.connect(addr);
68-
} catch (IOException e) {
69-
_selector.close();
70-
throw e;
71-
}
65+
InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
66+
sch.connect(peerAddr);
7267

73-
SSLEngine sslEngine = null;
74-
try {
68+
SSLEngine sslEngine = null;
7569
// Begin SSL handshake in BLOCKING mode
7670
sch.configureBlocking(true);
7771

@@ -82,25 +76,21 @@ protected void init() throws IOException {
8276
Link.doHandshake(sch, sslEngine, true);
8377
s_logger.info("SSL: Handshake done");
8478
s_logger.info("Connected to " + _host + ":" + _port);
85-
} catch (Exception e) {
86-
_selector.close();
87-
throw new IOException("SSL: Fail to init SSL! " + e);
88-
}
8979

90-
Task task = null;
91-
try {
80+
9281
sch.configureBlocking(false);
93-
Link link = new Link(addr, this);
82+
Link link = new Link(peerAddr, this);
9483
link.setSSLEngine(sslEngine);
9584
SelectionKey key = sch.register(_selector, SelectionKey.OP_READ);
9685
link.setKey(key);
9786
key.attach(link);
9887
// Notice we've already connected due to the handshake, so let's get the
9988
// remaining task done
10089
task = _factory.create(Task.Type.CONNECT, link, null);
101-
} catch (Exception e) {
90+
} catch (GeneralSecurityException e) {
91+
throw new IOException("Failed to initialise security", e);
92+
} finally {
10293
_selector.close();
103-
throw new IOException("Fail to init NioClient! " + e);
10494
}
10595
_executor.execute(task);
10696
}

utils/src/com/cloud/utils/nio/NioServer.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class NioServer extends NioConnection {
3333
private final static Logger s_logger = Logger.getLogger(NioServer.class);
3434

3535
protected InetSocketAddress _localAddr;
36+
private ServerSocketChannel _serverSocket;
3637

3738
protected WeakHashMap<InetSocketAddress, Link> _links;
3839

@@ -46,17 +47,26 @@ public NioServer(String name, int port, int workers, HandlerFactory factory) {
4647
protected void init() throws IOException {
4748
_selector = SelectorProvider.provider().openSelector();
4849

49-
ServerSocketChannel ssc = ServerSocketChannel.open();
50-
ssc.configureBlocking(false);
50+
_serverSocket = ServerSocketChannel.open();
51+
_serverSocket.configureBlocking(false);
5152

5253
_localAddr = new InetSocketAddress(_port);
53-
ssc.socket().bind(_localAddr);
54+
_serverSocket.socket().bind(_localAddr);
5455

55-
ssc.register(_selector, SelectionKey.OP_ACCEPT, null);
56+
_serverSocket.register(_selector, SelectionKey.OP_ACCEPT, null);
5657

5758
s_logger.info("NioConnection started and listening on " + _localAddr.toString());
5859
}
5960

61+
@Override
62+
public void cleanUp() throws IOException {
63+
super.cleanUp();
64+
if (_serverSocket != null) {
65+
_serverSocket.close();
66+
}
67+
s_logger.info("NioConnection stopped on " + _localAddr.toString());
68+
}
69+
6070
@Override
6171
protected void registerLink(InetSocketAddress addr, Link link) {
6272
_links.put(addr, link);

0 commit comments

Comments
 (0)