From a6e1811e07d44fcd495d7e72d648a388d0ea1402 Mon Sep 17 00:00:00 2001 From: Toni Lappalainen Date: Mon, 8 Jan 2024 12:14:46 +0200 Subject: [PATCH] Calculate full length for ICMP(v6) messages --- .../com/github/hirsivaja/ip/icmp/DestinationUnreachable.java | 2 +- src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java | 2 +- src/main/java/com/github/hirsivaja/ip/icmp/EchoRequest.java | 2 +- .../java/com/github/hirsivaja/ip/icmp/GenericIcmpMessage.java | 2 +- src/main/java/com/github/hirsivaja/ip/icmp/IcmpMessage.java | 1 + src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java | 4 ++-- .../github/hirsivaja/ip/icmpv6/DestinationUnreachable.java | 2 +- src/main/java/com/github/hirsivaja/ip/icmpv6/EchoRequest.java | 2 +- .../java/com/github/hirsivaja/ip/icmpv6/EchoResponse.java | 2 +- .../java/com/github/hirsivaja/ip/icmpv6/Icmpv6Message.java | 1 + .../java/com/github/hirsivaja/ip/icmpv6/Icmpv6Payload.java | 4 ++-- .../java/com/github/hirsivaja/ip/icmpv6/PacketTooBig.java | 2 +- .../java/com/github/hirsivaja/ip/icmpv6/ParameterProblem.java | 2 +- .../java/com/github/hirsivaja/ip/icmpv6/TimeExceeded.java | 2 +- .../com/github/hirsivaja/ip/icmpv6/mld/GenericMldMessage.java | 2 +- .../ip/icmpv6/mld/MulticastListenerQueryMessage.java | 2 +- .../ip/icmpv6/mld/MulticastListenerReportV2Message.java | 2 +- .../github/hirsivaja/ip/icmpv6/ndp/NeighborAdvertisement.java | 2 +- .../github/hirsivaja/ip/icmpv6/ndp/NeighborSolicitation.java | 2 +- .../com/github/hirsivaja/ip/icmpv6/ndp/RedirectMessage.java | 2 +- .../github/hirsivaja/ip/icmpv6/ndp/RouterAdvertisement.java | 2 +- .../github/hirsivaja/ip/icmpv6/ndp/RouterSolicitation.java | 2 +- .../com/github/hirsivaja/ip/icmpv6/rpl/RplControlMessage.java | 2 +- .../java/com/github/hirsivaja/ip/icmp/IcmpPayloadTest.java | 2 ++ .../com/github/hirsivaja/ip/icmpv6/Icmpv6PayloadTest.java | 2 ++ 25 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/DestinationUnreachable.java b/src/main/java/com/github/hirsivaja/ip/icmp/DestinationUnreachable.java index aa5327f..5dfb986 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/DestinationUnreachable.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/DestinationUnreachable.java @@ -19,7 +19,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static IcmpMessage decode(ByteBuffer in, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java b/src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java index 6322d99..2f59b92 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java @@ -22,7 +22,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static IcmpMessage decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/EchoRequest.java b/src/main/java/com/github/hirsivaja/ip/icmp/EchoRequest.java index ec6d8f8..9fd566d 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/EchoRequest.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/EchoRequest.java @@ -22,7 +22,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static IcmpMessage decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/GenericIcmpMessage.java b/src/main/java/com/github/hirsivaja/ip/icmp/GenericIcmpMessage.java index 96b2373..653cbf3 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/GenericIcmpMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/GenericIcmpMessage.java @@ -20,7 +20,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return payload.length; + return BASE_LEN + payload.length; } public static IcmpMessage decode(ByteBuffer in, IcmpType type, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/IcmpMessage.java b/src/main/java/com/github/hirsivaja/ip/icmp/IcmpMessage.java index 3075688..61e7234 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/IcmpMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/IcmpMessage.java @@ -3,6 +3,7 @@ import java.nio.ByteBuffer; public interface IcmpMessage { + int BASE_LEN = 4; IcmpType getType(); byte getCode(); diff --git a/src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java b/src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java index 5c01f07..77e3e14 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java +++ b/src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java @@ -26,7 +26,7 @@ public void encode(ByteBuffer out) { } private static byte[] getChecksumData(IcmpMessage message) { - ByteBuffer checksumBuf = ByteBuffer.allocate(4 + message.getLength()); + ByteBuffer checksumBuf = ByteBuffer.allocate(message.getLength()); checksumBuf.put(message.getType().getType()); checksumBuf.put(message.getCode()); checksumBuf.putShort((short) 0); @@ -38,7 +38,7 @@ private static byte[] getChecksumData(IcmpMessage message) { @Override public int getLength() { - return header.getLength() + 4 + message.getLength(); + return header.getLength() + message.getLength(); } public static Ipv4Payload decode(ByteBuffer in, Ipv4Header header) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/DestinationUnreachable.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/DestinationUnreachable.java index 83db71a..b3613b7 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/DestinationUnreachable.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/DestinationUnreachable.java @@ -22,7 +22,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoRequest.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoRequest.java index d1714d7..4f76095 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoRequest.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoRequest.java @@ -22,7 +22,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoResponse.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoResponse.java index b3b9c2c..674a79e 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoResponse.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/EchoResponse.java @@ -22,7 +22,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Message.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Message.java index aad46a4..84a10f2 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Message.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Message.java @@ -10,6 +10,7 @@ import java.nio.ByteBuffer; public interface Icmpv6Message { + int BASE_LEN = 4; Icmpv6Type getType(); byte getCode(); diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Payload.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Payload.java index 27268f9..b42f88f 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Payload.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/Icmpv6Payload.java @@ -27,7 +27,7 @@ public void encode(ByteBuffer out) { } private static byte[] getChecksumData(Ipv6Header header, Icmpv6Message message) { - ByteBuffer checksumBuf = ByteBuffer.allocate(Ipv6Header.HEADER_LEN + 4 + message.getLength()); + ByteBuffer checksumBuf = ByteBuffer.allocate(Ipv6Header.HEADER_LEN + message.getLength()); checksumBuf.put(header.getPseudoHeader()); checksumBuf.put(message.getType().getType()); checksumBuf.put(message.getCode()); @@ -40,7 +40,7 @@ private static byte[] getChecksumData(Ipv6Header header, Icmpv6Message message) @Override public int getLength() { - return header.getLength() + 4 + message.getLength(); + return header.getLength() + message.getLength(); } public static Ipv6Payload decode(ByteBuffer in, Ipv6Header header) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/PacketTooBig.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/PacketTooBig.java index 1fc6a6d..d6f49d9 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/PacketTooBig.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/PacketTooBig.java @@ -21,7 +21,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ParameterProblem.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ParameterProblem.java index b0ca972..4398dae 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ParameterProblem.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ParameterProblem.java @@ -21,7 +21,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/TimeExceeded.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/TimeExceeded.java index 925cd41..e979948 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/TimeExceeded.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/TimeExceeded.java @@ -19,7 +19,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + payload.length; + return BASE_LEN + 4 + payload.length; } public static Icmpv6Message decode(ByteBuffer in, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/GenericMldMessage.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/GenericMldMessage.java index 56a4335..c1deaff 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/GenericMldMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/GenericMldMessage.java @@ -28,7 +28,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 20; + return BASE_LEN + 20; } public static Icmpv6Message decode(ByteBuffer in, Icmpv6Type type, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerQueryMessage.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerQueryMessage.java index 9058b82..fce8ef5 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerQueryMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerQueryMessage.java @@ -41,7 +41,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 24 + (sourceAddresses.length * 16); + return BASE_LEN + 24 + (sourceAddresses.length * 16); } public static Icmpv6Message decode(ByteBuffer in, Icmpv6Type type, byte code) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerReportV2Message.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerReportV2Message.java index 4944113..64c99d4 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerReportV2Message.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/mld/MulticastListenerReportV2Message.java @@ -23,7 +23,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - int length = 4; + int length = BASE_LEN + 4; for(MulticastAccessRecord multicastAccessRecord : multicastAccessRecords) { length += multicastAccessRecord.getLength(); } diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborAdvertisement.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborAdvertisement.java index 53b6d07..f222e37 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborAdvertisement.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborAdvertisement.java @@ -31,7 +31,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 20 + options.stream().mapToInt(NdpOption::getLength).sum(); + return BASE_LEN + 20 + options.stream().mapToInt(NdpOption::getLength).sum(); } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborSolicitation.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborSolicitation.java index e46f851..620aa52 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborSolicitation.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/NeighborSolicitation.java @@ -29,7 +29,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 20 + options.stream().mapToInt(NdpOption::getLength).sum(); + return BASE_LEN + 20 + options.stream().mapToInt(NdpOption::getLength).sum(); } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RedirectMessage.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RedirectMessage.java index e3619f0..a888809 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RedirectMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RedirectMessage.java @@ -32,7 +32,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 36 + options.stream().mapToInt(NdpOption::getLength).sum(); + return BASE_LEN + 36 + options.stream().mapToInt(NdpOption::getLength).sum(); } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterAdvertisement.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterAdvertisement.java index 57f3410..44f68cc 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterAdvertisement.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterAdvertisement.java @@ -40,7 +40,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 12 + options.stream().mapToInt(NdpOption::getLength).sum(); + return BASE_LEN + 12 + options.stream().mapToInt(NdpOption::getLength).sum(); } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterSolicitation.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterSolicitation.java index bb0731a..754af92 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterSolicitation.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/ndp/RouterSolicitation.java @@ -25,7 +25,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return 4 + options.stream().mapToInt(NdpOption::getLength).sum(); + return BASE_LEN + 4 + options.stream().mapToInt(NdpOption::getLength).sum(); } public static Icmpv6Message decode(ByteBuffer in) { diff --git a/src/main/java/com/github/hirsivaja/ip/icmpv6/rpl/RplControlMessage.java b/src/main/java/com/github/hirsivaja/ip/icmpv6/rpl/RplControlMessage.java index 82e3deb..78404bd 100644 --- a/src/main/java/com/github/hirsivaja/ip/icmpv6/rpl/RplControlMessage.java +++ b/src/main/java/com/github/hirsivaja/ip/icmpv6/rpl/RplControlMessage.java @@ -21,7 +21,7 @@ public void encode(ByteBuffer out) { @Override public int getLength() { - return payload.getLength(); + return BASE_LEN + payload.getLength(); } public static Icmpv6Message decode(ByteBuffer in, RplPayloadType code) { diff --git a/src/test/java/com/github/hirsivaja/ip/icmp/IcmpPayloadTest.java b/src/test/java/com/github/hirsivaja/ip/icmp/IcmpPayloadTest.java index 5f90567..cf755f6 100644 --- a/src/test/java/com/github/hirsivaja/ip/icmp/IcmpPayloadTest.java +++ b/src/test/java/com/github/hirsivaja/ip/icmp/IcmpPayloadTest.java @@ -19,6 +19,8 @@ public void echoRequestTest() { Assert.assertTrue(((IcmpPayload) ipv4Payload).getMessage() instanceof EchoRequest); Assert.assertEquals(0x13C2, ((EchoRequest) ((IcmpPayload) ipv4Payload).getMessage()).getIdentifier()); Assert.assertEquals(0x0001, ((EchoRequest) ((IcmpPayload) ipv4Payload).getMessage()).getSequenceNumber()); + Assert.assertEquals(1408, ((IcmpPayload) ipv4Payload).getMessage().getLength()); + Assert.assertEquals(1428, ipv4Payload.getLength()); Assert.assertArrayEquals(reqBytes, TestUtils.toBytes(ipv4Payload)); } diff --git a/src/test/java/com/github/hirsivaja/ip/icmpv6/Icmpv6PayloadTest.java b/src/test/java/com/github/hirsivaja/ip/icmpv6/Icmpv6PayloadTest.java index 8274fb8..0197f67 100644 --- a/src/test/java/com/github/hirsivaja/ip/icmpv6/Icmpv6PayloadTest.java +++ b/src/test/java/com/github/hirsivaja/ip/icmpv6/Icmpv6PayloadTest.java @@ -26,6 +26,8 @@ public void echoRequestTest() { Assert.assertTrue(((Icmpv6Payload) ipv6Payload).getMessage() instanceof EchoRequest); Assert.assertEquals(0x7620, ((EchoRequest) ((Icmpv6Payload) ipv6Payload).getMessage()).getIdentifier()); Assert.assertEquals(0x0100, ((EchoRequest) ((Icmpv6Payload) ipv6Payload).getMessage()).getSequenceNumber()); + Assert.assertEquals(16, ((Icmpv6Payload) ipv6Payload).getMessage().getLength()); + Assert.assertEquals(56, ipv6Payload.getLength()); Assert.assertArrayEquals(reqBytes, TestUtils.toBytes(ipv6Payload)); }