Skip to content

Commit 4312f92

Browse files
author
Sheng Yang
committed
CLOUDSTACK-5723: Add timeout for SSL handshake
To prevent malfunction agent block the future SSL connections
1 parent 725bed4 commit 4312f92

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.net.InetSocketAddress;
24+
import java.net.SocketTimeoutException;
2425
import java.nio.ByteBuffer;
26+
import java.nio.channels.Channels;
2527
import java.nio.channels.ClosedChannelException;
28+
import java.nio.channels.ReadableByteChannel;
2629
import java.nio.channels.SelectionKey;
2730
import java.nio.channels.SocketChannel;
2831
import java.security.KeyStore;
@@ -449,6 +452,10 @@ public static void doHandshake(SocketChannel ch, SSLEngine sslEngine, boolean is
449452
ByteBuffer out_pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize() + 40);
450453
ByteBuffer out_appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40);
451454
int count;
455+
ch.socket().setSoTimeout(10 * 1000);
456+
InputStream inStream = ch.socket().getInputStream();
457+
// Use readCh to make sure the timeout on reading is working
458+
ReadableByteChannel readCh = Channels.newChannel(inStream);
452459

453460
if (isClient) {
454461
hsStatus = SSLEngineResult.HandshakeStatus.NEED_WRAP;
@@ -479,7 +486,15 @@ public static void doHandshake(SocketChannel ch, SSLEngine sslEngine, boolean is
479486
// One packet may contained multiply operation
480487
if (in_pkgBuf.position() == 0 || !in_pkgBuf.hasRemaining()) {
481488
in_pkgBuf.clear();
482-
count = ch.read(in_pkgBuf);
489+
count = 0;
490+
try {
491+
count = readCh.read(in_pkgBuf);
492+
} catch (SocketTimeoutException ex) {
493+
if (s_logger.isTraceEnabled()) {
494+
s_logger.trace("Handshake reading time out! Cut the connection");
495+
}
496+
count = -1;
497+
}
483498
if (count == -1) {
484499
throw new IOException("Connection closed with -1 on reading size.");
485500
}

0 commit comments

Comments
 (0)