/* This function will create an instance node in mib-tree according to oid given * in which the prefix can be already created or not existing group node(s), and * the last id number must be the not existing instance node. */ static struct mib_instance_node * mib_tree_instance_insert(const oid_t *oid, uint32_t id_len, int callback) { struct mib_node *node = (struct mib_node *)&mib_dummy_node; struct mib_group_node *gn; while (id_len > 0) { switch (node->type) { case MIB_OBJ_GROUP: gn = (struct mib_group_node *)node; if (is_raw_group(gn)) { gn->sub_id_cnt++; gn->sub_id[0] = *oid++; if (--id_len == 0) { /* Allocate new instance node */ node = gn->sub_ptr[0] = mib_instance_node_new(callback); return (struct mib_instance_node *)node; } else { /* Allocate new group node */ node = gn->sub_ptr[0] = mib_group_node_new(); } } else { /* Search in exist sub-ids */ int i = oid_binary_search(gn->sub_id, gn->sub_id_cnt, *oid); if (i >= 0) { /* Sub-id found, go on traversing */ oid++; id_len--; node = gn->sub_ptr[i]; } else { /* Sub-id not found, that's it. */ i = -i - 1; /* resize sub_id[] */ group_node_expand(gn, i); gn->sub_id_cnt++; gn->sub_id[i] = *oid++; if (--id_len == 0) { /* Allocate new instance node */ node = gn->sub_ptr[i] = mib_instance_node_new(callback); return (struct mib_instance_node *)node; } else { /* Allocate new group node */ node = gn->sub_ptr[i] = mib_group_node_new(); } } } continue; case MIB_OBJ_INSTANCE: /* Bad traversal */ return NULL; default: assert(0); } } /* Bad traversal */ return NULL; }
/* This function create group node(s) in mib-tree according to oid given in * which the prefix can be already created as group node(s) and the last id * number must not be created yet. the id number before the last one will be * created as new dummy group node(s). */ static struct mib_group_node * mib_tree_group_insert(const oid_t *oid, uint32_t id_len) { struct mib_node *node; struct mib_group_node *gn; struct node_pair pair; /* Init something */ pair.parent = pair.child = NULL; node = (struct mib_node *)&internet_group; oid += INTERNET_PREFIX_LENGTH; id_len -= INTERNET_PREFIX_LENGTH; while (id_len > 0) { switch (node->type) { case MIB_OBJ_GROUP: gn = (struct mib_group_node *)node; if (is_raw_group_node(gn)) { /* Allocate intermediate group node */ node = gn->sub_ptr[0] = mib_group_node_new(); gn->sub_id_cnt++; gn->sub_id[0] = *oid++; id_len--; } else { /* Search in exist sub-ids */ int i = oid_binary_search(gn->sub_id, gn->sub_id_cnt, *oid); if (i >= 0) { /* Sub-id found, go on traversing */ oid++; id_len--; node = gn->sub_ptr[i]; } else { /* Sub-id not found, that's it. */ i = -i - 1; /* realloc sub_id[] */ group_node_resize(gn, i); /* Allocate new group node */ node = gn->sub_ptr[i] = mib_group_node_new(); gn->sub_id_cnt++; gn->sub_id[i] = *oid++; id_len--; /* Record the new allocated root group node */ if (pair.child == NULL && pair.parent == NULL) { pair.parent = (struct mib_node *)gn; pair.child = (struct mib_node *)node; pair.sub_idx = i; } } } continue; case MIB_OBJ_INSTANCE: /* Bad traversal, clear all temporarily built group nodes */ __mib_tree_delete(&pair); return NULL; default: assert(0); break; } } /* id_len == 0 */ return (struct mib_group_node *)node; }