Example of pcap file parsing after Java grab package _java

Source: Internet
Author: User
Tags unpack

This is a simple analysis of their own pcap file, easy to read Pcap file, we refer to the use of the bar

Copy Code code as follows:

InputStream is = DataParser.class.getClassLoader (). getResourceAsStream ("Baidu_cdr.pcap");

Pcap Pcap = Pcapparser.unpack (IS);
Is.close ();
byte[] t = pcap.getdata (). Get (0). GetContent ();
byte[] data = Arrays.copyofrange (t, t.length);


Pcapparser.java

Copy Code code as follows:

Package com.hylanda.pcap;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;

/**
* @author Zhouqisheng
*
*/
public class Pcapparser {

public static Pcap unpack (InputStream is) throws IOException {
Pcap pcap = null;
byte[] buffer_4 = new Byte[4];
byte[] buffer_2 = new byte[2];
Pcap = new Pcap ();

Pcapheader Header = new Pcapheader ();
int m = Is.read (buffer_4);
if (M!= 4) {
return null;
}
Reversebytearray (buffer_4);
Header.setmagic (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_2);
Reversebytearray (buffer_2);
Header.setmagor_version (Bytearraytoshort (buffer_2, 0));
m = Is.read (buffer_2);
Reversebytearray (buffer_2);
Header.setminor_version (Bytearraytoshort (buffer_2, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Header.settimezone (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Header.setsigflags (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Header.setsnaplen (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Header.setlinktype (Bytearraytoint (buffer_4, 0));

Pcap.setheader (header);

list<pcapdata> dataList = new arraylist<pcapdata> ();
while (M > 0) {
Pcapdata data = new Pcapdata ();
m = Is.read (buffer_4);
if (M < 0) {
Break
}
Reversebytearray (buffer_4);
Data.settime_s (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Data.settime_ms (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Data.setplength (Bytearraytoint (buffer_4, 0));
m = Is.read (buffer_4);
Reversebytearray (buffer_4);
Data.setlength (Bytearraytoint (buffer_4, 0));
byte[] content = new byte[data.getplength ()];
m = is.read (content);
Data.setcontent (content);

Datalist.add (data);
}

Pcap.setdata (dataList);
return pcap;
}

private static int bytearraytoint (byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4-1-i) * 8;
Value + = (b[i + offset] & 0x000000ff) << shift;
}
return value;
}

private static short Bytearraytoshort (byte[] b, int offset) {
Short value = 0;
for (int i = 0; i < 2; i++) {
int shift = (2-1-i) * 8;
Value + = (b[i + offset] & 0x000000ff) << shift;
}
return value;
}

/**
* Reverse Array
* @param arr
*/
private static void Reversebytearray (byte[] arr) {
byte temp;
int n = arr.length;
for (int i=0; i<n/2; i++) {
temp = Arr[i];
Arr[i] = arr[n-1-i];
Arr[n-1-i] = temp;
}
}

}

Pcap.java

Copy Code code as follows:

/**
*
*/
Package com.hylanda.pcap;

Import java.util.List;

/**
* @author Zhouqisheng
*
*/
public class Pcap {
Private Pcapheader header;
Private list<pcapdata> data;
Public Pcapheader GetHeader () {
return header;
}
public void SetHeader (Pcapheader header) {
This.header = header;
}
Public list<pcapdata> GetData () {
return data;
}
public void SetData (list<pcapdata> data) {
This.data = data;
}
@Override
Public String toString () {
StringBuilder s = new StringBuilder ();
S.append ("header{\n");
S.append (Header.tostring ());
S.append ("}\n");
S.append ("Data part count="). Append (Data.size ());

return s.tostring ();
}
}

Pcapdata.java

Copy Code code as follows:

Package com.hylanda.pcap;

/**
* @author Zhouqisheng
* Packet Header
*/
public class Pcapdata {
private int time_s;//Time stamp (sec)
private int time_ms;//time stamp (subtle)
private int plength;//Grab bag length
private int length;//actual length
Private byte[] content;//data
public int gettime_s () {
return time_s;
}
public void settime_s (int time_s) {
this.time_s = time_s;
}
public int Gettime_ms () {
return Time_ms;
}
public void Settime_ms (int time_ms) {
This.time_ms = Time_ms;
}
public int getplength () {
return plength;
}
public void setplength (int plength) {
This.plength = Plength;
}
public int GetLength () {
return length;
}
public void SetLength (int length) {
this.length = length;
}
Public byte[] GetContent () {
return content;
}
public void SetContent (byte[] content) {
this.content = content;
}

@Override
Public String toString () {
StringBuilder s = new StringBuilder ();
S.append ("time_s="). Append (this.time_s);
S.append ("\ntime_ms="). Append (This.time_ms);
S.append ("\nplength="). Append (This.plength);
S.append ("\nlength="). Append (This.length);
return null;
}
}

Pcapheader.java

Copy Code code as follows:

Package com.hylanda.pcap;

/**
* @author Zhouqisheng
* Pcap File Header
*/
public class Pcapheader {
private int magic;//file identification header, for 0XA1B2C3D4
Private short magor_version;//Major version
Private short minor_version;//minor version
private int timezone;//Local Standard Time
private int sigflags;//time stamp precision
private int snaplen;//Maximum storage length
/**
* 0 BSD loopback devices, except for later OpenBSD
1 Ethernet, and Linux loopback devices
6 802.5 Token Ring
7 ARCnet
8 SLIP
9 PPP
FDDI
MB llc/snap-encapsulated ATM
"Raw IP", with no link
102 Bsd/os SLIP
Bsd/os PPP
Cisco HDLC
105 802.11
108 later OpenBSD loopback devices (with the Af_value in Network byte order)
113 Special Linux "cooked" capture
114 LocalTalk
*/
private int linktype;//LINK type
public int getmagic () {
Return magic;
}
public void setmagic (int magic) {
This.magic = Magic;
}
public short getmagor_version () {
return magor_version;
}
public void Setmagor_version (short magor_version) {
This.magor_version = magor_version;
}
public short getminor_version () {
return minor_version;
}
public void Setminor_version (short minor_version) {
This.minor_version = minor_version;
}
public int getTimeZone () {
return timezone;
}
public void Settimezone (int timezone) {
This.timezone = timezone;
}
public int getsigflags () {
return sigflags;
}
public void setsigflags (int sigflags) {
This.sigflags = Sigflags;
}
public int Getsnaplen () {
return Snaplen;
}
public void Setsnaplen (int snaplen) {
This.snaplen = Snaplen;
}
public int Getlinktype () {
return linktype;
}
public void Setlinktype (int linktype) {
This.linktype = Linktype;
}

@Override
Public String toString () {
StringBuilder s = new StringBuilder ();
S.append ("magic="). Append ("0x" + integer.tohexstring (this.magic));
S.append ("\nmagor_version="). Append (this.magor_version);
S.append ("\nminor_version="). Append (this.minor_version);
S.append ("\ntimezone="). Append (This.timezone);
S.append ("\nsigflags="). Append (This.sigflags);
S.append ("\nsnaplen="). Append (This.snaplen);
S.append ("\nlinktype="). Append (This.linktype);
return s.tostring ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.