@Test public void findIPStartEN10MB_8021Q() { byte[] packet = new byte[20]; PcapReader xreader = new PcapReader(PcapReader.LinkType.EN10MB); byte[] ethernetType8021Q = PcapReaderUtil.convertShort(PcapReader.ETHERNET_TYPE_8021Q); packet[12] = ethernetType8021Q[0]; packet[13] = ethernetType8021Q[1]; byte[] ethernetTypeIp = PcapReaderUtil.convertShort(PcapReader.ETHERNET_TYPE_IP); packet[16] = ethernetTypeIp[0]; packet[17] = ethernetTypeIp[1]; assertEquals(18, xreader.findIPStart(packet)); }
protected int findIPStart(byte[] packet) { int start = -1; switch (linkType) { case NULL: return 4; case EN10MB: start = ETHERNET_HEADER_SIZE; int etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET); if (etherType == ETHERNET_TYPE_8021Q) { etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET + 4); start += 4; } if (etherType == ETHERNET_TYPE_IP || etherType == ETHERNET_TYPE_IPV6) return start; break; case RAW: return 0; case LOOP: return 4; case LINUX_SLL: start = SLL_HEADER_BASE_SIZE; int sllAddressLength = PcapReaderUtil.convertShort(packet, SLL_ADDRESS_LENGTH_OFFSET); start += sllAddressLength; return start; } return -1; }
PcapReader implements Iterable<Packet> { protected int findIPStart(byte[] packet) { int start = -1; switch (linkType) { case NULL: return 4; case EN10MB: start = ETHERNET_HEADER_SIZE; int etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET); if (etherType == ETHERNET_TYPE_8021Q) { etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET + 4); start += 4; } if (etherType == ETHERNET_TYPE_IP || etherType == ETHERNET_TYPE_IPV6) return start; break; case RAW: return 0; case LOOP: return 4; case LINUX_SLL: start = SLL_HEADER_BASE_SIZE; int sllAddressLength = PcapReaderUtil.convertShort(packet, SLL_ADDRESS_LENGTH_OFFSET); start += sllAddressLength; return start; } return -1; } }
PcapReader implements Iterable<Packet> { protected int findIPStart(byte[] packet) { int start = -1; switch (linkType) { case NULL: return 4; case EN10MB: start = ETHERNET_HEADER_SIZE; int etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET); if (etherType == ETHERNET_TYPE_8021Q) { etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET + 4); start += 4; } if (etherType == ETHERNET_TYPE_IP || etherType == ETHERNET_TYPE_IPV6) return start; break; case RAW: return 0; case LOOP: return 4; case LINUX_SLL: start = SLL_HEADER_BASE_SIZE; int sllAddressLength = PcapReaderUtil.convertShort(packet, SLL_ADDRESS_LENGTH_OFFSET); start += sllAddressLength; return start; } return -1; } PcapReader(DataInputStream is); protected  PcapReader(LinkType lt); }
PcapReader implements Iterable<Packet> { protected int findIPStart(byte[] packet) { int start = -1; switch (linkType) { case NULL: return 4; case EN10MB: start = ETHERNET_HEADER_SIZE; int etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET); if (etherType == ETHERNET_TYPE_8021Q) { etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET + 4); start += 4; } if (etherType == ETHERNET_TYPE_IP || etherType == ETHERNET_TYPE_IPV6) return start; break; case RAW: return 0; case LOOP: return 4; case LINUX_SLL: start = SLL_HEADER_BASE_SIZE; int sllAddressLength = PcapReaderUtil.convertShort(packet, SLL_ADDRESS_LENGTH_OFFSET); start += sllAddressLength; return start; } return -1; } PcapReader(DataInputStream is); protected  PcapReader(LinkType lt); @Override Iterator<Packet> iterator(); }
PcapReader implements Iterable<Packet> { protected int findIPStart(byte[] packet) { int start = -1; switch (linkType) { case NULL: return 4; case EN10MB: start = ETHERNET_HEADER_SIZE; int etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET); if (etherType == ETHERNET_TYPE_8021Q) { etherType = PcapReaderUtil.convertShort(packet, ETHERNET_TYPE_OFFSET + 4); start += 4; } if (etherType == ETHERNET_TYPE_IP || etherType == ETHERNET_TYPE_IPV6) return start; break; case RAW: return 0; case LOOP: return 4; case LINUX_SLL: start = SLL_HEADER_BASE_SIZE; int sllAddressLength = PcapReaderUtil.convertShort(packet, SLL_ADDRESS_LENGTH_OFFSET); start += sllAddressLength; return start; } return -1; } PcapReader(DataInputStream is); protected  PcapReader(LinkType lt); @Override Iterator<Packet> iterator(); static final Log LOG; static final long MAGIC_NUMBER; static final int HEADER_SIZE; static final int PCAP_HEADER_SNAPLEN_OFFSET; static final int PCAP_HEADER_LINKTYPE_OFFSET; static final int PACKET_HEADER_SIZE; static final int TIMESTAMP_OFFSET; static final int TIMESTAMP_MICROS_OFFSET; static final int CAP_LEN_OFFSET; static final int ETHERNET_HEADER_SIZE; static final int ETHERNET_TYPE_OFFSET; static final int ETHERNET_TYPE_IP; static final int ETHERNET_TYPE_IPV6; static final int ETHERNET_TYPE_8021Q; static final int SLL_HEADER_BASE_SIZE; static final int SLL_ADDRESS_LENGTH_OFFSET; static final int IPV6_HEADER_SIZE; static final int IP_VHL_OFFSET; static final int IP_TTL_OFFSET; static final int IP_TOTAL_LEN_OFFSET; static final int IPV6_PAYLOAD_LEN_OFFSET; static final int IPV6_HOPLIMIT_OFFSET; static final int IP_PROTOCOL_OFFSET; static final int IPV6_NEXTHEADER_OFFSET; static final int IP_SRC_OFFSET; static final int IPV6_SRC_OFFSET; static final int IP_DST_OFFSET; static final int IPV6_DST_OFFSET; static final int IP_ID_OFFSET; static final int IPV6_ID_OFFSET; static final int IP_FLAGS_OFFSET; static final int IPV6_FLAGS_OFFSET; static final int IP_FRAGMENT_OFFSET; static final int IPV6_FRAGMENT_OFFSET; static final int UDP_HEADER_SIZE; static final int PROTOCOL_HEADER_SRC_PORT_OFFSET; static final int PROTOCOL_HEADER_DST_PORT_OFFSET; static final int PROTOCOL_HEADER_TCP_SEQ_OFFSET; static final int PROTOCOL_HEADER_TCP_ACK_OFFSET; static final int TCP_HEADER_DATA_OFFSET; static final String PROTOCOL_ICMP; static final String PROTOCOL_TCP; static final String PROTOCOL_UDP; static final String PROTOCOL_FRAGMENT; public byte[] pcapHeader; public byte[] pcapPacketHeader; public byte[] packetData; }