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

📄 aodv_routing_nexttwo_rreq.pr.c

📁 adhoc网络中AODV协议的仿真码, 并有改进的
💻 C
📖 第 1 页 / 共 5 页
字号:
		 				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

	// invalidate 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 done !\n");

}


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

/************************ handle link breakage  ******************/

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


void 
aodv_link_repair_attempt(int dest, int ttl_src)

{

// vars
RoutingTableEntry* entryPtr;
int                TTL_REQUEST;
//op_stat_write(global_repair_pk_cnt_stathandle, ++global_repair_pk_cnt);
/*
/* This function checks whether the lost link
/* is repairable or not. If so, it generates a 
/* RREQ with the appropriate TTL. Else, it 
/* generates a RERR for the lost destination.
*/

// init vars
entryPtr =   aodv_routingTable_entry_get(dest);

//begin
if(DEBUG > 1 ) printf("    - Function aodv_link_repair_attempt(destination %d)\n", dest);



if(entryPtr != OPC_NIL)
	{
	// reset request sent repository
	if(RequestSent[dest].status != WAITING_FOR_REPAIR)
		{
		if(RequestSent[dest].status != OFF)
			aodv_requestSent_repository_reset(dest);
       
		// check if destination is no farther than MAX_REPAIR_TTL
	/*	if(entryPtr->lastHopCount <(MAX_REPAIR_TTL+1))
			{
			if(DEBUG > 1 ) printf("    > Link is repairable: mark entry UNDER_REPAIR\n");

			// Set entry status to "UNDER_REPAIR"
			entryPtr->status = UNDER_REPAIR;
			// print entry
			if(DEBUG) aodv_entryPtr_print(entryPtr);
			// Update the RequestSent repository:
			// Compute the TTL value to use
			RequestSent[dest].ttl = max_int(MIN_REPAIR_TTL, ttl_src)+LOCAL_ADD_TTL; 
			// generate RREQ
			if(DEBUG > 1 ) printf("    > Generating RREQ\n");
			
			aodv_rreq_pk_generate(dest,WAITING_FOR_REPAIR);

			}
		else
			{
			// generate RERR packet for the lost destination
			if(DEBUG > 1 ) printf("    > Node is too far: generating RERR\n");
			aodv_rerr_pk_generate(dest,0);
		    }
	*/

		// check if destination is no farther than MAX_REPAIR_TTL
	     if(entryPtr->lastHopCount == 1)
		  {
		    if(DEBUG > 1 ) printf("    > Link is repairable: mark entry UNDER_REPAIR\n");

			// Set entry status to "UNDER_REPAIR"
			entryPtr->status = UNDER_REPAIR;
			// print entry
			if(DEBUG) aodv_entryPtr_print(entryPtr);
			// Update the RequestSent repository:
			// Compute the TTL value to use
			RequestSent[dest].ttl = max_int(MIN_REPAIR_TTL, ttl_src)+LOCAL_ADD_TTL; 
			// generate RREQ
			if(DEBUG > 1 ) printf("    > Generating RREQ\n");
			
			aodv_rreq_pk_generate(dest,WAITING_FOR_REPAIR);

		  }
		 else
		  {
			if(DEBUG > 1 ) printf("    > Link is repairable: mark entry UNDER_REPAIR\n");

			// Set entry status to "UNDER_REPAIR"
			entryPtr->status = UNDER_REPAIR;
			// print entry
			if(DEBUG) aodv_entryPtr_print(entryPtr);
			// Update the RequestSent repository:
			// Compute the TTL value to use
			RequestSent[dest].ttl = max_int(MIN_REPAIR_TTL, ttl_src)+LOCAL_ADD_TTL; 
			// generate RREQ
			if(DEBUG > 1 ) printf("    > Generating RREQ\n");
			
			aodv_rreq_pk_generate_for_repairNextTwoHop(dest,WAITING_FOR_REPAIR);

		   }
		
		}
	else
		if(DEBUG > 1 ) printf("    > Link already under repair: waiting for reply\n");

	}
}






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

/********* aodv_listOfPrecursors_node_remove  **************/

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

void 
aodv_listOfPrecursors_node_remove(int precursor)

	{

	// var

	int*                 destPtr;

	int                  dest,size,t;

	int*                 precursorPtr;

	RoutingTableEntry*   entryPtr;

	sFifo*               listOfDestination;

	/*
	/* When a node (precursor) is no longer available, 
	/* the current function is called to remove it from
	/* any precursor list within the routing table. To 
	/* do so, the function reads the entry corresponding
	/* to the node "precursor" within the 
	/* reversePrecursorList and gets the list of entries
	/* where "precursor" is a precursor. It then goes 
	/* through these entries and removes the lost node
	/* form their precursor lists.
	*/

	// begin

	if(DEBUG > 3 ) printf("      - Function aodv_listOfPrecursors_node_remove(): precursor = %d\n",  precursor);

	

	// check if the unreachableDestination has an entry in the reversePrecursorList

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

	if(listOfDestination != OPC_NIL)

		{

		size= getFifoSize(listOfDestination);
		if(DEBUG > 3 ) printf("      > Node is at least on one node\'s precursor list: total = %d\n",size);

		for(t=0;t<size;t++)

			{

			destPtr=getInFifo(listOfDestination);

			dest= (*destPtr); // PRECURSOR is present in DEST routing table entry

			if(DEBUG > 3 ) printf("         - on entry# %d... ", dest);

			op_prg_mem_free(destPtr);

			// the entry is read

			entryPtr= aodv_routingTable_entry_get( dest);


			if(entryPtr != OPC_NIL)
				{
				// the PRECURSOR entry is extracted from the list of precursors

				precursorPtr= (int*) getInFifoMultiplex(

							entryPtr->listOfPrecursors,precursor);

				op_prg_mem_free(precursorPtr);

				}
			else
				{
				if(DEBUG > 3 ) printf("         - Entry# %d does not exist", dest);
				}
			}

		}

	else
		if(DEBUG > 3 ) printf("      > Node was not found on any list of precursors !\n");
	if(DEBUG > 3 ) printf("\n      - Function aodv_listOfPrecursors_node_remove()... done !\n");

	}






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

