Skip to content

Commit

Permalink
Merge pull request #2 from alyosama/string_matching
Browse files Browse the repository at this point in the history
String matching
  • Loading branch information
alyosama committed Mar 4, 2016
2 parents 7d00d69 + ca24a4c commit 33fd35e
Show file tree
Hide file tree
Showing 13 changed files with 938 additions and 17 deletions.
14 changes: 11 additions & 3 deletions ASY-DPI.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

SOURCES += main.cpp \
mainwindow.cpp \
settingsdialog.cpp
settingsdialog.cpp \
stringmatch.cpp \
statistics.cpp \
statisticsdialog.cpp


INCLUDEPATH+= $$PWD/../Libraries/PcapPlusPlus/Dist/header
Expand All @@ -15,6 +18,7 @@ LIBS+= -static-libstdc++
LIBS+=-L$$PWD/../Libraries/PcapPlusPlus/Dist -lPcap++ -lPacket++ -lCommon++
LIBS+= -lpcap -lpthread

CONFIG += c++11

DESTDIR= build

Expand All @@ -23,11 +27,15 @@ qtcAddDeployment()

FORMS += \
mainwindow.ui \
settingsdialog.ui
settingsdialog.ui \
statisticsdialog.ui

HEADERS += \
mainwindow.h \
packetcapture.h \
settingsdialog.h \
common.h
common.h \
stringmatch.h \
statistics.h \
statisticsdialog.h

264 changes: 264 additions & 0 deletions ASY-DPI.pro.user.1.3

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ASY-DPI.pro.user → ASY-DPI.pro.user.18
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2016-01-27T13:54:23. -->
<!-- Written by QtCreator 3.5.1, 2015-12-11T23:50:01. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
264 changes: 264 additions & 0 deletions ASY-DPI.pro.user~

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <PlatformSpecificUtils.h>
#include <PcapFileDevice.h>
#include <cstdlib>
#include<iostream>
#ifdef WIN32
#include <winsock2.h>
#else
Expand All @@ -34,7 +35,8 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
settingsDialog=NULL;

sm = new StringMatch();
stat = new statistics();
ui->tableWidget->setColumnCount(8);
m_TableHeader<<"#"<<"Type"<<"Mac Source"<<"Mac Dist"<<"IP Source"<<"IP Dist"<<"TCP/UDP Source"<<"TCP/UDP Dist";
ui->tableWidget->setHorizontalHeaderLabels(m_TableHeader);
Expand All @@ -52,11 +54,11 @@ MainWindow::~MainWindow() {
}


void MainWindow::addPacketToTable(RawPacket *rawPacket,Ui::MainWindow *ui){
void MainWindow::addPacketToTable(RawPacket *rawPacket,MainWindow* win){
Ui::MainWindow *ui = win->ui;
int rowc = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rowc);
Packet packet(rawPacket);

ui->tableWidget->setItem(rowc,0,new QTableWidgetItem(QString::number(rowc)));

QString type="";
Expand Down Expand Up @@ -101,12 +103,16 @@ void MainWindow::addPacketToTable(RawPacket *rawPacket,Ui::MainWindow *ui){
ui->tableWidget->setItem(rowc,7,new QTableWidgetItem(QString::number(ntohs(udpLayer->getUdpHeader()->portDst))));

}

ui->tableWidget->setItem(rowc,1,new QTableWidgetItem(type));
if(win->sm->matchPacket(rawPacket))
{
for(int i=0;i<8;i++)
ui->tableWidget->item(rowc,i)->setBackground(Qt::red);
}
ui->tableWidget->scrollToBottom();
}

int count=0;

void printPacket(RawPacket* rawPacket){

Packet packet(rawPacket);
Expand Down Expand Up @@ -161,12 +167,11 @@ void packetRecieved(RawPacket* rawPacket, PcapLiveDevice* pDevice,void* userCook
{
// printf("packet received %d\n",++count);
// printPacket(rawPacket);

MainWindow* win = (MainWindow*)userCookie;
MainWindow::addPacketToTable(rawPacket,win->ui);

MainWindow::addPacketToTable(rawPacket,win);
//stat(rawPacket);
//qtwi->~QTableWidgetItem();

win->stat->check(rawPacket);
// TODO: add the new packet data to the table or call some member function to do it.
}
void MainWindow::savePacketsToFile(const char* fileName,RawPacketVector& packets, char* errString)
Expand Down Expand Up @@ -219,8 +224,9 @@ void MainWindow::readPackets(QString filename){
for (RawPacketVector::VectorIterator packetIter = packets.begin(); packetIter != packets.end(); packetIter++)
{
//printf("packet read %d\n",++i);
MainWindow::addPacketToTable(*packetIter,ui);
MainWindow::addPacketToTable(*packetIter,this);
//printPacket(*packetIter);
stat->check(*packetIter);
}


Expand Down Expand Up @@ -295,6 +301,8 @@ void MainWindow::on_actionOpen_triggered()
QString(), tr("Raw Packets (*.pcap);;All Files (*)"));
if (!filename.isEmpty())
readPackets(filename);

stat->openStatisticsDialog();
}

void MainWindow::on_actionSettings_triggered()
Expand All @@ -314,6 +322,7 @@ void MainWindow::on_actionStop_Capture_triggered()
Msgbox.exec();
return;
}
stat->openStatisticsDialog();
pIfaceDevice->stopCapture();
count=0;
}
9 changes: 6 additions & 3 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QMainWindow>
#include "settingsdialog.h"
#include "stringmatch.h"
#include "statistics.h"

