Here is the example I have created for demonstrating the problem. I am using 
NET-SNMP 5.7.1.
I have not yet attempted to set any SNMP values. At the moment, I am concerned 
with GETNEXT and GETBULK.

#!/bin/bash
# file: testTableDemo.sh
env MIBS="+TABLE-DEMO-MIB" snmptable -l authPriv -u authPrivUser -a SHA -A test 
-x DES -X test -Ci localhost tableDemoMyTableTable



/*!
 ****************************************************************************
 *
 *  \file    tableDemo.h
 *
 *  \brief   Implements the tableDemo table and associated
 *           scalars.
 */

#ifndef _TABLEDEMO_H
#define _TABLEDEMO_H

void init_tableDemo(void);

#endif // _TABLEDEMO_H




/*!
 ****************************************************************************
 *
 *  \file    tableDemo.c
 *
 *  \brief   Implements the tableDemo table and associated
 *           scalars.
 */

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "tableDemo.h"

#define MAXINDEX 8

static uint32_t value_tableDemoNumber = 0;
static int values[MAXINDEX+1], bkupvalues[MAXINDEX+1];

static void initialize_table_tableDemoTable(void);
static Netsnmp_Node_Handler tableDemoTable_handler;
static int tableDemoNumber_handler(netsnmp_mib_handler *handler,
                netsnmp_handler_registration *reginfo,
                netsnmp_agent_request_info   *reqinfo,
                netsnmp_request_info         *requests);

void init_tableDemo(void)
{
    const oid tableDemoNumber_oid[] = { 1,3,6,1,4,1,8072,2007,1,1,1,2,1 };
    netsnmp_register_scalar(
        netsnmp_create_handler_registration("tableDemoMyTableNumber", 
tableDemoNumber_handler,
                               tableDemoNumber_oid, 
OID_LENGTH(tableDemoNumber_oid),
                               HANDLER_CAN_RONLY
        ));

    initialize_table_tableDemoTable();
}

static int tableDemoQueryIndex(netsnmp_table_request_info* table_info)
{
        int index = -1;
        int i;
        netsnmp_variable_list* varlist;

        varlist = table_info->indexes;
        for (i = 0; (NULL != varlist) && (i < table_info->number_indexes); i++)
        {
                if (ASN_UNSIGNED == varlist->type)
                {
                        switch(varlist->val_len)
                        {
                        case 1:
                                index = *((uint8_t*)(varlist->val.integer));
                                break;
                        case 2:
                                index = *((uint16_t*)(varlist->val.integer));
                                break;
                        case 8:
                                index = *((uint64_t*)(varlist->val.integer));
                                break;
                        case 4:
                                /* no break */
                        default:
                                index = *((uint32_t*)(varlist->val.integer));
                                break;
                        }
                }
                varlist = varlist->next_variable;
        }

        return index;
}

static void initialize_table_tableDemoTable(void)
{
    const oid tableDemoTable_oid[] = {1,3,6,1,4,1,8072,2007,1,1,1,2,2};
    netsnmp_table_data_set* table_set;
    netsnmp_handler_registration* table_reg;
    netsnmp_table_row* row;
    unsigned int index;
    int value;
    int version;

    table_reg = netsnmp_create_handler_registration("tableDemoMyTableTable",
                tableDemoTable_handler,
            tableDemoTable_oid,
            OID_LENGTH(tableDemoTable_oid),
            HANDLER_CAN_RWRITE);
    table_set = netsnmp_create_table_data_set("tableDemoMyTableTable");
    netsnmp_table_set_add_indexes(table_set, ASN_UNSIGNED, 0);
    netsnmp_table_set_multi_add_default_row(table_set,
                1, ASN_UNSIGNED, 0, NULL, 0,
            3, ASN_INTEGER, 1, NULL, 0,
            5, ASN_INTEGER, 0, NULL, 0,
            0);
    netsnmp_register_table_data_set(table_reg, table_set, NULL);

    for (index = 0; index <= MAXINDEX; index++)
    {
        if ((5 <= index) && (index <= MAXINDEX))
        {
                values[index] = 100 + (int) index;
                row = netsnmp_create_table_data_row();
                netsnmp_table_row_add_index(row, ASN_UNSIGNED, &index, 
sizeof(index));
                netsnmp_set_row_column(row, 1, ASN_UNSIGNED, &index, 
sizeof(index));
                netsnmp_mark_row_column_writable(row, 1, 0);
                netsnmp_set_row_column(row, 3, ASN_INTEGER, &(values[index]), 
sizeof(values[index]));
                netsnmp_mark_row_column_writable(row, 3, 1);
                netsnmp_set_row_column(row, 5, ASN_INTEGER, &version, 
sizeof(version));
                netsnmp_mark_row_column_writable(row, 5, 1);
            netsnmp_table_dataset_add_row(table_set, row);

            value_tableDemoNumber++;
        }
    }
}

