Re: SCALAR Object code example

"Ronnel P. Maglasang" <[email protected]>
Newsgroups gmane.network.net-snmp.user
Message-ID <[email protected]>
here's a sample code for scalar objects. one object is read-only and
the other read-write.






Mahapatra, Surjit wrote:

>Hi All
>
>I am using net-snmp 5.2.1.2 version.
>Can some body help me finding one sample example generated from mib2c
>using mib2c.scalar.conf file.
>
>Sample example should have both GET and SET instruction implemented in
>the template code generated by mib2c.
>
>I have searched the tutorial where I am not able to find any code for
>scalar object ,for table it is given.
> 
>
>Regards
>Surjit
>OSS Team Hp
>
>
>-------------------------------------------------------
>This SF.Net email is sponsored by:
>Power Architecture Resource Center: Free content, downloads, discussions,
>and more. http://solutions.newsforge.com/ibmarch.tmpl
>_______________________________________________
>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
>  
>
sample.c (text/x-csrc, 5.8 KB)
/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.scalar.conf,v 1.8 2004/10/14 12:57:34 dts12 Exp $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "sample.h"


/* control variables */
#define BUF_LEN             51
static char hostName[BUF_LEN];
static char hostDesc[BUF_LEN];

char *tmp_ptr;

/* initialize control vars */
void init_control_vars(void)
{
  strcpy(hostName, "shokoipc.buang.cebu.com");
  strcpy(hostDesc, "workstation");
}

/** Initializes the sample module */
void
init_sample(void)
{
    static oid hostName_oid[] = { 1,3,6,1,4,1,998877,1,1,1 };
    static oid hostDesc_oid[] = { 1,3,6,1,4,1,998877,1,1,2 };

  DEBUGMSGTL(("sample", "Initializing\n"));

  init_control_vars();

    netsnmp_register_scalar(
        netsnmp_create_handler_registration("hostName", handle_hostName,
                               hostName_oid, OID_LENGTH(hostName_oid),
                               HANDLER_CAN_RONLY
        ));
    netsnmp_register_scalar(
        netsnmp_create_handler_registration("hostDesc", handle_hostDesc,
                               hostDesc_oid, OID_LENGTH(hostDesc_oid),
                               HANDLER_CAN_RWRITE
        ));
}

int
handle_hostName(netsnmp_mib_handler *handler,
                          netsnmp_handler_registration *reginfo,
                          netsnmp_agent_request_info   *reqinfo,
                          netsnmp_request_info         *requests)
{
    /* We are never called for a GETNEXT if it's registered as a
       "instance", as it's "magically" handled for us.  */

    /* a instance handler also only hands us one request at a time, so
       we don't need to loop over a list of requests; we'll only get one. */
    
    switch(reqinfo->mode) {

        case MODE_GET:
            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                     (u_char *)hostName, strlen(hostName) + 1);
            break;


        default:
            /* we should never get here, so this is a really bad error */
            snmp_log(LOG_ERR, "unknown mode (%d) in handle_hostName\n", reqinfo->mode );
            return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
int
handle_hostDesc(netsnmp_mib_handler *handler,
                          netsnmp_handler_registration *reginfo,
                          netsnmp_agent_request_info   *reqinfo,
                          netsnmp_request_info         *requests)
{
    /* We are never called for a GETNEXT if it's registered as a
       "instance", as it's "magically" handled for us.  */

    /* a instance handler also only hands us one request at a time, so
       we don't need to loop over a list of requests; we'll only get one. */

   printf("reqinfo->mode: [%d]\n", reqinfo->mode);
   printf("requests->requestvb->type: [%d]\n", requests->requestvb->type);
   printf("requests->requestvb->val_len: [%d]\n", requests->requestvb->val_len);

    switch(reqinfo->mode) {

        case MODE_GET:
            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                     (u_char *)hostDesc, strlen(hostDesc) + 1);
            break;

        /*
         * SET REQUEST
         *
         * multiple states in the transaction.  See:
         * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
         */
        case MODE_SET_RESERVE1:
            tmp_ptr = NULL;
            if (requests->requestvb->type != ASN_OCTET_STR)
            {
                printf("SNMP_ERR_WRONGTYPE!\n");
                netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_WRONGTYPE);
                return (SNMP_ERR_WRONGTYPE);
            }

            if (requests->requestvb->val_len >= BUF_LEN)
            {
                printf("SNMP_ERR_WRONGLENGTH!\n");
                netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_WRONGLENGTH);
                return (SNMP_ERR_WRONGTYPE);
            }
            break;

        case MODE_SET_RESERVE2:
            tmp_ptr = (char *)malloc(requests->requestvb->val_len + 1);
            if (tmp_ptr == NULL)
            {
                printf("SNMP_ERR_RESOURCEUNAVAILABLE!\n");
                netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);
                return (SNMP_ERR_RESOURCEUNAVAILABLE);
            }

            printf("memory alloced!\n");
            break;

        case MODE_SET_FREE:
            if (tmp_ptr)
            {
              free(tmp_ptr);
              printf("memory freed!\n");
            }

            break;

        case MODE_SET_ACTION:

            if (tmp_ptr == NULL)
            {
                printf("SNMP_ERR_RESOURCEUNAVAILABLE!\n");
                netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);
                return (SNMP_ERR_RESOURCEUNAVAILABLE);
            }

            memset(tmp_ptr, 0, BUF_LEN);
            strncpy(tmp_ptr, (char *)requests->requestvb->val.string, (BUF_LEN-1));
            break;

        case MODE_SET_COMMIT:
            if (strlen(tmp_ptr) >= BUF_LEN)
            {
                printf("SNMP_ERR_COMMITFAILED!\n");
                netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED);
                return (SNMP_ERR_COMMITFAILED);
            }

            strcpy(hostDesc, tmp_ptr);
            free(tmp_ptr);
            printf("memory freed in commit!\n");
            break;

        case MODE_SET_UNDO:
            free(tmp_ptr);
            printf("memory freed in undo!\n");
            break;

        default:
            /* we should never get here, so this is a really bad error */
            snmp_log(LOG_ERR, "unknown mode (%d) in handle_hostDesc\n", reqinfo->mode );
            return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
sample.h (text/x-chdr, 321 B)
/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.scalar.conf,v 1.8 2004/10/14 12:57:34 dts12 Exp $
 */
#ifndef SAMPLE_H
#define SAMPLE_H

/* function declarations */
void init_sample(void);
Netsnmp_Node_Handler handle_hostName;
Netsnmp_Node_Handler handle_hostDesc;

#endif /* SAMPLE_H */
smime.p7s (application/x-pkcs7-signature, 3.1 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.