Question

I want the SNMP agent to response differently depending on the source requester, but cannot find a way to magic convey some data to make it distinguishable by the SNMP agent.

What I have tried setting is the netsnmp_session structure and netsnmp_pdu structure. because they're two parameters of snmp_send. The data field I tried to facilitate is myvoid and callback_magic.

But unfortunately on the SNMP agent, the data received are all 0, which is not what I have set on the SNMP client.

Was it helpful?

Solution

Sorry to answer myselv's question.

Finally I found the following trick to circumvent the issue: insert a well known SNMP object(such as ifNumber) immediately after the target SNMP object to identify the specific SNMP query.

The handler function in agent should check the variable next to current variable to see whether it's exactly the well known SNMP object ifNumber. If yes then the query comes from you, which using NET-SNMP API to form the variable list of this query.

client code:

    oid dest_OID[ MAX_OID_LEN ] = {0};
    size_t dest_OID_len = COUNT_OF(dest_OID);
    get_node(g_snmp_name_ifNumber, dest_OID, &dest_OID_len );
    snmp_add_null_var(pdu, dest_OID, dest_OID_len);

On agent side:

 int get_status(netsnmp_mib_handler *handler,
            netsnmp_handler_registration *reginfo,
            netsnmp_agent_request_info *reqinfo,
            netsnmp_request_info *requests)
 {
    switch (reqinfo->mode) {

        case MODE_GET:
            {

                    bool is_sent_by_manager = false;
                    if( requests->requestvb->next_variable )
                    {
                        struct variable_list * v = requests->requestvb->next_variable;

                        oid dest_OID[ MAX_OID_LEN ] = {0};
                        size_t dest_OID_len = COUNT_OF(dest_OID);
                        get_node(g_snmp_name_ifNumber, dest_OID, &dest_OID_len );

                        const int nbytes = v->name_length * sizeof(v->name[0]);
                        if( dest_OID_len >= v->name_length
                            && memcmp(dest_OID, v->name, nbytes) == 0 ) {
                            is_sent_by_manager = true;
                        }
                    }

                    if( is_sent_by_manager ) {
                       ...
                    }
                    else {
                       ...
                    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top