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

📄 aodv_routing_nexttwo_rreq.pr.c

📁 adhoc网络中AODV协议的仿真码, 并有改进的
💻 C
📖 第 1 页 / 共 5 页
字号:
void 
aodv_rreq_pk_forward(Packet* rreq_pk_ptr)
	{
	
	int      ttl, 
	         previousHop,
			 RepairNextTwoHop,
	         hopCount;

	/*
	/* Node does not have a fresh enough entry
	/* (or does not have an entry at all) to answer
	/* the received RREQ, so it decides to forward 
	/* it. Node increments the Hop Count field
	/* and decrements the TTL field. 
	/* Note that packet is sent to mac layer only if 
	/* TTL > 0.
	*/
	op_pk_nfd_get (rreq_pk_ptr,"TTL",&ttl);		
	op_pk_nfd_get (rreq_pk_ptr,"HopCount",&hopCount);

	op_pk_nfd_get (rreq_pk_ptr,"PreviousHop",&previousHop);
	op_pk_nfd_get (rreq_pk_ptr,"RepairNextTwoHop",&RepairNextTwoHop);

	if(ttl > 1)
		{
		if(DEBUG > 1 ) printf("      > Node forwards request\n",ttl);

		op_pk_nfd_set(rreq_pk_ptr,"TTL",ttl-1);

		op_pk_nfd_set(rreq_pk_ptr,"HopCount",hopCount+1);
		op_pk_nfd_set(rreq_pk_ptr,"PreviousTwoHop",previousHop);

		// send to mac
		if((RepairNextTwoHop!= NOT_VALID)&&(RepairNextTwoHop!= NON_EXISTENT))
			{  
			  if(aodv_entry_hopCount_get(RepairNextTwoHop)==1)
				  {
				  	op_pk_nfd_set(rreq_pk_ptr,"RepairNextTwoHop",-1);
					aodv_pk_send_to_mac_layer(rreq_pk_ptr, RepairNextTwoHop);
				  }
			  else 
				  {
				    op_pk_destroy(rreq_pk_ptr);
				  }
			}
		else 
			{
		       aodv_pk_send_to_mac_layer(rreq_pk_ptr, BROADCAST);

		    }
		}
	else
		{
		if(DEBUG > 1 ) printf("      > TTL expired: rreq destroyed\n");
		op_pk_destroy(rreq_pk_ptr);
		}
	}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_entry_update_or_create_from_rreq(Packet*     ) //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 
aodv_entry_update_or_create_from_rreq(Packet* rreq_pk_ptr)
	{

	RoutingTableEntry *    reverseEntryPtr;
	int                    source,
	                       previousHop,
						   previousTwoHop,
						   srcSeqNb,
						   hopCount;
	/*
	/* Update or create reverse entry following a RREQ reception.
	*/
	
	// Read RREQ packet
	op_pk_nfd_get (rreq_pk_ptr,"SRC",&source);
	op_pk_nfd_get (rreq_pk_ptr,"PreviousHop",&previousHop); 

	op_pk_nfd_get (rreq_pk_ptr,"SrcSeqNb",&srcSeqNb);

	op_pk_nfd_get (rreq_pk_ptr,"HopCount",&hopCount);

	op_pk_nfd_get (rreq_pk_ptr,"PreviousTwoHop",&previousTwoHop);
	
	// Check if any request is pending for this destination
	if(RequestSent[source].status != OFF)
		aodv_requestSent_repository_reset(source);
	
	// Node creates or updates a REVERSE route

	reverseEntryPtr=aodv_routingTable_entry_get(source);

	if(reverseEntryPtr != OPC_NIL) 


		{

		//entry already exists
		if((srcSeqNb > reverseEntryPtr->destSeqNb) ||
((srcSeqNb == reverseEntryPtr->destSeqNb)

		&&((hopCount+1)< reverseEntryPtr->hopCount)) || (reverseEntryPtr->status != ACTIVE) )

			{
			// entry updated only if (greater seqNb) or 

			//(same seqNbs but smaller hopCount)
			if(DEBUG > 1 ) printf("      > Node updates its reverse route to source node\n");

			reverseEntryPtr->destSeqNb=srcSeqNb;

			reverseEntryPtr->nextHop= previousHop;
			reverseEntryPtr->nextTwoHop = previousTwoHop;
			if(reverseEntryPtr->hopCount != INFINITY)

				reverseEntryPtr->lastHopCount= reverseEntryPtr->hopCount;

			reverseEntryPtr->hopCount=hopCount+1;

			}
		else
			if(DEBUG > 1) printf("      > Node does not need to update its reverse entry\n");

		
		// Refresh entry timeout to max(current expiration time, currentTime+REV_ROUTE_LIFE)	
		if((op_sim_time()+REV_ROUTE_LIFE) > reverseEntryPtr->expirationTime)			
			{			
			aodv_entry_expirationTime_set(source,op_sim_time()+REV_ROUTE_LIFE);
			}
		else
			if(DEBUG > 1) printf("      > Reverse entry timeout does not need to be refreshened\n");


		// set entry status
		reverseEntryPtr->status = ACTIVE;
		}
	else  

		{
		//entry doesn't exist: node creates it
		if(DEBUG > 1 ) printf("      > Node creates a reverse entry to source node\n");

		reverseEntryPtr=aodv_entry_create_new();
		reverseEntryPtr->listOfPrecursors=newFifo();

		reverseEntryPtr->status = ACTIVE;
		reverseEntryPtr->dest=source;

		reverseEntryPtr->destSeqNb=srcSeqNb;

		reverseEntryPtr->nextHop= previousHop;
		reverseEntryPtr->nextTwoHop = previousTwoHop;

		reverseEntryPtr->hopCount=hopCount+1;
		reverseEntryPtr->lastHopCount = hopCount+1;
		// Add the entry to the RoutingTable
		aodv_routingTable_entryPtr_put(reverseEntryPtr);

		// Refresh entry timeout
		aodv_entry_expirationTime_set(source,op_sim_time()+REV_ROUTE_LIFE);
		}
	
	// if reverse entry updated or created, then
	// Serve buffer if any packet's waiting
	if(reverseEntryPtr->hopCount != INFINITY)
		aodv_buffer_serve(source);
	}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_rreq_pk_regenerate(int destination)            //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 
