#include "arppingdialog.h" #include "ui_arppingdialog.h" ArpPingDialog::ArpPingDialog(Arp *arp, QWidget *parent) : QDialog(parent), ui(new Ui::ArpPingDialog) { this->arp = arp; mac = arp->mac(); ui->setupUi(this); /* create timer and connect it into slot */ timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(ping())); reset(); } ArpPingDialog::~ArpPingDialog() { delete ui; if (timer->isActive()) timer->stop(); } void ArpPingDialog::reset() { ui->lineEditFromIp->setText(arp->ipFrom()); ui->lineEditToIp->setText(arp->ipTo()); } void ArpPingDialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void ArpPingDialog::on_buttonAction_clicked() { void *ptr; if (timer->isActive()) { ui->buttonAction->setText(tr("&Start")); timer->stop(); ui->lineEditFromIp->setEnabled(true); ui->lineEditToIp->setEnabled(true); ui->checkBoxClearMacList->setEnabled(true); ui->spinBoxPacketsPerSecond->setEnabled(true); } else { /* checks IP adress validity and save it for older use */ ptr = Arp::QStringToIp(ui->lineEditFromIp->text()); if (ptr == NULL) { QMessageBox::critical(this,"", tr("Error: From IP address is not valid."), QMessageBox::Ok, QMessageBox::Ok); return; } memcpy(&ip, ptr, ETH_ALEN); ptr = Arp::QStringToIp(ui->lineEditToIp->text()); if (ptr == NULL) { QMessageBox::critical(this,"", tr("Error: To IP address is not valid."), QMessageBox::Ok, QMessageBox::Ok); return; } memcpy(&ipTo, ptr, ETH_ALEN); if (ui->checkBoxClearMacList->checkState() == Qt::Checked) emit clearMacList(); timer->start(second / ui->spinBoxPacketsPerSecond->value()); ui->buttonAction->setText(tr("&Stop")); ui->lineEditFromIp->setEnabled(false); ui->lineEditToIp->setEnabled(false); ui->checkBoxClearMacList->setEnabled(false); ui->spinBoxPacketsPerSecond->setEnabled(false); } } void ArpPingDialog::ping() { arp_t arpData; int i, tmp; /* prepares ethernet frame header and arp packet data */ arpData.arp_operation = ARP_AR_REQUEST; arpData.ether_mac_src = arp->mac(); arpData.ether_mac_dst = "ff:ff:ff:ff:ff:ff"; arpData.arp_mac_src = arp->mac(); arpData.arp_mac_dst = "00:00:00:00:00:00"; arpData.arp_ip_src = arp->ip(); arpData.arp_ip_dst = Arp::ipToQString(ip); /* sends packet */ bool needStop = true; if (arp->sendArp(arpData)) { tmp = 1; /* and now the magic! */ /* increase ip address */ for(i = IP_ALEN - 1; i >=0; i--) { tmp = ip[i] + tmp; if (tmp > 255) { ip[i] = 0; tmp%=255; } else { ip[i]++; break; } } /* checks the limit */ needStop = false; for(i = 0; i < IP_ALEN; i++) { if (ip[i] < ipTo[i]) break; if (ip[i] > ipTo[i]) { needStop = true; break; } } } if (needStop) { ui->buttonAction->setText(tr("&Start")); ui->lineEditFromIp->setEnabled(true); ui->lineEditToIp->setEnabled(true); ui->checkBoxClearMacList->setEnabled(true); ui->spinBoxPacketsPerSecond->setEnabled(true); timer->stop(); } } void ArpPingDialog::stop() { ui->buttonAction->setText(tr("&Start")); ui->lineEditFromIp->setEnabled(true); ui->lineEditToIp->setEnabled(true); ui->checkBoxClearMacList->setEnabled(true); ui->spinBoxPacketsPerSecond->setEnabled(true); timer->stop(); }