Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 3 / 5.tar / 5 / 5 / net_sniff / s_form

.ui.h
Скачиваний:
19
Добавлен:
17.04.2013
Размер:
5.9 Кб
Скачать
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/
#include <qapplication.h>
#include <qfiledialog.h>
#include <qtextstream.h>

void SDialog::uiface()
{
    if (nsniff->running()){
	ifaceBox->setEnabled(false);
	quit_Button->setEnabled(false);
	start_Button->setText("Stop.");
	
	chTime->setEnabled(false);
	chPCount->setEnabled(false);
	countBox->setEnabled(false);
	timeBox->setEnabled(false);
    }
    else{
	chTime->setEnabled(true);
	chPCount->setEnabled(true);
	
	if (chPCount->isOn()) countBox->setEnabled(true);
	else countBox->setEnabled(false);
    
	if (chTime->isOn()) timeBox->setEnabled(true);
	else timeBox->setEnabled(false);
	
	ifaceBox->setEnabled(true);
	quit_Button->setEnabled(true);
	start_Button->setText("Start!");
    }
}


void SDialog::start_sniff()
{   
    if (nsniff->running()){
    	nsniff->stop();
	    usleep(80000);
    	if (nsniff->running()){
    		QMessageBox::information( this, "NSniff",
    			"Failed to stop! Try again, pls...");
    	}
    }
    else{
		int res = nsniff->setParams(ifaceBox->text(),
		      (chTime->isOn()) ? (timeBox->value()) : (0),
		      (chPCount->isOn()) ? (countBox->value()) : (0)
		      );
		if (res < 0){
	    	QMessageBox::information( this, "NSniff",
				  "Sniff failed! Check params.");
		}
		else{
	    	nsniff->start();
	    	usleep(80000);
	    	if (nsniff->err_state()){
				QMessageBox::information( this, "NSniff",
				  nsniff->err_msg());
	    	}
	    	else{
				PacketsLCD->display(0);
				timer->start(200, false);
				lView->clear();
	    	}
		}
    }
}


void SDialog::timer_out()
{
	PacketsLCD->display((int)nsniff->getCount());
    
    if (nsniff->running()){
    }
    else{
	/* show shiffed packets */
		timer->stop();
		uiface();
		show_packets();
    }
}

void SDialog::self_init()
{
    nsniff = new NSniff( this );
    timer = new QTimer( this );
    ddialog = new DisplayDialog(this, 0, 0, 0);
    ddialog->self_init();
    
    connect(timer, SIGNAL(timeout()), this, SLOT(timer_out()));
}

void SDialog::self_exit()
{
    delete nsniff;
    delete timer;
    delete ddialog;
}


void SDialog::show_packets()
{
//	setEnabled(false);
	load_label->setText("<b>Loading!</b>");
	
	struct npacket **queue;
	size_t qlen;
	nsniff->getResults(&queue, &qlen);
	
	char s_addr[128];
	QString hw_addr, from_ip, type, to_ip, id;
	
	QListViewItem *item, *subitem;
	 
	size_t i;
    for (i=0; i < qlen; i++){
    	if (i % 30 == 0){
    		qApp->processEvents();
    		progressBar->setProgress ( i, qlen );
    	}

    	/* id */
    	id = QString("%1").arg(i);
    	
    	
    	if (queue[i]->eth_proto == ETH_P_IP){
    		/* from ip */
    		inet_ntop(AF_INET, &queue[i]->ip_saddr, s_addr, sizeof(s_addr));
    		from_ip = s_addr;
			/* to ip */
    		//inet_ntop(AF_INET, &queue[i]->ip_daddr, s_addr, sizeof(s_addr));
    		//to_ip = s_addr;
    		
    		if (queue[i]->ip_protocol == IPPROTO_ICMP){
    			type = "icmp";
    		}
    		else if(queue[i]->ip_protocol == IPPROTO_TCP){
    			type = "tcp";
    		}
    		else if(queue[i]->ip_protocol == IPPROTO_UDP){
    			type = "udp";
    		}
    		else{
    			type = QString("ip %1").arg(queue[i]->ip_protocol);
    		}
    	}
/*    	
    	else if (queue[i]->eth_proto == ETH_P_ARP){
    		type = "arp";
    		from_ip = "*";
    		to_ip = "*";
    	}
  */  	
    	else{
    		type = QString("eth %1").arg(queue[i]->eth_proto);
    		from_ip = "*";
    		to_ip = "*";
    	}

    	item = lView->findItem ( from_ip, 0 );
    	
    	if (item){
    		subitem = item->firstChild();
    		while (subitem){
    			if (subitem->text(0) == type)
    				break;
    			subitem = subitem->nextSibling();
    		}
    		if (subitem){
    			new QListViewItem( subitem, id );
    		}
    		else{
    			item = new QListViewItem( item, type );
    			new QListViewItem (item, id );
    		}
    	}
    	else{
    		item = new QListViewItem( lView, from_ip );
    		item = new QListViewItem( item, type );
    		new QListViewItem (item, id );
    	}
    	
    }
    progressBar->setProgress ( qlen, qlen );
	load_label->setText("All loaded");
    
//	setEnabled(true);
}


void SDialog::view_packet( QListViewItem *item )
{
	if (!item){
		return;
	}
	
	if (item->childCount () != 0){
		return;
	}
	
	size_t index = atoi(item->text(0).ascii());
	if (index == 0 && item->text(0)!="0"){
		return;
	}
	
	ddialog->displayText(nsniff->print_packet(index));				 
}


void SDialog::save_to_file()
{
	    QString s = QFileDialog::getSaveFileName(
                    "/home",
                    "*",
                    this,
                    "open file dialog",  
                    "Choose a file" );
    QString smsg;
    smsg = "<html><body>";
    smsg += "<h1>NetSniff program</h1>";
    smsg += QString("<p>Total packets count: %1</p>").arg(nsniff->getCount());
    if (s == "")
    	return;
    	
  	load_label->setText("<b>Saving!</b>");
    size_t i;
    size_t qlen = nsniff->getCount();
    for (i=0; i < qlen; i++){
    	if (i % 300 == 0){
    		qApp->processEvents();
    		progressBar->setProgress ( i, qlen );
    	}
    	smsg += nsniff->print_packet(i);
    }
    progressBar->setProgress ( i, qlen );
    smsg += "</body></html>";
    
    
    QFile file( s );
    if ( file.open( IO_WriteOnly ) ) {
        QTextStream stream( &file );
        stream << smsg;
        file.close();
    }
    load_label->setText("<b>Done.</b>");
    
}
Соседние файлы в папке net_sniff