op_pk_fd_index_to_name()
Abstract
Converts a numeric field index into the corresponding field name, based on the format of the specified packet.
Syntax
op_pk_fd_index_to_name (pkptr, fd_index, fd_name)
| Argument | Type | Description |
|---|---|---|
pkptr |
Packet* |
pointer to the packet of interest. [A pointer to a packet can be obtained from many KPs, including op_pk_create(), op_pk_create_fmt(), op_pk_copy(), op_pk_get(), op_subq_pk_access(), and op_subq_pk_remove().] |
fd_index |
int |
numeric index of the field-of-interest [This value should be non-negative; however, no error will occur if it is not.] |
fd_name |
char* |
name of the specified field (filled in by this KP) [This argument should be a statically declared character array, or a character pointer which points to dynamically allocated memory; it must point to a string large enough to hold the name of the specified field.] |
Return Type
void
No return value.
Example
/* pull the next packet off the queue */
pkptr = op_subq_pk_remove (0, OPC_QPOS_HEAD);
/* determine the number of fields the packet has */
num_fds = op_pk_fd_max_index (pkptr);
/* loop through each field, printing its field name */
for (j = 0; j < num_fds; j++)
{
/* obtain the field name */
op_pk_fd_index_to_name (pkptr, j, fd_name);
/* print the field name */
printf ("Field (%d): Name (%s)\n", j, fd_name);
}
Details
This KP is useful for determining the names of fields only in formatted packets. However, applying this KP to an unformatted packet will not cause an error. Instead, the field name string will be assigned the constructed value field_<field_no>. This type of value will also result if the specified field index is larger than the last defined field, or smaller than zero.
Purpose
Provides a mechanism for determining the name of a formatted packet field if it is not known a priori by a process. This can be useful for generic processes which must handle arriving packets of different format. A process can loop through the fields of a packet (the upper bound of the loop can be determined using the KP op_pk_fd_max_index()), obtain the name of each field via this KP, and compare the field names to one or more names it is searching for based on its processing goal.
Errors
- Program Abort: Segmentation violation. (due to an invalid packet pointer, malformed packet, or malformed fd_name argument)
- Program Abort: Packet pointer is NIL.
- Program Abort: Packet pointer references destroyed packet.
- Recoverable Error: Field index (
<fd_index>) is out-of-range for packet (<pk_id>).
Reference
- Use the KP op_pk_nfd_name_to_index() to convert a field name to an numeric field index
- Use the KP op_pk_fd_max_index() to determine the highest field index in a packet
- Use the KP op_pk_nfd_is_set() to determine if a field is set, based on its name
- Use the KP op_pk_nfd_type() to determine the type of a field, based on its name
- Use the KP op_pk_nfd_size() to determine the size of a field, based on its name