IPv4Framer implements Framer<Packet, IPv4Packet> { @Override public IPv4Packet frame(final Packet parent, final Buffer payload) throws IOException { if (parent == null) { throw new IllegalArgumentException("The parent frame cannot be null"); } final Buffer headers = payload.readBytes(20); final byte b = headers.getByte(0); final int version = b >>> 5 & 0x0F; final int headerLength = b & 0x0F; final byte tos = headers.getByte(1); final int totalLength = headers.getUnsignedShort(2); int options = 0; if (headerLength > 5) { options = payload.readInt(); } final int tcpLength = payload.getReaderIndex() + totalLength - (headerLength * 4); final Buffer data = payload.slice(Math.min(tcpLength, payload.capacity())); return new IPv4PacketImpl(parent, headers, options, data); } IPv4Framer(); @Override Protocol getProtocol(); @Override IPv4Packet frame(final Packet parent, final Buffer payload); @Override boolean accept(final Buffer data);  }
@Test public void testFragmentedIpPacket() throws Exception { final EthernetFramer framer = new EthernetFramer(); final MACPacket frame = framer.frame(mock(PCapPacket.class), Buffers.wrap(RawData.fragmented)); final IPv4Packet ip = (IPv4Packet) frame.getPacket(Protocol.IPv4); assertThat(ip.isMoreFragmentsSet(), is(true)); }
@Test public void testPaddedIpPacket() throws Exception { final EthernetFramer framer = new EthernetFramer(); final MACPacket frame = framer.frame(mock(PCapPacket.class), Buffers.wrap(RawData.padded)); assertTrue(new String(frame.getPayload().getArray(), StandardCharsets.US_ASCII).endsWith("padding")); final UDPPacket udp = (UDPPacket) frame.getPacket(Protocol.UDP); assertThat(udp.getPayload().getArray(), is("data".getBytes(StandardCharsets.US_ASCII))); }
@Test public void testTruncatedIpPacket() throws Exception { final EthernetFramer framer = new EthernetFramer(); final MACPacket frame = framer.frame(mock(PCapPacket.class), Buffers.wrap(RawData.truncatedFrame)); frame.getPacket(Protocol.IPv4); }
@Test(expected = IllegalArgumentException.class) public void testIPFramerNoParent() throws Exception { final IPv4Framer framer = new IPv4Framer(); framer.frame(null, this.ipv4FrameBuffer); }