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

📄 aodv_routing_nexttwo.pr.c

📁 adhoc网络中AODV协议的仿真码, 并有改进的
💻 C
📖 第 1 页 / 共 5 页
字号:
if(DEBUG > 1 ) printf("      > Check routing table for entry\n");

// print entry if any available
if(DEBUG >1) aodv_entry_print(destination);
// get packet from sub-queue
if(DEBUG > 1 ) printf("      > Get packet from buffer\n");


data_pk_ptr = aodv_data_pk_dequeue(destination);

if((nextHop != NOT_VALID) && (nextHop != NON_EXISTENT))
	{
	if(DEBUG > 1 ) printf("      > Node has a fresh active entry\n");

	// schedule an event if no ack received within the waiting window 

	if(DEBUG > 3 ) printf("      > Arming Ack Timer\n");

	aodv_ack_timeout_schedule(data_pk_ptr,destination);
	// send packet to mac layer
	aodv_pk_send_to_mac_layer(data_pk_ptr,nextHop);

	// refresh entry timeout
	if(DEBUG > 2 ) printf("      > Refresh expiration time for entry:\n");
	aodv_entry_expirationTime_set(destination,op_sim_time()+ACTIVE_ROUTE_TIMEOUT); 
	}

else
	{
	if(DEBUG > 1 ) printf("    > Error @ aodv_buffer_serve():: no valid entry was found !\n");
	// call route function
	aodv_data_pk_route(data_pk_ptr);

	}

}
else
	{
	if(DEBUG > 1 ) printf("    > Error @ aodv_buffer_serve():: buffer empty !\n");
	}
if(DEBUG > 1 ) printf("    - Function aodv_buffer_serve() done\n");

}



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_ack_timeout_schedule(Packet* pk, int destination)  //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 
aodv_ack_timeout_schedule(Packet* pk, int destination)

{


AckEvt*   ackEvt;


/*
/* This routine is called when a data packet is sent to the 
/* MAC Layer. As a matter of fact, this procedure schedules 
/* an interruption in Wait_Ack seconds of the current time. 
/* If no ack is received within this period of time, the 
/* process transits to the Ack_Timeout State in order to re-
/* transmit the lost packet.
/* However, if an ack is received, the ack-timeout interruption
/* is cancelled (see Rcv_Ack State).
*/

if(DEBUG > 3 ) printf("      - Function aodv_ack_timeout_schedule(destination %d)\n", destination);

// create an AckEvt structure to store interruption attributes
ackEvt= (AckEvt*) op_prg_mem_alloc(sizeof(AckEvt));

if (ackEvt!=NULL)

	{
	// Schedule interruption with the appropriate code (destination to which packet has been sent)

	ackEvt->evt=op_intrpt_schedule_remote(op_sim_time()+ Wait_ACK,destination,op_id_self());
	// store a copy of the sent packet
	ackEvt->copy_pk_ptr = op_pk_copy(pk);

	// Store the Ack evt in the AckEvt list
	putInFifoMultiplex(&ackEvtFifo,ackEvt,destination);
	}

}


/////////////////////////////////////////////////////////////////

/*********************** Generate RREQ *************************/
/////////////////////////////////////////////////////////////////



void 
aodv_rreq_pk_generate(int destination, Request_Status_Type status) 

{


Packet*       rreq_pk_ptr;

/*
/* Initiate the discovery process by generating a RREQ for a given
/* destination.
/* The procedure not only generates a RREQ packet with the appropiate
/* fields. But it also updates node's "RequestSent" repository (see 
/* function aodv_requestSent_repository_update).
*/

if(DEBUG > 1) printf("    - Function aodv_rreq_pk_generate(destination %d)\n", destination);


// check if no request is already pending for that destination
if(RequestSent[destination].status == OFF)
	{
	// Create a request type packet 

	rreq_pk_ptr = op_pk_create_fmt("AODV_RREQ_NEXTTWO");


	// set RREQ fields
	op_pk_nfd_set(rreq_pk_ptr,"TTL",RequestSent[destination].ttl);

	op_pk_nfd_set(rreq_pk_ptr,"G",RequestSent[destination].gratuitous);
	op_pk_nfd_set(rreq_pk_ptr,"SRC",node_addr);

	op_pk_nfd_set(rreq_pk_ptr,"DEST",destination);

	op_pk_nfd_set(rreq_pk_ptr,"DestSeqNb",aodv_entry_destSeqNb_get(destination));

	mySeqNb++;
	op_pk_nfd_set(rreq_pk_ptr,"SrcSeqNb",mySeqNb);

	op_pk_nfd_set(rreq_pk_ptr,"BroadcastID",myBroadcastID);
	op_pk_nfd_set(rreq_pk_ptr,"PreviousTwoHop", -1);
	//increment node's boadcastID
	myBroadcastID++;

	// send to mac layer
	if(DEBUG > 1 ) printf("      > RREQ packet has been generated\n");

	aodv_pk_send_to_mac_layer(rreq_pk_ptr, BROADCAST);
	// update RequestSent repository
	aodv_requestSent_repository_update(destination, status);
	}
else 
	{
	if(DEBUG > 1 ) printf("      > A request is pending: cannot renew it\n");

	}
if(DEBUG > 1 ) printf("    - Function aodv_rreq_pk_generate() done\n");


}


