我想使用jpcap发送UDP数据包。
我的代码是:
PcapNetworkInterface nif = Pcaps.getDevByName(args[0]);
int snapLen = 65536;
PromiscuousMode mode = PromiscuousMode.NONPROMISCUOUS;
int timeout = 10000;
PcapHandle handle = nif.openLive(snapLen, mode, timeout);
UdpPort srcPort = new UdpPort((short)1002,"");
UdpPort dstPort = new UdpPort((short)2001, "");;
short length = (short)12;
short checksum = (short)0xABCD;
UnknownPacket.Builder unknownb = new UnknownPacket.Builder();
unknownb.rawData(new byte[] { (byte)0, (byte)1, (byte)2, (byte)3 });
UdpPacket.Builder b = new UdpPacket.Builder();
b.dstPort(dstPort)
.srcPort(srcPort)
.length(length)
.checksum(checksum)
.correctChecksumAtBuild(false)
.correctLengthAtBuild(false)
.payloadBuilder(unknownb);
EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
etherBuilder.dstAddr(MacAddress.getByName("FF:FF:FF:FF:FF:FF"))
.srcAddr(MacAddress.getByName("FF:FF:FF:FF:FF:FF"))
.type( EtherType.IPV4) //
.payloadBuilder(b)
.paddingAtBuild(true);
Packet p = etherBuilder.build();
int i=1;
while(true) {
handle.sendPacket(p);
System.out.println("send "+i);i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
执行完之后,我得到:
org.pcap4j.core.PcapNativeException:在pcap_sendpacket()中发生错误:
煮熟模式不支持发送数据包
在org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1242)
在org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1212)
这是c实现的链接:
https://github.com/frgtn/rpcapd-linux/blob/master/libpcap/pcap-linux.c#L1091
最佳答案
我用过https://gist.github.com/austinmarton/1922600
或其他具有jpcap功能的界面
关于java - jpcap发送数据包熟模式时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47349371/