aodv_rreq_pk_regenerate(int destination)

{


/*
/* This function is called in the Renew_Request state when 
/* the previous RREQ didn't receive any RREP and the number
/* of retries is smaller than TTL_RETRIES_TRESHOLD
*/

aodv_rreq_pk_generate(destination, WAITING_FOR_REPLY);

}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// void aodv_reverseListOfPrecursors_update(int prec, int dest)       //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void 
aodv_reverseListOfPrecursors_update(int precursor, int destination)

	{


	int      *destinationPtr; 

	sFifo    *listOfDestination;


	/*
	/* Each time a node "A" (precursor) is added to the list of precursor of 
	/* a given destination "B" (detination), this function is called to add
	/* "B" in the list of nodes to which "A" is a precursor. Thus, when 
	/* link to A is no longer available and node wants to expunge it from 
	/* its routing table, node has only to process entry that are listed
	/* within the reverseListOfPrecursol at the entry "A".
	*/
	
	// var init
	destinationPtr = (int*) op_prg_mem_alloc(sizeof(int));

	*destinationPtr = destination;

	// begin

	if(DEBUG > 3 ) printf("      - Function aodv_reverseListOfPrecursors_update(): precursor = %d || entry = %d\n", precursor,destination);

	// check if a reverse entry already exists for PRECURSOR

	if(DEBUG > 3 ) printf("      > check if a reverse entry already exists\n");

	listOfDestination= (sFifo*) readInFifoMultiplex(reverseListOfPrecursors, precursor);

	

	if(listOfDestination == OPC_NIL) // no entry available

		{

		if(DEBUG > 3 ) printf("      > No entry available: node has to create a new one\n");

		// have to create a new entry

		listOfDestination=newFifo();

		putInFifo(listOfDestination, destinationPtr);

		putInFifoMultiplex(reverseListOfPrecursors,listOfDestination, precursor);

		}

	else

		{

		// only add destination to the list of destination

		if(DEBUG > 3 ) printf("      > entry already exists: only add destination to the list of destination\n");

		putInFifo(listOfDestination, destinationPtr);

		}

	if(DEBUG > 3 ) printf("      - function aodv_reverseListOfPrecursors_update()... done !\n");

   }


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

/***************** HANDLE_DATA ***************************/

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



void 
aodv_data_pk_receive(Packet* data_pk_ptr)

