これは、自分が書いた単純な解析PCAPファイルであり、PCAPファイルを参照してください。
コードコピーは次のとおりです。
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、42、t.length);
pcapparser.java
コードコピーは次のとおりです。
パッケージcom.hylanda.pcap;
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.util.arraylistをインポートします。
java.util.listをインポートします。
/**
* @author zhouqisheng
*
*/
パブリッククラスのpcapparser {
public static pcap unpack(inputstream is)は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){
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);
リスト<PCAPDATA> DATALIST = new ArrayList <PcapData>();
while(m> 0){
pcapdata data = new pcapdata();
m = is.read(buffer_4);
if(m <0){
壊す;
}
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);
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;
}
返品値。
}
private static short bytearraytoshort(byte [] b、int offset){
短い値= 0;
for(int i = 0; i <2; i ++){
int shift =(2-1 -i) * 8;
value + =(b [i + offset]&0x000000ff)<< Shift;
}
返品値。
}
/**
*配列を反転します
* @param arr
*/
private static void ReverseBytearray(byte [] arr){
バイト温度;
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
コードコピーは次のとおりです。
/**
*
*/
パッケージcom.hylanda.pcap;
java.util.listをインポートします。
/**
* @author zhouqisheng
*
*/
パブリッククラスPCAP {
プライベートPcapheaderヘッダー。
プライベートリスト<PCAPDATA>データ;
public pcapheader getheader(){
ヘッダーを返します。
}
public void setheader(pcapheaderヘッダー){
this.header = header;
}
パブリックリスト<PCAPDATA> getData(){
データを返す;
}
public void setData(リスト<PCAPDATA>データ){
this.data = data;
}
@オーバーライド
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());
s.toString()を返します。
}
}
pcapdata.java
コードコピーは次のとおりです。
パッケージcom.hylanda.pcap;
/**
* @author zhouqisheng
*パケットヘッダー
*/
パブリッククラスpcapdata {
private int time_s; //タイムスタンプ(秒)
private int time_ms; //タイムスタンプ(微妙)
private int plength; //パケット長
プライベートINTの長さ; //実際の長さ
プライベートバイト[]コンテンツ; //データ
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(){
plengthを返します。
}
public void setPlength(int plength){
this.plength = plength;
}
public int getLength(){
戻り長。
}
public void setLength(int length){
this.length = length;
}
public byte [] getContent(){
コンテンツを返す;
}
public void setContent(byte [] content){
this.content = content;
}
@オーバーライド
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);
nullを返します。
}
}
pcapheader.java
コードコピーは次のとおりです。
パッケージcom.hylanda.pcap;
/**
* @author zhouqisheng
* PCAPファイルヘッダー
*/
パブリッククラスのpcapheader {
Private int Magic; //ファイル認識ヘッダー、0xa1b2c3d4
プライベートショートMagor_version; //メインバージョン
プライベートショートマイナー_version; //マイナーバージョン
Private int Timezone; //ローカル標準時間
Private int sigflags; //タイムスタンプの精度
Private int Snaplen; //最大ストレージ長
/**
* 0 BSDループバックデバイス。ただし、後のOpenBSDを除く
1つのイーサネット、およびLinuxループバックデバイス
6 802.5トークンリング
7 arcnet
8スリップ
9 ppp
10 fddi
100 LLC/Snap-Capsulated ATM
101「生のIP」、リンクなし
102 BSD/OSスリップ
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108後のOpenBSDループバックデバイス(AF_VALUEがネットワークバイトの順序で)
113特別なLinux「調理済み」キャプチャ
114 LocalTalk
*/
private int linktype; // link type
public int getMagic(){
魔法を返す;
}
public void setmagic(int magic){
this.magic = magic;
}
public short getmagor_version(){
MAGOR_VERSIONを返します。
}
public void setmagor_version(short magor_version){
this.magor_version = magor_version;
}
public short getminor_version(){
minor_versionを返します。
}
public void setminor_version(short minor_version){
this.minor_version = minor_version;
}
public int gettimezone(){
Return TimeZone;
}
public void setimezone(int timezone){
this.timezone = timezone;
}
public int getSigflags(){
sigflagsを返します。
}
public void setsigflags(int sigflags){
this.sigflags = sigflags;
}
public int getSnaplen(){
Snaplenを返します。
}
public void setsnaplen(int snaplen){
this.snaplen = snapplen;
}
public int getLinkType(){
return linktype;
}
public void setlinktype(int linktype){
this.linktype = linktype;
}
@オーバーライド
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);
s.toString()を返します。
}
}