static int tableDemoNumber_handler(netsnmp_mib_handler* handler,
                                                             
netsnmp_handler_registration* reginfo,
                                                                 
netsnmp_agent_request_info* reqinfo,
                                                                         
netsnmp_request_info* requests)
{
    switch(reqinfo->mode) {
        case MODE_GET:
            snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
                                     &value_tableDemoNumber, 
sizeof(value_tableDemoNumber));
            break;

        default:
            snmp_log(LOG_ERR, "unknown mode (%d) in handle_tableDemoNumber\n", 
reqinfo->mode );
            return SNMP_ERR_GENERR;
            break;
    }

    return SNMP_ERR_NOERROR;
}

static int tableDemoTable_handler(
                netsnmp_mib_handler* handler, netsnmp_handler_registration* 
reginfo,
                netsnmp_agent_request_info* reqinfo, netsnmp_request_info* 
requests)
{
    netsnmp_request_info       *request;
    netsnmp_table_request_info *table_info;
    int value, version, ret;
    unsigned int index;

    //return SNMP_ERR_NOERROR;

    switch (reqinfo->mode) {
    case MODE_GET:
        for (request=requests; request; request=request->next) {
            table_info = netsnmp_extract_table_info(request);
            index = tableDemoQueryIndex(table_info);
            version = 1;

            printf("index=%d colnum=%d\n", index, table_info->colnum);

            switch (table_info->colnum) {
            case 1:
                if (!((5 <= index) && (index <= MAXINDEX)))
                {
                    netsnmp_set_request_error(reqinfo, request, 
SNMP_NOSUCHINSTANCE);
                        continue;
                }
                snmp_set_var_typed_integer(request->requestvb, ASN_UNSIGNED, 
index);
                break;
            case 3:
                if (!((5 <= index) && (index <= MAXINDEX)))
                {
                    netsnmp_set_request_error(reqinfo, request, 
SNMP_NOSUCHINSTANCE);
                        continue;
                }
                snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER, 
value);
                break;
            case 5:
                if (!((5 <= index) && (index <= MAXINDEX)))
                {
                    netsnmp_set_request_error(reqinfo, request, 
SNMP_NOSUCHINSTANCE);
                        continue;
                }
                snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER, 
version);
                break;
            default:
                netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;

    case MODE_SET_RESERVE1:
        for (request=requests; request; request=request->next) {
            table_info = netsnmp_extract_table_info(request);
            index = tableDemoQueryIndex(table_info);

            switch (table_info->colnum) {
            case 3:
                if (!((5 <= index) && (index <= MAXINDEX)))
                {
                    netsnmp_set_request_error(reqinfo, request, 
SNMP_NOSUCHINSTANCE);
                        continue;
                }
                        ret = netsnmp_check_vb_int(request->requestvb);
                if (ret != SNMP_ERR_NOERROR) {
                        netsnmp_set_request_error(reqinfo, request, ret);
                }
                break;
            default:
                netsnmp_set_request_error(reqinfo, request, 
SNMP_ERR_NOTWRITABLE);
                break;
            }
        }
        break;

    case MODE_SET_RESERVE2:
        for (request=requests; request; request=request->next) {
            table_info = netsnmp_extract_table_info(request);
            index = tableDemoQueryIndex(table_info);

            switch (table_info->colnum) {
            case 3:
                if ((5 <= index) && (index <= MAXINDEX))
                {
                    bkupvalues[index] = values[index];
                }
                break;
            default:
                break;
            }
        }
        break;

    case MODE_SET_FREE:
        break;

    case MODE_SET_ACTION:
        for (request=requests; request; request=request->next) {
            table_info = netsnmp_extract_table_info(request);
            index = tableDemoQueryIndex(table_info);

            switch (table_info->colnum) {
            case 3:
                if ((5 <= index) && (index <= MAXINDEX))
                {
                        values[index] = *(request->requestvb->val.integer);
                }
                break;
            default:
                break;
            }
        }
        break;

    case MODE_SET_UNDO:
        for (request=requests; request; request=request->next) {
            table_info = netsnmp_extract_table_info(request);
            index = tableDemoQueryIndex(table_info);

            switch (table_info->colnum) {
            case 3:
                if ((5 <= index) && (index <= MAXINDEX))
                {
                        values[index] = bkupvalues[index];
                }
                break;
            default:
                break;
            }
        }
        break;

    case MODE_SET_COMMIT:
        break;
    }

    return SNMP_ERR_NOERROR;
}




