0

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

JoshDM
  • 4,939
  • 7
  • 43
  • 72
user1928596
  • 1,503
  • 16
  • 21

2 Answers2

0

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. ....

user1928596
  • 1,503
  • 16
  • 21
0

Example and fragment of code:

JPacket packet =  
 new JMemoryPacket(JProtocol.ETHERNET_ID,  
      " 001801bf 6adc0025 4bb7afec 08004500 "  
    + " 0041a983 40004006 d69ac0a8 00342f8c "  
    + " ca30c3ef 008f2e80 11f52ea8 4b578018 "  
    + " ffffa6ea 00000101 080a152e ef03002a "  
    + " 2c943538 322e3430 204e4f4f 500d0a");  

tested on Windows.

Michael
  • 1,453
  • 3
  • 20
  • 28