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

📄 dymo_pkt_support.ex.c

📁 dymo是基于多径累计的ADHOC路由算法优化,并在OPNET11.0上实现.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* dymo_pkt_support.ex.c */
/* C file for DYMO Packet Support APIs */


/****************************************/
/*		Copyright (c) 1987-2003			*/
/*		by OPNET Technologies, Inc.		*/
/*		(A Delaware Corporation)		*/
/*	7255 Woodmont Av., Suite 250  		*/
/*     Bethesda, MD 20814, U.S.A.       */
/*			All Rights Reserved.		*/
/****************************************/

/***** Includes *****/
#include <opnet.h>
#include <dymo.h>
#include <dymo_pkt_support.h>
#include <dymo_ptypes.h>
#include <ip_addr_v4.h>

/***** Prototypes *****/
static DymoT_Packet_Option*			dymo_pkt_support_option_mem_copy (DymoT_Packet_Option* option_ptr);
static void							dymo_pkt_support_option_mem_free (DymoT_Packet_Option* option_ptr);
static List*						dymo_pkt_support_unreachable_nodes_copy (List* unreachable_nodes_lptr);
static DymoT_Packet_Option*			dymo_pkt_support_option_mem_alloc (void);
static DymoT_Route_Ele*				dymo_pkt_support_re_option_mem_alloc (void);
static DymoT_Hello*					dymo_pkt_support_hello_option_mem_alloc (void);
static DymoT_Rerr*					dymo_pkt_support_rerr_option_mem_alloc (void);
static void							dymo_pkt_support_re_option_mem_free (DymoT_Route_Ele* re_option_ptr);
static void							dymo_pkt_support_hello_option_mem_free (DymoT_Hello* re_option_ptr);
static void							dymo_pkt_support_rerr_option_mem_free (DymoT_Rerr* rerr_option_ptr);


Packet*
dymo_pkt_support_pkt_create (DymoT_Packet_Option* pkt_option_ptr, int option_size)
	{
	static int		fd_options_index = OPC_FIELD_INDEX_INVALID;
	Packet*			dymo_pkptr;
	
	/** Creates and sets the information	**/
	/** in the DYMO packet					**/
	FIN (dymo_pkt_support_pkt_create (<args>));
	
	/* Create the DYMO packet	*/
	dymo_pkptr = op_pk_create_fmt ("dymo");
	
	/* Get the field index for the options field	*/
	if (fd_options_index == OPC_FIELD_INDEX_INVALID)
		fd_options_index = op_pk_nfd_name_to_index (dymo_pkptr, "Options");
	
	/* Set the options in the packet	*/
	op_pk_fd_set (dymo_pkptr, fd_options_index, OPC_FIELD_TYPE_STRUCT, pkt_option_ptr, option_size,
		dymo_pkt_support_option_mem_copy, dymo_pkt_support_option_mem_free, sizeof (DymoT_Packet_Option));

	FRET (dymo_pkptr);
	}


DymoT_Packet_Option*
dymo_pkt_support_re_option_create (int ttl, int len, Boolean i_flag, int t_hop_count, InetT_Address target_addr, 
				int target_seq_num, InetT_Address re_node_addr, int re_node_seq_num, Boolean ack_flag, 
									Boolean gateway_flag, int prefix, int re_hop_cnt)
	{
    DymoT_Route_Ele*			re_option_ptr;
	DymoT_Packet_Option*		dymo_pkt_option_ptr;
	DymoT_Re_Block_Entry*		first_reblock_ptr;

	/** Creates the route element option	**/
	FIN (dymo_pkt_support_re_option_create (<args>));
	
	/* Allocate memory for the Route Element option **/
	re_option_ptr = dymo_pkt_support_re_option_mem_alloc ();
	
	/* Set the variables of the option	*/
	re_option_ptr->elem_ttl = ttl;
	re_option_ptr->elem_len = len;
	re_option_ptr->ignore_flag = i_flag;
	re_option_ptr->elem_target_addr = inet_address_copy (target_addr);
	re_option_ptr->elem_target_seq_num = target_seq_num;
	re_option_ptr->t_hop_cnt = t_hop_count;
	re_option_ptr->ack_flag = ack_flag;
	re_option_ptr->reblock_lptr = op_prg_list_create();

	/*allocate memory and set the first re block entry */
	first_reblock_ptr = dymo_pkt_support_re_block_entry_mem_alloc();
	first_reblock_ptr->gateway_flag = gateway_flag;
	first_reblock_ptr->prefix = prefix;
	first_reblock_ptr->re_hop_count = re_hop_cnt;
	first_reblock_ptr->re_node_addr = inet_address_copy (re_node_addr);
	first_reblock_ptr->re_node_seq_num = re_node_seq_num;
	op_prg_list_insert (re_option_ptr->reblock_lptr, first_reblock_ptr, OPC_LISTPOS_TAIL);

	/* Allocate memory to set into the DYMO packet option	*/
	dymo_pkt_option_ptr = dymo_pkt_support_option_mem_alloc ();
	dymo_pkt_option_ptr->elem_type = DYMOC_ROUTE_ELEMENT;
	
	dymo_pkt_option_ptr->value_ptr = (void*) re_option_ptr;
	
	FRET (dymo_pkt_option_ptr);
	}

