我想使用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
答案 0 :(得分:0)
我使用过https://gist.github.com/austinmarton/1922600
或其他与jpcap
一起使用的接口答案 1 :(得分:0)
我肯定迟到了,但我希望这段代码对其他人有帮助。
import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.PcapNetworkInterface;
import org.pcap4j.packet.*;
import org.pcap4j.packet.namednumber.EtherType;
import org.pcap4j.packet.namednumber.IpNumber;
import org.pcap4j.packet.namednumber.IpVersion;
import org.pcap4j.packet.namednumber.UdpPort;
import org.pcap4j.util.MacAddress;
import org.pcap4j.util.NifSelector;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
public class RAWUDP {
public static void main(String[] args) throws Exception {
byte[] pck = "Meow".getBytes();
UnknownPacket unknownPacket = UnknownPacket.newPacket(pck, 0, pck.length);
UdpPacket udpPacket = new UdpPacket.Builder()
.srcAddr(InetAddress.getByName("192.168.1.6"))
.dstAddr(InetAddress.getByName("192.168.1.6"))
.srcPort(UdpPort.getInstance((short) 54163))
.dstPort(UdpPort.getInstance((short) 9110))
.correctLengthAtBuild(true)
.correctChecksumAtBuild(true)
.payloadBuilder(unknownPacket.getBuilder())
.build();
IpV4Packet.Builder ipV4Builder = new IpV4Packet.Builder();
ipV4Builder.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte) 0))
.ttl((byte) 100)
.protocol(IpNumber.UDP)
.srcAddr((Inet4Address) InetAddress.getByName("192.168.1.6"))
.dstAddr((Inet4Address) InetAddress.getByName("192.168.1.6"))
.payloadBuilder(udpPacket.getBuilder())
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
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 handle4send = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);
EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
etherBuilder.dstAddr(MacAddress.getByName("00:A0:C9:14:C8:29", ":"))
.srcAddr(MacAddress.getByName("00:A0:C9:14:C8:29", ":"))
.type(EtherType.IPV4)
.payloadBuilder(ipV4Builder.build().getBuilder())
.paddingAtBuild(true);
Packet p = etherBuilder.build();
handle4send.sendPacket(p);
}
}
答案 2 :(得分:0)
似乎在其上发送数据包的设备是程序的参数。
在构造以太网数据包时,请确保它是以太网设备。请注意,“任何”设备不仅不是以太网设备,也根本不是设备-它实际上是“无设备”,这意味着它在所有设备上侦听,但不会在任何设备上发送。>