TABLE-DEMO-MIB DEFINITIONS ::= BEGIN
IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32, Unsigned32,
    FROM SNMPv2-SMI
    netSnmp
    FROM NET-SNMP-MIB
;

tableDemo MODULE-IDENTITY
    LAST-UPDATED "201112050002Z"
    ORGANIZATION "nowhere"
    CONTACT-INFO
         ""
    DESCRIPTION
        "Based on NET-SNMP-EXAMPLES-MIB"
    REVISION     "201112050001Z"
    DESCRIPTION
        ""
    REVISION     "201112050000Z"
    DESCRIPTION
        ""
    ::= { netSnmp 2007 }

tableDemoTables OBJECT IDENTIFIER ::= { tableDemo 1 }
tableDemoTables1 OBJECT IDENTIFIER ::= { tableDemoTables 1 }
tableDemoTables2 OBJECT IDENTIFIER ::= { tableDemoTables1 1 }

-- 1.3.6.1.4.1.8072.2007.1.1.1.2
tableDemoMyTable OBJECT IDENTIFIER ::= { tableDemoTables2 2 }


-- 1.3.6.1.4.1.8072.2007.1.1.1.2.1
tableDemoMyTableNumber OBJECT-TYPE
        SYNTAX Unsigned32
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION
                "Indicates the number of entries in tableDemoMyTableTable."
        ::= { tableDemoMyTable 1 }


-- 1.3.6.1.4.1.8072.2007.1.1.1.2.2
tableDemoMyTableTable OBJECT-TYPE
        SYNTAX SEQUENCE OF tableDemoMyTableEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION
                ""
        ::= { tableDemoMyTable 2 }


-- 1.3.6.1.4.1.8072.2007.1.1.1.2.2.1
tableDemoMyTableEntry OBJECT-TYPE
        SYNTAX tableDemoMyTableEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION
                ""
        INDEX { tableDemoMyTableIndex }
        ::= { tableDemoMyTableTable 1 }


tableDemoMyTableEntry ::=
        SEQUENCE {
                tableDemoMyTableIndex
                        Unsigned32,
                tableDemoMyTableValue
                        Integer32,
                tableDemoMyTableVersion
                        Integer32
         }

-- 1.3.6.1.4.1.8072.2007.1.1.1.2.2.1.1
tableDemoMyTableIndex OBJECT-TYPE
        SYNTAX Unsigned32 (1..255)
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION
                "Index for tableDemoMyTableTable"
        ::= { tableDemoMyTableEntry 1 }


-- 1.3.6.1.4.1.8072.2007.1.1.1.2.2.1.3
tableDemoMyTableValue OBJECT-TYPE
        SYNTAX Integer32
        MAX-ACCESS read-write
        STATUS current
        DESCRIPTION
                ""
        ::= { tableDemoMyTableEntry 3 }


-- 1.3.6.1.4.1.8072.2007.1.1.1.2.2.1.5
tableDemoMyTableVersion OBJECT-TYPE
        SYNTAX Integer32
        MAX-ACCESS read-only
        STATUS current
        DESCRIPTION
                ""
        ::= { tableDemoMyTableEntry 5 }

END

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf 
Of Dave Shield
Sent: Tuesday, December 06, 2011 5:44 AM
To: STOTTS Timothy
Cc: [email protected]
Subject: Re: how to determine table query index in snmp subagent table handler?

On 5 December 2011 21:27, STOTTS Timothy
<[email protected]> wrote:
> Thank you for your answer Dave. However, your suggestion of using the varbind 
> list only
> works for single GET operations of the table entries.

No - it ought to work for all requests.


> As a reminder, my table initialization code looks like this.
      [snip]

> An example of what I am doing:
>    switch (reqinfo->mode) {
>    case MODE_GET:
         [snip]

It's actually quite difficult to work out what might be going wrong
from individual snippets of code.
   Could you please post the full code files (.c and .h) for your module,
so I can try this out for myself.

Thanks
Dave

________________________________
CONFIDENTIALITY : This e-mail and any attachments are confidential and may be 
privileged. If you are not a named recipient, please notify the sender 
immediately and do not disclose the contents to another person, use it for any 
purpose or store or copy the information in any medium.

------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of 
discussion for anyone considering optimizing the pricing and packaging model 
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to