Java - Send a TCP packet to loopback address using pcap4j

169 views Asked by At

I got PacketSendPacket failed: The request is not supported. (50)

While i try to send a simple packet to loopback

import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.PcapNetworkInterface;
import org.pcap4j.core.Pcaps;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.TcpPacket;
import org.pcap4j.packet.namednumber.TcpPort;

import java.net.InetAddress;

public class Test {
    public static void main(String[] args) throws Exception {
        PcapNetworkInterface nif = Pcaps.getDevByAddress(InetAddress.getLoopbackAddress());

        int snaplen = 65536;
        int timeout = 10;
        PcapHandle handle = nif.openLive(snaplen, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, timeout);

        TcpPort srcPort = TcpPort.getInstance((short) 12345);
        TcpPort dstPort = TcpPort.getInstance((short) 12346);

        TcpPacket.Builder tcpBuilder = new TcpPacket.Builder();
        tcpBuilder.srcPort(srcPort)
                .dstPort(dstPort)
                .acknowledgmentNumber(1)
                .sequenceNumber(1)
                .window((byte) 100)
                .padding("123123123".getBytes());

        Packet tcpPacket = tcpBuilder.build();

        handle.sendPacket(tcpPacket); // PcapNativeException, request not supported (50)
        handle.close();
    }
}

I am using pcap4j: 1.8.2

please correct me // suggest me some library friendly, easy to use for beginner, like scapy/python

Update:

I got Ex at:

int rc = NativeMappings.pcap_sendpacket(handle, bytes, len);
if (rc < 0) {
    throw new PcapNativeException("Error occurred in              
            pcap_sendpacket(): " + getError(), rc);
}

and NativeMappings.pcap_sendpacket was define with native keyword:

static native int pcap_sendpacket(Pointer p, byte[] buf, int size);

Exception in thread "main" org.pcap4j.core.PcapNativeException: 
Error occurred in pcap_sendpacket(): send error: PacketSendPacket 
    failed: 
The request is not supported.  (50)

at org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1046)
at org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1017)
at org.pcap4j.core.PcapHandle.sendPacket(PcapHandle.java:1007)
at Test.main(Test.java:31)
0

There are 0 answers