Search code examples
c++snmpnet-snmp

How to figure out if requested OID is in Table with net-snmp API?


I'm working on a solution for Windows SNMP Agent Extension. Simultaneously, I use Net-SNMP for reading and loading OIDs from MIB files. Matching old SNMP objects to Net-SNMP objects is a hard work in C++ and I did it severely. Now lots of problems appears on Set, Get-Next requests for Table objects.

The important thing is how to set OID which refer to table entries or field now.

How can I check if requested OID for Get or Set request is the field or entries of a table in MIB with Net-SNMP API?


Solution

  • Here is some function I prepare for figure out table elements:

    If Oid is a field of a table:

    BOOL IsInTable(AsnObjectIdentifier pAsnOid) {
        char *          szOidTemp;
        char *          pch;
        oid             root[MAX_OID_LEN];
        size_t          rootlen;
    
        SnmpMgrOidToStr(&pAsnOid, &szOidTemp);
    
        memset(root, 0, sizeof(root));
        rootlen = MAX_OID_LEN;
        if (snmp_parse_oid(szOidTemp, root, &rootlen) == NULL) {
            snmp_perror(szOidTemp);
            //exit(1);
            return false;
        }
    
        struct tree    *tbl = NULL;
        tbl = get_tree(root, rootlen, get_tree_head());
    
        if (tbl) {
            if (tbl->parent && strstr(strlwr(tbl->parent->label), "entry") > 0) {
                return true;
            }
        }
        return false;
    }
    

    If Oid is root of a table:

    BOOL IsTableRoot(AsnObjectIdentifier pAsnOid) {
        char *          szOidTemp;
        char *          pch;
        oid             root[MAX_OID_LEN];
        size_t          rootlen;
    
        SnmpMgrOidToStr(&pAsnOid, &szOidTemp);
    
        memset(root, 0, sizeof(root));
        rootlen = MAX_OID_LEN;
        if (snmp_parse_oid(szOidTemp, root, &rootlen) == NULL) {
            snmp_perror(szOidTemp);
            //exit(1);
            return false;
        }
    
        struct tree    *tbl = NULL;
        tbl = get_tree(root, rootlen, get_tree_head());
    
        if (tbl) {
            if (strstr(strlwr(tbl->label), "table") > 0) {
                return true;
            }
        }
        return false;
    }
    

    If Oid is entry or first child of a table:

    BOOL IsTableEntry(AsnObjectIdentifier pAsnOid) {
        char *          szOidTemp;
        char *          pch;
        oid             root[MAX_OID_LEN];
        size_t          rootlen;
    
        SnmpMgrOidToStr(&pAsnOid, &szOidTemp);
    
        memset(root, 0, sizeof(root));
        rootlen = MAX_OID_LEN;
        if (snmp_parse_oid(szOidTemp, root, &rootlen) == NULL) {
            snmp_perror(szOidTemp);
            //exit(1);
            return false;
        }
    
        struct tree    *tbl = NULL;
        tbl = get_tree(root, rootlen, get_tree_head());
    
        if (tbl) {
            if (strstr(strlwr(tbl->label), "entry") > 0) {
                return true;
            }
        }
        return false;
    }