Geen omschrijving

mainwindow.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. /// \fn MainWindow::MainWindow(QWidget *parent)
  4. /// \~English
  5. /// \brief Constructor
  6. /// \~Spanish
  7. /// \brief Constructor
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. vector<string> devices ;
  13. ui->setupUi(this);
  14. /* Starts the sniffer thread and connect the signals between
  15. the sniffer and the GUI
  16. Comienza el hilo del sniffer y conecta las sinales entre
  17. el sniffer y el GUI
  18. */
  19. pause = 0;
  20. sniff = new Sniffer(&wait_pause, &mutex, &pause) ;
  21. sniff->moveToThread(&sniff_thread);
  22. connect(&sniff_thread, SIGNAL(started()), sniff, SLOT(process()));
  23. connect(sniff, SIGNAL(resultReady(unsigned long)), this, SLOT(handleResults(unsigned long))) ;
  24. connect(&sniff_thread, SIGNAL(finished()), sniff, SLOT(deleteLater()));
  25. /* Sets the display of the payload fonts to courier */
  26. /* Ajusta el tipo de letra para desplegar la carga a courier */
  27. ui->packetSummary->setFont (QFont ("Courier", 13));
  28. ui->resetButton->setEnabled(false);
  29. /* Fill the devices box with the machine devices
  30. Llena la caja de los devices con los devices de la maquina.
  31. */
  32. sniff->find_devices(devices);
  33. for(unsigned int i = 0; i < devices.size(); i++)
  34. ui->selDevices->addItem(QString::fromStdString(devices[i]));
  35. /* Set Network manager to search for images */
  36. /* Ajusta al manejador de red para buscar las imagenes */
  37. netManager = new QNetworkAccessManager(this) ;
  38. connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*))) ;
  39. }
  40. /// \fn void MainWindow::slot_netwManagerFinished(QNetworkReply *reply)
  41. /// \~English
  42. /// \brief This slot is called when an image is successfuly retreived.
  43. /// The image is then added to the GUI display.
  44. /// \param reply object that contains the reply to our request.
  45. /// \~Spanish
  46. /// \brief Este "slot" es llamado cuando se recibe una imagen con exito.
  47. /// La imagen entonces es anadida a la vizualizacion en el GUI.
  48. /// \param reply objeto que contiene la respuesta a nuestra solicitud
  49. void MainWindow::slot_netwManagerFinished(QNetworkReply *reply)
  50. {
  51. if (reply->error() != QNetworkReply::NoError) {
  52. qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
  53. return;
  54. }
  55. QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  56. if (attribute.isValid()) {
  57. QUrl url = attribute.toUrl();
  58. //qDebug() << "must go to:" << url;
  59. return;
  60. }
  61. //qDebug() << "ContentType:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
  62. QByteArray jpegData = reply->readAll();
  63. QPixmap pixmap;
  64. pixmap.loadFromData(jpegData);
  65. ui->imageDisplayList->setIconSize(QSize(ui->imageDisplayList->width(),ui->imageDisplayList->width()));
  66. ui->imageDisplayList->addItem(new QListWidgetItem(QIcon(pixmap),""));
  67. }
  68. /// \fn MainWindow::~MainWindow()
  69. /// \~English
  70. /// \brief Destructor
  71. /// \~Spanish
  72. /// \brief Destructor
  73. MainWindow::~MainWindow()
  74. {
  75. delete ui;
  76. }
  77. /// \fn void MainWindow::on_capture_clicked()
  78. /// \~English
  79. /// \brief Signal that starts or pause the capture of the packets in the sniffer.
  80. /// \~Spanish
  81. /// \brief Senal que inicial o pausa la captura de paquetes en el sniffer
  82. void MainWindow::on_capture_clicked()
  83. {
  84. /* if is running then it is not the first time it is sent to capture. */
  85. /* si esta corriendo entonces no es la primra ves que se envia a
  86. capturar. */
  87. if(sniff_thread.isRunning()){
  88. if(ui->capture->text() == "Capture"){
  89. qDebug() << "Now Capture" ;
  90. sniff->setDevice(ui->selDevices->currentText().toStdString()) ;
  91. mutex.lock();
  92. pause = 0 ;
  93. wait_pause.wakeAll();
  94. mutex.unlock();
  95. ui->capture->setText("Stop");
  96. ui->resetButton->setEnabled(true);
  97. ui->resetButton->setEnabled(false);
  98. }
  99. else{
  100. mutex.lock();
  101. pause = 1 ;
  102. mutex.unlock();
  103. qDebug() << "Now Stop" ;
  104. ui->capture->setText("Capture") ;
  105. ui->resetButton->setEnabled(true);
  106. }
  107. }
  108. else{
  109. //qDebug() << "First time\n" ;
  110. sniff->setDevice(ui->selDevices->currentText().toStdString()) ;
  111. //sniff->setFilter(ui->filterText->toPlainText().toStdString()) ;
  112. sniff_thread.start() ;
  113. ui->capture->setText("Stop");
  114. }
  115. }
  116. /// \fn void MainWindow::handleResults(unsigned long index)
  117. /// \~English
  118. /// \brief A signal was received from the sniffer that a packet of the form
  119. /// Ethernet -> IP -> (TCP|UDP) has to be displayed in the GUI.
  120. /// Also checks if the packet is an http image request to display.
  121. /// \param index number of the packet in the table
  122. /// \~Spanish
  123. /// \brief Una senal es recibida desde el sniffer que un paquete de la forma
  124. /// Ethernet ->IP -> (TCP|UDP) tiene que ser desplegado en el GUI.
  125. /// Tambien revisa si el paquete es una solicitud a una imagen en http
  126. /// para desplegar.
  127. /// \param index numero de el paquete en la tabla
  128. void MainWindow::handleResults(unsigned long index){
  129. vector<ip_packet> *pkt = sniff->getPacketList() ;
  130. /* Packet is added to the GUI packet list */
  131. /* Se anade el paquete al la lista de paquete el GUI */
  132. ui->packetDisplay->insertRow(ui->packetDisplay->rowCount());
  133. ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 0, new QTableWidgetItem(QString::fromStdString(pkt->at(index-1).getIPSAddress()))) ;
  134. ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 1, new QTableWidgetItem(QString::fromStdString(pkt->at(index-1).getIPDAddress()))) ;
  135. ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 2, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPSPort()))) ;
  136. ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 3, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPDPort()))) ;
  137. ui->packetDisplay->setItem(ui->packetDisplay->rowCount()-1, 4, new QTableWidgetItem(QString::number(pkt->at(index-1).getIPProto()))) ;
  138. /* Check if the packet is an http image request */
  139. /* Revisa si el paquete es una solicitud de imagen en http */
  140. imagepacket hrI ;
  141. if(pkt->at(index-1).getIPDPort() == 80){
  142. if(hrI.isImage(pkt->at(index-1).getPayload())){
  143. if(!imageList.contains(hrI.getImage())){
  144. QUrl url(QStringLiteral("http://")+hrI.getImage());
  145. QNetworkRequest request(url);
  146. netManager->get(request);
  147. imageList << hrI.getImage() ;
  148. }
  149. else{
  150. imageList.removeOne(hrI.getImage()) ;
  151. }
  152. }
  153. }
  154. }
  155. /// \fn void MainWindow::on_packetDisplay_itemClicked(QTableWidgetItem *item)
  156. /// \~English
  157. /// \brief When a line of the packets captured is selected from the GUI
  158. /// a summary of the payload in ascii is presented in the summary box.
  159. /// \param item item selected in the GUI
  160. /// \~Spanish
  161. /// \brief Cuando una linea de los paquetes es seleccionada desde el GUI
  162. /// un resumen de la carga en ascii es presentado en la caja de resumen.
  163. /// \param item articulo seleccionado en el GUI
  164. void MainWindow::on_packetDisplay_itemClicked(QTableWidgetItem *item)
  165. {
  166. vector<ip_packet> *pkt = sniff->getPacketList() ;
  167. string payload = pkt->at(item->row()).getPayload() ;
  168. QString summary = "Source Hardware Address: " + QString::fromStdString(pkt->at(item->row()).getEtherSHost()) +
  169. "\nDestin Hardware Address: " + QString::fromStdString(pkt->at(item->row()).getEtherDHost()) +
  170. "\nSource IP Address: " + QString::fromStdString(pkt->at(item->row()).getIPSAddress()) +
  171. "\nSource IP Address: " + QString::fromStdString(pkt->at(item->row()).getIPDAddress()) +
  172. "\nTransport Proto: " + QString::number(pkt->at(item->row()).getIPProto()) +
  173. "\nSource Port: " + QString::number(pkt->at(item->row()).getIPSPort()) +
  174. "\nDestin Port: " + QString::number(pkt->at(item->row()).getIPDPort()) +
  175. "\n" + QString::fromStdString(sniff->format_payload(payload.c_str(), payload.size())) ;
  176. ui->packetSummary->setText(summary);
  177. }
  178. /// \fn void MainWindow::on_resetButton_clicked()
  179. /// \~English
  180. /// \brief Empties the packets vector, resets the GUI packet table and
  181. /// summary
  182. /// \~Spanish
  183. /// \brief Vacia el vector de paquetes, reajusta la tabla de paquetes y
  184. /// el resumen del GUI.
  185. void MainWindow::on_resetButton_clicked()
  186. {
  187. vector<ip_packet> *pkt = sniff->getPacketList() ;
  188. pkt->clear() ;
  189. ui->packetDisplay->setRowCount(0);
  190. ui->packetSummary->setText("");
  191. }