diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index 9b792c1d9..d18fa86e5 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -36,6 +36,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; +import java.io.EOFException; import java.io.IOException; import java.net.*; import java.nio.BufferUnderflowException; @@ -512,11 +513,44 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo } + /** + * check that the version number is valid text, + * + * @param versionNumber the version number to check + * @return true, if the version number looks correct + */ + boolean isValidVersionNumber(String versionNumber) { + + if (versionNumber.length()<=2) + return false; + + for (char c : versionNumber.toCharArray()) { + + if (c >= '0' && c <= '9') + continue; + + if (c == '.' || c == '-' || c == '_') + continue; + + if (c >= 'A' && c <= 'Z') + continue; + + if (c >= 'a' && c <= 'z') + continue; + + return false; + + } + + return true; + } + private void checkVersions(final Attached attached) { final String localVersion = BuildVersion.version(); final String remoteVersion = attached.serverVersion; + if (!remoteVersion.equals(localVersion)) { byte remoteIdentifier = attached.remoteIdentifier; LOG.warn("DIFFERENT CHRONICLE-MAP VERSIONS : " + @@ -613,10 +647,21 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo } if (attached.serverVersion == null) { - attached.serverVersion = reader.readRemoteServerVersion(); + try { + attached.serverVersion = reader.readRemoteServerVersion(); + } catch (IllegalStateException e1) { + socketChannel.close(); + } + if (attached.serverVersion == null) return; + if (!isValidVersionNumber(attached.serverVersion)) { + LOG.warn("Please check that you don't have a third party system incorrectly connecting to ChronicleMap, " + + "Closing the remote connection as Chronicle can not make sense of the remote version number received from the external connection, version="+attached.serverVersion+", Chronicle is expecting the version number to only contain '.',A-Z,a-z,0-9"); + socketChannel.close(); + } + checkVersions(attached); } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java index 09cd83e4f..ef6656b4c 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java @@ -96,6 +96,8 @@ public class TCPSocketReplicationTest { assertEquals(map1, map2); assertTrue(!map1.isEmpty()); + + Thread.sleep(9999999); } @Test