이것은 간단한 구문 분석 PCAP 파일로 PCAP 파일을 읽는 데 편리합니다.
코드 사본은 다음과 같습니다.
inputStream은 = dataparser.class.getClassLoader (). getResourceAsStream ( "baidu_cdr.pcap");
PCAP PCAP = PCAPPARSER.unpack (IS);
is.close ();
바이트 [] t = pcap.getData (). get (0) .getContent ();
바이트 [] data = arrays.copyofrange (t, 42, t.length);
pcapparser.java
코드 사본은 다음과 같습니다.
패키지 com.hylanda.pcap;
import java.io.ioexception;
import java.io.inputstream;
java.util.arraylist 가져 오기;
Java.util.list 가져 오기;
/**
* @Author Zhouqisheng
*
*/
공개 클래스 pcapparser {
공개 정적 PCAP 포장 풀기 (InputStream IS)는 ioException {
PCAP PCAP = NULL;
바이트 [] buffer_4 = 새로운 바이트 [4];
바이트 [] buffer_2 = 새로운 바이트 [2];
PCAP = 새로운 PCAP ();
pcapheader header = new pcapheader ();
int m = is.read (buffer_4);
if (m! = 4) {
널 리턴;
}
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 (헤더);
List <CapData> Datalist = New ArrayList <PCAPDATA> ();
while (m> 0) {
pcapdata data = 새로운 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));
바이트 [] content = new Byte [data.getPlength ()];
m = is.read (내용);
Data.SetContent (컨텐츠);
Datalist.add (데이터);
}
PCAP.SETDATA (Datalist);
반환 PCAP;
}
개인 정적 int bytearraytoint (byte [] b, int offset) {
int 값 = 0;
for (int i = 0; i <4; i ++) {
int shift = (4-1 -i) * 8;
value + = (b [i + 오프셋] & 0x000000ff) << shift;
}
반환 값;
}
Private STATIC SHORT BYTEARRAYTOSHORT (BYTE [] B, int 오프셋) {
짧은 값 = 0;
for (int i = 0; i <2; i ++) {
int shift = (2-1-i) * 8;
value + = (b [i + 오프셋] & 0x000000ff) << shift;
}
반환 값;
}
/**
* 배열을 뒤집습니다
* @param arr
*/
Private STATIC void ReverseByTearRay (byte [] arr) {
바이트 온도;
int n = arr.length;
for (int i = 0; i <n/2; i ++) {
온도 = ARR [i];
ARR [i] = ARR [N-1-I];
ARR [N-1-I] = 온도;
}
}
}
pcap.java
코드 사본은 다음과 같습니다.
/**
*
*/
패키지 com.hylanda.pcap;
Java.util.list 가져 오기;
/**
* @Author Zhouqisheng
*
*/
공개 클래스 PCAP {
개인 pcapheader 헤더;
개인 목록 <PCAPDATA> 데이터;
공개 pcapheader getheader () {
리턴 헤더;
}
Public Void Setheader (PCAPHEADER HEADER) {
this.header = 헤더;
}
공개 목록 <CapData> getData () {
반환 데이터;
}
public void setdata (list <capdata> data) {
this.data = 데이터;
}
@보수
공개 문자열 tostring () {
StringBuilder s = new StringBuilder ();
s.append ( "헤더 {/n");
s.append (header.toString ());
s.append ( "}/n");
s.append ( "data part count ="). append (data.size ());
return s.tostring ();
}
}
pcapdata.java
코드 사본은 다음과 같습니다.
패키지 com.hylanda.pcap;
/**
* @Author Zhouqisheng
* 패킷 헤더
*/
공개 클래스 pcapdata {
private int time_s; // timestamp (초)
private int time_ms; // timestamp (미묘한)
개인 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 () {
리턴 플레이트;
}
공개 void settlength (int plength) {
이 .plength = plength;
}
공개 int getLength () {
반환 길이;
}
public void setlength (int length) {
길이 = 길이;
}
public byte [] getContent () {
반환 내용;
}
public void setContent (byte [] content) {
this.content = 내용;
}
@보수
공개 문자열 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);
널 리턴;
}
}
pcapheader.java
코드 사본은 다음과 같습니다.
패키지 com.hylanda.pcap;
/**
* @Author Zhouqisheng
* PCAP 파일 헤더
*/
공개 클래스 pcapheader {
Private Int Magic; // 파일 인식 헤더, 0xa1b2c3d4
개인 짧은 magor_version; // 메인 버전
개인 짧은 Minor_version; // 마이너 버전
Private Int Timezone; // 로컬 표준 시간
개인 int sigflags; // 타임 스탬프의 정확도
Private Int Snaplen; // 최대 저장 길이
/**
* 나중에 OpenBSD를 제외하고 0 BSD 루프백 장치
1 이더넷 및 Linux 루프백 장치
6 802.5 토큰 링
7 아크넷
8 슬립
9 PPP
10 FDDI
100 LLC/SNAP 캡슐화 ATM
101 링크가없는“원시 IP”
102 BSD/OS 슬립
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 나중에 OpenBSD 루프백 장치 (네트워크 바이트 순서의 AF_VALUE 포함)
113 특수 리눅스 "요리 된"캡처
114 LocalTalk
*/
개인 int linktype; // 링크 유형
public int getMagic () {
리턴 마법;
}
public void setMagic (int magic) {
this.magic = 마법;
}
public short getmagor_version () {
반환 magor_version;
}
public void setmagor_version (짧은 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 () {
리턴 타임 존;
}
public void settimezone (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 () {
리턴 링크 유형;
}
public void setlinktype (int linktype) {
this.linktype = linktype;
}
@보수
공개 문자열 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 ();
}
}