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

📄 myauth.cc

📁 自己在NS2下写的关于认证的小例子
💻 CC
字号:
#include "myauth.h"//static int hdr_myauth::offset_;int hdr_myauth::offset_;static class MyAuthHeaderClass : public PacketHeaderClass
{public:
	MyAuthHeaderClass() : PacketHeaderClass("PacketHeader/MyAuth", sizeof(hdr_myauth))
	{		bind_offset(&hdr_myauth::offset_);	}} class_myauthhdr;static class MyAuthClass : public TclClass
{public:
	MyAuthClass() : TclClass("Agent/MyAuth") {}
	TclObject* create(int, const char*const*)
	{		return (new MyAuthAgent());	}} class_myauth;MyAuthAgent::MyAuthAgent() : Agent(PT_MyAuth){
	seq=0;
	send_no=0;
	rcv_no=0;
	auth_no=0;
	auth_pro=0;
	//pktlist=0;	for(int j=0;j<MAXSIZE;j++)	{		pktlist[j]=0;	}

	bind("packetSize_", &size_);}void MyAuthAgent::send()
{
	Packet* pkt = allocpkt();
	hdr_myauth* hdr = hdr_myauth::access(pkt);
	//hdr->seq=seq++;	hdr->seq=seq;
	hdr->send_time = Scheduler::instance().clock();
	hdr->rcv_time=-1;//默认为-1,表示未收到
	
	auth_send(pkt,0);
	hdr->authed=0;//默认不能被认证
	seq++;//此语句必须在auth_send(pkt,0);后面,否则auth_send()中的seq变成了当前包的seq加2.

	send_no++;
	
	//可在此处调用一个Otcl过程,打印输出类似seq为0的packet已被发送	hdr_ip* hdrip=hdr_ip::access(pkt);
	char sendc[100];//send char
	sprintf(sendc, "%s outp send %d %d %3.1f", name(),hdr->seq,
		hdrip->src_.addr_ >> Address::instance().NodeShift_[1], 
	    (hdr->rcv_time-hdr->send_time) * 1000);
    	Tcl& tcl = Tcl::instance();
    	tcl.eval(sendc);

	Agent::send(pkt, 0);	//return 0;}

void MyAuthAgent::auth_send(Packet* pkt,Handler*)
{
	hdr_myauth* hdr = hdr_myauth::access(pkt);
	hdr->myauth.auth_seq=seq+1;//当前packet携带下一个packet的认证信息	hdr->myauth.sign=-1;//表示形式化的数字签名
	hdr->myauth.hash=-1;//表示形式化的摘要
}




void MyAuthAgent::recv(Packet* pkt,Handler*)
{
	pktlist[rcv_no]=pkt;
	rcv_no++;

	hdr_myauth* hdr = hdr_myauth::access(pkt);
	hdr->rcv_time = Scheduler::instance().clock();

	//可在此处调用一个Otcl过程,打印输出类似接收到seq为0的packet
	hdr_ip* hdrip=hdr_ip::access(pkt);
	char rcvc[100];//receive char
	sprintf(rcvc, "%s outp rcv %d %d %3.1f", name(),hdr->seq,
		hdrip->src_.addr_ >> Address::instance().NodeShift_[1], 
	    (hdr->rcv_time-hdr->send_time) * 1000);
    	Tcl& tcl = Tcl::instance();
    	tcl.eval(rcvc);

	/*
	Agent/MyAuth instproc outp {typ n from rtt} {
		$self instvar node_
		switch $typ {
			send {puts "node [$node_ id] sends seq $n packet."
			}
			rcv {puts "node [$node_ id] received seq $n packet from \
					$from with round-trip-time $rtt ms."
			}
			auth {puts "node [$node_ id] received seq $n packet from \
					$from,which is authenticated."
			}
			default {puts "args error!"}
		}
	}

	*/


	if(rcv_no>1)
		//auth_recv(pkt,0);		auth_recv();
	
}


/*每个包在接受到的时候都会调用auth_recv(Packet* pkt,Handler*),有一个问题:当前收到了seq为i的
*packet,但是携带其认证信息的包由于某种原因在它后面才到达,这时怎么办?
*将for(int i=0;i<rcv_no-1;i++)改为:i<rcv_no,还是i<MAXSIZE????????????
*/
/*
void MyAuthAgent::auth_recv(Packet* pkt,Handler*)
{
	hdr_myauth* hdr1 = hdr_myauth::access(pkt);
	hdr_myauth* hdr2;
	for(int i=0;i<rcv_no;i++)//???????????????
	{
		hdr2=hdr_myauth::access(pktlist[i]);

		if(hdr2->myauth.auth_seq==hdr1->seq)//已接收到的某packet中含有当前packet的认证信息		{
			hdr1->authed=1;
			auth_no++;

			//可在此处调用一个Otcl过程,打印输出类似seq为0的packet被认证			hdr_ip* hdrip=hdr_ip::access(pkt);
			char authc[100];//auth char
			sprintf(authc, "%s outp auth %d %d %3.1f", name(),hdr1->seq,
				hdrip->src_.addr_ >> Address::instance().NodeShift_[1], 
				(hdr1->rcv_time-hdr1->send_time) * 1000);
			Tcl& tcl = Tcl::instance();
			tcl.eval(authc);
		}
	}	
}
*/void MyAuthAgent::auth_recv(){	for(int k=0;k<rcv_no;k++)
	{
		hdr_myauth* hdr1=hdr_myauth::access(pktlist[k]);		if(hdr1->authed==0)
			for(int m=0;m<rcv_no;m++)
			{
				if(m!=k)
				{
					hdr_myauth* hdr2=hdr_myauth::access(pktlist[m]);
					if(hdr2->myauth.auth_seq==hdr1->seq)//已接收到的某packet中含有当前packet的认证信息
					{
						hdr1->authed=1;
						auth_no++;

						//可在此处调用一个Otcl过程,打印输出类似seq为0的packet被认证
						hdr_ip* hdrip=hdr_ip::access(pktlist[k]);
						char authc[100];//auth char
						sprintf(authc, "%s outp auth %d %d %3.1f", name(),hdr1->seq,
							hdrip->src_.addr_ >> Address::instance().NodeShift_[1], 
							(hdr1->rcv_time-hdr1->send_time) * 1000);
						Tcl& tcl = Tcl::instance();
						tcl.eval(authc);
					}
				}
			}
	}	}/*
void MyAuthAgent::auth()
{
	for(int k=0;k<rcv_no;k++)
	{
		hdr_myauth* hdr1=hdr_myauth::access(pktlist[k]);

		for(int m=0;m<rcv_no;m++)
		{
			if(m!=k)
			{
				hdr_myauth* hdr2=hdr_myauth::access(pktlist[m]);
				if(hdr2->myauth.auth_seq==hdr1->seq)//已接收到的某packet中含有当前packet的认证信息
				{
					hdr1->authed=1;
					auth_no++;

					//可在此处调用一个Otcl过程,打印输出类似seq为0的packet被认证
					hdr_ip* hdrip=hdr_ip::access(pktlist[k]);
					char authc[100];//auth char
					sprintf(authc, "%s outp auth %d %d %3.1f", name(),hdr1->seq,
						hdrip->src_.addr_ >> Address::instance().NodeShift_[1], 
						(hdr1->rcv_time-hdr1->send_time) * 1000);
					Tcl& tcl = Tcl::instance();
					tcl.eval(authc);
				}
			}
		}
	}	
}
*/int MyAuthAgent::command(int argc, const char*const* argv){	Tcl& tcl = Tcl::instance();
	if(argc==2)
	{
		if(strcmp(argv[1], "send") == 0)
		{
			send();
			return TCL_OK;
		}
		//必须先进行"auth"命令对所有接收到的packet完成认证检测处理后,才能调用"auth_no"和"auth_pro"。		/*
		if(strcmp(argv[1], "auth") == 0)
		{
			auth();
			return TCL_OK;
		}		*/
		if(strcmp(argv[1], "send_no") == 0)
		{
			//Tcl& tcl = Tcl::instance();
			tcl.evalf("puts \"the sender sends %d packets in total !\"",send_no);//????????
			return TCL_OK;
		}
		if(strcmp(argv[1], "rcv_no") == 0)
		{
			//Tcl& tcl = Tcl::instance();
			tcl.evalf("puts \"the receiver receives %d packets in total !\"",rcv_no);//?????????
			return TCL_OK;
		}		if(strcmp(argv[1], "auth_no") == 0)
		{
			//Tcl& tcl = Tcl::instance();
			tcl.evalf("puts \"%d packets are authed in total !\"",auth_no);//?????????
			return TCL_OK;
		}
		if(strcmp(argv[1], "auth_pro") == 0)
		{
			auth_pro=(double)auth_no/rcv_no;//这样定义的认证概率的计算确切吗?
			//Tcl& tcl = Tcl::instance();
			tcl.evalf("puts \"the authentication probalility is %f !\"",auth_pro);//???????
			return TCL_OK;
		}
	}
	return (Agent::command(argc, argv));}

⌨️ 快捷键说明

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