Name

Synopsis

Description

The mib2c tool is designed to take a portion of the MIB tree (as defined by a MIB file) and generate the template C code necessary to implement the corresponding MIB module.

To implement a new MIB module, three files are necessary:

MIB definition file

C header file

C implementation file

The mib2c tool uses the MIB definition file to produce the two C code files. Thus, mib2c generates a template that you can edit to add logic necessary to obtain information from the operating system or application to complete the module.

The operand mibnode is the top level MIB node for which you want to generate code. You must give mib2c a MIB node (for example, ifTable), not a MIB file, on the command line. This distinction is a common source of user error.

The mib2c tool accepts both SMIv1 and SMIv2 MIBs.

mib2c needs to be able to find and load a MIB file in order to generate C code for the MIB. To enable mib2c to find the MIB file, set the MIBS environment variable to include the MIB file you are using. An example of setting this environment variable
is:

MIBS=+NET-SNMP-TUTORIAL-MIB

or

MIBS=ALL

The first example ensures that mib2c finds the NET-SNMP-TUTORIAL-MIB MIB, in addition to the default MIB modules. The default list of MIB modules is set when the suite is first configured and built. The list corresponds to the list of modules that the agent
supports. The second example ensures that mib2c finds all MIBs in the search location for MIB files. The default search location for MIB files is DATADIR/snmp/mibs. This search location can be modified by the MIBDIRS environment variable.

Both the MIB files to be loaded and the MIB file search location can also be configured in the snmp.conf file. Please see snmp.conf(4)
for more information.

The generated .c and .h files are created in the current working directory.

Options

The following options are supported:

-h

Display a help message.

-cconfigfile

Use configfile when generating code. These files are searched for first in the current directory and then in the DATADIR directory, which is where the default mib2c configuration files are located. Running mib2c without the -cconfigfile option displays a description of the valid values for configfile, that is, the available configuration files, including new
ones that you might have created.

For example:

% mib2c ifTable

...displays the contents of the mib2.conf file, which displays hints on choosing the best configfile option for the mibnode.

In the first invocation of mib2c, above, the indent errors are of no consequence.

-q

Run in "quiet" mode, which minimizes the status messages mib2c generates.

-Svar=val

Preset a variable var in the mib2c.*.conf file to the value val. None of the existing mib2c
configuration files (mib2c.*.conf) currently makes use of this feature. Consider this option available only for future use.

Examples

Example 1 Generating Code for Scalar Objects

If you are writing code for some scalars, run:

% mib2c -c mib2c.scalar.conf mibnode

If you want to magically "tie" integer variables to integer scalars, use:

% mib2c -c mib2c.int_watch.conf mibnode

Example 2 Generating Code for Tables

Consider the case where:

You need to "iterate" over your table data to find the correct data for the SNMP row being accessed.

Your table data is not kept within the agent (for example, it is in the kernel and not in the memory of the agent itself).

Under such conditions, use a command such as:

% mib2c -c mib2c.iterate.conf mibnode

You can find a similar example in agent/mibgroup/mibII/vacm_context.c.

If your table data is kept in the agent (that is, it is not located in an external source) and is purely data-driven (that is, you do not need to perform any work when a set occurs), you can use a command such as the following:

% mib2c -c mib2c.create-dataset.conf mibnode

See agent/mibgroup/examples/data_set.c for a similar example.

If your table data is kept in the agent (that is, it is not located in an external source) and you can keep your data sorted by the table index, but you do need to perform work when a set occurs, use a command such as the following:

% mib2c -c mib2c.array-user.conf mibnode

Example 3 Generating Header File Definitions

To generate just a header with a define for each column number in your table, enter a command such as:

% mib2c -c mib2c.column_defines.conf mibnode

To generate only a header with a define for each enum for any column containing enums, enter:

% mib2c -c mib2c.column_enums.conf mibnode

Example 4 Generating Code for the SunOS 4.X Line of Code

The following command generates code for SunOS 4.x:

% mib2c -c mib2c.old-api.conf mibnode

Example 5 Generating Code for ucdDemoPublic

The command below generates C template code for the header and implementation files to implement UCD-DEMO-MIB::ucdDemoPublic.