Это простой файл 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 Encack (InputStream IS) бросает ioException {
Pcap pcap = null;
byte [] buffer_4 = новый байт [4];
byte [] buffer_2 = новый байт [2];
pcap = new pcap ();
Pcaphader Header = new Pcapheader ();
int m = is.read (buffer_4);
if (m! = 4) {
вернуть ноль;
}
ReactByteArray (Buffer_4);
header.setmagic (BytearrayToint (buffer_4, 0));
m = is.read (buffer_2);
ReactBytearray (Buffer_2);
header.setmagor_version (bytearraytoshort (buffer_2, 0));
m = is.read (buffer_2);
ReactBytearray (Buffer_2);
header.setminor_version (bytearraytoshort (buffer_2, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
header.settimezone (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
Header.setsigflags (BytearrayToint (Buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
header.setsnaplen (BytearrayToint (buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
header.setLinkType (BytearRayToint (Buffer_4, 0));
pcap.setheader (заголовок);
Список <pcapdata> datalist = new ArrayList <capdata> ();
while (m> 0) {
Pcapdata data = new pcapdata ();
m = is.read (buffer_4);
if (m <0) {
перерыв;
}
ReactByteArray (Buffer_4);
data.settime_s (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
data.settime_ms (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (Buffer_4);
Data.SetPlength (BytearRayToint (Buffer_4, 0));
m = is.read (buffer_4);
ReactByteArray (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;
}
Частный статический int bytearraytoint (byte [] b, int offset) {
int value = 0;
для (int i = 0; i <4; i ++) {
int shift = (4 - 1 - i) * 8;
значение + = (b [i + offset] & 0x000000fff) << shift;
}
возвращаемое значение;
}
Частный статический короткий байтрайтошорт (byte [] b, int offset) {
короткое значение = 0;
для (int i = 0; i <2; i ++) {
int shift = (2 - 1 - i) * 8;
значение + = (b [i + offset] & 0x000000fff) << shift;
}
возвращаемое значение;
}
/**
* Инвертировать массив
* @param arr
*/
Private Static void ReactyByTearray (byte [] arr) {
байтовая температура;
int n = arr.length;
для (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 {
частный заголовок Pcaphader;
Частный список <pcapdata> data;
public pcaphader getheader () {
вернуть заголовок;
}
public void Setheader (заголовок Pcaphader) {
this.Header = заголовок;
}
публичный список <pcapdata> getData () {
вернуть данные;
}
public void setData (список <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 ("count count ="). Append (data.size ());
вернуть S.ToString ();
}
}
Pcapdata.java
Кода -копия выглядит следующим образом:
пакет com.hylanda.pcap;
/**
* @author Zhouqisheng
* Заголовок пакета
*/
открытый класс pcapdata {
private int time_s; // timeStamp (секунды)
private int time_ms; // timeStamp (тонкая)
Private Int Plength; // Длина пакета
Private Int Length; // Фактическая длина
Частный байт [] Контент; // Данные
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 () {
вернуть плину;
}
public void setPlength (int plength) {
this.plength = plength;
}
public int getLength () {
Длина возврата;
}
public void setlength (int length) {
this.length = длина;
}
public byte [] getContent () {
вернуть контент;
}
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);
вернуть ноль;
}
}
Pcaphader.java
Кода -копия выглядит следующим образом:
пакет com.hylanda.pcap;
/**
* @author Zhouqisheng
* Заголовок файла PCAP
*/
открытый класс pcaphader {
Private Int Magic; // Заголовок распознавания файлов, 0xa1b2c3d4
Private Short Magor_version; // Основная версия
private short minor_version; // незначительная версия
частный часовой застройки; // местное стандартное время
private int sigflags; // Точность временных метров
private int snaplen; // максимальная длина хранения
/**
* 0 BSD Loopback Devices, за исключением более поздних OpenBSD
1 Ethernet и Linux Loopback устройства
6 802,5 токенового кольца
7 Arcnet
8 проскальзывать
9 PPP
10 FDDI
100 LLC/SNAP-ENCAPSULED ATM
101 «RAW IP», без ссылки
102 BSD/OS Slip
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 позже устройства OpenBSD Loopback (с AF_VALUE в порядке сетевого байта)
113 Special Linux "приготовлен"
114 LocalTalk
*/
private 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 (короткая миновая_версция) {
this.minor_version = minor_version;
}
public int gettimezone () {
вернуть часовой застрой;
}
public void settimezone (int timezone) {
this.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 () {
вернуть 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);
вернуть S.ToString ();
}
}