DymoT_Packet_Option*
dymo_pkt_support_hello_option_create (Boolean repair, Boolean ack_required, int hop_count, 
			InetT_Address dest_addr, int dest_seq_num, InetT_Address src_addr, double lifetime, int elem_type)
	{
	DymoT_Hello*				hello_option_ptr;
	DymoT_Packet_Option*		dymo_pkt_option_ptr;
	
	/** Creates the hello option	**/
	FIN (dymo_pkt_support_hello_option_create (<args>));
	
	/* Allocate memory for the hello option	**/
	hello_option_ptr = dymo_pkt_support_hello_option_mem_alloc ();
	
	/* Set the variables of the option	*/
	hello_option_ptr->repair_flag = repair;
	hello_option_ptr->ack_required_flag = ack_required;
	hello_option_ptr->hop_count = hop_count;
	hello_option_ptr->dest_seq_num = dest_seq_num;
	hello_option_ptr->lifetime = lifetime;
	hello_option_ptr->dest_addr = inet_address_copy (dest_addr);
	hello_option_ptr->src_addr = inet_address_copy (src_addr);
	
	/* Allocate memory to set into the DYMO packet option	*/
	dymo_pkt_option_ptr = dymo_pkt_support_option_mem_alloc ();
	dymo_pkt_option_ptr->elem_type = elem_type;
	dymo_pkt_option_ptr->value_ptr = (void*) hello_option_ptr;
	
	FRET (dymo_pkt_option_ptr);
	}

DymoT_Packet_Option*
dymo_pkt_support_rerr_option_create (Boolean ignore_flag, int elem_ttl, int elem_len,
										List* unreachable_dest_lptr)
	{
	DymoT_Rerr*					rerr_option_ptr;
	DymoT_Packet_Option*		dymo_pkt_option_ptr;

	/** Creates the route error option	**/
	FIN (dymo_pkt_support_rerr_option_create (<args>));
	
	/* Allocate memory for the route error option	**/
	rerr_option_ptr = dymo_pkt_support_rerr_option_mem_alloc ();
	
	/* Set the variables of the option	*/
	rerr_option_ptr->ignore_flag = ignore_flag;
	rerr_option_ptr->elem_ttl = elem_ttl;
	rerr_option_ptr->elem_len = elem_len;
	rerr_option_ptr->unreachable_dest_lptr = unreachable_dest_lptr;

	/* Allocate memory to set into the DYMO packet option	*/
	dymo_pkt_option_ptr = dymo_pkt_support_option_mem_alloc ();
	dymo_pkt_option_ptr->elem_type = DYMOC_ROUTE_ERROR;
	dymo_pkt_option_ptr->value_ptr = (void*) rerr_option_ptr;
	
	FRET (dymo_pkt_option_ptr);
	}


DymoT_Unreachable_Node*
dymo_pkt_support_unreachable_nodes_mem_alloc (void)
	{
	static Pmohandle			unreachable_node_option_pmh;
	DymoT_Unreachable_Node*		unreachable_node_ptr = OPC_NIL;
	static Boolean				unreachable_node_option_pmh_defined = OPC_FALSE;
	
	/** Allocates pooled memory for the unreachable nodes	**/
	/** in the route error option.							**/
	FIN (dymo_pkt_support_unreachable_nodes_mem_alloc (void));
	
	if (unreachable_node_option_pmh_defined == OPC_FALSE)
		{
		/* Define the pool memory handle for the unreachable node */
		unreachable_node_option_pmh = op_prg_pmo_define ("DYMO Unreachable Node", sizeof (DymoT_Unreachable_Node), 32);
		unreachable_node_option_pmh_defined = OPC_TRUE;
		}
	
	/* Allocate the unreachable node from the pooled memory	*/
	unreachable_node_ptr = (DymoT_Unreachable_Node*) op_prg_pmo_alloc (unreachable_node_option_pmh);
	
	FRET (unreachable_node_ptr);
	}

void
dymo_pkt_support_re_block_entry_mem_free (DymoT_Route_Ele* re_option_ptr, int block_index)
	{
	DymoT_Re_Block_Entry*		remove_entry_ptr = OPC_NIL;
	
	/** Frees the memory allocated to the route element option	**/
	FIN (dymo_pkt_support_re_block_entry_mem_free (<args>));

	remove_entry_ptr = op_prg_list_remove(re_option_ptr->reblock_lptr, block_index);

	/*Destroy Ip address in the ReBlock*/
	inet_address_destroy (remove_entry_ptr->re_node_addr);

	/*Remove the entry in the ReBlock List*/
	op_prg_mem_free (remove_entry_ptr);
	
	FOUT;
	}