/******************** update list of precursors ****************/

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


void 
aodv_listOfPrecursors_node_put(RoutingTableEntry* forwardEntryPtr,int previousHop)
	{
	int      *int_ptr,
	         *previousHopPtr;
	
	/*
	/* Add the node "previousHop" to the precursor list of 
	/* the given entry. 
	/* Note that node is put in subqueue whose index equals
	/* to its MAC address (previousHop). This makes it easier
	/* to remove the node if it lost.
	*/
	
	// if listOfPrecursors not initialized, then initialize it
	if(forwardEntryPtr->listOfPrecursors == OPC_NIL)
		{	
		forwardEntryPtr->listOfPrecursors = newFifo();
		}
	
	//check if node is already in
	int_ptr= (int*) readInFifoMultiplex(forwardEntryPtr->listOfPrecursors, 	previousHop); 
	if(int_ptr != OPC_NIL)

		{
		if(DEBUG > 3 ) printf("      > Precursor is already on the list !\n");
		}

	 else
		{
		if(DEBUG > 3 ) printf("      > Precursor does not exist, function has to add it!\n");
		// create a pointer to the node's address value
		previousHopPtr = (int*) op_prg_mem_alloc(sizeof(int));
		*previousHopPtr
= previousHop;
		// add node to the precursol list
		if(DEBUG > 3 ) printf("      > Adding precursor %d\n",previousHop);
		putInFifoMultiplex(forwardEntryPtr->listOfPrecursors,
previousHopPtr, previousHop);

		if(DEBUG > 3 ) printf("      > Previous Hop (%d) should have been added to precursor list of entry# %d\n", previousHop, forwardEntryPtr->dest);
		// update reverse list of precursors
		aodv_reverseListOfPrecursors_update(previousHop,forwardEntryPtr->dest);

		}
	}
	


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

/*********************** HANDLE HELLO MESSAGE ******************/

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

void 
aodv_hello_msg_receive(Packet* rrep_pk_ptr)

{


// var



int                 source, dest, hopCount, lastHopCount;

int                 destSeqNb, previousHop;

double              lifetime;
RoutingTableEntry * newEntryPtr, * forwardEntryPtr;
Boolean             request_was_pending = 
OPC_FALSE;


/*
/* This routine processes the upcoming hello message.
*/
// begin



//op_stat_write(global_nerror_pk_cnt_stathandle, ++global_nerror_pk_cnt);

// packet read

op_pk_nfd_get (rrep_pk_ptr,"PreviousHop",&previousHop);

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

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

op_pk_nfd_get (rrep_pk_ptr,"DestSeqNb",&destSeqNb);

// N.B. hop count field is incremented by 1
at reception
op_pk_nfd_get (rrep_pk_ptr,"HopCount",&hopCount);

op_pk_nfd_set (rrep_pk_ptr,"HopCount",hopCount+1);

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

op_pk_nfd_get (rrep_pk_ptr,"Lifetime",&lifetime);


if(DEBUG > 3) printf("    - Function aodv_hello_msg_receive(from node %d)\n",source);

// check if entry already exists
forwardEntryPtr=aodv_routingTable_entry_get( dest);
// if entry does not exist: create it and add it to table (with default values)
if(forwardEntryPtr == OPC_NIL)
	{
	// entry does not exist
	if(DEBUG > 1 ) printf("      > Entry does not exist\n");
	forwardEntryPtr=aodv_entry_create_new();

	forwardEntryPtr->listOfPrecursors=newFifo();
	forwardEntryPtr->dest = source;
	// add entry to routing table
	aodv_routingTable_entryPtr_put(forwardEntryPtr);
	}



if(aodv_entryPtr_hopCount_get(forwardEntryPtr) == INFINITY) 
	{
	// entry is either invalid or under repair or newly created
	if(DEBUG > 1 ) printf("      > Node creates entry\n");
	// Set entry values
	forwardEntryPtr->dest=dest;

	forwardEntryPtr->destSeqNb=destSeqNb;
	forwardEntryPtr->hopCount=hopCount;

	forwardEntryPtr->nextHop=previousHop;

	// check whether a request is pending
	if(RequestSent[dest].status != OFF)
		{
		if(DEBUG > 1 ) printf("      > A request was running for the newly created entry\n");
		// update requestSent repository
		aodv_requestSent_r

⌨️ 快捷键说明

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