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

📄 aodv_routing.pr.c

📁 opnet ad hoc adov routing
💻 C
📖 第 1 页 / 共 5 页
字号:
		my_seq_nb++;		op_pk_nfd_set(rreq_pk_ptr, "HopCount"   , 0);		op_pk_nfd_set(rreq_pk_ptr, "TTL"        , rreq_ttl);		op_pk_nfd_set(rreq_pk_ptr, "G"          , 1);		// Set size		op_pk_total_size_set(rreq_pk_ptr, 192);				// Record the request in the req_sent_rep		req_sent_rep[dest].pending         = 1;		req_sent_rep[dest].attempts_count++;		req_sent_rep[dest].retries_count   = 0;		req_sent_rep[dest].rreq_pk_copy    = op_pk_copy(rreq_pk_ptr);		req_sent_rep[dest].evh             = op_intrpt_schedule_self(op_sim_time()+2*rreq_ttl*NODE_TRAVERSAL_TIME, dest+N);		req_sent_rep[dest].emission_time   = op_sim_time();		discovery_attempt_stat++;				// Send packet to mac layer		aodv_pk_send(rreq_pk_ptr, BROADCAST);		}	}/*****************************************/* This routine is called whenever a node /* needs to generate a RERR packet. This could/* happened in two cases/* (i)  the link with the node upstream has/* broken and no repair was attempted./* (ii) the node has received a packet for /* which it does not have a route./* RERR packet generation is different /* according the case./* case (iii) fail to repair a specific /* destination. the difference between this/* case and case (i) is that node does not/* to be invalidated as it already has been*****************************************/void aodv_rerr_pk_generate(int dest_ip_addr, int err_case)	{	Aodv_Route_Table_Entry * entry;	Aodv_Lost_Node         * lost_node;	Aodv_List              * lost_node_list;	Packet                 * rerr_pk_ptr;	int                      generate_rerr = 0;	char                     msg[64];	int                      i;	// init	lost_node_list = aodv_list_create();	// Step one:	// In both cases (i && ii), the original destination	// (for which the routine is called) is invalidated and	// added to the list of lost nodes	// Trace	sprintf(msg, "Generating RERR: Unreachable destination is %d", dest_ip_addr);	aodv_print_minor(msg, TRACE_LEV_1);	// trace	aodv_print_minor("Processing main destination:", TRACE_LEV_1);	aodv_prg_print_entry(dest_ip_addr,TRACE_LEV_1);	// If entry exists, then invalidate it (this includes 	// the following actions:	// 1. Copying hop count value into last hop count field	// 2. Setting hop count value to infinity	// 3. Incrementing seq nb by one.)	if(my_route_table[dest_ip_addr].entry_exist)		{		if(err_case != case_iii)			{			aodv_print_minor(">>> Entry has been invalidated", TRACE_LEV_1);			aodv_entry_invalidate (&my_route_table[dest_ip_addr]);			}		else			{			aodv_print_minor(">>> Entry already invalid", TRACE_LEV_1);			}		// Schedule deletion		aodv_entry_reschedule_intrpt(&my_route_table[dest_ip_addr], op_sim_time()+DELETE_PERIOD);		}	else		{		aodv_print_minor(">>> Entry does not exist", TRACE_LEV_1);		}	// remove node from precursor lists	aodv_print_minor(">>> Node has been removed from precursor lists", TRACE_LEV_1);	aodv_prg_remove_from_prec_lists(dest_ip_addr);	// Drop data buffer	aodv_print_minor(">>> Data buffer has been dropped", TRACE_LEV_1);	pk_destroyed_gen_rerr+=aodv_buffer_size(dest_ip_addr);	aodv_buffer_drop(dest_ip_addr);	// Init a Lost Node Struct, which will be included into the RERR packet	lost_node = (Aodv_Lost_Node *) op_prg_mem_alloc(sizeof(Aodv_Lost_Node));	lost_node->dest_ip_addr = dest_ip_addr;	// Check the precursor list	// If it is not empty, then flush it and re-insert destination	// in the list of unreachable destinations		if(my_route_table[dest_ip_addr].entry_exist && aodv_list_size(my_route_table[dest_ip_addr].list_prec) > 0)		{		aodv_print_minor(">>> Precursor list not empty", TRACE_LEV_1);		// set the generate_rerr flag		generate_rerr = 1;		// Flush precursor list		aodv_entry_flush_prec_list(&my_route_table[dest_ip_addr]);		// trace		aodv_print_minor(">>> Precursor list has been flushed", TRACE_LEV_1);		// Insert into list of unreachable destination		// Set seq nb		lost_node->dest_seq_nb  = my_route_table[dest_ip_addr].dest_seq_nb;		}	else		{		// trace		aodv_print_minor(">>> Precursor list is empty or entry not found: Generate RERR anyway", TRACE_LEV_1);		// set seq nb to 0		lost_node->dest_seq_nb  = 0;		}	// Insert into list of unreachable destination	// trace	aodv_print_minor(">>> Main destination added to the list of unreachable dest", TRACE_LEV_1);					aodv_list_insert(lost_node_list, lost_node, lost_node->dest_ip_addr);		// Step two:	// If case i, then list all the destinations that	// are now unavailable because of the link	// breakge	if(err_case == case_i)		{		// go through all the entries of the routing		// tables (except the original destination), and		// check whether the lost node is the next hop		// toward this destination or not. If it is the case,		// then invalidate the entry, add it to RERR packet, 		// etc.		// trace		aodv_print_minor("Listing the rest of unreachable destinations:", TRACE_LEV_1);        // For each entry        for(i =0; i< N; i++)          {		  // Read entry		  // Proceed only if entry->next_hop is the original unreachable dest		  if(my_route_table[i].entry_exist 			  && 			  my_route_table[i].next_hop == dest_ip_addr 			  &&			  my_route_table[i].dest_ip_addr != dest_ip_addr)			  {  			  // Trace			  sprintf(msg, ">>> Processing entry %d", i);			  aodv_print_minor(msg, TRACE_LEV_1);			  aodv_prg_print_entry(i,TRACE_LEV_1);			  // Invalidate entry (this includes the following actions:			  // 1. Copying hop count value into last hop count field			  // 2. Setting hop count value to infinity			  // 3. Incrementing seq nb by one.)			  // trace			  aodv_print_minor(">>>>>> Entry has been invalidated", TRACE_LEV_1);			  aodv_entry_invalidate (&my_route_table[i]);			  // Schedule deletion			  aodv_entry_reschedule_intrpt(&my_route_table[i], op_sim_time()+DELETE_PERIOD);			  // trace			  aodv_print_minor(">>>>>> Node has been removed from precursor lists", TRACE_LEV_1);			  // remove node from precursor lists			  aodv_prg_remove_from_prec_lists(my_route_table[i].dest_ip_addr);			  // Drop data buffer			  // trace			  aodv_print_minor(">>>>>> Data buffer has been dropped", TRACE_LEV_1);			  pk_destroyed_gen_rerr+=aodv_buffer_size(i);			  aodv_buffer_drop(my_route_table[i].dest_ip_addr);			  // Check the precursor list			  // If it is not empty, then flush it and re-insert destination			  // in the list of unreachable destinations				  if(aodv_list_size(my_route_table[i].list_prec) > 0)				  {				  // set the generate_rerr flag				  generate_rerr = 1;				  // Flush precursor list				  aodv_entry_flush_prec_list(&my_route_table[i]);				  // trace				  aodv_print_minor(">>>>>> Precursor list has been flushed", TRACE_LEV_1);				  // Insert into list of unreachable destination				  // trace				  aodv_print_minor(">>>>>> Destination  added to the list of unreachable dest", TRACE_LEV_1);				  // Insert lost node in the list				  lost_node = (Aodv_Lost_Node *) op_prg_mem_alloc(sizeof(Aodv_Lost_Node));				  lost_node->dest_ip_addr = my_route_table[i].dest_ip_addr;				  lost_node->dest_seq_nb  = my_route_table[i].dest_seq_nb;				  aodv_list_insert(lost_node_list, lost_node, lost_node->dest_ip_addr);				  }			  else				  {				  // trace				  aodv_print_minor(">>>>>> Precursor list is empty: skip to the next destination", TRACE_LEV_1);				  }			  }		  else if(my_route_table[i].entry_exist)			  {			  // Trace			  sprintf(msg, ">>> Skipping entry %d", my_route_table[i].dest_ip_addr);			  aodv_print_minor(msg, TRACE_LEV_1);			  }          }	}	// Trace	sprintf(msg, "All entries checked: Generating RERR");	aodv_print_minor(msg, TRACE_LEV_1);	// if generate_rerr flag is set, then do so	if(generate_rerr)		{		// Create RERR pk		rerr_pk_ptr = op_pk_create_fmt("AODV_RERR");		// Set N field value		op_pk_nfd_set(rerr_pk_ptr,"N", 0);		// Set DestCount field		op_pk_nfd_set(rerr_pk_ptr,"DestCount", aodv_list_size(lost_node_list));		// Set RERR payload		op_pk_nfd_set(rerr_pk_ptr,"Payload",lost_node_list,op_prg_mem_copy_create,op_prg_mem_free,sizeof(Aodv_List));		// Set size		op_pk_total_size_set(rerr_pk_ptr, 32*(1+2*aodv_list_size(lost_node_list)));		// Trace		aodv_print_minor("RERR packet has been generated", TRACE_LEV_1);
		// Send to mac Layer 		aodv_pk_send(rerr_pk_ptr, BROADCAST);		}	else		aodv_print_minor("No RERR packet has been generated: list is empty", TRACE_LEV_1);	}	/*****************************************/* This routine is called to cancel a/* pending request for a given destination./* Generally, this means that a reply was/* received.*****************************************/void aodv_cancel_pending_request( int dest)	{	if(req_sent_rep[dest].pending)		{		req_sent_rep[dest].pending         = 0;		req_sent_rep[dest].attempts_count  = 0;		req_sent_rep[dest].retries_count   = 0;		op_pk_destroy(req_sent_rep[dest].rreq_pk_copy);		op_ev_cancel(req_sent_rep[dest].evh);		// Update stats		total_discovery_time_stat+= (op_sim_time() - req_sent_rep[dest].emission_time);		op_stat_write(avg_discovery_time_handle, (double) total_discovery_time_stat/ (double) discovery_attempt_stat);		// Update csv file		//aodv_stat_write_csvf();				}	else		{		// trace		aodv_print_minor("warning @ cancel pending req: no req found",TRACE_LEV_1);		}	}/**************************************************/* This routine receives regular RERR packets ( N/* flag set to 0). When such a packet is received, node/* effectively processes it, only if the previous hop/* is the next hop toward the unreacheable destination/* (this is done for each destination included in the/* list of unreachable destinations of the RERR packet)./* If so, invalidates the entry with the new sequence/* number and checks the precursor list of the entry./* If it is empty, destination is removed from the list/* of unreachable destinations. Else, it remains there./* If at least one destination is still in the list of/* unreachable destinations, then the RERR is rebroadcast/* In the other case, RERR is destroyed.**************************************************/void aodv_rerr_pk_process(Packet* rerr_pk_ptr)	{	Aodv_Lost_Node           *lost_node;	Aodv_List                *rerr_lost_node_list;	Aodv_List_Element        *current_element;      	Aodv_Lost_Node           *new_lost_node;	Aodv_List                *new_rerr_lost_node_list;	Packet                   *new_rerr_pk_ptr;	Aodv_Route_Table_Entry   *entry;	int                      rerr_previous_hop;	int                      rerr_dest_count;	int                      i;	char                     msg[64];		// var init	new_rerr_lost_node_list = aodv_list_create();	// Trace	aodv_print_minor("N flag is not set", TRACE_LEV_1);	// Read packet's fields    op_pk_nfd_get   (rerr_pk_ptr, "PreviousHop" , &rerr_previous_hop);	op_pk_nfd_get   (rerr_pk_ptr, "DestCount"   , &rerr_dest_count);	op_pk_nfd_access(rerr_pk_ptr, "Payload"     , &rerr_lost_node_list);			// Init first element	current_element = aodv_list_access_first_element(rerr_lost_node_list);	// for each destination included in the packet	while(current_element != OPC_NIL)		{		// Read first lost node structure		lost_node = (Aodv_Lost_Node*) current_element->data;				// Proceed, only if active entry exist, and if the next hop field is the previous hop from which		// the RERR was received		if(my_route_table[lost_node->dest_ip_addr].entry_exist 			&& 			my_route_table[lost_node->dest_ip_addr].breakage_flag != 1 			&& 			my_route_table[lost_node->dest_ip_addr].next_hop == rerr_previous_hop			&&			my_route_table[lost_node->dest_ip_addr].dest_seq_nb < lost_node->dest_seq_nb)			{			// Trace			sprintf(msg, "Processing destination %d", lost_node->dest_ip_addr);			aodv_print_minor(msg, TRACE_LEV_1);			// Invalidate entry (this includes the following actions:			// 1. Copying hop count value into last hop count field			// 2. Setting hop count value to infinity			// 3. Incrementing seq nb by one.)			// trace			aodv_print_minor(">>> Entry has been invalidated", TRACE_LEV_1);			aodv_entry_invalidate (&my_route_table[lost_node->dest_ip_addr]);			// Set seq nb			my_route_table[lost_node->dest_ip_addr].dest_seq_nb = lost_node->dest_seq_nb;			// Schedule deletion			aodv_entry_reschedule_intrpt(&my_route_table[lost_node->dest_ip_addr], op_sim_time()+DELETE_PERIOD);			if(DEBUG) aodv_entry_print(&my_route_table[lost_node->dest_ip_addr], my_node_addr);			// trace			aodv_print_minor(">>> Node has been removed from precursor lists", TRACE_LEV_1);			// remove node from precursor lists			aodv_prg_remove_from_prec_lists(lost_node->dest_ip_addr);			// Drop data buffer			// trace			aodv_print_minor(">>> Data buffer has been dropped", TRACE_LEV_1);			pk_destroyed_rcv_rerr+=aodv_buffer_size(lost_node->dest_ip_addr);			aodv_buffer_drop(lost_node->dest_ip_addr);			// Check the precursor list			// If it is not empty, then flush it and re-insert destination			// in the list of unreachable destinations				if(aodv_list_size(my_route_table[lost_node->dest_ip_addr].list_prec) > 0)				{				// Flush precursor list				aodv_entry_flush_prec_list(&my_route_table[lost_node->dest_ip_addr]);				// trace				aodv_print_minor(">>> Precursor list has been flushed", TRACE_LEV_1);				// Insert into list of unreachable destination				// trace				aodv_print_minor(">>> Destination added to the new list of unreachable dest", TRACE_LEV_1);				new_lost_node = (Aodv_Lost_Node*) op_prg_mem_alloc(sizeof(Aodv_Lost_Node));				new_lost_node->dest_ip_addr = lost_node->dest_ip_addr;				new_lost_node->dest_seq_nb  = lost_node->dest_seq_nb;				aodv_list_insert(new_rerr_lost_node_list, new_lost_node, new_lost_node->dest_ip_addr);				}			else				{				// trace

⌨️ 快捷键说明

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