Extract packet payload and store in a string

1 reply [Last post]
Mayank1807
Offline
Joined: 01/16/2015

I need to extract payload of a packet and want to store in a string. How this can be done. Currently I am doing like :

import org.jnetpcap.Pcap;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.JPacketHandler;

public class Hexdump
{
public static void main(String[] args)
{
String FILENAME1 = "/home/mayank/Pcaps/DNS_Skype.pcap";
final StringBuilder errbuf = new StringBuilder();
final Pcap pcap1 = Pcap.openOffline(FILENAME1, errbuf);

if (pcap1 == null)
{
System.err.println(errbuf);
return;
}

pcap1.loop(-1, new JPacketHandler()
{
public void nextPacket(JPacket packet, StringBuilder errbuf)
{
String hexdump = packet.toHexdump(packet.size(), false, true, false);
System.out.println(hexdump);
}
}, errbuf);

}
}

In this way payload is not coming in a proper format. Please help.

Mark Bednarczyk
Offline
Joined: 03/22/2008
I am not sure what format you

I am not sure what format you want it in, since you do not specify. If you want it all in a long HEX string with no spaces, you can use java's BigInteger class to convert:

final byte[] data = packet.toByteArray(0, packet.size());
String hexString = new BigInteger(data).toString(16);

Sly Technologies, Inc.
http://slytechs.com

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.