-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
org.pcap4j.sample.Loop only shows Hex stream #20
Comments
While I have your attention, I was hoping you could help me discover how I'm doing this wrong. The below code never prints out "IpV4Packet: " or "Dot1qVlanTagPacket: ". I have a feeling the problem is closely related to the reason why sample.Loop only spits out a Hex stream. I followed the examples SendArpRequest.java and Loop.java. While debugging, I've discovered "packet" has no IpV4Packet class. But I was pinging this computer from somewhere else, so I'm reasonably sure at least one of the packets was an IPv4 packet. Any help you could offer would be greatly appreciated! Thanks! /*
* PacketAnalysis.java
*
* Updated: May 11, 2014
*/
package org.jdrews.netcap;
import java.io.IOException;
import java.sql.Timestamp;
import org.pcap4j.core.BpfProgram.BpfCompileMode;
import org.pcap4j.core.NotOpenException;
import org.pcap4j.core.PacketListener;
import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.PcapNativeException;
import org.pcap4j.core.PcapNetworkInterface;
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode;
import org.pcap4j.core.PcapStat;
import org.pcap4j.packet.Dot1qVlanTagPacket;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.Packet;
import org.pcap4j.util.NifSelector;
import com.sun.jna.Platform;
@SuppressWarnings("javadoc")
public class PacketAnalysis
{
private static final String COUNT_KEY
= PacketAnalysis.class.getName() + ".count";
private static final int COUNT
= Integer.getInteger(COUNT_KEY, 5);
private static final String READ_TIMEOUT_KEY
= PacketAnalysis.class.getName() + ".readTimeout";
private static final int READ_TIMEOUT
= Integer.getInteger(READ_TIMEOUT_KEY, 10); // [ms]
private static final String SNAPLEN_KEY
= PacketAnalysis.class.getName() + ".snaplen";
private static final int SNAPLEN
= Integer.getInteger(SNAPLEN_KEY, 65536); // [bytes]
public static void main(String[] args) throws PcapNativeException, NotOpenException
{
String filter = args.length != 0 ? args[0] : "";
System.out.println(COUNT_KEY + ": " + COUNT);
System.out.println(READ_TIMEOUT_KEY + ": " + READ_TIMEOUT);
System.out.println(SNAPLEN_KEY + ": " + SNAPLEN);
System.out.println("\n");
PcapNetworkInterface nif;
try
{
nif = new NifSelector().selectNetworkInterface();
}
catch (IOException e)
{
e.printStackTrace();
return;
}
if (nif == null)
{
return;
}
System.out.println(nif.getName() + "(" + nif.getDescription() + ")");
final PcapHandle handle
= nif.openLive(SNAPLEN, PromiscuousMode.PROMISCUOUS, READ_TIMEOUT);
if (filter.length() != 0)
{
handle.setFilter(
filter,
BpfCompileMode.OPTIMIZE
);
}
PacketListener listener
= new PacketListener()
{
public void gotPacket(Packet packet)
{
Timestamp ts = new Timestamp(handle.getTimestampInts() * 1000L);
ts.setNanos(handle.getTimestampMicros() * 1000);
System.out.println(ts);
if (packet.contains(IpV4Packet.class)) {
IpV4Packet ipv4p = packet.get(IpV4Packet.class);
System.out.println("IpV4Packet: " + ipv4p);
}
if (packet.contains(Dot1qVlanTagPacket.class)) {
Dot1qVlanTagPacket dot1qp = packet.get(Dot1qVlanTagPacket.class);
System.out.println("Dot1qVlanTagPacket: " + dot1qp);
}
System.out.println("packetlength: " + packet.length());
System.out.println(packet);
System.out.println("=================================================");
}
};
try
{
handle.loop(COUNT, listener);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
PcapStat ps = handle.getStat();
System.out.println("ps_recv: " + ps.getNumPacketsReceived());
System.out.println("ps_drop: " + ps.getNumPacketsDropped());
System.out.println("ps_ifdrop: " + ps.getNumPacketsDroppedByIf());
if (Platform.isWindows())
{
System.out.println("bs_capt: " + ps.getNumPacketsCaptured());
}
handle.close();
}
} And the output:
|
You just need to add a packet factory module (i.e. pcap4j-packetfactory-propertiesbased.jar or pcap4j-packetfactory-static.jar) to class path. |
That was the solution. Thanks! |
In the example documentation for org.pcap4j.sample.Loop it shows user readable descriptions of each packet. See link below:
https://github.com/kaitoy/pcap4j/blob/master/www/sample_Loop.md
But for some reason when I run it, I get just a hex stream. Perhaps I'm doing something wrong? Thanks for your help!
Windows 8, WinPCap 4.1.3
Java 1.6.0_45
The text was updated successfully, but these errors were encountered: