Comments (3)
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:
org.jdrews.netcap.PacketAnalysis.count: 5
org.jdrews.netcap.PacketAnalysis.readTimeout: 10
org.jdrews.netcap.PacketAnalysis.snaplen: 65536
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
NIF[0]: \Device\NPF_{2BF655AC-5148-4E45-880A-C347550A9016}
: description: Realtek PCIe GBE Family Controller
NIF[1]: \Device\NPF_{6CAE8DF5-518E-4487-B7E5-130AA0E5A836}
: description: Microsoft
: address: /fe80:0:0:0:e5fe:a0ff:cff0:1a4c
: address: /fe80:0:0:0:e5fe:a0ff:cff0:1a4c
NIF[2]: \Device\NPF_{6E9128D0-0819-4253-82C2-DD9477179E77}
: description: Microsoft
: address: /fe80:0:0:0:711e:20b9:6a71:a872
: address: /192.168.3.35
NIF[3]: \Device\NPF_{0239C018-BC3B-493B-B3B3-5CD514F3B591}
: description: TAP-Win32 Adapter OAS
: address: /fe80:0:0:0:d9c9:2ac0:dae6:c0ac
: address: /0.0.0.0
NIF[4]: \Device\NPF_{ED2111D2-D451-4B4D-84FC-026C4C223468}
: description: Microsoft Corporation
: address: /fe80:0:0:0:d4f6:ff3b:d8e0:d443
: address: /0.0.0.0
NIF[5]: \Device\NPF_{740479C1-5477-4D1D-BA33-773F7763E646}
: description: Microsoft Corporation
: address: /10.9.8.177
Select a device number to capture packets, or enter 'q' to quit > 2
\Device\NPF_{6E9128D0-0819-4253-82C2-DD9477179E77}(Microsoft)
2014-05-11 16:11:43.209417
packetlength: 54
[data (54 bytes)]
Hex stream: c0 18 85 c4 6d ff c0 c1 c0 7a d8 fb 08 00 45 20 00 28 d3 b3 40 00 35 06 a0 d2 44 32 c9 2c c0 a8 03 23 7f 20 c4 c1 18 80 8b 77 b6 04 75 49 50 10 3f ff 8b 83 00 00
=================================================
2014-05-11 16:11:43.212146
packetlength: 61
[data (61 bytes)]
Hex stream: c0 18 85 c4 6d ff c0 c1 c0 7a d8 fb 08 00 45 20 00 2f ea 0b 40 00 35 06 8a 73 44 32 c9 2c c0 a8 03 23 7f 20 c4 c1 18 80 8b 77 b6 04 75 49 50 18 40 00 da 8c 00 00 65 31 74 9e b2 16 25
=================================================
2014-05-11 16:11:43.434085
packetlength: 207
[data (207 bytes)]
Hex stream: c0 18 85 c4 6d ff c0 c1 c0 7a d8 fb 08 00 45 00 00 c1 00 00 40 00 40 11 b2 b7 c0 a8 03 01 c0 a8 03 23 00 35 fb 48 00 ad 10 29 65 4c 81 80 00 01 00 04 00 00 00 00 0c 66 6f 6f 64 61 6e 64 64 72 69 6e 6b 04 74 69 6c 65 05 61 70 70 65 78 04 62 69 6e 67 03 63 6f 6d 00 00 01 00 01 c0 0c 00 05 00 01 00 00 0c e3 00 29 08 77 69 6c 64 63 61 72 64 08 61 70 70 65 78 2d 72 66 03 6d 73 6e 03 63 6f 6d 09 65 64 67 65 73 75 69 74 65 03 6e 65 74 00 c0 3e 00 05 00 01 00 00 0d 10 00 12 05 61 31 38 35 36 02 67 32 06 61 6b 61 6d 61 69 c0 62 c0 73 00 01 00 01 00 00 00 13 00 04 17 3e 6f 40 c0 73 00 01 00 01 00 00 00 13 00 04 17 3e 6f 48
=================================================
2014-05-11 16:11:43.475627
packetlength: 66
[data (66 bytes)]
Hex stream: c0 18 85 c4 6d ff c0 c1 c0 7a d8 fb 08 00 45 20 00 34 00 00 40 00 3a 06 f6 5a 17 3e 6f 40 c0 a8 03 23 00 50 c5 c1 d7 5c fb 2e 5f dc e2 da 80 12 39 08 10 61 00 00 02 04 05 b4 01 01 04 02 01 03 03 01
=================================================
2014-05-11 16:11:43.50824
packetlength: 54
[data (54 bytes)]
Hex stream: c0 18 85 c4 6d ff c0 c1 c0 7a d8 fb 08 00 45 20 00 28 1c 8e 40 00 3a 06 d9 d8 17 3e 6f 40 c0 a8 03 23 00 50 c5 c1 d7 5c fb 2f 5f dc e3 96 50 10 1e 9c 6a dd 00 00
=================================================
ps_recv: 7
ps_drop: 0
ps_ifdrop: 0
bs_capt: 0
from pcap4j.
You just need to add a packet factory module (i.e. pcap4j-packetfactory-propertiesbased.jar or pcap4j-packetfactory-static.jar) to class path.
from pcap4j.
That was the solution. Thanks!
from pcap4j.
Related Issues (20)
- Writing to a dump file HOT 13
- Interrupt pool Thread
- [Linux os][Windows "System32" Path.get] crash on init HOT 2
- Install native libs (Npcap/libpcap) programmatically automatically HOT 1
- win10pcap can't get the linklayerAddress,it's not compatible
- How can I get mac address? HOT 2
- support 802.1x?
- Unknow tcp option doesnt support reserved options
- Moving forward with 2.0.0?
- About the getNextPacketEx problem under linux
- with pcap4j find the process name which makes the network traffic in windows with java
- Having trouble retrieving package data HOT 2
- macos issues
- How to get source and destination Ip from TCP packet HOT 1
- Library does not initialise on Linux HOT 2
- Runing on the a linux service reports a null pointer exception
- 通过抓包或者分析数据包,如何将HTTP的请求和响应关联起来呢?
- Getting timestamp and src/dest IP addresses for UDP packet? HOT 5
- Ignore this...
- Decoding mDNS packets?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pcap4j.