Skip to content

Commit

Permalink
Fix UDP discovery to work with the new fingerprint size
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Nov 17, 2024
1 parent 4f0fc9c commit a032cd5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
enum ProtocolVersion
{
VERSION_0,
VERSION_1
VERSION_1,
VERSION_2
}
33 changes: 28 additions & 5 deletions app/src/main/java/io/xeres/app/net/bdisc/UdpDiscoveryProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import static io.xeres.app.net.bdisc.ProtocolVersion.VERSION_0;
import static io.xeres.app.net.bdisc.ProtocolVersion.VERSION_1;
import static io.xeres.app.net.bdisc.ProtocolVersion.*;

public final class UdpDiscoveryProtocol
{
Expand Down Expand Up @@ -66,7 +65,7 @@ public static UdpDiscoveryPeer parsePacket(ByteBuffer buffer, InetSocketAddress
case MAGIC_HEADER_VERSIONED ->
{
var versionNum = buffer.get();
if (versionNum > VERSION_1.ordinal())
if (versionNum > VERSION_2.ordinal())
{
log.warn("Unsupported protocol version: {}", versionNum);
return null;
Expand Down Expand Up @@ -116,7 +115,20 @@ public static UdpDiscoveryPeer parsePacket(ByteBuffer buffer, InetSocketAddress
}

var buf = Unpooled.wrappedBuffer(buffer);
peer.setFingerprint((ProfileFingerprint) Serializer.deserializeIdentifier(buf, ProfileFingerprint.class));
if (protocolVersion != VERSION_2)
{
peer.setFingerprint((ProfileFingerprint) Serializer.deserializeIdentifierWithSize(buf, ProfileFingerprint.class, ProfileFingerprint.V4_LENGTH));
}
else
{
var fingerPrintSize = buffer.get();
switch (fingerPrintSize)
{
case 20 -> peer.setFingerprint((ProfileFingerprint) Serializer.deserializeIdentifierWithSize(buf, ProfileFingerprint.class, ProfileFingerprint.V4_LENGTH));
case 32 -> peer.setFingerprint((ProfileFingerprint) Serializer.deserializeIdentifierWithSize(buf, ProfileFingerprint.class, ProfileFingerprint.LENGTH));
default -> throw new IllegalArgumentException("Unknown fingerprint size:" + fingerPrintSize);
}
}
peer.setLocationId((LocationId) Serializer.deserializeIdentifier(buf, LocationId.class));
peer.setLocalPort(Serializer.deserializeShort(buf));
peer.setProfileName(Serializer.deserializeString(buf));
Expand All @@ -129,7 +141,18 @@ public static ByteBuffer createPacket(int maxSize, Status status, int appId, int
var buffer = ByteBuffer.allocate(maxSize);

buffer.putInt(MAGIC_HEADER_VERSIONED);
buffer.put((byte) VERSION_1.ordinal()); // protocol version
if (fingerprint.getLength() == 32)
{
buffer.put((byte) VERSION_2.ordinal()); // protocol version
}
else if (fingerprint.getLength() == 20)
{
buffer.put((byte) VERSION_1.ordinal()); // protocol version
}
else
{
throw new IllegalArgumentException("Unknown fingerprint size:" + fingerprint.getLength());
}
buffer.put((byte) 0);
buffer.put((byte) 0);
buffer.put((byte) 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ static Identifier deserialize(ByteBuf buf, Class<?> identifierClass)
}
}

static Identifier deserializeWithSize(ByteBuf buf, Class<?> identifierClass, int size)
{
try
{
//noinspection PrimitiveArrayArgumentToVarargsMethod
var identifier = (Identifier) identifierClass.getDeclaredConstructor(byte[].class).newInstance(ByteArraySerializer.deserialize(buf, size));
if (Arrays.equals(identifier.getNullIdentifier(), identifier.getBytes()))
{
return null;
}
return identifier;
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e)
{
throw new IllegalStateException(e.getMessage());
}
}

static int getIdentifierLength(Class<?> identifierClass)
{
try
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/io/xeres/app/xrs/serialization/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.netty.buffer.ByteBuf;
import io.xeres.app.database.model.gxs.GxsMetaAndData;
import io.xeres.common.id.Identifier;
import io.xeres.common.id.ProfileFingerprint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -265,6 +266,21 @@ public static Identifier deserializeIdentifier(ByteBuf buf, Class<?> identifierC
return IdentifierSerializer.deserialize(buf, identifierClass);
}

/**
* Deserializes an identifier while specifying its size.
* <p>
* This is required for some identifier that can have a varying size, like {@link ProfileFingerprint}.
*
* @param buf the buffer
* @param identifierClass the class of the identifier
* @param size the size to deserialize
* @return the identifier
*/
public static Identifier deserializeIdentifierWithSize(ByteBuf buf, Class<?> identifierClass, int size)
{
return IdentifierSerializer.deserializeWithSize(buf, identifierClass, size);
}

/**
* Serializes a byte array.
*
Expand Down

0 comments on commit a032cd5

Please sign in to comment.