⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sniff_tab.cpp

📁 嵌入式Linux环境下的网络安全检测软件
💻 CPP
字号:
#include "sniff_tab.h"#include "sniff_protocol.h"const int SIZE_ETHER=14;const int CW=25;const int SNAP_LEN=1518;ofstream out("find.txt");string Sniff_Tab::str_key="name";Sniff_Tab::Sniff_Tab()	:net(0),mask(0){	Sniff_clear = new QPushButton( this, "Sniff_clear" );	Sniff_clear->setGeometry( QRect( 130, 250, 70, 32 ) ); 	Sniff_clear->setText( tr( "Clear" ) );	sniff_key_label = new QLabel( this, "sniff_key_label" );	sniff_key_label->setGeometry( QRect( 10, 10, 50, 20 ) ); 	sniff_key_label->setText( tr( "Key word:" ) );	sniff_filter_label = new QLabel( this, "sniff_filter_label" );	sniff_filter_label->setGeometry( QRect( 10, 40, 40, 20 ) ); 	sniff_filter_label->setText( tr( "Filter:" ) );	sniff_num_label = new QLabel( this, "sniff_num_label" );	sniff_num_label->setGeometry( QRect( 10, 70, 50, 20 ) ); 	sniff_num_label->setText( tr( "Number:" ) );	Sniff_start = new QPushButton( this, "Sniff_start" );	Sniff_start->setGeometry( QRect( 30, 250, 70, 32 ) ); 	Sniff_start->setText( tr( "start" ) );	sniff_key_lineedit = new QLineEdit( this, "sniff_key_lineedit" );	sniff_key_lineedit->setGeometry( QRect( 60, 10, 90, 23 ) ); 	sniff_filter_lineedit = new QLineEdit( this, "sniff_filter_lineedit" );	sniff_filter_lineedit->setGeometry( QRect( 60, 40, 150, 26 ) ); 	sniff_num_lineedit = new QLineEdit( this, "sniff_num_lineedit" );	sniff_num_lineedit->setGeometry( QRect( 60, 70, 50, 26 ) ); 	sniff_Eth_lineedit = new QLineEdit( this, "sniff_Eth_lineedit" );	sniff_Eth_lineedit->setEnabled( FALSE );	sniff_Eth_lineedit->setGeometry( QRect( 170, 70, 50, 26 ) ); 	sniff_Eth_lineedit->setText( tr( "eth0" ) );	disp_listview = new QListView( this, "disp_listview" );	disp_listview->addColumn( tr( "Seq" ) );	disp_listview->setGeometry( QRect( 10, 100, 210, 140 ) ); 	sniff_default_check = new QCheckBox( this, "sniff_default_check" );	sniff_default_check->setGeometry( QRect( 170, 10, 90, 20 ) ); 	sniff_default_check->setText( tr( "Default" ) );	sniff_eth_check = new QCheckBox( this, "sniff_eth_check" );	sniff_eth_check->setGeometry( QRect( 120, 70, 40, 20 ) ); 	sniff_eth_check->setText( tr( "eth:" ) );		connect( Sniff_start, SIGNAL( clicked() ), this, SLOT( start_sniff() ) );	connect( Sniff_clear, SIGNAL( clicked() ), this, SLOT( clear_sniff() ) );	connect( sniff_eth_check, SIGNAL( clicked() ), this, SLOT( enableEth() ) );	connect( sniff_default_check, SIGNAL( clicked() ), this, SLOT( enableDefault() ) );	connect( Sniff_start, SIGNAL( clicked() ), this, SLOT( start_sniff() ) );	connect( Sniff_start, SIGNAL( clicked() ), this, SLOT( start_display() ) );		device=NULL;	filter_exp=" ";	num_packets=-1;	sniff_stopped = TRUE;	display_stopped = TRUE;		disp_listview->setSorting(1, FALSE);	//禁止排序		sniff_default_check->setChecked(TRUE);	//界面显示默认设置	sniff_key_lineedit->setText(tr("name"));	sniff_filter_lineedit->setText(tr(" "));	sniff_num_lineedit->setText(tr("-1"));			sniff_key_lineedit->setEnabled(FALSE);	sniff_filter_lineedit->setEnabled(FALSE);	sniff_num_lineedit->setEnabled(FALSE);			readfiledisplay_thread = new SniffFileDisplay_Thread(this);}Sniff_Tab::~Sniff_Tab(){}void Sniff_Tab::start_sniff(){	if(sniff_stopped)	{		start();		Sniff_start->setText(tr("Running..."));		sniff_stopped = FALSE;	}}void Sniff_Tab::start_display(){	if(display_stopped)	{		readfiledisplay_thread->start();		Sniff_start->setText(tr("Running..."));		display_stopped = FALSE;	}}void Sniff_Tab::clear_sniff(){	disp_listview->clear();}void Sniff_Tab::enableEth(){  	if(sniff_eth_check->isChecked())	{		sniff_Eth_lineedit->setEnabled(TRUE);	}	else	{		sniff_Eth_lineedit->setEnabled(FALSE);	}}void Sniff_Tab::enableDefault()		//缺省设置{	if(sniff_default_check->isChecked())	{		sniff_key_lineedit->setText(tr("name"));		sniff_num_lineedit->setText(tr(" "));		sniff_num_lineedit->setText(tr("-1"));				sniff_key_lineedit->setEnabled(FALSE);		sniff_filter_lineedit->setEnabled(FALSE);		sniff_num_lineedit->setEnabled(FALSE);		}	else	{		str_key = sniff_key_lineedit->text().ascii();		num_packets = sniff_filter_lineedit->text().toInt();		filter_exp = sniff_num_lineedit->text().ascii();				sniff_key_lineedit->setEnabled(TRUE);		sniff_filter_lineedit->setEnabled(TRUE);		sniff_num_lineedit->setEnabled(TRUE);		}}void Sniff_Tab::run(){	init();    	sniff_run();}void Sniff_Tab::stop(){}void Sniff_Tab::display(QString output, QListView* disp_listview, QListViewItem *list_item){//注意内存占用过多的问题,需要增加计数器进行记录,到达上限后清空,存放至文件	list_item = new QListViewItem(disp_listview);	list_item -> setText(0, output);}vector<string> Sniff_Tab::slip_str(string str){    int i=0;    vector<string> keywords;    while( i<(int)str.length() )    {	int k=i;	string temp;	while( str.at(i)!=' ' && i<(int)str.length() )	{	    ++i;	    temp+=str.at(i);	}	if( k<i )	    keywords.push_back(temp);	++i;    }    return keywords;}//鲁玫脢艗禄炉拢卢禄帽脠隆卤鸥禄煤脨脜脧垄虏垄脧脭脢鸥void Sniff_Tab::init(){	str_key = sniff_key_lineedit->text().ascii();	num_packets = sniff_num_lineedit->text().toInt();	filter_exp = sniff_filter_lineedit->text().ascii();		char device_tmpstr[STRING_LEN];	strcpy(device_tmpstr, sniff_Eth_lineedit->text().ascii());	device = device_tmpstr;	//从用户界面返回设置值		if (device == NULL)	{		device = pcap_lookupdev(errbuf);		if (device == NULL)		{			error_buf.sprintf("pcap_lookupdev() failed: %s\n", errbuf);			display(error_buf, disp_listview, list_item);			exit();		}	}		if( pcap_lookupnet(device,&net,&mask,errbuf)==-1 )	//路碌禄脴脥酶脗莽潞脜拢卢脩脷脗毛	{		error_buf.sprintf("Error:Couldn't get netmask: %s\n", errbuf);		display(error_buf, disp_listview, list_item);		net=0;		mask=0;		exit();	}}//脰沤脨脨脳楼掳眉void Sniff_Tab::sniff_run(){//沤貌驴陋脡猫卤啪脦脛艗镁拢卢禄帽碌脙鸥盲卤煤拢卢脳艗卤啪露脕脠隆脢媒鸥脻	handle=pcap_open_live(device,	//脡猫卤啪脙没  			SNAP_LEN,	//虏露禄帽掳眉碌脛脳卯沤贸鲁鈧

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -