简体   繁体   中英

jpcap error sending packets cooked mode

I want to send a UDP packet using jpcap.

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)

And this is the link to c implementation:

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

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

or another interface that function with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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