Este es un archivo PCAP de análisis simple escrito por usted mismo, que es conveniente para leer archivos PCAP.
La copia del código es la siguiente:
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);
PCAPRARSER.JAVA
La copia del código es la siguiente:
paquete com.hylanda.pcap;
import java.io.ioException;
import java.io.inputstream;
import java.util.arrayList;
import java.util.list;
/**
* @author zhouqisheng
*
*/
Clase pública PCAPPARSER {
Public static pcap desempaquetado (inputstream is) lanza ioexception {
PCAP PCAP = NULL;
byte [] buffer_4 = new Byte [4];
byte [] buffer_2 = new Byte [2];
PCAP = nuevo PCAP ();
Encabezado de pcapheader = new PCapheader ();
int m = is.read (buffer_4);
if (m! = 4) {
regresar nulo;
}
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 (encabezado);
List <CcapData> DataList = New ArrayList <CcapData> ();
while (m> 0) {
PcapData data = new PCAPDATA ();
m = is.read (buffer_4);
if (m <0) {
romper;
}
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 (contenido);
data.setContent (contenido);
dataList.Add (datos);
}
PCAP.SetData (DataList);
devolver PCAP;
}
privado static int byteArrayToint (byte [] b, int offset) {
valor int = 0;
para (int i = 0; i <4; i ++) {
int shift = (4 - 1 - i) * 8;
valor + = (b [i + offset] y 0x000000ff) << shift;
}
valor de retorno;
}
privado static corto bytearrayToshort (byte [] b, int offset) {
valor corto = 0;
para (int i = 0; i <2; i ++) {
int shift = (2 - 1 - i) * 8;
valor + = (b [i + offset] y 0x000000ff) << shift;
}
valor de retorno;
}
/**
* Invertir la matriz
* @param arr
*/
vacío estático privado reverseByTearray (byte [] arr) {
temperatura de byte;
int n = arr.length;
para (int i = 0; i <n/2; i ++) {
temp = arr [i];
arr [i] = arr [n-1-i];
arr [n-1-i] = temp;
}
}
}
Pcap.java
La copia del código es la siguiente:
/**
*
*/
paquete com.hylanda.pcap;
import java.util.list;
/**
* @author zhouqisheng
*
*/
PCAP de clase pública {
encabezado privado de pcapheader;
Lista privada <capData> datos;
Public PCAPHeader GetHeader () {
encabezado de regreso;
}
public void setheader (encabezado de pcapheader) {
this.header = encabezado;
}
Lista pública <CcapData> getData () {
devolver datos;
}
public void setData (list <ccapData> data) {
this.data = data;
}
@Anular
public String toString () {
StringBuilder s = new StringBuilder ();
s.append ("encabezado {/n");
S.Append (Header.ToString ());
S.Append ("}/n");
S.Append ("Data Parte Count ="). Append (data.size ());
return s.toString ();
}
}
Pcapdata.java
La copia del código es la siguiente:
paquete com.hylanda.pcap;
/**
* @author zhouqisheng
* Encabezado de paquetes
*/
PcapData de clase pública {
Private int time_s; // timestamp (segundos)
Private int time_ms; // timestamp (sutil)
PRIVADA INT PLAGGY; // Longitud del paquete
longitud de intivate int; // Longitud real
Contenido de byte privado []; // Datos
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 () {
RETROBACIÓN PLAZO;
}
public void setplength (int plength) {
this.plength = plurgment;
}
public int getLength () {
longitud de retorno;
}
public void setLength (int long) {
this.length = longitud;
}
Public byte [] getContent () {
devolver contenido;
}
public void setContent (byte [] content) {
this.content = content;
}
@Anular
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);
regresar nulo;
}
}
Pcafeader.java
La copia del código es la siguiente:
paquete com.hylanda.pcap;
/**
* @author zhouqisheng
* Encabezado de archivo PCAP
*/
clase pública pcapheader {
Private int Magic; // encabezado de reconocimiento de archivos, 0xa1b2c3d4
privado corto magor_version; // versión principal
Private Short Minor_version; // versión menor
zona de tiempo privada int; // tiempo estándar local
private int sigflags; // La precisión de las marcas de tiempo
Private int Snaplen; // Longitud máxima de almacenamiento
/**
* 0 dispositivos de bucleback de 0 BSD, excepto para OpenBSD posterior
1 dispositivos Ethernet y Linux Loopback
6 802.5 Anillo de token
7 Arcnet
8 deslizamiento
9 PPP
10 FDDI
100 LLC/cajero automático Snap- Envapsulado
101 "IP en bruto", sin enlace
102 Slip BSD/OS
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 dispositivos de bucleback de OpenBsd más tarde (con AF_Value en el orden de byte de red)
113 Captura especial "cocinada" de Linux
114 LocalTalk
*/
Private int LinkType; // Tipo de enlace
public int getMagic () {
Magia de regreso;
}
public void setmagic (int magic) {
this.magic = Magic;
}
público corto 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 () {
zona horaria de regreso;
}
public void setTimezone (int TimeZone) {
this.Timezone = TimeZone;
}
public int getsigflags () {
regresar sigflags;
}
public void setSigFlags (int sigflags) {
this.sigflags = sigflags;
}
public int getSnaplen () {
regresar Snaplen;
}
public void setsnaplen (int snaplen) {
this.snaplen = snapplen;
}
public int getLinkType () {
return LinkType;
}
public void setLinkType (int LinkType) {
this.linkType = LinkType;
}
@Anular
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 ();
}
}