我使用以下代码来读取存储在pcap文件中的 数据包 中的源和目标IP。 public void nextPacket(PcapPacket packet,String user){
Ip4 ip = new Ip4();
Ethernet eth=new Ethernet();
String sIP;
String dIP;
int totalLength=0;
if (packet.hasHeader(ip) == false){
return;
}
totalLength = totalLength+ ip.getPayloadLength();
sIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.source());
dIP = org.jnetpcap.packet.format.FormatUtils.ip(ip.destination());
System.out.println("SIP = "+sIP+" "+"destIP = "+dIP+" "+"Payload Length = "+ip.getPayloadLength());
System.out.println("Total Length = "+totalLength);
}
但它没有显示任何内容,有标题包。请帮帮我。
答案 0 :(得分:2)
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
final Ip4 ip = new Ip4();
public void nextPacket(PcapPacket packet, String user) {
if(packet.hasHeader(Ip4.ID)){
packet.getHeader(ip);
byte[] dIP = new byte[4], sIP = new byte[4];
dIP = packet.getHeader(ip).destination();
sIP = packet.getHeader(ip).source();
String sourceIP = FormatUtils.ip(sIP);
String destinationIP = FormatUtils.ip(dIP);
System.out.printf("tcp.ip_src=%s%n",sourceIP);
System.out.printf("tcp.ip_dest=%s%n",destinationIP);
}
}
};
答案 1 :(得分:0)
你的意思是什么?它只是返回,因为数据包没有标题或是否在你的代码中打印null?我不得不用这个小信息猜测数据包没有ip头。