/////////////////////////////////////////////////////////////////

/******************* Generate RREQ to nextHop*******************/
/////////////////////////////////////////////////////////////////



void 
aodv_rreq_pk_generate_to_nextHop(int destination, Request_Status_Type status, int nextHop) 

{


Packet*       rreq_pk_ptr;

/*
/* Initiate the discovery process by generating a RREQ for a given
/* destination.
/* The procedure not only generates a RREQ packet with the appropiate
/* fields. But it also updates node's "RequestSent" repository (see 
/* function aodv_requestSent_repository_update).
*/

if(DEBUG > 1) printf("    - Function aodv_rreq_pk_generate(destination %d)\n", destination);


// check if no request is already pending for that destination
if(RequestSent[destination].status == OFF)
	{
	// Create a request type packet 

	rreq_pk_ptr = op_pk_create_fmt("AODV_RREQ_NEXTTWO");


	// set RREQ fields
	op_pk_nfd_set(rreq_pk_ptr,"TTL",RequestSent[destination].ttl);

	op_pk_nfd_set(rreq_pk_ptr,"G",RequestSent[destination].gratuitous);
	op_pk_nfd_set(rreq_pk_ptr,"SRC",node_addr);

	op_pk_nfd_set(rreq_pk_ptr,"DEST",destination);

	op_pk_nfd_set(rreq_pk_ptr,"DestSeqNb",aodv_entry_destSeqNb_get(destination));

	mySeqNb++;
	op_pk_nfd_set(rreq_pk_ptr,"SrcSeqNb",mySeqNb);

	op_pk_nfd_set(rreq_pk_ptr,"BroadcastID",myBroadcastID);
	op_pk_nfd_set(rreq_pk_ptr,"PreviousTwoHop", -1);
	//increment node's boadcastID
	myBroadcastID++;

	// send to mac layer
	if(DEBUG > 1 ) printf("      > RREQ packet has been generated\n");

	aodv_pk_send_to_mac_layer(rreq_pk_ptr, nextHop);
	// update RequestSent repository
	aodv_requestSent_repository_update(destination, status);
	}
else 
	{
	if(DEBUG > 1 ) printf("      > A request is pending: cannot renew it\n");

	}
if(DEBUG > 1 ) printf("    - Function aodv_rreq_pk_generate() done\n");


}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_requestSent_repository_update(int dest  )  //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void 
aodv_requestSent_repository_update(int destination, Request_Status_Type status)
	{
	
	/*
	/* When a request is sent, a corresponding entry is      
	/* updated in the RequestSent repository in order 
	/* to control route requests broadcast. For each request    
	/* that has been generated, node stores the destination
	/* sequence number, the number of retries, the status
	/* of the request, the expiration time (time before 
	/* renewal), and finally the future TTL to use in case 
	/* of renewal
	*/
	
	
	// Store the dest seq nb of the request
	RequestSent[destination].sequence_number=aodv_entry_destSeqNb_get(destination);

	// Set the status of the request 
	RequestSent[destination].status= status;

	
	// schedule for interruption if no reply received within appropriate period
	RequestSent[destination].evt = op_intrpt_schedule_remote(op_sim_time()+2*RequestSent[destination].ttl*NODE_TRAVERSAL_TIME,NREPLY_CODE+destination,op_id_self());
	RequestSent[destination].expirationTime= op_sim_time()+ 2*RequestSent[destination].ttl* NODE_TRAVERSAL_TIME;

	
	// increment TTL for next eventual renewal 
	RequestSent[destination].ttl=RequestSent[destination].ttl+TTL_INCREMENT;
	if(RequestSent[destination].ttl > TTL_TRESHOLD)
		{
		RequestSent[destination].ttl= NET_DIAMETER;
		// Increment the number of retries
		RequestSent[destination].nbOfRetries++;
		}
	}



/////////////////////////////////////////////////////////////////

/*********************** HANDLE REQUEST ************************/
/////////////////////////////////////////////////////////////////



void 
aodv_rreq_pk_receive(Packet* rreq_pk_ptr)