DymoT_Re_Block_Entry*
dymo_pkt_support_re_block_entry_mem_alloc (void)
	{
	static Pmohandle			re_block_entry_option_pmh;
	DymoT_Re_Block_Entry*		re_block_entry_ptr = OPC_NIL;
	static Boolean				re_block_entry_option_pmh_defined = OPC_FALSE;
	
	/** Allocates pooled memory for the re block entries	**/
	/** in the route element option.							**/
	FIN (dymo_pkt_support_re_block_entry_mem_alloc (void));
	
	if (re_block_entry_option_pmh_defined == OPC_FALSE)
		{
		/* Define the pool memory handle for the re blocks */
		re_block_entry_option_pmh = op_prg_pmo_define ("DYMO Route Elem Blocks", sizeof (DymoT_Re_Block_Entry), 32);
		re_block_entry_option_pmh_defined = OPC_TRUE;
		}
	
	/* Allocate the re block mem from the pooled memory	*/
	re_block_entry_ptr = (DymoT_Re_Block_Entry*) op_prg_pmo_alloc (re_block_entry_option_pmh);
	
	FRET (re_block_entry_ptr);
	}


/*** Internally callable functions	***/

static DymoT_Packet_Option*
dymo_pkt_support_option_mem_copy (DymoT_Packet_Option* option_ptr)
	{
	DymoT_Packet_Option*		copy_option_ptr;
	DymoT_Route_Ele*			re_option_ptr;
	DymoT_Route_Ele*			re_copy_option_ptr;
	DymoT_Hello*				hello_option_ptr;
	DymoT_Rerr*					rerr_option_ptr;
	List*						copy_unreachable_lptr;
	int							i;
	DymoT_Re_Block_Entry*		first_re_block_entry_ptr = OPC_NIL;
	DymoT_Re_Block_Entry*		old_re_block_entry_ptr = OPC_NIL;
	DymoT_Re_Block_Entry*		new_re_block_entry_ptr = OPC_NIL;

	
	/** Makes a copy of the options field	**/
	/** in the DYMO packet					**/
	FIN (dymo_pkt_support_option_mem_copy (<args>));
	
	/* Based on the element type of option, copy it	*/
	switch (option_ptr->elem_type)
		{
		case (DYMOC_ROUTE_ELEMENT):
			{
			/* This is a route element option	*/
			re_option_ptr = (DymoT_Route_Ele*) option_ptr->value_ptr;
	
			first_re_block_entry_ptr = (DymoT_Re_Block_Entry*) op_prg_list_access(re_option_ptr->reblock_lptr, 
																							OPC_LISTPOS_HEAD);

			copy_option_ptr = dymo_pkt_support_re_option_create (re_option_ptr->elem_ttl, re_option_ptr->elem_len, 
										re_option_ptr->ignore_flag, re_option_ptr->t_hop_cnt, 
										re_option_ptr->elem_target_addr, re_option_ptr->elem_target_seq_num, 
										first_re_block_entry_ptr->re_node_addr, first_re_block_entry_ptr->re_node_seq_num, 
										re_option_ptr->ack_flag, first_re_block_entry_ptr->gateway_flag, 
										first_re_block_entry_ptr->prefix, first_re_block_entry_ptr->re_hop_count);
			
			re_copy_option_ptr = (DymoT_Route_Ele*) copy_option_ptr->value_ptr;

			for(i = 1; i < op_prg_list_size(re_option_ptr->reblock_lptr); i++)
				{

				old_re_block_entry_ptr = (DymoT_Re_Block_Entry*) op_prg_list_access(re_option_ptr->reblock_lptr, i);
				new_re_block_entry_ptr = dymo_pkt_support_re_block_entry_mem_alloc();

				new_re_block_entry_ptr->re_node_addr = inet_address_copy(old_re_block_entry_ptr->re_node_addr);
				new_re_block_entry_ptr->re_node_seq_num = old_re_block_entry_ptr->re_node_seq_num;
				new_re_block_entry_ptr->re_hop_count = old_re_block_entry_ptr->re_hop_count;
				new_re_block_entry_ptr->prefix = old_re_block_entry_ptr->prefix;
				new_re_block_entry_ptr->gateway_flag = old_re_block_entry_ptr->gateway_flag;
				op_prg_list_insert(re_copy_option_ptr->reblock_lptr, new_re_block_entry_ptr, OPC_LISTPOS_TAIL);
				}

⌨️ 快捷键说明

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