Ns2下protoname路由协议的添加.docx
《Ns2下protoname路由协议的添加.docx》由会员分享,可在线阅读,更多相关《Ns2下protoname路由协议的添加.docx(21页珍藏版)》请在冰豆网上搜索。
![Ns2下protoname路由协议的添加.docx](https://file1.bdocx.com/fileroot1/2023-3/19/5189addc-b18c-4d28-9472-2207e436b9c8/5189addc-b18c-4d28-9472-2207e436b9c81.gif)
Ns2下protoname路由协议的添加
目录
1. 介绍
2. 建立protoname目录
3. 需要修改的文件
4. 编译
5. 测试
6. 参考文献
介绍
本文适合初识ns2,想懵懂的知道ns2添加路由协议过程的读者。
^_^
一. 建立protoname目录
如我们在ns2目录下看见的dsr或dsdv协议一样,我们要建立自己的协议,也要首先建立一个文件夹,我们以protoname命名。
当完成所有的添加工作后,我们的协议的名字就叫做protoname了,当然你也可以为它起一个你喜欢的名字。
闲言碎语莫要谈,现在let’sbegin!
一个协议最起码要包含以下5个文件,也就是说我们需要把这5个文件放在protoname的文件夹下:
protoname.h protoname.cc protoname_pkt.h protoname_rtable.h protoname_rtable.cc
从这些文件的名字大致可以知道它们是关于什么的(详见[1])
下面是相应文件的代码(在[1]中缺少一些头文件):
1. protoname.h
#ifndef__protoname_h__
#define__protoname_h__
#include"protoname_pkt.h"
#include"protoname_rtable.h"
#include
#include
#include
#include
#include
#include
#include
#include"arp.h"
#include"ll.h"
#include"mac.h"
#include"ip.h"
#include"delay.h"
#defineCURRENT_TIMEScheduler:
:
instance().clock()
#defineJITTER(Random:
:
uniform()*0.5)
classProtoname;//forwarddeclaration
/*Timers*/
classProtoname_PktTimer:
publicTimerHandler{
public:
Protoname_PktTimer(Protoname*agent):
TimerHandler(){
agent_=agent;
}
protected:
Protoname*agent_;
virtualvoidexpire(Event*e);
};
/*Agent*/
classProtoname:
publicAgent{
/*Friends*/
friendclassProtoname_PktTimer;
/*Privatemembers*/
nsaddr_tra_addr_;
//protoname_statestate_;
protoname_rtablertable_;
intaccesible_var_;
u_int8_tseq_num_;
protected:
MobileNode*node_;
PortClassifier*dmux_;//Forpassingpacketsuptoagents.
Trace*logtarget_;//Forlogging.
Protoname_PktTimerpkt_timer_;//Timerforsendingpackets.
inlinensaddr_t&ra_addr(){returnra_addr_;}
//inlineprotoname_state&state(){returnstate_;}
inlineint&accessible_var(){returnaccesible_var_;}
voidforward_data(Packet*);
voidrecv_protoname_pkt(Packet*);
voidsend_protoname_pkt();
voidreset_protoname_pkt_timer();
public:
Protoname(nsaddr_t);
intcommand(int,constchar*const*);
voidrecv(Packet*,Handler*);
//voidmac_failed(Packet*);
};
#endif
2. protoname.cc
#include"protoname.h"
#include"protoname_pkt.h"
#include
#include
#include
inthdr_protoname_pkt:
:
offset_;
staticclassProtonameHeaderClass:
publicPacketHeaderClass{
public:
ProtonameHeaderClass():
PacketHeaderClass("PacketHeader/Protoname",sizeof(hdr_protoname_pkt)){
bind_offset(&hdr_protoname_pkt:
:
offset_);
}
}class_rtProtoProtoname_hdr;
staticclassProtonameClass:
publicTclClass{
public:
ProtonameClass():
TclClass("Agent/Protoname"){}
TclObject*create(intargc,constchar*const*argv){
assert(argc==5);
return(newProtoname((nsaddr_t)Address:
:
instance().str2addr(argv[4])));
}
}class_rtProtoProtoname;
void
Protoname_PktTimer:
:
expire(Event*e){
agent_->send_protoname_pkt();
agent_->reset_protoname_pkt_timer();
}
Protoname:
:
Protoname(nsaddr_tid):
Agent(PT_PROTONAME),pkt_timer_(this){
bind_bool("accesible_var_",&accesible_var_);
ra_addr_=id;
node_=(MobileNode*)Node:
:
get_node_by_address(id);
}
int
Protoname:
:
command(intargc,constchar*const*argv){
if(argc==2){
if(strcasecmp(argv[1],"start")==0){
pkt_timer_.resched(0.0);
returnTCL_OK;
}
elseif(strcasecmp(argv[1],"print_rtable")==0){
if(logtarget_!
=0){
sprintf(logtarget_->pt_->buffer(),"P%f_%d_RoutingTable",CURRENT_TIME,ra_addr());
logtarget_->pt_->dump();
rtable_.print(logtarget_);
}
else{
fprintf(stdout,"%f_%d_Ifyouwanttoprintthisroutingtable"
"youmustcreateatracefileinyourtclscript",CURRENT_TIME,ra_addr());
}
returnTCL_OK;
}
}
elseif(argc==3){
//Obtainscorrespondingdmuxtocarrypacketstoupperlayers
if(strcmp(argv[1],"port-dmux")==0){
dmux_=(PortClassifier*)TclObject:
:
lookup(argv[2]);
if(dmux_==0){
fprintf(stderr,"%s:
%slookupof%sfailed\n",__FILE__,argv[1],argv[2]);
returnTCL_ERROR;
}
returnTCL_OK;
}
//Obtainscorrespondingtracer
elseif(strcmp(argv[1],"log-target")==0||strcmp(argv[1],"tracetarget")==0){
logtarget_=(Trace*)TclObject:
:
lookup(argv[2]);
if(logtarget_==0)
returnTCL_ERROR;
returnTCL_OK;
}
}
//Passthecommandtothebaseclass
returnAgent:
:
command(argc,argv);
}
void
Protoname:
:
recv(Packet*p,Handler*h){
structhdr_cmn*ch=HDR_CMN(p);
structhdr_ip*ih=HDR_IP(p);
if(ih->saddr()==ra_addr()){
//Ifthereexistsaloop,mustdropthepacket
if(ch->num_forwards()>0){
drop(p,DROP_RTR_ROUTE_LOOP);
return;
}
//elseifthisisapacketIamoriginating,mustaddIPheader
elseif(ch->num_forwards()==0)
ch->size()+=IP_HDR_LEN;
}
//Ifitisaprotonamepacket,mustprocessit
if(ch->ptype()==PT_PROTONAME)
recv_protoname_pkt(p);
//Otherwise,mustforwardthepacket(unlessTTLhasreachedzero)
else{
ih->ttl_--;
if(ih->ttl_==0){
drop(p,DROP_RTR_TTL);
return;
}
forward_data(p);
}
}
void
Protoname:
:
recv_protoname_pkt(Packet*p){
structhdr_ip*ih=HDR_IP(p);
structhdr_protoname_pkt*ph=HDR_PROTONAME_PKT(p);
//AllroutingmessagesaresentfromandtoportRT_PORT,
//sowecheckit.
assert(ih->sport()==RT_PORT);
assert(ih->dport()==RT_PORT);
/*...processingofprotonamepacket...*/
//Releaseresources
Packet:
:
free(p);
}
void
Protoname:
:
send_protoname_pkt(){
Packet*p=allocpkt();
structhdr_cmn*ch=HDR_CMN(p);
structhdr_ip*ih=HDR_IP(p);
structhdr_protoname_pkt*ph=HDR_PROTONAME_PKT(p);
ph->pkt_src()=ra_addr();
ph->pkt_len()=7;
ph->pkt_seq_num()=seq_num_++;
ch->ptype()=PT_PROTONAME;
ch->direction()=hdr_cmn:
:
DOWN;
ch->size()=IP_HDR_LEN+ph->pkt_len();
ch->error()=0;
ch->next_hop()=IP_BROADCAST;
ch->addr_type()=NS_AF_INET;
ih->saddr()=ra_addr();
ih->daddr()=IP_BROADCAST;
ih->sport()=RT_PORT;
ih->dport()=RT_PORT;
ih->ttl()=IP_DEF_TTL;
Scheduler:
:
instance().schedule(target_,p,JITTER);
}
void
Protoname:
:
reset_protoname_pkt_timer(){
pkt_timer_.resched((double)5.0);
}
void
Protoname:
:
forward_data(Packet*p){
structhdr_cmn*ch=HDR_CMN(p);
structhdr_ip*ih=HDR_IP(p);
if(ch->direction()==hdr_cmn:
:
UP&&
((u_int32_t)ih->daddr()==IP_BROADCAST||ih->daddr()==ra_addr())){
dmux_->recv(p,0);
return;
}
else{
ch->direction()=hdr_cmn:
:
DOWN;
ch->addr_type()=NS_AF_INET;
if((u_int32_t)ih->daddr()==IP_BROADCAST)
ch->next_hop()=IP_BROADCAST;
else{
nsaddr_tnext_hop=rtable_.lookup(ih->daddr());
if(next_hop==IP_BROADCAST){
debug("%f:
Agent%dcannotforwardapacketdestinedto%d\n",
CURRENT_TIME,
ra_addr(),
ih->daddr());
drop(p,DROP_RTR_NO_ROUTE);
return;
}
else
ch->next_hop()=next_hop;
}
Scheduler:
:
instance().schedule(target_,p,0.0);
}
}
3. protoname_pkt.h
#ifndef__protoname_pkt_h__
#define__protoname_pkt_h__
#include
#defineHDR_PROTONAME_PKT(p)hdr_protoname_pkt:
:
access(p)
structhdr_protoname_pkt{
nsaddr_tpkt_src_;//Nodewhichoriginatedthispacket
u_int16_tpkt_len_;//Packetlength(inbytes)
u_int8_tpkt_seq_num_;//Packetsequencenumber
inlinensaddr_t&pkt_src(){returnpkt_src_;}
inlineu_int16_t&pkt_len(){returnpkt_len_;}
inlineu_int8_t&pkt_seq_num(){returnpkt_seq_num_;}
staticintoffset_;
inlinestaticint&offset(){returnoffset_;}
inlinestatichdr_protoname_pkt*access(constPacket*p){
return(hdr_protoname_pkt*)p->access(offset_);
}
};
#endif
4. protoname_rtable.h
#ifndef__protoname_rtable_h__
#define__protoname_rtable_h__
#include
#include
typedefstd:
:
maprtable_t;
classprotoname_rtable{
rtable_trt_;
public:
protoname_rtable();
voidprint(Trace*);
voidclear();
voidrm_entry(nsaddr_t);
voidadd_entry(nsaddr_t,nsaddr_t);
nsaddr_tlookup(nsaddr_t);
u_int32_tsize();
};
#endif
5. protoname_rtable.cc
#include"protoname_rtable.h"
#include"ip.h"
protoname_rtable:
:
protoname_rtable(){}
void
protoname_rtable:
:
p