How can I pass a string element (a char*) in a packet field? Is there example code?

Categories:
Solution Number:
S20608
Last Modified:
2013-08-20
Issue

How can I pass a string element (a char*) in a packet field? Is there example code?

Solution

The following is an example illustrating how to store a string in a packet field element. The general code for setting as well as getting the string element from the packet are described below.Include a construct similar to the following code in the process model where the string value needs to be set in the packet: Header block: ------------- #define MAX_STR_SIZE 128 Temp Var block: --------------- Packet* test_pkptr; char* str_name; In a process state edit pad: ---------------------------- /* Create a packet format. Note that this packet format should be */ /* created in the Parameter editor, and must have a field of type */ /* structure with the field name as test_string. */ test_pkptr = op_pk_create_fmt (test_pk_fmt); /* Allocate space for the test data string. */ str_name = (char *) op_prg_mem_alloc (MAX_STR_SIZE); /* Load the string with the required string value. */ strcpy (str_name, TEST_DATA_STRING); /* Set the relevant field for string data in the packet. */ op_pk_nfd_set (test_pkptr, test_string, str_name, op_prg_mem_copy_create, op_prg_mem_free, MAX_STR_SIZE);In the process model where you are trying to get the value from the packet field, the following code can be followed: Header block: ------------- #define MAX_STR_SIZE 128 Temp Var block: --------------- Packet* test_pkptr; char* get_string; char msg_string1[128]; char msg_string2[128]; In a process state edit pad: ---------------------------- /* Acquire the packet from the incoming stream. */ test_pkptr = op_pk_get (op_intrpt_strm()); /* Obtain the relevant structure element from the pkptr. */ /* Note that op_pk_nfd_get() strips the structure field from */ /* the packet, so we must free the string below. */ /* Alternatively, we could have used the op_pk_nfd_access () */ /* function so that we would not need to explicitly free the string.*/ /* See note below. */ op_pk_nfd_get (test_pkptr, test_string, &get_string); /* Print the contents of the string array. */ sprintf(msg_string1, The content of the retrieved character string:); sprintf(msg_string2, %s, get_string); op_sim_message (msg_string1, msg_string2); /* Deallocate the memory allocated to the structure element. */ op_prg_mem_free (get_string);Note that for OPNET products version 5.1.D and later, a new kernel procedure, op_pk_<n>fd_access () has been added. When the structure was being retrieved above, in place of the op_pk_nfd_get() function call, the op_pk_nfd_access() function call could have been used. op_pk_nfd_access() does not remove the structure fields when accessing the values (unlike op_pk_nfd_get ()). In this example, op_pk_nfd_access() would get a pointer to the structure object while the packet would still retain a pointer to the same structure object. The packet would still own the structure object. This implies: a) we should not explicitly free the structure object. b) when the packet is destroyed, it will free the structure object.As an aside, note that if multiple processes use op_pk_nfd_access() to access the structure object from the same packet, each process would be working on the same structure object (potentially affecting each other unintentionally). For more information, you can search in the online FAQ for structure. You can also read the OPNET Simulation Kernel Manual, Chapter Pk Package for more information on the op_pk_nfd_set() KP.

Environment

DES Kernel->Process Modeling/Coding

Attachments
NOTICE: Riverbed® product names have changed. Please refer to the Product List for a complete list of product names.
Can't find an answer? Create a case