diff --git a/core/src/main/scala/com/nutomic/ensichat/core/header/AbstractHeader.scala b/core/src/main/scala/com/nutomic/ensichat/core/header/AbstractHeader.scala index 97e1073..70a4752 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/header/AbstractHeader.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/header/AbstractHeader.scala @@ -10,6 +10,8 @@ object AbstractHeader { val InitialForwardingTokens = 3 + val MaxForwardingTokens = 6 + val Version = 0 private[header] val Length = 10 + 2 * Address.Length diff --git a/core/src/main/scala/com/nutomic/ensichat/core/header/MessageHeader.scala b/core/src/main/scala/com/nutomic/ensichat/core/header/MessageHeader.scala index 2f859a4..3b9a240 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/header/MessageHeader.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/header/MessageHeader.scala @@ -24,6 +24,8 @@ object MessageHeader { throw new ReadMessageException("Failed to parse message with unsupported version " + version) val protocolType = BufferUtils.getUnsignedByte(b) val tokens = BufferUtils.getUnsignedByte(b) + if (tokens > AbstractHeader.MaxForwardingTokens) + throw new ReadMessageException(s"Received message with too many forwarding tokens ($tokens tokens)") val hopCount = BufferUtils.getUnsignedByte(b) val length = BufferUtils.getUnsignedInt(b) diff --git a/core/src/test/scala/com/nutomic/ensichat/core/header/ContentHeaderTest.scala b/core/src/test/scala/com/nutomic/ensichat/core/header/ContentHeaderTest.scala index bb94cd3..9717959 100644 --- a/core/src/test/scala/com/nutomic/ensichat/core/header/ContentHeaderTest.scala +++ b/core/src/test/scala/com/nutomic/ensichat/core/header/ContentHeaderTest.scala @@ -10,16 +10,16 @@ import org.junit.Assert._ object ContentHeaderTest { val h1 = new ContentHeader(AddressTest.a1, AddressTest.a2, 1234, - Text.Type, Some(123), Some(new GregorianCalendar(1970, 1, 1).getTime), 5) + Text.Type, Some(123), Some(new GregorianCalendar(1970, 1, 1).getTime), 3) val h2 = new ContentHeader(AddressTest.a1, AddressTest.a3, - 30000, Text.Type, Some(8765), Some(new GregorianCalendar(2014, 6, 10).getTime), 20) + 30000, Text.Type, Some(8765), Some(new GregorianCalendar(2014, 6, 10).getTime), 2) val h3 = new ContentHeader(AddressTest.a4, AddressTest.a2, - 250, Text.Type, Some(77), Some(new GregorianCalendar(2020, 11, 11).getTime), 123) + 250, Text.Type, Some(77), Some(new GregorianCalendar(2020, 11, 11).getTime), 1) val h4 = new ContentHeader(Address.Null, Address.Broadcast, - ContentHeader.SeqNumRange.last, 0, Some(0xffff), Some(new Date(0L)), 0xff) + ContentHeader.SeqNumRange.last, 0, Some(0xffff), Some(new Date(0L)), 6) val h5 = new ContentHeader(Address.Broadcast, Address.Null, 0, 0xff, Some(0), Some(new Date(0xffffffffL)), 0) diff --git a/core/src/test/scala/com/nutomic/ensichat/core/header/MessageHeaderTest.scala b/core/src/test/scala/com/nutomic/ensichat/core/header/MessageHeaderTest.scala index e1c660b..8cf0e48 100644 --- a/core/src/test/scala/com/nutomic/ensichat/core/header/MessageHeaderTest.scala +++ b/core/src/test/scala/com/nutomic/ensichat/core/header/MessageHeaderTest.scala @@ -7,11 +7,11 @@ import org.junit.Assert._ object MessageHeaderTest { - val h1 = new MessageHeader(ContentHeader.ContentMessageType, AddressTest.a1, AddressTest.a2, 1234, + val h1 = new MessageHeader(ContentHeader.ContentMessageType, AddressTest.a1, AddressTest.a2, 3, 0) val h2 = new MessageHeader(ContentHeader.ContentMessageType, Address.Null, Address.Broadcast, - ContentHeader.SeqNumRange.last, 0xff, 3) + ContentHeader.SeqNumRange.last, 6, 3) val h3 = new MessageHeader(ContentHeader.ContentMessageType, Address.Broadcast, Address.Null, 0, 3)