Search code examples
javaarrayseclipsejnetpcap

jnetpcap: can't create PcapPacket using byte array on ubuntu


I'm trying to run this code which just take a HexString of bytes transform it to a byte array and try to create a PcapPacket from this array:

import org.jnetpcap.packet.PcapPacket;

public class PcapTest {

    public static void main(String[] args) {
        PcapPacket packet= new PcapPacket(hexStringToByteArray("08002760f9b952540012350208004500008f6ee600004006ab16d5f87e650a00020f15b3d192006546b55996b53d5018fffffe0c00000dcd6405003156656e6420efbfbc206d7020766f73206f66667265206a65207661697320646f646f2064616e732032206d696e7574657354cf1d3400087a376b706d7338700059006500084968656273657468050cf0a100013fcc07000100467c3c99e2ac5401"));//tcp Packet from wireshark
    }

    //this function is jute for completeness of the example
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
            //System.out.println(Integer.toHexString(data[i / 2]));
        }
        return data;
    }
}

I get the following error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Invalid [16,3696,3680) range.
    at org.jnetpcap.nio.JMemory.peer(Unknown Source)
    at org.jnetpcap.packet.JPacket$State.peerTo(Unknown Source)
    at org.jnetpcap.packet.PcapPacket.peerStateAndData(Unknown Source)
    at org.jnetpcap.packet.PcapPacket.transferStateAndDataFrom(Unknown Source)
    at org.jnetpcap.packet.PcapPacket.<init>(Unknown Source)
    at jNetPcapTest.JNetPcapTest.main(JNetPcapTest.java:12)

I checked the function it seems to be working well, also I'm using eclipse with root privilege (on ubuntu).

to add the library in eclipse, I've created a new User Library on eclipse added the jar and the native library location (folder where is located the .so/.dll)

I have the same problem on both linux(ubuntu) and windows. Thank you for your help cheers


Solution

  • I found the answer here should have read the javadoc: http://jnetpcap.com/docs/javadocs/jnetpcap-1.3/org/jnetpcap/packet/PcapPacket.html

    A pcap packet. Fully decoded packet that provides access to protocol headers as determined during the decoding process. A PcapPacket class is designed to work with pcap library. It can not be used to create a new packet from an external memory buffer that only contains packet data, such as preparing a packet to be sent from a network interface. You can use JMemoryPacket to create an in memory packet from scratch. ....