namespace Ui {
class MainWindow;
Expand All @@ -16,7 +18,9 @@ class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
friend void packetRecieved(RawPacket* rawPacket, PcapLiveDevice* pDevice, void* userCookie);
static void addPacketToTable(RawPacket *RawPacket,Ui::MainWindow *ui);
static void addPacketToTable(RawPacket *RawPacket,MainWindow *win);
StringMatch *sm;
statistics *stat;
private slots:
void on_actionStart_Capture_triggered();
void on_actionSave_triggered();
Expand All @@ -32,8 +36,7 @@ private slots:
void savePackets(PcapLiveDevice *pDevice,QString filename,int time);
void readPacketsFromFile(QString filename,RawPacketVector& packets, char* errorString);
void savePacketsToFile(const char* fileName,RawPacketVector& packets, char* errString);


int count=0;
};

#endif // MAINWINDOW_H
57 changes: 57 additions & 0 deletions statistics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "statistics.h"

statistics::statistics()
{

ipConverter["213.158.163.117"]="Google";
ipConverter["93.184.220.127"]="soundcloud";
ipConverter["96.17.211.172"]="cambridge";
map2["Google"]=0;
map2["soundcloud"]=0;
map2["cambridge"]=0;
}

void statistics::check(RawPacket * r){

Packet packet(r);
if (packet.isPacketOfType(IPv4))
{
IPv4Layer* ipv4layer= packet.getLayerOfType<IPv4Layer>();

total++;
if( ipConverter.find(ipv4layer->getDstIpAddress().toString()) != ipConverter.end())
{
this->mycount(ipConverter[ipv4layer->getDstIpAddress().toString()]);
}

}
}


int statistics::mycount(string y)
{
return ++map2[y];
}

void statistics::print()
{
map<string, int>::iterator it = map2.begin();
map<string, int>::iterator it1 = map2.end();

for(;it!=it1;it++)
cout<< it->first << " "<< it->second <<endl;

cout<<"total: "<< total<<endl;
}

void statistics::openStatisticsDialog()
{
statDialog = new statisticsDialog();
map<string, int>::iterator it = map2.begin();
map<string, int>::iterator it1 = map2.end();

for(;it!=it1;it++)
statDialog->addItem(it->first, it->second, (1.0*it->second/total) *100);
statDialog->show();
}

29 changes: 29 additions & 0 deletions statistics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef STATISTICS_H
#define STATISTICS_H
#include<map>
#include <Packet.h>
#include <RawPacket.h>
#include <IPv4Layer.h>
#include <iostream>
#include "statisticsdialog.h"

using namespace std;
class statistics
{
public:
statistics();
map<string,string> ipConverter;
map<string,int> map2;
int total=0;
statisticsDialog *statDialog;

void check(RawPacket * r);
int mycount(string x);
void openStatisticsDialog();


void print();

};

#endif // STATISTICS_H
36 changes: 36 additions & 0 deletions statisticsdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "statisticsdialog.h"
#include "ui_statisticsdialog.h"

statisticsDialog::statisticsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::statisticsDialog)
{
ui->setupUi(this);
ui->tableWidget->setColumnCount(3);
QStringList m_TableHeader;
m_TableHeader<<"Domain Name"<<"Number of Packets"<<"%";
ui->tableWidget->setHorizontalHeaderLabels(m_TableHeader);
ui->tableWidget->verticalHeader()->setVisible(false);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
}

statisticsDialog::~statisticsDialog()
{
delete ui;
}

void statisticsDialog::addItem(string domainName, int n, float perc)
{
int rowc = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rowc);
ui->tableWidget->setItem(rowc,0,new QTableWidgetItem(QString::fromStdString(domainName)));
ui->tableWidget->setItem(rowc,1,new QTableWidgetItem(QString::number(n)));
ui->tableWidget->setItem(rowc,2,new QTableWidgetItem(QString::number(perc)));
}

void statisticsDialog::on_pushButton_clicked()
{
this->~statisticsDialog();
}
28 changes: 28 additions & 0 deletions statisticsdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef STATISTICSDIALOG_H
#define STATISTICSDIALOG_H

#include <QDialog>
#include <string>
using namespace std;

namespace Ui {
class statisticsDialog;
}

class statisticsDialog : public QDialog
{
Q_OBJECT

public:
explicit statisticsDialog(QWidget *parent = 0);
~statisticsDialog();
void addItem(string domainName, int n, float perc);

private slots:
void on_pushButton_clicked();

private:
Ui::statisticsDialog *ui;
};

#endif // STATISTICSDIALOG_H
48 changes: 48 additions & 0 deletions statisticsdialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>statisticsDialog</class>
<widget class="QDialog" name="statisticsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Statistics</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>150</x>
<y>250</y>
<width>85</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>OK</string>
</property>
</widget>
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>311</width>
<height>192</height>
</rect>
</property>
<property name="columnCount">
<number>3</number>
</property>
<column/>
<column/>
<column/>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Loading

0 comments on commit 33fd35e

Please sign in to comment.