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

📄 aodv_routing_nexttwo.pr.c

📁 adhoc网络中AODV协议的仿真码, 并有改进的
💻 C
📖 第 1 页 / 共 5 页
字号:
	if(DEBUG > 1) printf("      > Check routing table for entry\n");
	if(DEBUG > 1) aodv_entry_print(dest);
	
   if(twoHopRepairFlag == 0)
   {   
	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");
		 //if((aodv_entry_nextTwoHop_get(dest)!= NOT_VALID)&&(aodv_entry_nextTwoHop_get(dest)!= NON_EXISTENT))
		 { 
		    twoHopRepairFlag = 0;
			op_pk_nfd_set (data_pk_ptr,"NextTwoHop",aodv_entry_nextTwoHop_get(dest));
			op_pk_nfd_set (data_pk_ptr,"TwoHopRepairFlag",twoHopRepairFlag);
		 }
		 
		 // 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), twoHopRepairFlag);
			if(twoHopRepairFlag == 1)
				 {
				    if((aodv_entry_nextTwoHop_get(dest)!= NOT_VALID)&&(aodv_entry_nextTwoHop_get(dest)!= NON_EXISTENT))
				      { 
					    op_pk_nfd_set (data_pk_ptr,"NextTwoHop",aodv_entry_nextTwoHop_get(dest));
					    op_pk_nfd_set (data_pk_ptr,"TwoHopRepairFlag",twoHopRepairFlag);
						//aodv_pk_send_to_mac_layer(data_pk_ptr,aodv_entry_nextHop_get(dest));
						aodv_buffer_serve(dest);
					  }
					else aodv_rerr_pk_generate(dest,0);
				 }
			 }//end if(RequestSent...

		 }
	 else // entry does not exist
		 {
		 // generate a rreq
		 aodv_rreq_pk_generate(dest, WAITING_FOR_REPLY);
		 }



   }
   else//else if(twoHopRepairFlag == 0)
   {
     if(nextHop_PnodeInfor != TWOHOP_REPAIR)
		{	   

         nextTwoHop = TWOHOP_REPAIR;
		 op_pk_nfd_set (data_pk_ptr,"NextTwoHop",nextTwoHop);
		 op_pk_nfd_set (data_pk_ptr,"TwoHopRepairFlag",twoHopRepairFlag);

		 if(aodv_entry_hopCount_get(nextHop_PnodeInfor)== 1)
		 {
			 // 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,nextHop_PnodeInfor);

		 	// 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); 
		 
		   //repair routing table
		   forwardEntryPtr = aodv_routingTable_entry_get(source);
		   if(forwardEntryPtr != OPC_NIL)
		   {
			  forwardEntryPtr->nextHop = previousHop;      
		   }
		   else
		   {
		      aodv_rreq_pk_generate_to_nextHop(source, WAITING_FOR_REPAIR, previousHop);
			  //forwardEntryPtr = aodv_entry_create_new();
			  //forwardEntryPtr->nextHop = previousHop;			 
		   }
			   
		   entryPtr=aodv_routingTable_entry_get(dest);
		   if(entryPtr != OPC_NIL)
		   {
			  entryPtr->nextHop = nextHop_PnodeInfor;      
		   }
		   else
		   {
		      aodv_rreq_pk_generate_to_nextHop(dest, WAITING_FOR_REPAIR, nextHop_PnodeInfor);
			  //entryPtr = aodv_entry_create_new();
			  //entryPtr->nextHop = nextHop_PnodeInfor; 
		   }
		   //repair end
		 }
		 else op_pk_destroy(data_pk_ptr);

		}
	 else// else if(nextHop_PnodeInfor...
		{
		   

		   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");
			   //if((aodv_entry_nextTwoHop_get(dest) != NOT_VALID)&&(aodv_entry_nextTwoHop_get(dest) != NON_EXISTENT))
				{ 
				  twoHopRepairFlag = 0;
				  op_pk_nfd_set (data_pk_ptr,"NextTwoHop",aodv_entry_nextTwoHop_get(dest));
				  op_pk_nfd_set (data_pk_ptr,"TwoHopRepairFlag",twoHopRepairFlag);
				}
		 
			// 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); 
			
		    //repair routing table		
		    forwardEntryPtr=aodv_routingTable_entry_get(source);
		    if(forwardEntryPtr!= OPC_NIL)
		    {
			  forwardEntryPtr->nextHop = previousHop;      
		    }
		    else
		    {
		      aodv_rreq_pk_generate_to_nextHop(source, WAITING_FOR_REPAIR, previousHop);
			  //forwardEntryPtr = aodv_entry_create_new();
			  //forwardEntryPtr->nextHop = previousHop;			 
		    }
		    //repair end
		   }

		   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), twoHopRepairFlag);
				   if(twoHopRepairFlag == 1)
					   {
					   if((aodv_entry_nextTwoHop_get(dest)!= NOT_VALID)&&(aodv_entry_nextTwoHop_get(dest)!= NON_EXISTENT))
						   { 
						   op_pk_nfd_set (data_pk_ptr,"NextTwoHop",aodv_entry_nextTwoHop_get(dest));
						   op_pk_nfd_set (data_pk_ptr,"TwoHopRepairFlag",twoHopRepairFlag);
						   //aodv_pk_send_to_mac_layer(data_pk_ptr,aodv_entry_nextHop_get(dest));
						   aodv_buffer_serve(dest);
						   }
					   else aodv_rerr_pk_generate(dest,0);
					   }
				   }// end if(RequestSent...
			   

			   }//end else if(aodv_entry...
		   else // entry does not exist
			   {
			   // generate a rreq
	 		   aodv_rreq_pk_generate(dest, WAITING_FOR_REPLY);
			   }

		   
		   }//end if(nextHop_PnodeInfor
	 }//end if(twoHopRepairFlag == 0)
	 
   }//end (if (dest == node_addr) )
    if(DEBUG > 1 ) printf("    - Function aodv_data_pk_receive() done\n ");  

}// end aodv_data_pk_receive(...  







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

