Skip to content

Commit

Permalink
Added PcapHandle.getOriginalLength().
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoy committed Dec 5, 2015
1 parent 8cf5a16 commit cde47fe
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Future
======
### New Features ###
* [Issues#49](https://github.com/kaitoy/pcap4j/issues/49): Add PcapHandle.getOriginalLength().

### Bug Fixes ###

Expand Down
55 changes: 49 additions & 6 deletions pcap4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,57 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/PacketFactoryBinder.class</exclude>
</excludes>
</configuration>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete>
<fileset
dir="${project.build.outputDirectory}"
includes="**/PacketFactoryBinder.class"
/>
</delete>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.8,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
Expand Down
6 changes: 6 additions & 0 deletions pcap4j-core/src/main/java/org/pcap4j/core/NativeMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ public static class pcap_pkthdr extends Structure {

public static final int TS_OFFSET;
public static final int CAPLEN_OFFSET;
public static final int LEN_OFFSET;

public timeval ts;// struct timeval
public int caplen; // bpf_u_int32
Expand All @@ -639,6 +640,7 @@ public static class pcap_pkthdr extends Structure {
pcap_pkthdr ph = new pcap_pkthdr();
TS_OFFSET = ph.fieldOffset("ts");
CAPLEN_OFFSET = ph.fieldOffset("caplen");
LEN_OFFSET = ph.fieldOffset("len");
}

public pcap_pkthdr() {}
Expand Down Expand Up @@ -673,6 +675,10 @@ static int getCaplen(Pointer p) {
return p.getInt(CAPLEN_OFFSET);
}

static int getLen(Pointer p) {
return p.getInt(LEN_OFFSET);
}

}

public static class timeval extends Structure {
Expand Down
14 changes: 14 additions & 0 deletions pcap4j-core/src/main/java/org/pcap4j/core/PcapHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class PcapHandle {
private final Pointer handle;
private final ThreadLocal<Timestamp> timestamps
= new ThreadLocal<Timestamp>();
private final ThreadLocal<Integer> originalLengths
= new ThreadLocal<Integer>();
private final ReentrantReadWriteLock handleLock = new ReentrantReadWriteLock(true);
private static final Object compileLock = new Object();

Expand Down Expand Up @@ -232,6 +234,12 @@ public TimestampPrecision getTimestampPrecision() {
*/
public Timestamp getTimestamp() { return timestamps.get(); }

/**
* @return the original length of the last packet
* captured by this handle in the current thread.
*/
public Integer getOriginalLength() { return originalLengths.get(); }

/**
*
* @return the dimension of the packet portion (in bytes) that is delivered to the application.
Expand Down Expand Up @@ -619,6 +627,7 @@ public byte[] getNextRawPacket() throws NotOpenException {
if (packet != null) {
Pointer headerP = header.getPointer();
timestamps.set(buildTimestamp(headerP));
originalLengths.set(pcap_pkthdr.getLen(headerP));
return packet.getByteArray(0, pcap_pkthdr.getCaplen(headerP));
}
else {
Expand Down Expand Up @@ -683,6 +692,7 @@ public byte[] getNextRawPacketEx()
}

timestamps.set(buildTimestamp(headerP));
originalLengths.set(pcap_pkthdr.getLen(headerP));
return dataP.getByteArray(0, pcap_pkthdr.getCaplen(headerP));
case -1:
throw new PcapNativeException(
Expand Down Expand Up @@ -1364,6 +1374,7 @@ public void got_packet(
Pointer args, Pointer header, final Pointer packet
) {
final Timestamp ts = buildTimestamp(header);
final int len = pcap_pkthdr.getLen(header);
final byte[] ba = packet.getByteArray(0, pcap_pkthdr.getCaplen(header));

try {
Expand All @@ -1372,6 +1383,7 @@ public void got_packet(
@Override
public void run() {
timestamps.set(ts);
originalLengths.set(len);
listener.gotPacket(
PacketFactories.getFactory(Packet.class, DataLinkType.class)
.newInstance(ba, 0, ba.length, dlt)
Expand Down Expand Up @@ -1403,6 +1415,7 @@ public void got_packet(
Pointer args, Pointer header, final Pointer packet
) {
final Timestamp ts = buildTimestamp(header);
final int len = pcap_pkthdr.getLen(header);
final byte[] ba = packet.getByteArray(0, pcap_pkthdr.getCaplen(header));

try {
Expand All @@ -1411,6 +1424,7 @@ public void got_packet(
@Override
public void run() {
timestamps.set(ts);
originalLengths.set(len);
listener.gotPacket(ba);
}
}
Expand Down
139 changes: 127 additions & 12 deletions pcap4j-core/src/test/java/org/pcap4j/core/PcapHandleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.namednumber.DataLinkType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,12 +31,9 @@ public static void tearDownAfterClass() throws Exception {

@Before
public void setUp() throws Exception {
try {
ph = Pcaps.findAllDevs().get(0)
.openLive(55555, PromiscuousMode.PROMISCUOUS, 100);
} catch (IndexOutOfBoundsException e) {
ph = Pcaps.openDead(DataLinkType.EN10MB, 2048);
}
ph = Pcaps.openOffline(
"src/test/resources/org/pcap4j/core/PcapHandleTest.pcap"
);
}

@After
Expand All @@ -47,26 +45,143 @@ public void tearDown() throws Exception {

@Test
public void testGetStats() throws Exception {
try {
if (ph != null) {
ph.close();
}

List<PcapNetworkInterface> nifs = Pcaps.findAllDevs();
if (nifs.isEmpty()) {
ph = Pcaps.openDead(DataLinkType.EN10MB, 2048);
try {
ph.getStats();
} catch (PcapNativeException e) {
assertEquals("Statistics aren't available from a pcap_open_dead pcap_t", e.getMessage());
}
}
else {
ph = nifs.get(0).openLive(55555, PromiscuousMode.PROMISCUOUS, 100);
PcapStat ps = ph.getStats();
assertNotNull(ps);
} catch (PcapNativeException e) {
assertEquals("Statistics aren't available from a pcap_open_dead pcap_t", e.getMessage());
}
}

@Test
public void testListDatalinks() throws Exception {
List<DataLinkType> list = ph.listDatalinks();
assertNotNull(list);
for (DataLinkType type: list) {
logger.info("A DLT supported by " + ph + ": " + type.toString());
}
assertEquals(1, list.size());
assertEquals(DataLinkType.EN10MB, list.get(0));
}

@Test
public void testSetDlt() throws Exception {
ph.setDlt(ph.getDlt());
}

@Test
public void testGetTimestamp() throws Exception {
assertNull(ph.getTimestamp());
ph.getNextPacket();
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}

@Test
public void testGetTimestampEx() throws Exception {
assertNull(ph.getTimestamp());
ph.getNextPacketEx();
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}

@Test
public void testGetTimestampRaw() throws Exception {
assertNull(ph.getTimestamp());
ph.getNextRawPacket();
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}

@Test
public void testGetTimestampLoop() throws Exception {
assertNull(ph.getTimestamp());
ph.loop(1, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}
});
}

@Test
public void testGetTimestampLoopRaw() throws Exception {
assertNull(ph.getTimestamp());
ph.loop(1, new RawPacketListener() {
@Override
public void gotPacket(byte[] packet) {
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}
});
}

@Test
public void testGetTimestampRawEx() throws Exception {
assertNull(ph.getTimestamp());
ph.getNextRawPacketEx();
assertEquals(1434220771517L, ph.getTimestamp().getTime());
}

@Test
public void testGetOriginalLength() throws Exception {
assertNull(ph.getOriginalLength());
Packet packet = ph.getNextPacket();
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length(), ph.getOriginalLength().intValue());
}

@Test
public void testGetOriginalLengthEx() throws Exception {
assertNull(ph.getOriginalLength());
Packet packet = ph.getNextPacketEx();
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length(), ph.getOriginalLength().intValue());
}

@Test
public void testGetOriginalLengthRaw() throws Exception {
assertNull(ph.getOriginalLength());
byte[] packet = ph.getNextRawPacket();
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length, ph.getOriginalLength().intValue());
}

@Test
public void testGetOriginalLengthRawEx() throws Exception {
assertNull(ph.getOriginalLength());
byte[] packet = ph.getNextRawPacketEx();
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length, ph.getOriginalLength().intValue());
}

@Test
public void testGetOriginalLengthLoop() throws Exception {
assertNull(ph.getOriginalLength());
ph.loop(1, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length(), ph.getOriginalLength().intValue());
}
});
}

@Test
public void testGetOriginalLengthLoopRaw() throws Exception {
assertNull(ph.getOriginalLength());
ph.loop(1, new RawPacketListener() {
@Override
public void gotPacket(byte[] packet) {
assertEquals(new Integer(74), ph.getOriginalLength());
assertEquals(packet.length, ph.getOriginalLength().intValue());
}
});
}

}
Binary file not shown.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</plugin>
</plugins>
</pluginManagement>

Expand Down

0 comments on commit cde47fe

Please sign in to comment.