A method of embedding self-write protocol in ns2.35

Source: Internet
Author: User
Tags static class strcmp

This article describes how to embed in the latest version of the NS2.35 of a simple new protocol, the reader can not be serious about the implementation of the Protocol code, first familiar with the embedded process because this code still has a certain bug, but the overall framework of the implementation of a protocol is right. Subsequent articles will explain how to write a new protocol, such as TCL how to pass the value to C + + and so on. I. Preparation of a new agreement Simple_trans

We create a folder in the Ns_home root directory kgn, the directory has two files: Simple_trans.h and simple_trans.cc, these two files are our new Protocol's main implementation file.

Simple_trans.h Code Content

#ifndef ns_simple_trans_h #define NS_SIMPLE_TRANS_H #include "agent.h" #include "tclcl.h" #include "packet.h" #include "a Ddress.h "#include" ip.h//protocol default transport port #define Protocol_default_port 1023//sync instruction (similar to the first step of three handshake in a TCP protocol,
	
	In fact, our agreement is ultimately to implement a simplified version of the three-step handshake #define PROTOCOL_INIT_SYN 1//Only type This is my definition, the other content is ns2 system needs struct Hdr_simple_trans {
	
	int type;
    static int offset_;
    inline static int& offset () {return offset_; } inline static Hdr_simple_trans * Access (const PACKET * p) {return (hdr_simple_trans*) p->access (offset_)
    ;


}	
};
Where data packets are generated, packets are sent, and packets are received, the target variable included is the next target given by the data packages.
    	Class Simple_trans_agent:public Agent {public:simple_trans_agent (); virtual void recv (Packet *, Handler *),//"Automatically" receives packets transmitted over the network (more deeply through address and port filters) void send_simple_msg (int type, int targe t);//create and send data int get_target () {return simple_target;}
		interface protection Protected:int Simple_target;
		int simple_port; int command (int argc, const CHAR*CONST*ARGV);

};  A timer (alarm clock) will call a expire (timeout) function class Syntimer:public Timerhandler {public:syntimer (simple_trans_agent* T) at the time:
	Timerhandler (), T_ (t) {} inline virtual void expire (Event *);
protected:simple_trans_agent* T_;

};
 #endif
simple_trans.cc Code Content

#include "simple_trans.h"//Display system time #define now scheduler::instance (). Clock ()//Get the address of the current node #define MYNODE addresses::