{


int       dest, 
		  destSeqNb, 
		  source,G;

/*
/* Process the received RREQ. This procedure decides 
/* whether node should generate a RREP packet for the 
/* requested destination or simply forward it to the 
/* neighbouring nodes.
/* Note that RREQ is automatically discarded if seen 
/* less than BROADCAST_RECORD_TIME seconds ago
*/


if(DEBUG > 1) printf("    - Function aodv_rreq_pk_receive()\n");
	

// reading packet 
op_pk_nfd_get (rreq_pk_ptr,"DEST",&dest); 

op_pk_nfd_get (rreq_pk_ptr,"SRC",&source);
op_pk_nfd_get (rreq_pk_ptr,"DestSeqNb",&destSeqNb);

op_pk_nfd_get (rreq_pk_ptr,"G",&G);


// check if RREQ has already been seen

if(aodv_rreq_pk_is_fresh_enough(rreq_pk_ptr) == OPC_TRUE)
				

	{
	//request NEVER seen OR SEEN more than BROADCAST_RECORD_TIME ago

	if(DEBUG > 1 ) printf("      > RREQ is fresh enough: request handled \n");

	// update or create reverse entry 
	aodv_entry_update_or_create_from_rreq(rreq_pk_ptr);
	
	// handle request

	if(node_addr == dest) // the current node is the destination

		{

		if(DEBUG > 1 ) printf("      > RREQ has reached its destination: node replies\n");

		aodv_rrep_pk_generate_from_destination(rreq_pk_ptr);
		}
	else  // the node is an intermediate node

		{
		

		if(DEBUG > 1 ) printf("      > Node is a relay\n");

		if(aodv_fresh_enough_entry_is_available(dest,destSeqNb)== OPC_TRUE)
			{
			if(DEBUG > 1) aodv_entry_print(dest);  
			// node has a fresh enough route

			if(DEBUG > 1 ) printf("      > Node has active fresh enough entry: node replies\n");

			// if gratuitous mode requested, generate gratuitous RREP for destination
			if(G)
				{
				if(DEBUG > 1 ) printf("      > Gratuitous reply requested: generate reply for destination\n");
				aodv_gratuitous_rrep_pk_generate(rreq_pk_ptr);
				}
			// generate rrep for source



			if(DEBUG > 1 ) printf("      > Generate reply for the source node\n");
 
			aodv_rrep_pk_generate_from_relay(rreq_pk_ptr);
			}

		else // node doesn't have any active entry for the requested
route

			{

			if(DEBUG > 1 ) printf("      > No active entry for requested route: RREQ should be forwarded\n");
			aodv_rreq_pk_forward(rreq_pk_ptr);
			}
		}
	// print reverse entry if any
	if(DEBUG > 1) printf("      > Print reverse entry if any available\n");
	if(DEBUG > 1) aodv_entry_print(source);
	// print forward entry if any
	if(DEBUG > 1) printf("      > Print forward entry if any available\n");
	if(DEBUG > 1) aodv_entry_print(dest);
	

	}
else //request seen less than BROADCAST_RECORD_TIME ago

	{

	// RREQ discarded 

	if(DEBUG > 1 ) printf("      > Request seen less than BRODCAST_RECORD_TIME ago: RREQ discarded\n");

	op_pk_destroy(rreq_pk_ptr);

	}

		

if(DEBUG > 1 ) printf("    - Function aodv_rreq_pk_receive() done\n");

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_rrep_pk_generate_from_destination(Packet* rreq_pk_ptr)   //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 
aodv_rrep_pk_generate_from_destination(Packet* rreq_pk_ptr)
	{
	
	Packet*     rrep_pk_ptr;	
	int         previousHop,        
		        source,
	            dest,
				destSeqNb;
	/*
	/* This function is called when a RREQ has reached its 
	/* destination and node wants to reply.
	/* Once the RREQ packet is generated, the procedure unicat
	/* it back to the node upstream (node from which RREQ was
	/* received) (PreviousHop field).
	*/
	
	// Read the received RREQ packet
	op_pk_nfd_get (rreq_pk_ptr,"SRC",&source);

	op_pk_nfd_get (rreq_pk_ptr,"DEST",&dest);
	op_pk_nfd_get (rreq_pk_ptr,"PreviousHop",&previousHop);
	op_pk_nfd_get (rreq_pk_ptr,"DestSeqNb",&destSeqNb);


	// Create reply packet
	rrep_pk_ptr = op_pk_create_fmt("AODV_RREP_NEXTTWO");
	// Assign source and dest to the reply packet
	op_pk_nfd_set(rrep_pk_ptr,"SRC",source);

	op_pk_nfd_set(rrep_pk_ptr,"DEST",dest);

	op_pk_nfd_set(rrep_pk_ptr,"HopCount",0);

	op_pk_nfd_set(rrep_pk_ptr,"Lifetime",MY_ROUTE_TIMEOUT);

	op_pk_nfd_set(rrep_pk_ptr,"PreviousTwoHop", -1);
	
	// Assign seq number  
	mySeqNb = max_int(destSeqNb, mySeqNb);

	op_pk_nfd_set(rrep_pk_ptr,"DestSeqNb",mySeqNb);

	if(DEBUG > 1 ) printf("      > RREP has been generated from destination: unicast it to source node [next hop is %d]\n", previousHop);

	// send reply packet 
	aodv_pk_send_to_mac_layer(rrep_pk_ptr,previousHop);

	// Refresh timeout
	aodv_entry_expirationTime_set(source,op_sim_time()+ACTIVE_ROUTE_TIMEOUT);
	// Destroy rreq 
	op_pk_destroy(rreq_pk_ptr);
	}		   


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_gratuitous_rrep_pk_generate(rreq_pk_ptr)   //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 

⌨️ 快捷键说明

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