package pcap_ReaderV2;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Vector;
public class PcapDecode extends Thread{
private static String pcapHeader = "";
/***
*
* Config Variablen
*
*/
private boolean readinPcapHeader = false;
private boolean invSpeicher = true;
/***
*
* Klassen referezen
*
*/
private HexToInt conv = new HexToInt();
private Vector<FramePcap> decodePcap = new Vector<FramePcap>();
/***
*
* Laufzeiger
*
*/
private int frameNr = 1;
private int zeiger = 0;
private int wireBytes = 0;
private int captureBytes = 0;
private boolean frameHeaderRead = false;
private String source = "";
private String destination = "";
private String sourceMac = "";
private String destinationMac = "";
private String time = "";
private String date = "";
private String tms = "";
File pcapFile, logFile;
boolean checkModus[] = { true, true };
/***
*
* Methoden
*
*/
private PcapDecode(){
}
public PcapDecode(File input, File output, boolean ip, boolean ts){
pcapFile = input;
logFile = output;
}
public static void main(String[] args) {
PcapDecode m = new PcapDecode();
m.run();
}
public void run(){
DataInputStream din = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(pcapFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
din = new DataInputStream(fin);
String pcap = "";
String tmp = "";
int z = 0;
int buf;
try{
while(true){
buf = din.readUnsignedByte();
tmp = Integer.toHexString(buf).toUpperCase();
if(conv.doIt(tmp) < 16){
pcap += "0" + Integer.toHexString(buf);
}else{
pcap += Integer.toHexString(buf);
}
pcap = pcap.toUpperCase();
/**
* Pcap Header
*/
if(!readinPcapHeader){
if(22 < z){
setPcapHeader(pcap);
pcap = "";
readinPcapHeader = true;
zeiger = 22 + 17;
}
}else{
/**
* Frame Header
*/
if(!frameHeaderRead){
if((z == zeiger)){
setFrameHeader(pcap);
pcap = "";
frameHeaderRead = true;
zeiger += wireBytes;
}
}else{
/**
* Frame
*/
if(z == zeiger){
getSourceAndDestinationIp(pcap);
getSourceAndDestinationEt(pcap);
pcap = "";
zeiger += 16;
frameHeaderRead = false;
decodePcap.addElement(new FramePcap(frameNr, wireBytes, captureBytes, source,destination,sourceMac,destinationMac,date,time));
frameNr++;
}
}
}
z++;
}
}catch(EOFException e){
try {
din.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
if(logFile != null)
writeLogFile();
}
PcapFilter ff = new PcapFilter();
@SuppressWarnings("unchecked")
public Vector getVec(){
return decodePcap;
}
public void writeLogFile(){
LogFileWrite log = new LogFileWrite(this.decodePcap, logFile);
log.makeLog();
}
String getInvertHex(String hex){
String invHex = "";
for(int i = hex.length()-1;i>=0;i = i-2){
invHex += hex.charAt(i-1);
invHex += hex.charAt(i);
}
if(invSpeicher){
return invHex;
}else{
return hex;
}
}
void setPcapHeader(String pcap){
pcapHeader = pcap;
if(pcapHeader.charAt(pcapHeader.length()-1)==48){
invSpeicher = true;
}else{
invSpeicher = false;
}
}
void setFrameHeader(String pcap){
getTimeStamp(pcap);
String size = pcap.substring(16, pcap.length());
wireBytes = (int)conv.doIt(getInvertHex(size.substring(8, size.length())));
captureBytes = (int)conv.doIt(getInvertHex(size.substring(0, 8)));
}
void getTimeStamp(String frameHeader){
String hexSec = this.getInvertHex(frameHeader.substring(0,8));
String hexMilliSec = this.getInvertHex(frameHeader.substring(8,14));
long sec = conv.doIt(hexSec);
long milli = conv.doIt(hexMilliSec);
Date test = new Date(sec*1000);
tms = test.toString();
date = tms.substring(0, 10);
date = date.concat(" "+ tms.substring(tms.length()-4, tms.length()));
time = tms.substring(11, 19);
time = time.concat("."+milli);
}
void getSourceAndDestinationIp(String frame){
source = "";
destination = "";
String hexSource = frame.substring(52, 60);
String hexDestination = frame.substring(60,68);
String tmp = "";
hexSource = hexSource.toUpperCase();
hexDestination = hexDestination.toUpperCase();
for(int i = 0;i<=6;i+=2){
tmp += String.valueOf(hexSource.charAt(i));
tmp += String.valueOf(hexSource.charAt(i+1));
source += conv.doIt(tmp);
source += ".";
tmp = "";
}
source = source.substring(0, source.length()-1);
for(int i = 0;i<=6;i+=2){
tmp += String.valueOf(hexDestination.charAt(i));
tmp += String.valueOf(hexDestination.charAt(i+1));
destination += conv.doIt(tmp);
destination += ".";
tmp = "";
}
destination = destination.substring(0, destination.length()-1);
}
void getSourceAndDestinationEt(String frame){
sourceMac = "";
destinationMac = "";
String hdm = frame.substring(0,12);
String hsm = frame.substring(12, 24);
String tmp = "";
for(int i = 0;i<12;i = i+2){
tmp = tmp.concat(String.valueOf(hdm.charAt(i)));
tmp = tmp.concat(String.valueOf(hdm.charAt(i+1)));
tmp += ":";
}
tmp = tmp.substring(0,tmp.length()-1);
destinationMac = tmp;
tmp = "";
for(int i = 0;i<12;i = i+2){
tmp = tmp.concat(String.valueOf(hsm.charAt(i)));
tmp = tmp.concat(String.valueOf(hsm.charAt(i+1)));
tmp += ":";
}
tmp = tmp.substring(0,tmp.length()-1);
sourceMac = tmp;
}
}