HCOLL-309 added code to better reject third party connections which turn out not to be chronicle.

This commit is contained in:
Rob Austin
2015-02-16 11:06:35 +00:00
parent 517e9e8059
commit 7c09d1dfaf
3 changed files with 49 additions and 2 deletions
@@ -309,7 +309,7 @@ final class SerializationBuilder<E> implements Cloneable, Serializable {
}
// set max size once more, because current maxSize could be x64 or x2 larger than needed
maxSize(constantSize);
sizeMarshaller(SizeMarshallers.constant(constantSize));
sizeMarshaller(net.openhft.chronicle.hash.serialization.SizeMarshallers.constant(constantSize));
return this;
}
@@ -32,6 +32,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;
@@ -509,11 +510,44 @@ final class TcpReplicator<K, V> 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<K, V> 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 : " +
@@ -610,10 +644,21 @@ final class TcpReplicator<K, V> 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);
}
@@ -97,6 +97,8 @@ public class TCPSocketReplicationTest {
assertEquals(map1, map2);
assertTrue(!map1.isEmpty());
Thread.sleep(9999999);
}
@Test