{

// var

int        previousHop, source, dest; 

/*
/* This function receives the upcoming data packet from the MAC Layer.
/* Two possibilities:
/* 1. Packet has reached its destination. Packet is then discarded toward
/*    the upper_layer.
/* 2. Packet has to be forwarded. Node checks its routing table and
/*    either forwards the packet if an entry is available or generates
/*    a RERR otherwise.
/* 
*/

// Read data packet 
op_pk_nfd_get (data_pk_ptr,"PreviousHop",&previousHop);


op_pk_nfd_get (data_pk_ptr,"SRC",&source);

op_pk_nfd_get (data_pk_ptr,"DEST",&dest); 



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

// update stats
if(previousHop != source)
	{
	stats[previousHop].forward_output++;
	}

// check packet final destination
if (dest == node_addr)  

	{
	// pk has reached its destination
	if(DEBUG > 1 ) printf("      > Data packet has reached its destination\n");
	// send packet to upper layer 
	if(DEBUG > 1 ) printf("      > Data packet sent to app_manager layer\n");
	op_pk_send(data_pk_ptr,DISCARD_STRM); 

	// update stats
	output ++;
	stats[source].own_output++;
	op_stat_write(global_receive_pk_cnt_stathandle, ++global_receive_data_pk_cnt);
	}

else  

	{
	// pk has to be forwarded
	if(DEBUG > 1 ) printf("      > Node is a relay: packet has to be routed\n");
	// update forward input stat
	stats[node_addr].forward_input++;

	// check routing table for desired entry 
	if(DEBUG > 1) printf("      > Check routing table for entry\n");
	if(DEBUG > 1) aodv_entry_print(dest);
	if ((aodv_entry_nextHop_get(dest) != NOT_VALID) && (aodv_entry_nextHop_get(dest) != NON_EXISTENT))
		 {

		 // entry available: node forwards the packet 

		 if(DEBUG > 1 ) printf("      > Node has fresh enough active entry\n      > Data packet forwarded\n");
		 // schedule an event if no ack received within the waiting window 
		 if(DEBUG > 3) printf("      > Schedule Event if no ack received\n");

		 aodv_ack_timeout_schedule(data_pk_ptr,dest);

		 //send to mac

		 aodv_pk_send_to_mac_layer(data_pk_ptr,aodv_entry_nextHop_get(dest));

		 // re-active the route timeout (must delay the expiration event 

		 // which has been scheduled for the current entry)
		 aodv_entry_expirationTime_set(dest,op_sim_time()+ACTIVE_ROUTE_TIMEOUT); 
		 }

	 else if(aodv_entry_nextHop_get(dest) == NOT_VALID)
		 {

		 printf("      > Error @ node %d: No forward route available: no active entry for dest %d\n",node_addr,dest);
		 // insert data packet into buffer and try to repair the broken link
		 if(DEBUG > 1 ) printf("      > Insert packet into buffer\n");
		 aodv_data_pk_queue(data_pk_ptr);
		 // if no repair pending
		 if(RequestSent[dest].status != WAITING_FOR_REPAIR)
			 {
			 // cancel expiration time
			 aodv_entryPtr_expirationInterrupt_cancel(aodv_routingTable_entry_get(dest));
			 // attempt to repair
			 if(DEBUG > 1 ) printf("      > Attempt to repair link\n");
			 aodv_link_repair_attempt(dest, aodv_entry_hopCount_get(source));
			 }
		 }
	 else // entry does not exist
		 {
		 // generate a rreq
		 aodv_rreq_pk_generate(dest, WAITING_FOR_REPLY);
		 }

	 }

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

}






/***********************************************************/

/***************** INIT ROUTING TABLE ENTRY ****************/

/***********************************************************/


RoutingTableEntry* 
aodv_entry_create_new()

{


RoutingTableEntry* entryPtr;


/*
/* Returns a new routing table entry with the default
/* values.
*/

entryPtr= (RoutingTableEntry*) op_prg_mem_alloc(sizeof(RoutingTableEntry));

entryPtr->status             = ACTIVE;

entryPtr->dest               = -1;
entryPtr->nextHop            = -1;

entryPtr->nextTwoHop         = -1;
entryPtr->destSeqNb          =  0; 
entryPtr->hopCount           = -1;
entryPtr->lastHopCount       = -1;
entryPtr->expirationTime     =  0;
entryPtr->lastExpirationTime =  0;

return entryPtr;

}



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

/*********************** GENERATE ERROR ************************/

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

void 
aodv_rerr_pk_generate(int unreachableNode, int n_flag)

{

// vars

RoutingTableEntry *  indexEntryError;
ErrorContentStruct*  newErrorStructPtr;
 
sObject*             object;

Packet*              rerr_pk_ptr;
int                  source,
                     dest,
					 destSeqNb,
					 unreachableDest,
					 unreachableDestSeqNb,
					 i;

/*
/* Generate a RERR packet containing the list of all
/* destinations (and their associated Dest. Seq. Nb.)
/* that are now unreachable because of the loss of 
/* node given in the first parameter (unreachableNode). 
/* The N flag (n_flag) indicates whether entry should 
/* be deleted or not.
*/

//init vars
object=  routingTable->firstObject;
newErrorStructPtr= (ErrorContentStruct *) 

⌨️ 快捷键说明

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