/***************** 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));
printf("      >RoutingTableEntry op_prg_mem_alloc! \n");
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 *) 

		 				op_prg_mem_alloc(sizeof(ErrorContentStruct));

newErrorStructPtr->listOfUnreachableDest=newFifo();


// Begin
//if(DEBUG > 1) 
	printf("    - Function aodv_rerr_pk_generate(destination %d)\n", unreachableNode);

// Read corresponding entry
indexEntryError = aodv_routingTable_entry_get(unreachableNode);
// check the non_delete flag
if(n_flag == 0)
	{
	// If not set, invalidate entry and go through all listOfPrecursors and remove the unreachable  
    // destination from them: this is done via reverseListOfPrecursors

	// invalidat e entry (dest seq nb was previously incremented while attempting to repair)
	// this function will also schedule deletion for the current entry
	//if(DEBUG > 1) 
	printf("    - Invalidate entry\n");
	aodv_entry_invalidate(indexEntryError->dest, indexEntryError->destSeqNb, n_flag);
	//if(DEBUG > 1 )
	printf("      > Remove unreachable node from all the precursor lists\n");

	aodv_listOfPrecursors_node_remove(indexEntryError->dest);
	}
// add the new unreachable destination to the RERR packet

//if(DEBUG > 1 )
printf("      > Add destination to RERR packet\n");

// add destination to the list of new unreachable destinations 
aodv_listOfUnreachableDest_insert(newErrorStructPtr, indexEntryError->dest, indexEntryError->destSeqNb);

// If non_delete flag is not set, process the rest of routingTable entries:
// go through all entries, and process only those whos nextHop is the unreachable node
//if(DEBUG > 1)
printf("    - Look for other lost destinations\n");
if(n_flag == 0)
	{
	for(i=0;i<getFifoSize(routingTable);i++)

		{
		// read entry

		indexEntryError = (RoutingTableEntry*)(object->data);
		// Proceed only if the unreachable node is the next hop for this destination
		if(indexEntryError->nextHop == unreachableNode && indexEntryError->dest != unreachableNode) 
			{
			// read entry 
			unreachableDest = indexEntryError->dest;
			unreachableDestSeqNb = indexEntryError->destSeqNb;
			
			// invalidate entry
			//if(DEBUG > 1) 
			printf("    >  Ivalidate entry %d\n",unreachableDest);
			// invalidate entry and schedule deletion
			aodv_entry_invalidate(unreachableDest, unreachableDestSeqNb+1, n_flag);
			// go through all listOfPrecursors and remove the unreachable 

			// destination from them: this is done via reverseListOfPrecursors

			//if(DEBUG > 1 ) 
			printf("      > Remove unreachable node from all the precursor lists\n");

			aodv_listOfPrecursors_node_remove(unreachableDest);
			// add the new unreachable destination to the RERR packet

			//if(DEBUG > 1 ) 
			printf("      > Add destination to RERR packet\n");

			// add destination to the list of new unreachable destinations 
			aodv_listOfUnreachableDest_insert(newErrorStructPtr, unreachableDest, unreachableDestSeqNb);
			// print entry
			if(DEBUG) aodv_entryPtr_print(indexEntryError);
			}
		else
			//if(DEBUG > 1) 
			printf("      > skip entry %d\n",indexEntryError->dest);
		
		object=object->next;
		}

	
}

// if list of unreachable destinations is not empty, then 
// generate a RERR packet.
if(getFifoSize(newErrorStructPtr->listOfUnreachableDest)>0)


	{
	// Create RERR packet
	rerr_pk_ptr = op_pk_create_fmt("AODV_RERR");
	// Set N field value
	op_pk_nfd_set(rerr_pk_ptr,"N", n_flag);

	// Set the error structure and add it to packet
	newErrorStructPtr->destCount = getFifoSize(newErrorStructPtr->listOfUnreachableDest);
	op_pk_nfd_set(rerr_pk_ptr,"ErrorContent",newErrorStructPtr,op_prg_mem_copy_create,op_prg_mem_free,sizeof(ErrorContentStruct));

	//if(DEBUG > 1) 
	printf("      > RERR packet has been generated\n");

	// Send to mac Layer 
	aodv_pk_send_to_mac_layer(rerr_pk_ptr, BROADCAST);
	}
else
	{
	//if(DEBUG > 1) 
	printf("      > No unreachable destination caused by link breakage\n");

	}
	

//if(DEBUG > 1)
printf("    - Function aodv_rerr_pk_generate

⌨️ 快捷键说明

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