예제 #1
0
파일: mib.c 프로젝트: Candouble/smartsnmp
/* This function creates one new instance node in mib-tree according to given
 * oid in which the last id number is the first id of the instance. Id number
 * before the last one are regarded as exist group node(s) by default.
 */
static struct mib_instance_node *
mib_tree_instance_insert(const oid_t *oid, uint32_t id_len, const char *lua_callback)
{
  struct mib_node *node;
  struct mib_group_node *gn;

  node = (struct mib_node *)&internet_group;
  oid += INTERNET_PREFIX_LENGTH;
  id_len -= INTERNET_PREFIX_LENGTH;

  while (node != NULL && id_len > 0) {
    switch (node->type) {

      case MIB_OBJ_GROUP:
        gn = (struct mib_group_node *)node;
        if (is_raw_group_node(gn) && id_len == 1) {
          /* Allocate intermediate group node */
          gn->sub_ptr[0] = mib_instance_node_new(lua_callback);
          gn->sub_id[0] = *oid;
          gn->sub_id_cnt++;
          return gn->sub_ptr[0];
        } 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];
            continue;
          } else {
            /* Sub-id not found, that's it. */
            i = -i - 1;
            if (id_len == 1) {
              /* The last only oid is for the new instance node */
              group_node_resize(gn, i);
              gn->sub_ptr[i] = mib_instance_node_new(lua_callback);
              gn->sub_id[i] = *oid;
              gn->sub_id_cnt++;
              return gn->sub_ptr[i];
            }
          }
        }

      case MIB_OBJ_INSTANCE:
        /* Bad traversal */
        return NULL;

      default:
        assert(0);
        break;
    }
  }

  /* Bad traversal */
  return NULL;
}
예제 #2
0
/* 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;
}