所以我在我的一个项目中使用了 jNetPcap来嗅探网络数据包。我的问题是如何从 UDP 数据包中提取 DNS 查询和响应?
如果有帮助的话, jNetPcap只是一个 Java 包装器。libpcap
我知道要提取我需要查看的 DNS 数据包port 53
,UDP 是否相同?
我目前正在使用这样的东西,然后处理 pcap 数据包:
Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
System.err.printf("Error while opening device for capture: " +
errbuf.toString());
return;
}
PcapBpfProgram program = new PcapBpfProgram();
String expression = "udp dst port 53";
int optimize = 0; // 0 = false
int netmask = 0xFFFFFF00; // 255.255.255.0
if (pcap.compile(program, expression, optimize, netmask) != Pcap.OK) {
System.err.println(pcap.getErr());
return;
}
if (pcap.setFilter(program) != Pcap.OK) {
System.err.println(pcap.getErr());
return;
}
System.out.println("Filter set !!! : " + expression);
// Receive packet from the loop
PcapPacketHandler<String> jpacketHandler = packet_handler();
还有其他过滤建议吗?
谢谢。