Instance (). Get_nodeaddr (addr ()) int hdr_simple_trans::offset_; Static class Simple_transheaderclass:public Packetheaderclass {Public:simple_transheaderclass (): PacketHeaderClass ("Packetheader/simple_trans", sizeof (Hdr_simple_trans))
		{Bind_offset (&hdr_simple_trans::offset_);

}} CLASS_SIMPLE_TRANSHDR; Static class Simple_transclass:public Tclclass {Public:simple_transclass (): Tclclass ("Agent/simple_trans") {} TclOb
	ject* Create (int, const char*const*) {return (new simple_trans_agent ());

}} Class_simple_trans; Simple_trans_agent::simple_trans_agent (): Agent (Pt_simple_trans_packet), Simple_target ( -1), Simple_port (PROTOCOL_
	Default_port) {bind ("simple_target_", &simple_target);
	
Bind ("Simple_port_", &simple_port); Send our SYN message to our target node void Syntimer::expire (Event *) {t_->send_simple_msg (Protocol_init_syn, t_->get_Target ());
This->resched (1.00);
	} void simple_trans_agent::send_simple_msg (int type, int target) {//packet generation packet* PKT = ALLOCPKT ();
	Packet Access Hdr_ip *iph = hdr_ip::access (PKT);
	
	Hdr_simple_trans *SHDR = hdr_simple_trans::access (PKT);
		if (type = = Protocol_init_syn) {if (Simple_target!=-1) {iph->daddr () = Simple_target;
			else {printf ("No target specificed \ n");
		return;
		} iph->dport () = Simple_port;
		IPH->SADDR () = Mynode;
		Shdr->type = Protocol_init_syn;	
	Send (PKT, 0); } int Simple_trans_agent::command (int argc, const char*const* argv) {if (argc = 2) {if (!strcmp (argv[1), "Beg
			In ")) {//sendsend_simple_msg (Protocol_init_syn, simple_target);
			Syntimer *syn_timer = new Syntimer (this);
			
			Syn_timer->resched (1.00);		
		return (TCL_OK);
			
			} if (argc = 3) {if (!strcmp (argv[1), set-target)) {simple_target = Atoi (argv[2)); printf ("=>set-target =%d \ n", Simple_target);
		return (TCL_OK);
} return (Agent::command (argc, argv));	
	} void Simple_trans_agent::recv (Packet *p, Handler *) {hdr_ip *iph = hdr_ip::access (p);
	
	Hdr_simple_trans *SHDR = hdr_simple_trans::access (p); if (Shdr->type = = Protocol_init_syn) {printf ("=>at%lf node%d receive Protocol_init_syn signaling from node
		
	%d \ n ", now, Mynode, iph->saddr ());
	else {printf ("wrong type siganling \ n");
 }
}

ii. Modification of the packet.h

In ns_home/common/packet.h add static const packet_t Pt_simple_trans_packet = 74;
Add name_[pt_simple_trans_packet] = "Simple_trans_packet" (not required) in class P_info.

Packet.h Key Code content:

Add by Song
static const packet_t Pt_simple_trans_packet =;	
Insert new packet types here
static packet_t pt_ntype = * * * must is the last one       

name_[pt_ntype]= "undefined";
Add by Song
Name_[pt_simple_trans_packet] = "Simple_trans_packet";

third, recompile the ns2.35

The final step is to compile our entire protocol to type it into the NS, before compiling we want to modify the makefile file, because we are in the NS_HOME/KGN directory, so makefile need to modify two places:

Adding-i./kgn to INCLUDES = Adds the advantage that we do not have to include KGN sub directories when using SIMPLE_TRANS.H in other directories;

Join KGN/SIMPLE_TRANS.O \ in OBJ_CC =.

Makefile Key code content:

INCLUDES = \
	-  I. \
	I. \
	-i/home/song/ns-allinone-2.35/tclcl-1.20-i/home/song/ns-allinone-2.35/otcl-1.14-i/home/song/ ns-allinone-2.35/include-i/home/song/ns-allinone-2.35/include-i/home/song/ns-allinone-2.35/include-i/usr/ Include/pcap \
	-i./tcp-i./sctp-i./common-i./link-i./queue \
	-i./adc-i./apps-i./mac-i./mobile-i./trace \
  -i./routing-i./tools-i./classifier-i./mcast \
	-i./diffusion3/lib/main-i./diffusion3/lib \
	-I./ Diffusion3/lib/nr-i./diffusion3/ns \
	-i./diffusion3/filter_core-i./asim/-i./qs \
	-i./diffserv-i./ Satellite \
	-i./wpan\
	-i./kgn

OBJ_CC = \ tools/random.o tools/rng.o tools/ranvar.o common/misc.o common/timer-handler.o \ common/scheduler.o common/ob
	JECT.O common/packet.o \ common/ip.o routing/route.o common/connector.o common/ttl.o \ trace/trace.o trace/trace-ip.o \ CLASSIFIER/CLASSIFIER.O classifier/classifier-addr.o \ CLASSIFIER/CLASSIFIER-HASH.O \ classifier/ CLASSIFIER-VIRTUAL.O \ classifier/classifier-mcast.o \ CLASSIFIER/CLASSIFIER-BST.O \ CLASSIFIER/CLASSIFIER-MPATH.O MCAST/REPLICATOR.O \ classifier/classifier-mac.o \ classifier/classifier-qs.o \ classifier/classifier-port.o src_rtg/ CLASSIFIER-SR.O \ src_rtg/sragent.o src_rtg/hdr_src.o adc/ump.o \ qs/qsagent.o qs/hdr_qs.o \ APPS/APP.O Apps/tel NET.O tcp/tcplib-telnet.o \ tools/trafgen.o trace/traffictrace.o tools/pareto.o \ tools/expoo.o tools/cbr_traffic.o \ A DC/TBF.O adc/resv.o adc/sa.o tcp/saack.o \ tools/measuremod.o adc/estimator.o adc/adc.o \ ADC/MS-ADC.O T.O ADC/ACTO-ADC.O \ ADC/POINTSAMPLE-EST.O ADC/SALINK.O adc/actp-adc.o \ adc/hb-adc.o adc/expavg-est.o\ adc/param-adc.o adc/null-estimator.o \ Adc/adaptive-receiv ER.O apps/vatrcvr.o adc/consrcvr.o \ common/agent.o common/message.o apps/udp.o \ common/session-rtp.o APPS/RTP.O tcp/rt CP.O \ common/ivs.o \ common/messpass.o common/tp.o common/tpm.o apps/worm.o \ TCP/TCP.O TCP/TCP-SINK.O \ tcp/tcp-newreno.o \ tcp/tcp-vegas.o tcp/tcp-rbp.o tcp/tcp-full.o tcp/rq.o \ baytcp/tcp-full-bay.o BAYTCP/FTPC.O P/FTPS.O \ tcp/scoreboard.o tcp/scoreboard-rq.o tcp/tcp-sack1.o tcp/tcp-fack.o \ tcp/scoreboard1.o tcp/tcp-linux.o tcp/  LINUX/NS-LINUX-UTIL.O \ tcp/tcp-asym.o tcp/tcp-asym-sink.o tcp/tcp-fs.o \ tcp/tcp-asym-fs.o \ tcp/tcp-int.o TCP/CHOST.O TCP/TCP-SESSION.O \ TCP/NILIST.O \ sctp/sctp.o apps/sctp_app1.o\ sctp/sctp-timestamp.o sctp/sctp-hbAfterRto.o \ sctp/
        SCTP-MULTIPLEFASTRTX.O sctp/sctp-mfrhbafterrto.o \ sctp/sctp-mfrtimestamp.o \ sctp/sctp-cmt.o \ SCTP/SCTPDEBUG.O \
     DCCP/DCCP_SB.O \   DCCP/DCCP_OPT.O \ dccp/dccp_ackv.o \ dccp/dccp_packets.o \ DCCP/DCCP.O \ dccp/dccp_tcpl IKE.O \ dccp/dccp_tfrc.o \ tools/integrator.o tools/queue-monitor.o \ tools/flowmon.o tools/loss-monitor.o \ Qu EUE/QUEUE.O queue/drop-tail.o \ adc/simple-intserv-sched.o queue/red.o \ queue/semantic-packetqueue.o queue/ SEMANTIC-RED.O \ tcp/ack-recons.o \ queue/sfq.o queue/fq.o queue/drr.o queue/srr.o queue/cbq.o \ queue/jobs.o Queue/mar KER.O queue/demarker.o \ link/hackloss.o queue/errmodel.o queue/fec.o\ link/delay.o tcp/snoop.o \ GAF/GAF.O \ Link/dyn ALINK.O routing/rtprotodv.o common/net-interface.o \ mcast/ctrmcast.o mcast/mcast_ctrl.o mcast/srm.o \ common/ SESSIONHELPER.O queue/delaymodel.o \ mcast/srm-ssm.o mcast/srm-topo.o \ routing/alloc-address.o routing/address.o \ $ (L ib_dir) Int. VEC.O $ (lib_dir) int. RVEC.O \ $ (lib_dir) DMALLOC_SUPPORT.O \ webcache/http.o webcache/tcp-simple.o WEBCACHE/PAGEPOOL.O \ Webcache/inval-agen T.O webcache/tcpAPP.O webcache/http-aux.o \ webcache/mcache.o webcache/webtraf.o \ webcache/webserver.o \ webcache/logweb.o \ empweb/e MPWEB.O \ empweb/empftp.o \ realaudio/realaudio.o \ mac/lanrouter.o classifier/filter.o \ COMMON/PKT-COUNTER.O \ Comm ON/DECAPSULATOR.O common/encapsulator.o \ COMMON/ENCAP.O \ mac/channel.o mac/mac.o mac/ll.o mac/mac-802_11.o \ mac/mac- 802_11EXT.O \ mac/mac-802_3.o mac/mac-tdma.o mac/smac.o \ mobile/mip.o mobile/mip-reg.o mobile/gridkeeper.o \ MOBILE/PR OPAGATION.O mobile/tworayground.o \ mobile/nakagami.o \ mobile/antenna.o mobile/omni-antenna.o \ MOBILE/SHADOWING.O Mob ILE/SHADOWING-VIS.O mobile/dumb-agent.o \ common/bi-connector.o common/node.o \ common/mobilenode.o \ mac/arp.o mobile/ GOD.O mobile/dem.o \ mobile/topography.o mobile/modulation.o \ queue/priqueue.o queue/dsr-priqueue.o \ mac/phy.o mac/wi  RED-PHY.O mac/wireless-phy.o \ mac/wireless-phyext.o \ mac/mac-timers.o trace/cmu-trace.o mac/varp.o \ MAC/MAC-SIMPLE.O \ SATELLITE/SAT-HDLC.O \ dsdv/dsdv.o dsdv/rtable.o queue/rtqueue.o \ routing/rttable.o \ imep/imep.o imep/dest_queue.o \ IME P/IMEP_RT.O imep/rxmit_queue.o imep/imep_timers.o imep/imep_util.o imep/imep_io.o \ tora/tora.o TORA/TORA_API.O TORA_DEST.O \ tora/tora_io.o tora/tora_logs.o tora/tora_neighbor.o \ dsr/dsragent.o dsr/hdr_sr.o dsr/mobicache.o dsr/pa TH.O \ dsr/requesttable.o dsr/routecache.o dsr/add_sr.o \ dsr/dsr_proto.o dsr/flowstruct.o dsr/linkcache.o \ Dsr/simple CACHE.O dsr/sr_forwarder.o \ aodv/aodv_logs.o aodv/aodv.o \ aodv/aodv_rtable.o aodv/aodv_rqueue.o \ AOMDV/AOMDV_LOGS.O AOMDV/AOMDV.O \ aomdv/aomdv_rtable.o aomdv/aomdv_rqueue.o \ puma/puma.o \ mdart/mdart_adp.o mdart/mdart_dht.o Mdart/mda RT_NDP.O \ mdart/mdart_neighbor.o mdart/mdart_queue.o mdart/mdart_table.o \ mdart/mdart.o \ COMMON/NS-PROCESS.O \ Sate LLITE/SATGEOMETRY.O satellite/sathandoff.o \ satellite/satlink.o satellite/satnode.o \ SATELLITE/SATPOSITION.O SATELLITE/SATROUTE.O \ satellite/sATTRACE.O \ rap/raplist.o rap/rap.o rap/media-app.o rap/utilities.o \ common/fsm.o tcp/tcp-abs.o \ diffusion/diffusion.
	o diffusion/diff_rate.o diffusion/diff_prob.o \ diffusion/diff_sink.o diffusion/flooding.o diffusion/omni_mcast.o \ DIFFUSION/HASH_TABLE.O diffusion/routing_table.o diffusion/iflist.o \ tcp/tfrc.o tcp/tfrc-sink.o mobile/ ENERGY-MODEL.O apps/ping.o tcp/tcp-rfc793edu.o \ queue/rio.o queue/semantic-rio.o TCP/TCP-SACK-RH.O SCOREBOARD-RH.O \ plm/loss-monitor-plm.o plm/cbr-traffic-pp.o \ linkstate/hdr-ls.o \ mpls/classifier-addr-mpls.o mpls/ LDP.O mpls/mpls-module.o \ routing/rtmodule.o classifier/classifier-hier.o \ ROUTING/ADDR-PARAMS.O \ Nix/hdr_nv . o nix/classifier-nix.o \ nix/nixnode.o \ routealgo/rnode.o \ routealgo/bfs.o \ Routea
	LGO/RBITMAP.O \ routealgo/rlookup.o \ routealgo/routealgo.o \ nix/nixvec.o \ NIX/NIXROUTE.O \ DIFFSERV/DSRED.O diffserv/dsredq.o \ diffserv/dsedge.o DIFFSERV/DSCORE.O \ diffserv/dspolicy.o diffserv/ew.o diffserv/dewp.o \ queue/red-pd.o queue/pi.o queue/vq.o queue/rem.o \ Qu EUE/GK.O \ pushback/rate-limit.o pushback/rate-limit-strategy.o \ pushback/ident-tree.o pushback/agg-spec.o \ Pushback /LOGGING-DATA-STRUCT.O \ pushback/rate-estimator.o \ pushback/pushback-queue.o pushback/pushback.o \ common/ PARENTNODE.O trace/basetrace.o \ common/simulator.o asim/asim.o \ common/scheduler-map.o common/splay-scheduler.o \ Lin KSTATE/LS.O linkstate/rtprotols.o \ pgm/classifier-pgm.o pgm/pgm-agent.o pgm/pgm-sender.o \ pgm/pgm-receiver.o mcast/ RCVBUF.O \ mcast/classifier-lms.o mcast/lms-agent.o mcast/lms-receiver.o \ mcast/lms-sender.o \ queue/delayer.o \ xcp/ XCPQ.O xcp/xcp.o xcp/xcp-end-sys.o \ wpan/p802_15_4csmaca.o wpan/p802_15_4fail.o \ wpan/p802_15_4hlist.o wpan/p802_15_ 4MAC.O \ wpan/p802_15_4nam.o wpan/p802_15_4phy.o \ wpan/p802_15_4sscs.o wpan/p802_15_4timer.o \ wpan/p802_15_4trace.o W PAN/P802_15_4TRANSAC.O \ apps/pbc.o \ kGN/SIMPLE_TRANS.O \ $ (OBJ_STL) 

How to recompile see another article in this series NS2 the source code recompile step. After the compilation completes the reinstallation, you can test it.

Four, the preparation of TCL test:

# This script are created by NSG2 Beta1 #  

Screenshot of Run Effect:

References: How to implement a simple network protocol in NS2, the original text based on the 2.31 version, this article on the basis of porting to the 2.35 version, the code modified to adapt to the new version and comment on critical code. original articles, reproduced please specify.

Copy to Google Translate translation results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.