123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
-
-
-
-
-
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- vector<string> devices ;
- ui->setupUi(this);
-
-
-
- pause = 0;
- sniff = new Sniffer(&wait_pause, &mutex, &pause) ;
- sniff->moveToThread(&sniff_thread);
- connect(&sniff_thread, SIGNAL(started()), sniff, SLOT(process()));
- connect(sniff, SIGNAL(resultReady(unsigned long)), this, SLOT(handleResults(unsigned long))) ;
- connect(&sniff_thread, SIGNAL(finished()), sniff, SLOT(deleteLater()));
-
-
-
- ui->packetSummary->setFont (QFont ("Courier", 13));
- ui->resetButton->setEnabled(false);
-
-
-
- sniff->find_devices(devices);
- for(unsigned int i = 0; i < devices.size(); i++)
- ui->selDevices->addItem(QString::fromStdString(devices[i]));
-
-
-
- netManager = new QNetworkAccessManager(this) ;
- connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*))) ;
- }
-
-
-
-
-
-
-
-
-
-
- void MainWindow::slot_netwManagerFinished(QNetworkReply *reply)
- {
- if (reply->error() != QNetworkReply::NoError) {
- qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
- return;
- }
- QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
- if (attribute.isValid()) {
- QUrl url = attribute.toUrl();
-
- return;
- }
-
- QByteArray jpegData = reply->readAll();
- QPixmap pixmap;
- pixmap.loadFromData(jpegData);
- ui->imageDisplayList->setIconSize(QSize(ui->imageDisplayList->width(),ui->imageDisplayList->width()));
- ui->imageDisplayList->addItem(new QListWidgetItem(QIcon(pixmap),""));
-
- }
-
-
-
-
-
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
-
-
-
-
-
- void MainWindow::on_capture_clicked()
- {
-
-
-
- if(sniff_thread.isRunning()){
- if(ui->capture->text() == "Capture"){
- qDebug() << "Now Capture" ;
- sniff->setDevice(ui->selDevices->currentText().toStdString()) ;
- mutex.lock();
- pause = 0 ;
- wait_pause.wakeAll();
- mutex.unlock();
- ui->capture->setText("Stop");
- ui->resetButton->setEnabled(true);
- ui->resetButton->setEnabled(false);
- }
- else{
- mutex.lock();
- pause = 1 ;
- mutex.unlock();
- qDebug() << "Now Stop" ;
- ui->capture->setText("Capture") ;
- ui->resetButton->setEnabled(true);
-
- }
- }
- else{
-
- sniff->setDevice(ui->selDevices->currentText().toStdString()) ;
-
- sniff_thread.start() ;
- ui->capture->setText("Stop");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- void MainWindow::handleResults(unsigned long index){
-
- vector<ip_packet> *pkt = sniff->getPacketList() ;
-
-
-
- ui->packetDisplay->insertRow(ui->packetDisplay->rowCount());
- ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 0, new QTableWidgetItem(QString::fromStdString(pkt->at(index-1).getIPSAddress()))) ;
- ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 1, new QTableWidgetItem(QString::fromStdString(pkt->at(index-1).getIPDAddress()))) ;
- ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 2, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPSPort()))) ;
- ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 3, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPDPort()))) ;
- ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 4, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPProto()))) ;
-
-
-
- imagepacket hrI ;
- if(pkt->at(index-1).getIPDPort() == 80){
- if(hrI.isImage(pkt->at(index-1).getPayload())){
- if(!imageList.contains(hrI.getImage())){
- QUrl url(QStringLiteral("http://")+hrI.getImage());
- QNetworkRequest request(url);
- netManager->get(request);
- imageList << hrI.getImage() ;
- }
- else{
- imageList.removeOne(hrI.getImage()) ;
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- void MainWindow::on_packetDisplay_itemClicked(QTableWidgetItem *item)
- {
- vector<ip_packet> *pkt = sniff->getPacketList() ;
- string payload = pkt->at(item->row()).getPayload() ;
- QString summary = "Source Hardware Address: " + QString::fromStdString(pkt->at(item->row()).getEtherSHost()) +
- "\nDestin Hardware Address: " + QString::fromStdString(pkt->at(item->row()).getEtherDHost()) +
- "\nSource IP Address: " + QString::fromStdString(pkt->at(item->row()).getIPSAddress()) +
- "\nSource IP Address: " + QString::fromStdString(pkt->at(item->row()).getIPDAddress()) +
- "\nTransport Proto: " + QString::number(pkt->at(item->row()).getIPProto()) +
- "\nSource Port: " + QString::number(pkt->at(item->row()).getIPSPort()) +
- "\nDestin Port: " + QString::number(pkt->at(item->row()).getIPDPort()) +
- "\n" + QString::fromStdString(sniff->format_payload(payload.c_str(), payload.size())) ;
- ui->packetSummary->setText(summary);
- }
-
-
-
-
-
-
-
-
- void MainWindow::on_resetButton_clicked()
- {
- vector<ip_packet> *pkt = sniff->getPacketList() ;
- pkt->clear() ;
- ui->packetDisplay->setRowCount(0);
- ui->packetSummary->setText("");
- }
|