Skip to content

Commit

Permalink
makes it an infinite loop, adds a stop button and saves stops the ses…
Browse files Browse the repository at this point in the history
…sion as well
  • Loading branch information
raymon1 committed Dec 12, 2015
1 parent 7c9c555 commit 432478e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
37 changes: 22 additions & 15 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ MainWindow::~MainWindow() {
if(settingsDialog)
delete settingsDialog;
}

int count=0;
void printPacket(RawPacket* rawPacket){

Packet packet(rawPacket);
Expand All @@ -59,7 +59,7 @@ void printPacket(RawPacket* rawPacket){
printf("Packet is of type: ");
if (packet.isPacketOfType(Ethernet))
{
type<<"Ethernet";
//type<<"Ethernet";
EthLayer* ethlayer= packet.getLayerOfType<EthLayer>();
printf("Src MAC: %s\n", ethlayer->getSourceMac().toString().c_str());
printf("Dst MAC: %s\n", ethlayer->getDestMac().toString().c_str());
Expand All @@ -69,7 +69,7 @@ void printPacket(RawPacket* rawPacket){
{
if (packet.isPacketOfType(IPv4))
{
type<<"- IPv4 ";
//type<<"- IPv4 ";
IPv4Layer* ipv4layer= packet.getLayerOfType<IPv4Layer>();
printf("Dst IP: %s\n",ipv4layer->getDstIpAddress().toString().c_str());
printf("Src IP: %s\n",ipv4layer->getSrcIpAddress().toString().c_str());
Expand All @@ -83,14 +83,14 @@ void printPacket(RawPacket* rawPacket){
}
if (packet.isPacketOfType(TCP))
{
type<<"- TCP ";
//type<<"- TCP ";
TcpLayer* tcpLayer = packet.getLayerOfType<TcpLayer>();
printf("Port Dst: %d\n", ntohs(tcpLayer->getTcpHeader()->portDst));
printf("Port Src: %d\n", ntohs(tcpLayer->getTcpHeader()->portSrc));
}
if (packet.isPacketOfType(UDP))
{
type<<"- UDP ";
//type<<"- UDP ";
UdpLayer* udpLayer = packet.getLayerOfType<UdpLayer>();
printf("Port Dst: %d\n", ntohs(udpLayer->getUdpHeader()->portDst));
printf("Port Src: %d\n", ntohs(udpLayer->getUdpHeader()->portSrc));
Expand All @@ -100,13 +100,12 @@ void printPacket(RawPacket* rawPacket){
}


int count=0;

void packetRecieved(RawPacket* rawPacket, PcapLiveDevice* pDevice,void* userCookie)
{
printf("packet received %d\n",++count);
printPacket(rawPacket);


}
void savePacketsToFile(const char* fileName,RawPacketVector& packets, char* errString)
{
Expand Down Expand Up @@ -173,7 +172,6 @@ void savePackets(PcapLiveDevice *pDevice,QString filename,int time){
printf("End Saving %d Packets\n",capturedPackets.size());
}


void MainWindow::on_actionStart_Capture_triggered()
{
QMessageBox Msgbox;
Expand All @@ -184,7 +182,7 @@ void MainWindow::on_actionStart_Capture_triggered()
Msgbox.exec();
return;
}

printf("\ndevice name: %s \n",pIfaceDevice->getName());
//Opening interface device
if (!pIfaceDevice->open())
{
Expand All @@ -195,8 +193,6 @@ void MainWindow::on_actionStart_Capture_triggered()

int time=settingsDialog->getCaptureTime();
pIfaceDevice->startCapture(packetRecieved, NULL);
PCAP_SLEEP(time);
pIfaceDevice->stopCapture();
}

void MainWindow::on_actionSave_triggered()
Expand All @@ -218,11 +214,12 @@ void MainWindow::on_actionSave_triggered()
return;
}
int time=settingsDialog->getCaptureTime();
pIfaceDevice->stopCapture();

QString filename = QFileDialog::getSaveFileName(this, "Save file", "", ".pcap");
if (!filename.isEmpty())
savePackets(pIfaceDevice,filename,time);

count=0;
}

void MainWindow::on_actionOpen_triggered()
Expand All @@ -240,6 +237,16 @@ void MainWindow::on_actionSettings_triggered()

}




void MainWindow::on_actionStop_Capture_triggered()
{
QMessageBox Msgbox;
PcapLiveDevice* pIfaceDevice=settingsDialog->getSelectedDevice();
if(!pIfaceDevice){
Msgbox.setText("Cannot find interface. \n"
"Please Select Device...");
Msgbox.exec();
return;
}
pIfaceDevice->stopCapture();
count=0;
}
6 changes: 6 additions & 0 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<string>File</string>
</property>
<addaction name="actionStart_Capture"/>
<addaction name="actionStop_Capture"/>
<addaction name="actionSave"/>
<addaction name="actionOpen"/>
</widget>
Expand Down Expand Up @@ -79,6 +80,11 @@
<string>Settings</string>
</property>
</action>
<action name="actionStop_Capture">
<property name="text">
<string>Stop Capture</string>
</property>
</action>
</widget>
<resources/>
<connections/>
Expand Down

0 comments on commit 432478e

Please sign in to comment.