@Test public void testDecode_ExtensionHeader() throws Exception { byte[] payload = { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67, (byte)0x86, (byte)0xdd, 0x60, (byte)0xf0, 0x00, 0x01, 0x00, 0x0d, 0x00, (byte)0x0f, 0x01, 0x23, 0x45, 0x67, (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef, (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98, 0x76, 0x54, 0x32, 0x10, (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef, 0x11, 0x00, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x01, 0x02, 0x03, 0x04, 0x05, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }; 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().setPayloadOffset(14).build()).build()); Ipv6PacketReceived notification = new Ipv6Decoder(npServiceMock).decode( new EthernetPacketReceivedBuilder().setPacketChain(packetChainList).setPayload(payload).build()); Ipv6Packet ipv6Packet = (Ipv6Packet) notification.getPacketChain().get(2).getPacket(); assertEquals(6, ipv6Packet.getVersion().intValue()); assertEquals(3, ipv6Packet.getDscp().getValue().intValue()); assertEquals(3, ipv6Packet.getEcn().intValue()); assertEquals(1, ipv6Packet.getFlowLabel().intValue()); assertEquals(13, ipv6Packet.getIpv6Length().intValue()); assertEquals(KnownIpProtocols.Hopopt, ipv6Packet.getNextHeader()); assertEquals(15, ipv6Packet.getHopLimit().intValue()); assertEquals("123:4567:89ab:cdef:fedc:ba98:7654:3210", ipv6Packet.getSourceIpv6().getValue()); assertEquals("fedc:ba98:7654:3210:123:4567:89ab:cdef", ipv6Packet.getDestinationIpv6().getValue()); assertEquals(KnownIpProtocols.Udp, ipv6Packet.getExtensionHeaders().get(0).getNextHeader()); assertEquals(0, ipv6Packet.getExtensionHeaders().get(0).getLength().intValue()); assertTrue( Arrays.equals(ipv6Packet.getExtensionHeaders().get(0).getData(), Arrays.copyOfRange(payload, 56, 62))); assertEquals(54, ipv6Packet.getPayloadOffset().intValue()); assertEquals(13, ipv6Packet.getPayloadLength().intValue()); assertTrue(Arrays.equals(payload, notification.getPayload())); }
@Override public Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived) { Ipv6PacketReceivedBuilder ipv6ReceivedBuilder = new Ipv6PacketReceivedBuilder(); List<PacketChain> packetChainList = ethernetPacketReceived.getPacketChain(); EthernetPacket ethernetPacket = (EthernetPacket) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ethernetPacket.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ethernetPacketReceived.getPayload(); Ipv6PacketBuilder builder = new Ipv6PacketBuilder(); try { builder.setVersion(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset, 4))); if (builder.getVersion().intValue() != 6) { LOG.debug("Version should be 6, but is {}", builder.getVersion()); } builder.setDscp(new Dscp(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 4, 6)))); builder.setEcn(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 10, 2))); builder.setFlowLabel(BitBufferHelper.getLong(BitBufferHelper.getBits(data, bitOffset + 12, 20))); builder.setIpv6Length(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setNextHeader(KnownIpProtocols .forValue(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 8)))); builder.setHopLimit(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 56, 8))); builder.setSourceIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 64, 128)).getHostAddress())); builder.setDestinationIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 192, 128)).getHostAddress())); builder.setPayloadOffset((320 + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE); builder.setPayloadLength(builder.getIpv6Length()); List<ExtensionHeaders> extensionHeaders = new ArrayList<>(); KnownIpProtocols nextHeader = builder.getNextHeader(); int extHeaderOffset = 0; while (nextHeader != null && !nextHeader.equals(KnownIpProtocols.Tcp) && !nextHeader.equals(KnownIpProtocols.Udp)) { short nextHeaderType = BitBufferHelper .getShort(BitBufferHelper.getBits(data, 320 + extHeaderOffset + bitOffset, 8)); nextHeader = KnownIpProtocols.forValue(nextHeaderType); int octetLength = BitBufferHelper .getInt(BitBufferHelper.getBits(data, 328 + extHeaderOffset + bitOffset, 8)); int start = (336 + extHeaderOffset + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + 6 + octetLength; extensionHeaders.add(new ExtensionHeadersBuilder().setNextHeader(nextHeader).setLength(octetLength) .setData(Arrays.copyOfRange(data, start, end)).build()); extHeaderOffset += 64 + octetLength * NetUtils.NUM_BITS_IN_A_BYTE; } if (!extensionHeaders.isEmpty()) { builder.setExtensionHeaders(extensionHeaders); } } catch (BufferException | UnknownHostException e) { LOG.debug("Exception while decoding IPv4 packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); ipv6ReceivedBuilder.setPacketChain(packetChainList); ipv6ReceivedBuilder.setPayload(ethernetPacketReceived.getPayload()); return ipv6ReceivedBuilder.build(); }
Ipv6Decoder extends AbstractPacketDecoder<EthernetPacketReceived, Ipv6PacketReceived> implements EthernetPacketListener { @Override public Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived) { Ipv6PacketReceivedBuilder ipv6ReceivedBuilder = new Ipv6PacketReceivedBuilder(); List<PacketChain> packetChainList = ethernetPacketReceived.getPacketChain(); EthernetPacket ethernetPacket = (EthernetPacket) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ethernetPacket.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ethernetPacketReceived.getPayload(); Ipv6PacketBuilder builder = new Ipv6PacketBuilder(); try { builder.setVersion(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset, 4))); if (builder.getVersion().intValue() != 6) { LOG.debug("Version should be 6, but is {}", builder.getVersion()); } builder.setDscp(new Dscp(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 4, 6)))); builder.setEcn(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 10, 2))); builder.setFlowLabel(BitBufferHelper.getLong(BitBufferHelper.getBits(data, bitOffset + 12, 20))); builder.setIpv6Length(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setNextHeader(KnownIpProtocols .forValue(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 8)))); builder.setHopLimit(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 56, 8))); builder.setSourceIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 64, 128)).getHostAddress())); builder.setDestinationIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 192, 128)).getHostAddress())); builder.setPayloadOffset((320 + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE); builder.setPayloadLength(builder.getIpv6Length()); List<ExtensionHeaders> extensionHeaders = new ArrayList<>(); KnownIpProtocols nextHeader = builder.getNextHeader(); int extHeaderOffset = 0; while (nextHeader != null && !nextHeader.equals(KnownIpProtocols.Tcp) && !nextHeader.equals(KnownIpProtocols.Udp)) { short nextHeaderType = BitBufferHelper .getShort(BitBufferHelper.getBits(data, 320 + extHeaderOffset + bitOffset, 8)); nextHeader = KnownIpProtocols.forValue(nextHeaderType); int octetLength = BitBufferHelper .getInt(BitBufferHelper.getBits(data, 328 + extHeaderOffset + bitOffset, 8)); int start = (336 + extHeaderOffset + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + 6 + octetLength; extensionHeaders.add(new ExtensionHeadersBuilder().setNextHeader(nextHeader).setLength(octetLength) .setData(Arrays.copyOfRange(data, start, end)).build()); extHeaderOffset += 64 + octetLength * NetUtils.NUM_BITS_IN_A_BYTE; } if (!extensionHeaders.isEmpty()) { builder.setExtensionHeaders(extensionHeaders); } } catch (BufferException | UnknownHostException e) { LOG.debug("Exception while decoding IPv4 packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); ipv6ReceivedBuilder.setPacketChain(packetChainList); ipv6ReceivedBuilder.setPayload(ethernetPacketReceived.getPayload()); return ipv6ReceivedBuilder.build(); } }
Ipv6Decoder extends AbstractPacketDecoder<EthernetPacketReceived, Ipv6PacketReceived> implements EthernetPacketListener { @Override public Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived) { Ipv6PacketReceivedBuilder ipv6ReceivedBuilder = new Ipv6PacketReceivedBuilder(); List<PacketChain> packetChainList = ethernetPacketReceived.getPacketChain(); EthernetPacket ethernetPacket = (EthernetPacket) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ethernetPacket.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ethernetPacketReceived.getPayload(); Ipv6PacketBuilder builder = new Ipv6PacketBuilder(); try { builder.setVersion(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset, 4))); if (builder.getVersion().intValue() != 6) { LOG.debug("Version should be 6, but is {}", builder.getVersion()); } builder.setDscp(new Dscp(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 4, 6)))); builder.setEcn(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 10, 2))); builder.setFlowLabel(BitBufferHelper.getLong(BitBufferHelper.getBits(data, bitOffset + 12, 20))); builder.setIpv6Length(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setNextHeader(KnownIpProtocols .forValue(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 8)))); builder.setHopLimit(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 56, 8))); builder.setSourceIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 64, 128)).getHostAddress())); builder.setDestinationIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 192, 128)).getHostAddress())); builder.setPayloadOffset((320 + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE); builder.setPayloadLength(builder.getIpv6Length()); List<ExtensionHeaders> extensionHeaders = new ArrayList<>(); KnownIpProtocols nextHeader = builder.getNextHeader(); int extHeaderOffset = 0; while (nextHeader != null && !nextHeader.equals(KnownIpProtocols.Tcp) && !nextHeader.equals(KnownIpProtocols.Udp)) { short nextHeaderType = BitBufferHelper .getShort(BitBufferHelper.getBits(data, 320 + extHeaderOffset + bitOffset, 8)); nextHeader = KnownIpProtocols.forValue(nextHeaderType); int octetLength = BitBufferHelper .getInt(BitBufferHelper.getBits(data, 328 + extHeaderOffset + bitOffset, 8)); int start = (336 + extHeaderOffset + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + 6 + octetLength; extensionHeaders.add(new ExtensionHeadersBuilder().setNextHeader(nextHeader).setLength(octetLength) .setData(Arrays.copyOfRange(data, start, end)).build()); extHeaderOffset += 64 + octetLength * NetUtils.NUM_BITS_IN_A_BYTE; } if (!extensionHeaders.isEmpty()) { builder.setExtensionHeaders(extensionHeaders); } } catch (BufferException | UnknownHostException e) { LOG.debug("Exception while decoding IPv4 packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); ipv6ReceivedBuilder.setPacketChain(packetChainList); ipv6ReceivedBuilder.setPayload(ethernetPacketReceived.getPayload()); return ipv6ReceivedBuilder.build(); } Ipv6Decoder(NotificationProviderService notificationProviderService); }
Ipv6Decoder extends AbstractPacketDecoder<EthernetPacketReceived, Ipv6PacketReceived> implements EthernetPacketListener { @Override public Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived) { Ipv6PacketReceivedBuilder ipv6ReceivedBuilder = new Ipv6PacketReceivedBuilder(); List<PacketChain> packetChainList = ethernetPacketReceived.getPacketChain(); EthernetPacket ethernetPacket = (EthernetPacket) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ethernetPacket.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ethernetPacketReceived.getPayload(); Ipv6PacketBuilder builder = new Ipv6PacketBuilder(); try { builder.setVersion(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset, 4))); if (builder.getVersion().intValue() != 6) { LOG.debug("Version should be 6, but is {}", builder.getVersion()); } builder.setDscp(new Dscp(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 4, 6)))); builder.setEcn(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 10, 2))); builder.setFlowLabel(BitBufferHelper.getLong(BitBufferHelper.getBits(data, bitOffset + 12, 20))); builder.setIpv6Length(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setNextHeader(KnownIpProtocols .forValue(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 8)))); builder.setHopLimit(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 56, 8))); builder.setSourceIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 64, 128)).getHostAddress())); builder.setDestinationIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 192, 128)).getHostAddress())); builder.setPayloadOffset((320 + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE); builder.setPayloadLength(builder.getIpv6Length()); List<ExtensionHeaders> extensionHeaders = new ArrayList<>(); KnownIpProtocols nextHeader = builder.getNextHeader(); int extHeaderOffset = 0; while (nextHeader != null && !nextHeader.equals(KnownIpProtocols.Tcp) && !nextHeader.equals(KnownIpProtocols.Udp)) { short nextHeaderType = BitBufferHelper .getShort(BitBufferHelper.getBits(data, 320 + extHeaderOffset + bitOffset, 8)); nextHeader = KnownIpProtocols.forValue(nextHeaderType); int octetLength = BitBufferHelper .getInt(BitBufferHelper.getBits(data, 328 + extHeaderOffset + bitOffset, 8)); int start = (336 + extHeaderOffset + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + 6 + octetLength; extensionHeaders.add(new ExtensionHeadersBuilder().setNextHeader(nextHeader).setLength(octetLength) .setData(Arrays.copyOfRange(data, start, end)).build()); extHeaderOffset += 64 + octetLength * NetUtils.NUM_BITS_IN_A_BYTE; } if (!extensionHeaders.isEmpty()) { builder.setExtensionHeaders(extensionHeaders); } } catch (BufferException | UnknownHostException e) { LOG.debug("Exception while decoding IPv4 packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); ipv6ReceivedBuilder.setPacketChain(packetChainList); ipv6ReceivedBuilder.setPayload(ethernetPacketReceived.getPayload()); return ipv6ReceivedBuilder.build(); } Ipv6Decoder(NotificationProviderService notificationProviderService); @Override Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived); @Override NotificationListener getConsumedNotificationListener(); @Override void onEthernetPacketReceived(EthernetPacketReceived notification); @Override boolean canDecode(EthernetPacketReceived ethernetPacketReceived); }
Ipv6Decoder extends AbstractPacketDecoder<EthernetPacketReceived, Ipv6PacketReceived> implements EthernetPacketListener { @Override public Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived) { Ipv6PacketReceivedBuilder ipv6ReceivedBuilder = new Ipv6PacketReceivedBuilder(); List<PacketChain> packetChainList = ethernetPacketReceived.getPacketChain(); EthernetPacket ethernetPacket = (EthernetPacket) packetChainList.get(packetChainList.size() - 1).getPacket(); int bitOffset = ethernetPacket.getPayloadOffset() * NetUtils.NUM_BITS_IN_A_BYTE; byte[] data = ethernetPacketReceived.getPayload(); Ipv6PacketBuilder builder = new Ipv6PacketBuilder(); try { builder.setVersion(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset, 4))); if (builder.getVersion().intValue() != 6) { LOG.debug("Version should be 6, but is {}", builder.getVersion()); } builder.setDscp(new Dscp(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 4, 6)))); builder.setEcn(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 10, 2))); builder.setFlowLabel(BitBufferHelper.getLong(BitBufferHelper.getBits(data, bitOffset + 12, 20))); builder.setIpv6Length(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 32, 16))); builder.setNextHeader(KnownIpProtocols .forValue(BitBufferHelper.getInt(BitBufferHelper.getBits(data, bitOffset + 48, 8)))); builder.setHopLimit(BitBufferHelper.getShort(BitBufferHelper.getBits(data, bitOffset + 56, 8))); builder.setSourceIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 64, 128)).getHostAddress())); builder.setDestinationIpv6(Ipv6Address.getDefaultInstance( InetAddress.getByAddress(BitBufferHelper.getBits(data, bitOffset + 192, 128)).getHostAddress())); builder.setPayloadOffset((320 + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE); builder.setPayloadLength(builder.getIpv6Length()); List<ExtensionHeaders> extensionHeaders = new ArrayList<>(); KnownIpProtocols nextHeader = builder.getNextHeader(); int extHeaderOffset = 0; while (nextHeader != null && !nextHeader.equals(KnownIpProtocols.Tcp) && !nextHeader.equals(KnownIpProtocols.Udp)) { short nextHeaderType = BitBufferHelper .getShort(BitBufferHelper.getBits(data, 320 + extHeaderOffset + bitOffset, 8)); nextHeader = KnownIpProtocols.forValue(nextHeaderType); int octetLength = BitBufferHelper .getInt(BitBufferHelper.getBits(data, 328 + extHeaderOffset + bitOffset, 8)); int start = (336 + extHeaderOffset + bitOffset) / NetUtils.NUM_BITS_IN_A_BYTE; int end = start + 6 + octetLength; extensionHeaders.add(new ExtensionHeadersBuilder().setNextHeader(nextHeader).setLength(octetLength) .setData(Arrays.copyOfRange(data, start, end)).build()); extHeaderOffset += 64 + octetLength * NetUtils.NUM_BITS_IN_A_BYTE; } if (!extensionHeaders.isEmpty()) { builder.setExtensionHeaders(extensionHeaders); } } catch (BufferException | UnknownHostException e) { LOG.debug("Exception while decoding IPv4 packet", e.getMessage()); } packetChainList.add(new PacketChainBuilder().setPacket(builder.build()).build()); ipv6ReceivedBuilder.setPacketChain(packetChainList); ipv6ReceivedBuilder.setPayload(ethernetPacketReceived.getPayload()); return ipv6ReceivedBuilder.build(); } Ipv6Decoder(NotificationProviderService notificationProviderService); @Override Ipv6PacketReceived decode(EthernetPacketReceived ethernetPacketReceived); @Override NotificationListener getConsumedNotificationListener(); @Override void onEthernetPacketReceived(EthernetPacketReceived notification); @Override boolean canDecode(EthernetPacketReceived ethernetPacketReceived);  }