IcmpDecoder extends AbstractPacketDecoder<Ipv4PacketReceived, IcmpPacketReceived> implements Ipv4PacketListener { @Override public IcmpPacketReceived decode(Ipv4PacketReceived ipv4PacketReceived) { IcmpPacketReceivedBuilder icmpReceivedBuilder = new IcmpPacketReceivedBuilder(); List<PacketChain> packetChainList = ipv4PacketReceived.getPacketChain(); Ipv4Packet ipv4Packet = (Ipv4Packet) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ipv4Packet.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ipv4PacketReceived.getPayload(); IcmpPacketBuilder builder = new IcmpPacketBuilder(); try { builder.setType(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 0, 8))); builder.setCode(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 8, 8))); builder.setCrc(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 16, 16))); builder.setIdentifier(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setSequenceNumber(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 16))); int payloadStartInBits = bitOffset + 64; int payloadEndInBits = data.length * NetUtils.NUM_BITS_IN_A_BYTE - payloadStartInBits - 4 * NetUtils.NUM_BITS_IN_A_BYTE; int start = payloadStartInBits / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + payloadEndInBits / NetUtils.NUM_BITS_IN_A_BYTE; builder.setPayloadOffset(start); builder.setPayloadLength(end - start); } catch (BufferException e) { LOG.debug("Exception while decoding ICMP packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); icmpReceivedBuilder.setPacketChain(packetChainList); icmpReceivedBuilder.setPayload(ipv4PacketReceived.getPayload()); return icmpReceivedBuilder.build(); } IcmpDecoder(NotificationProviderService notificationProviderService); @Override IcmpPacketReceived decode(Ipv4PacketReceived ipv4PacketReceived); @Override NotificationListener getConsumedNotificationListener(); @Override void onIpv4PacketReceived(Ipv4PacketReceived notification); @Override boolean canDecode(Ipv4PacketReceived ipv4PacketReceived);  }
@Test public void testDecode() throws Exception { byte[] ethPayload = { 0x00, 0x0c, (byte) 0xce, 0x13, (byte) 0xb9, (byte) 0xa0, 0x00, 0x22, 0x5f, 0x3f, (byte) 0x98, (byte) 0x91, 0x08, 0x00, 0x45, 0x00, 0x00, 0x3c, (byte) 0xc6, 0x3e, 0x00, 0x00, (byte) 0x80, 0x01, (byte) 0xf2, (byte) 0xd7, (byte) 0xc0, (byte) 0xa8, 0x00, 0x59, (byte) 0xc0, (byte) 0xa8, 0x00, 0x01, 0x08, 0x00, 0x42, 0x5c, 0x02, 0x00, 0x09, 0x00, 0, 0, 0, 0 }; NotificationProviderService npServiceMock = Mockito.mock(NotificationProviderService.class); ArrayList<PacketChain> packetChainList = new ArrayList<>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new EthernetPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder().setPacket(new Ipv4PacketBuilder() .setPayloadOffset(34).build()).build()); IcmpPacketReceived notification = new IcmpDecoder(npServiceMock) .decode(new Ipv4PacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload) .build()); IcmpPacket icmpPacket = (IcmpPacket) notification.getPacketChain().get(3).getPacket(); assertEquals(8, icmpPacket.getType().intValue()); assertEquals(0, icmpPacket.getCode().intValue()); assertEquals(0x425c, icmpPacket.getCrc().intValue()); assertEquals(512, icmpPacket.getIdentifier().intValue()); assertEquals(2304, icmpPacket.getSequenceNumber().intValue()); assertTrue(Arrays.equals(ethPayload, notification.getPayload())); }