Sending Packets

From nsnam
Jump to: navigation, search

ns2 is a nice tool for simulating networks and network traffic. But after having done that for a while and having studied the behavior of TCP in all its different implementations in ns2, one might want to dare the next step: creating a new protocol...

One of the most basic questions (and one of the first serious ones, too), is how to actually send a packet in ns2. This question is not easily answered. According to the OSI Reference Model there exist several layers in a network, each taking care of a different aspect of networking and hence sending out packets from and to different layers to perform their duties. This page tries to shed some light on how packets are created and send from those layers and how ns2 internal processes could be utilized to mimic that.

ns2 utilizes several different methods in order to send packets. If you are writing your own agent you might want to utilize some of these methods:


Searching the ns2 mailing list for target_ one can find this message:

It states:

As I understood, ns2 uses the following scheme to send 
	1) In ns2 Packet header, there were two fields "src" and" dst"
which are the "source node" and "destination node" copied from
corresponding "addr_" and "dst_" fields of Class agent when
allocating and sending packet from agent.
	2) "target_" is used to pass packet from higher
layor protocol to lower layro protocol, e.g.,  Tcp Application (App.
layor) pass the packet to Tcp Agent (Transport Layor) by calling
"target_->recv(p)" in Tcp Application.
	3)  At the "bottom" of protocol stack is "connector", so
when connector is called "recv(p)", it will demultiplex the
packet by it "dst_" field in header (maybe "src_" field also
in some demultiplexer), and then send the packet to corresponding
"link" which is composed by some components like queue, monitor,...,
so on...  finally the packet will arrive the connector of next
hop(node), and be passed from bottom of protocol stack to top.

void TcpAgent::sendmsg(int nbytes, const char*)

This is the packet sending method utilized by the tcp agent. It resides in tcp/ and is declared a virtual function.

void Scheduler::schedule(Handler* h, Event* e, double delay)

This method can be used to schedule the sending of a packet, since every packet is an event. When the function is called, the packet is scheduled to be received by h at time now+delay (i.e. Scheduler::instance().clock()+delay). The Handler * h pointer could be any acceptable handler, for example the link layer. Packets scheduled at the link layer show up in the trace file as belonging to MAC.

Agent->send(Packet *p, Callback *c)

This method is availbale in all C++ classes derived from Agent. Simple set the appropriate fields beforehand, namely the IP destination address and port address (use pointers to each layer and set fields - see example below), and call this function. On most simple agents, set callback to null (eg, 0).

Example live code taken from IST-CIMS in /ns2/cims/

void myAgent::recv(Packet* p, Handler *h)
	hdr_ip    *iph	= hdr_ip::access(p);           //set pointer to IP header
	hdr_cmn   *ch 	= hdr_cmn::access(p);          //set pointer to common header (that all packets have)
	hdr_timip *th	= hdr_timip::access(p);        // set pointer to custom made header

        // set auxiliary variables
	int MH_iaddr       = th->home_addr_iaddr_;
	int prev_Hop_iaddr = th->previous_hop_iaddr_;		// nova localização do terminal (hard)
	int prev_Hop_id    = th->previous_hop_id_;	
	int MH_location_RO_id = th->MH_location_RO_id;		// nova localização do terminal (soft)

        // perform agent logic with ifs, switches,  etc

        // prepare packet to be sent again with answer
	iph->saddr() = addr();              // IP source address
	iph->sport() = port();              // IP port address
	iph->daddr() = nextHop_iaddr;        // destination addreess

	send(p, 0);                          // send the packet

Some related information on the OTcl part of this method is given in the documentation of how to connect agents.