A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A pcap packet. Fully decoded packet that provides access to protocol headers as determined during the decoding process. A PcapPacket class is designed to work with pcap library. It can not be used to create a new packet from an external memory buffer that only contains packet data, such as preparing...
View Full Code of org.jnetpcap.packet.PcapPacket
* Second, setup a packet we're going to copy the captured contents into.
* Allocate 2K native memory block to hold both state and buffer. Notice
* that the packet has to be marked "final" in order for the JPacketHandler
* to be able to access that variable from within the loop.
**************************************************************************/
final PcapPacket result = new PcapPacket(2 * 1024);
/***************************************************************************
* Third, Enter our loop and count packets until we reach the index of the
* packet we are looking for.
**************************************************************************/
try {
pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<Pcap>() {
int i = 0;
public void nextPacket(PcapHeader header, JBuffer buffer, Pcap pcap) {
/*********************************************************************
* Forth, once we reach our packet transfer the capture data from our
* temporary, shared packet, to our preallocated permanent packet. The
* method transferStateAndDataTo will do a deep copy of the packet
* contents and state to the destination packet. The copy is done
* natively with memcpy. The packet content in destination packet is
* layout in memory as follows. At the front of the buffer is the
* packet_state_t structure followed immediately by the packet data
* buffer and its size is adjusted to the exact size of the temporary
* buffer. The remainder of the allocated memory block is unused, but
* needed to be allocated large enough to hold a decent size packet.
* To break out of the Pcap.loop we call Pcap.breakLoop().
********************************************************************/
if (i++ == index) {
PcapPacket packet = new PcapPacket(header, buffer);
packet.scan(JRegistry.mapDLTToId(pcap.datalink()));
System.out.println(packet.getState().toDebugString());
packet.transferStateAndDataTo(result);
pcap.breakloop();
return;
}
}
public void nextPacket(PcapPacket p1, Pcap user) {
i++;
p1.getHeader(ip1);
int c1 = ip1.calculateChecksum();
PcapPacket p2 = new PcapPacket(p1);
p2.getHeader(ip2);
int c2 = ip2.calculateChecksum();
if (c1 != c2) {
System.out.printf("#%d crc_before=%x crc_after=%x\n", i, c1, c2);
System.out.printf(
"P1: %s\nheader1=%s\n\nstate1=%s\npacket1=%s\n\nip1=%s\n", p1
.toHexdump(), p1.getCaptureHeader().toDebugString(), p1
.getState().toDebugString(), p1.toDebugString(), ip1
.toDebugString());
System.out.println("---------------------------");
System.out.printf(
"P2: %s\nheader2=%s\n\nstate2=%s\npacket2=%s\n\nip2=%s\n\n", p2
.toHexdump(), p2.getCaptureHeader().toDebugString(), p2
.getState().toDebugString(), p2.toDebugString(), ip2
.toDebugString());
System.out.println("p1-p2.memory.diff=\n"
+ FormatUtils.hexdump(DataUtils.diff(p1, p2)));
// + "00000002 00060000 00000000 00000000"
// + "00000002 00060000 00000000 00000000"
));
final PcapHeader header = new PcapHeader(buf.size(), buf.size());
PcapPacket packet = new PcapPacket(header, buf);
System.out.printf("injected packet size=%d bytes\n", buf.size());
for (int i = 0; i < COUNT; i++) {
PcapUtils.injectLoop(10000, JProtocol.ETHERNET_ID,
new PcapPacketHandler<String>() {
public void nextPacket(PcapPacket packet, String user) {
assertNotNull(packet);
count++;
b += packet.size();
h += packet.getState().getHeaderCount();
}
}, "", packet);
public void nextPacket(PcapHeader header, JBuffer buffer, Pcap pcap) {
assertNotNull(buffer);
final int id = JRegistry.mapDLTToId(pcap.datalink());
PcapPacket packet = new PcapPacket(header, buffer);
assertNotNull(packet);
packet.scan(id);
}
}, pcap);
}
for (int i = 0; i < COUNT; i++) {
loop(TEST_AFS, new PcapPacketHandler<Counter>() {
public void nextPacket(PcapPacket packet, Counter counter) {
counter.inc();
new PcapPacket(packet);
}
});
}
}
View Full Code of org.jnetpcap.packet.PcapPacket
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z