net-snmp parse code, How to parse MIB?

允我心安 提交于 2019-12-05 21:55:56

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

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.

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