简体   繁体   English

jpcap 错误发送数据包熟模式

[英]jpcap error sending packets cooked mode

I want to send a UDP packet using jpcap.我想使用 jpcap 发送一个 UDP 数据包。

My code is:我的代码是:

    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) { 

        } 
    }

After executing that ,i get :执行后,我得到:

org.pcap4j.core.PcapNativeException: Error occured in pcap_sendpacket(): Sending packets isn't supported in cooked mode at org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1242) at org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1212) org.pcap4j.core.PcapNativeException:pcap_sendpacket() 中发生错误:在 org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1242) 的熟模式下不支持发送数据包在 org.pcap4j.core.PcapHandle。发送数据包(PcapHandle.java:1212)

And this is the link to c implementation:这是 c 实现的链接:

https://github.com/frgtn/rpcapd-linux/blob/master/libpcap/pcap-linux.c#L1091 https://github.com/frgtn/rpcapd-linux/blob/master/libpcap/pcap-linux.c#L1091

I have used https://gist.github.com/austinmarton/1922600我用过https://gist.github.com/austinmarton/1922600

or another interface that function with jpcap或其他与 jpcap 一起工作的接口

I am definitely late to answer but I hope this code helps other people.我肯定迟到了,但我希望这段代码可以帮助其他人。

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);
    }
}

It appears that the device on which you're sending the packet is an argument to the program.您发送数据包的设备似乎是程序的参数。

Make sure it's an Ethernet device, as you're constructing an Ethernet packet.确保它是以太网设备,因为您正在构建以太网数据包。 Note that the "any" device not only isn't an Ethernet device, it's not a device at all - it's really the "no device", meaning that it listens on all devices, but won't send on any devices.请注意,“任何”设备不仅不是以太网设备,而且根本不是设备 - 它实际上是“无设备”,这意味着它会在所有设备上侦听,但不会在任何设备上发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM