net-snmp parse code, How to parse MIB?

冷暖自知 提交于 2019-12-07 16:07:29

问题


I am learning net-snmp code-base. To parsing MIB.

In parse.c and parse.h code keeps a hash bucket. (indexed bucket (tree list)).
There is also a tree structure, Which contains a next pointer pointing to Next node in hashed list of names.

struct tree{  
    .
    .
    struct tree    *next;       // Next node in hashed list of names 
    int             modid;     // The module containing this node 
}

I printed the MIB,

SNMP-FRAMEWORK-MIB:snmpFrameworkMIB(10) type=24 Next-> ' ipSystemStatsHCOctetGroup ipSystemStatsOutFragReqds ifStackGroup2 ifOutErrors '

I couldn't understand what is the relation among the name of objects appears after Next-> ?

What is the criteria on the basis of which object names are together? The Code is unclear to me at this point.

What is modid? Its value not equal to module OID!

NOTE: For purely traversing purpose in MIB-tree there is *child, *parent & *peer are given! Also modid is not part of OID.

A Data-Structure named 'module compatability' in parse.h:

struct module_compatability {
        const char     *old_module;
        const char     *new_module;
        const char     *tag;    /* NULL implies unconditional replacement,
                                 * otherwise node identifier or prefix */
        size_t          tag_len;        /* 0 implies exact match (or unconditional) */
        struct module_compatability *next;      /* linked list */
    };

What is the use of this structure? Compatible in What sense ?


回答1:


I am also working with Net-snmp from quite a some time, i am sharing my observation with you .
May be this will help you.

1. struct tree *next;

struct tree    * next; /* Next node in hashed list of names */   

Net-snmp feature provides query by 'name' of module,
Object When the queried object name (string) is of ASCII, i.e.

$ snmptranslate -On -IR bundleSize  
  -   
  -  
  .1.3.6.1.4.1.26149.2.1.2.2.1.9  

It has a hash-table(internal) data-structure 'bucket' of size 128.

Hash Function:

name_hash(str*) - return some of ASCII value. 

Then this hash value is passed into a macro NBUCKET(x) - returns index (0-127). The collision is resoled by chaining as follows. bucket[i]->next->next->next........


The code for this is present in parse.c --

tree->next and 'bucket' are managed in following way:

 tp->hash = name_hash(tp->name); // Fist find hash value as some of ASCII
 b = BUCKET(tp->hash);           // map hash value into (0-127)
 if (buckets[b])                 // check for collision 
     tp->next = buckets[b];     // collision is resolved ny chan chain 
 buckets[b] = tp;           // new coming node become first node in chain

2. int modid;

  • The module containing this node.
    • There is a linked list of type 'struct module'
    • modid is sequence number in linked-list of a module.
    • sequence number start with 0.
    • modid= number at which module started read
    • a function defined in parse.h 'find_module(int modid)' return node address stores the information about module.

A Data-Structure named 'module compatability' in parse.h:

This is an array of structre 'module compatability' use to store compatible 
basic MIB name (RFC's defined).   

const char     *old_module;   // snmp-v1  
const char     *new_module;   // snmp-v2



回答2:


The modid is not module OID. It is a single number (included in OID) definining module identity. All OIDs introduced by this MIB module will contain this number as OID prefix. The modid will be constant for all the nodes defined beneath. I believe, in your case the modid is 31 (ipTrafficStats)?

As you probably know, the MIB has a tree form. Nodes may contain other nodes etc. The structure you refer to represents a node. So by using the "next" pointer you are traversing through the nodes read by your parser.



来源:https://stackoverflow.com/questions/12798871/net-snmp-parse-code-how-to-parse-mib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!