Skip to content

Commit

Permalink
Calculate full length for ICMP(v6) messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsivaja committed Jan 8, 2024
1 parent 15f8430 commit a6e1811
Show file tree
Hide file tree
Showing 25 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/hirsivaja/ip/icmp/EchoReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.ByteBuffer;

public interface IcmpMessage {
int BASE_LEN = 4;

IcmpType getType();
byte getCode();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/hirsivaja/ip/icmp/IcmpPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.ByteBuffer;

public interface Icmpv6Message {
int BASE_LEN = 4;

Icmpv6Type getType();
byte getCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit a6e1811

Please sign in to comment.