This structure has the ability to contain large ranges of values, and is especially suited to the hierarchical organization of IP addresses and other configuration values.
The SDK supplies APIs for working with patricia trees. These APIs are contained in libjuniper, in the file patricia.h
. The application uses these APIs as follows:
config.c
, define a variable using the predefined type patroot
, for the initial root node of the tree:
static patroot root;
void init_messages_ds (void) { patricia_root_init(&root, FALSE, MESSAGE_STR_SIZE, 0); }
helloworld_data_t
, to store the data, in the header file helloworldd_config.h. The first field is a node in the tree, and the second field stores data (that is, the message):
typedef struct helloworld_data_s { patnode node; char message[MESSAGE_STR_SIZE]; } helloworld_data_t;
PATNODE_TO_STRUCT
macro to define functions that hide the patricia structure from the rest of the code.For details about how to use this macro, see Using the Patricia Tree Library.
INSIST_ERR
is defined in junos_trace.h
, in libjunos-sdk ):
static helloworld_data_t * add_message (const char * message) { helloworld_data_t *data; /* allocate & initialize data */ data = calloc(1, sizeof(helloworld_data_t)); /* calloc zeros mem */ INSIST_ERR(data != NULL); /* check that allocation worked */ INSIST_ERR(strlen(message) < MESSAGE_STR_SIZE); /* check for length */ strcpy(data->message, message); /* add to data_root patricia tree */ patricia_node_init_length(&data->node, strlen(data->message) + 1); if (!patricia_add(&root, &data->node)) { return NULL; } return data; }