[prev in list] [next in list] [prev in thread] [next in thread] 

List:       net-snmp-coders
Subject:    creation of table entry for mib2c.iterate.conf
From:       Prankur Chauhan <prankur.chauhan89 () gmail ! com>
Date:       2023-12-05 14:15:28
Message-ID: CAEgXv54=z3g7w2QvrSF-Ui6Xd_rz1n+BnoM0fpwcWv6BHcSo2Q () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Dear All,

I have used the mib2c.iterate.conf file to auto generate code for my simple
table.
My question is I can read a text file to initialize the table and use
table_createEntry to create the entry.

After creation I also wanted to "netsnmp_insert_iterator_context( NULL,
wgEntry );" like it was done in MODE_SET_RESERVE2. But it seems it is not
required ?

Please check the MIB file and also the c code

-- 
Cheers
Prankur

[Attachment #5 (text/html)]

<div dir="ltr"><div>Dear All,</div><div><br></div><div>I have used the \
mib2c.iterate.conf file to auto generate code for my simple table. <br></div><div>My \
question is I can read a text file to initialize the table and use table_createEntry \
to create the entry.<br><br></div><div>After creation I also wanted to \
&quot;netsnmp_insert_iterator_context( NULL, wgEntry );&quot; like it was done in \
MODE_SET_RESERVE2. But it seems it is not required ?<br><br></div><div>Please check \
the MIB file and also the c code<br></div><div><br><span \
class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" \
data-smartmail="gmail_signature"><div dir="ltr"><div><div \
dir="ltr"><div>Cheers<br></div>Prankur <br></div></div></div></div></div></div>

--0000000000005b00ef060bc3e047--


["KAIROS-MIB.txt" (text/plain)]

KAIROS-MIB DEFINITIONS ::= BEGIN

--
-- Example MIB objects for agent module example implementations
--

IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32, --Counter32, Gauge32,
	 --TimeTicks,
    NOTIFICATION-TYPE                       FROM SNMPv2-SMI
    SnmpAdminString                         FROM SNMP-FRAMEWORK-MIB
    netSnmpPlaypen                          FROM NET-SNMP-MIB
    TEXTUAL-CONVENTION, --PhysAddress, 
	 DisplayString			  						  
					    FROM SNMPv2-TC
    --InetAddressType, InetAddress          FROM INET-ADDRESS-MIB
	 --IANAifType										  FROM IANAifType-MIB
	 OBJECT-GROUP, NOTIFICATION-GROUP 	     FROM SNMPv2-CONF
	 --RowStatus, StorageType     FROM SNMPv2-TC
;

kairosExamples MODULE-IDENTITY
    LAST-UPDATED "200406150000Z"
    ORGANIZATION "Panasonic Inc."
    CONTACT-INFO    
         " email:    prankur.chauhan@eu.panasonic.com"
    DESCRIPTION
        "Example MIB objects for agent module example implementations"
    REVISION     "200406150000Z"
    DESCRIPTION
        "Corrected notification example definitions"
    REVISION     "200202060000Z"
    DESCRIPTION
        "First draft"
    ::= { netSnmpPlaypen 42 }

InterfaceIndex ::= TEXTUAL-CONVENTION
    DISPLAY-HINT "d"
    STATUS       current
    DESCRIPTION
            "A unique value, greater than zero, for each interface or
            interface sub-layer in the managed system.  It is
            recommended that values are assigned contiguously starting
            from 1.  The value for each interface sub-layer must remain
            constant at least from one re-initialization of the entity's
            network management system to the next re-initialization."
    SYNTAX       Integer32 (1..2147483647)


--
-- top level structure
--
kairosExampleScalars       OBJECT IDENTIFIER ::= { kairosExamples 1 }
kairosExampleTables        OBJECT IDENTIFIER ::= { kairosExamples 2 }
kairosNotifications        OBJECT IDENTIFIER ::= { kairosExamples 0 }
kairosNotificationObjects  OBJECT IDENTIFIER ::= { kairosNotifications 0 }
kairosGroups               OBJECT IDENTIFIER ::= { kairosExamples 99}

--
-- Example scalars
--

kairosScalarGroup OBJECT-GROUP
    OBJECTS { 
        		kairosExampleInteger,
        		kairosExampleString
			}
    STATUS  current
    DESCRIPTION
 				"A collection of scalar objects of kairos."
    ::= { kairosGroups 1 }

kairosExampleInteger OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DESCRIPTION
        "This is a simple object which merely houses a writable
         integer.  It's only purposes is to hold the value of a single
         integer.  Writing to it will simply change the value for
         subsequent GET/GETNEXT/GETBULK retrievals.

	 This example object is implemented in the
         agent/mibgroup/examples/scalar_int.c file"
    DEFVAL { 55 }
    ::= { kairosExampleScalars 1 }


kairosExampleString OBJECT-TYPE
    SYNTAX      SnmpAdminString
    MAX-ACCESS  read-write
    STATUS      current
    DESCRIPTION
        "This is a simple object which merely houses a writable
         string.  It's only purposes is to hold the value of a single
         string.  Writing to it will simply change the value for
         subsequent GET/GETNEXT/GETBULK retrievals.

         This example object is implemented in the
         agent/mibgroup/examples/watched.c file."
    DEFVAL { "So long, and thanks for all the crab!" }
    ::= { kairosExampleScalars 2 }

wgGroup    OBJECT-GROUP
    OBJECTS { 
    			wgName,
    			wgChair1,
    			wgChair2,
    			wgPrio
			}
    STATUS  current
    DESCRIPTION
 				"A collection of objects providing information applicable to workGroup tutorial"
    ::= { kairosGroups 2 }

wgTable OBJECT-TYPE
    SYNTAX      SEQUENCE OF WGEntry
    MAX-ACCESS  not-accessible
    STATUS      current
    DESCRIPTION
             "This table merely contains a set of data which is otherwise
             useless for true network management.  It is a table which
             describes properies about a IETF Working Group, such as the
             names of the two working group chairs.
             
             This example table is implemented in the
             agent/mibgroup/examples/data_set.c file."
    ::= { kairosExampleTables 2 }

wgEntry OBJECT-TYPE
    SYNTAX       WGEntry
    MAX-ACCESS   not-accessible
    STATUS       current
    DESCRIPTION
             "A row describing a given working group"
    INDEX        { wgIndex }
    ::= { wgTable 1 }

WGEntry ::= SEQUENCE {
    wgIndex      Integer32, 
    wgName       OCTET STRING,
    wgChair1     OCTET STRING,
    wgChair2     OCTET STRING,
    wgPrio       Integer32
}

wgIndex OBJECT-TYPE
    SYNTAX       Integer32 (1 .. 128)
    MAX-ACCESS   not-accessible
    STATUS       current
    DESCRIPTION
             "The table index, will be in sorted order"
    ::= { wgEntry 1 }

wgName OBJECT-TYPE
    SYNTAX       OCTET STRING (SIZE(1..32))
    MAX-ACCESS   read-create
    STATUS       current
    DESCRIPTION
             "The name of the Working Group this table describes"
    ::= { wgEntry 2 }

wgChair1 OBJECT-TYPE
    SYNTAX       OCTET STRING
    MAX-ACCESS   read-create
    STATUS       current
    DESCRIPTION
             "One of the names of the chairs for the working group"
    ::= { wgEntry 3 }

wgChair2 OBJECT-TYPE
    SYNTAX       OCTET STRING
    MAX-ACCESS   read-create
    STATUS       current
    DESCRIPTION
             "The other name, if one exits, of the chairs of the working group"
    ::= { wgEntry 4 }

wgPrio OBJECT-TYPE
    SYNTAX       Integer32
    MAX-ACCESS   read-create
    STATUS       current
    DESCRIPTION
             "The priority of the working group"
    ::= { wgEntry 5 }
    

---
--- Notifications/Traps
---

kairosTrapInfoGroup OBJECT-GROUP
    OBJECTS { 
        		kairosTrapTime,
        		kairosTrapData
			}
    STATUS  current
    DESCRIPTION
 				"A collection of meta info about traps of kairos."
    ::= { kairosGroups 3 }

kairosTrapTime OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  accessible-for-notify
    STATUS      current
    DESCRIPTION "time timestamp"
	 ::= {kairosNotificationObjects 1}

kairosTrapData OBJECT-TYPE
    SYNTAX      DisplayString
    MAX-ACCESS  accessible-for-notify
    STATUS      current
    DESCRIPTION "time data"
	 ::= {kairosNotificationObjects 2}

kairosNotificationGroup NOTIFICATION-GROUP
    NOTIFICATIONS { 
        		kairosNotif
			}
    STATUS  current
    DESCRIPTION
 				"A collection of kairos notifications."
   ::= { kairosGroups 4 }

kairosNotif NOTIFICATION-TYPE
    OBJECTS     { kairosTrapTime, kairosTrapData, kairosExampleInteger}
    STATUS      current
    DESCRIPTION " notify when the kairosExampleInteger is set to 5"
    ::= {kairosNotifications 1}

END

["wgTable.c" (text/x-csrc)]

/*
 * Note: this file originally auto-generated by mib2c
 * using mib2c.iterate.conf
 */

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

#define MAX_STRING_SIZE 256
#define MAX_LINE_SIZE 64
/** Initializes the wgTable module */
void
init_wgTable(void)
{
  /* here we initialize all the tables we're planning on supporting */
    initialize_table_wgTable();
}

/* Typical data structure for a row entry */
struct wgTable_entry {
    /* Index values */
    long wgIndex;

    /* Column values */
    char wgName[MAX_STRING_SIZE];
    size_t wgName_len;
    char old_wgName[MAX_STRING_SIZE];
    size_t old_wgName_len;
    char wgChair1[MAX_STRING_SIZE];
    size_t wgChair1_len;
    char old_wgChair1[MAX_STRING_SIZE];
    size_t old_wgChair1_len;
    char wgChair2[MAX_STRING_SIZE];
    size_t wgChair2_len;
    char old_wgChair2[MAX_STRING_SIZE];
    size_t old_wgChair2_len;
    long wgPrio;
    long old_wgPrio;

    /* Illustrate using a simple linked list */
    int   valid;
    struct wgTable_entry *next;
};


/** Initialize the wgTable table by defining its contents and how it's structured */
void
initialize_table_wgTable(void)
{
    const oid wgTable_oid[] = {1,3,6,1,4,1,8072,9999,9999,42,2,2};
    const size_t wgTable_oid_len   = OID_LENGTH(wgTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_iterator_info           *iinfo;
    netsnmp_table_registration_info *table_info;

    DEBUGMSGTL(("wgTable:init", "initializing table wgTable\n"));

    reg = netsnmp_create_handler_registration(
              "wgTable",     wgTable_handler,
              wgTable_oid, wgTable_oid_len,
              HANDLER_CAN_RWRITE
              );

    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
    netsnmp_table_helper_add_indexes(table_info,
                           ASN_INTEGER,  /* index: wgIndex */
                           0);
    table_info->min_column = COLUMN_WGNAME;
    table_info->max_column = COLUMN_WGPRIO;
    
    iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
    iinfo->get_first_data_point = wgTable_get_first_data_point;
    iinfo->get_next_data_point  = wgTable_get_next_data_point;
    iinfo->table_reginfo        = table_info;
    
    netsnmp_register_table_iterator( reg, iinfo );

    /* Initialise the contents of the table here */
    const long ret = init_wgTable_from_file("init_table.txt");
    if (ret != MFD_SUCCESS) {
        printf("ERROR: cannot init table from the init_table.txt, error : %ld\n", \
ret);  }
}

struct wgTable_entry  *wgTable_head;

/* create a new row in the (unsorted) table */
struct wgTable_entry *
wgTable_createEntry(long  wgIndex) {
    struct wgTable_entry *entry;

    entry = SNMP_MALLOC_TYPEDEF(struct wgTable_entry);
    if (!entry)
        return NULL;

    entry->wgIndex = wgIndex;
    entry->next = wgTable_head;
    wgTable_head = entry;
    return entry;
}

/* remove a row from the table */
void
wgTable_removeEntry( struct wgTable_entry *entry ) {
    struct wgTable_entry *ptr, *prev;

    if (!entry)
        return;    /* Nothing to remove */

    for ( ptr  = wgTable_head, prev = NULL;
          ptr != NULL;
          prev = ptr, ptr = ptr->next ) {
        if ( ptr == entry )
            break;
    }
    if ( !ptr )
        return;    /* Can't find it */

    if ( prev == NULL )
        wgTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE( entry );   /* XXX - release any other internal resources */
}


/* Example iterator hook routines - using 'get_next' to do most of the work */
netsnmp_variable_list *
wgTable_get_first_data_point(void **my_loop_context,
                          void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{
    *my_loop_context = wgTable_head;
    return wgTable_get_next_data_point(my_loop_context, my_data_context,
                                    put_index_data,  mydata );
}

netsnmp_variable_list *
wgTable_get_next_data_point(void **my_loop_context,
                          void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{
    struct wgTable_entry *entry = (struct wgTable_entry *)*my_loop_context;
    netsnmp_variable_list *idx = put_index_data;

    if ( entry ) {
        snmp_set_var_typed_integer( idx, ASN_INTEGER, entry->wgIndex );
        idx = idx->next_variable;
        *my_data_context = (void *)entry;
        *my_loop_context = (void *)entry->next;
        return put_index_data;
    } else {
        return NULL;
    }
}


/** handles requests for the wgTable table */
int
wgTable_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;
    struct wgTable_entry          *table_entry;
    int ret;

    DEBUGMSGTL(("wgTable:handler", "Processing request (%d)\n", reqinfo->mode));

    switch (reqinfo->mode) {
        /*
         * Read-support (also covers GetNext requests)
         */
    case MODE_GET:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            switch (table_info->colnum) {
            case COLUMN_WGNAME:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                printf("GET COULMN_WGNAME, table_entry %p: te->wgName %s, \
                te->wgName_len %lu\n", table_entry, table_entry->wgName, \
                table_entry->wgName_len);
                //TODO: get the values from kairos via some api and update the \
                table_entry->wgName
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->wgName,
                                          table_entry->wgName_len);
                break;
            case COLUMN_WGCHAIR1:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                printf("GET COULMN_WGCHAIR1, table_entry %p: te->wgChair1 %s, \
te->wgChair1_len %lu\n", table_entry, table_entry->wgChair1, \
                table_entry->wgChair1_len);
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->wgChair1,
                                          table_entry->wgChair1_len);
                break;
            case COLUMN_WGCHAIR2:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                printf("GET COULMN_WGCHAIR2, table_entry %p: te->wgChair2 %s, \
te->wgChair2_len %lu\n", table_entry, table_entry->wgChair2, \
                table_entry->wgChair2_len);
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->wgChair2,
                                          table_entry->wgChair2_len);
                break;
            case COLUMN_WGPRIO:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                printf("GET COULMN_WGPRIO, table_entry %p: te->wgPrio %ld\n", \
                table_entry, table_entry->wgPrio);
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->wgPrio);
                break;
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;

        /*
         * Write-support
         */
    case MODE_SET_RESERVE1:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            printf("MODE_SET_RESERVE1, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);
            switch (table_info->colnum) {
            case COLUMN_WGNAME:
	        /* or possibly 'netsnmp_check_vb_type_and_size' */
                ret = netsnmp_check_vb_type_and_max_size(
                          request->requestvb, ASN_OCTET_STR, \
sizeof(table_entry->wgName));  if ( ret != SNMP_ERR_NOERROR ) {
                    netsnmp_set_request_error( reqinfo, request, ret );
                    return SNMP_ERR_NOERROR;
                }
                break;
            case COLUMN_WGCHAIR1:
	        /* or possibly 'netsnmp_check_vb_type_and_size' */
                ret = netsnmp_check_vb_type_and_max_size(
                          request->requestvb, ASN_OCTET_STR, \
sizeof(table_entry->wgChair1));  if ( ret != SNMP_ERR_NOERROR ) {
                    netsnmp_set_request_error( reqinfo, request, ret );
                    return SNMP_ERR_NOERROR;
                }
                break;
            case COLUMN_WGCHAIR2:
	        /* or possibly 'netsnmp_check_vb_type_and_size' */
                ret = netsnmp_check_vb_type_and_max_size(
                          request->requestvb, ASN_OCTET_STR, \
sizeof(table_entry->wgChair2));  if ( ret != SNMP_ERR_NOERROR ) {
                    netsnmp_set_request_error( reqinfo, request, ret );
                    return SNMP_ERR_NOERROR;
                }
                break;
            case COLUMN_WGPRIO:
                /* or possibly 'netsnmp_check_vb_int_range' */
                ret = netsnmp_check_vb_int( request->requestvb );
                if ( ret != SNMP_ERR_NOERROR ) {
                    netsnmp_set_request_error( reqinfo, request, ret );
                    return SNMP_ERR_NOERROR;
                }
                break;
            default:
                netsnmp_set_request_error( reqinfo, request,
                                           SNMP_ERR_NOTWRITABLE );
                return SNMP_ERR_NOERROR;
            }
        }
        break;

    case MODE_SET_RESERVE2:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            printf("MODE_SET_RESERVE2, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);
            switch (table_info->colnum) {
            case COLUMN_WGNAME:
            case COLUMN_WGCHAIR1:
            case COLUMN_WGCHAIR2:
            case COLUMN_WGPRIO:
                if ( !table_entry ) {
                    table_entry = \
wgTable_createEntry(*table_info->indexes->val.integer);  if (table_entry) {
                        netsnmp_insert_iterator_context( request, table_entry );
                        printf("MODE_SET_RESERVE2, table_entry %p with request %p, \
                requestvb->val.string %s inserted in the iterator context\n",
                               table_entry, request, request->requestvb->val.string);
                    } else {
                        netsnmp_set_request_error( reqinfo, request,
                                                   SNMP_ERR_RESOURCEUNAVAILABLE );
                        return SNMP_ERR_NOERROR;
                    }
                }
                break;
            }
        }
        break;

    case MODE_SET_FREE:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);
            printf("MODE_SET_FREE, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);

            switch (table_info->colnum) {
            case COLUMN_WGNAME:
            case COLUMN_WGCHAIR1:
            case COLUMN_WGCHAIR2:
            case COLUMN_WGPRIO:
                if ( table_entry && !table_entry->valid ) {
                    wgTable_removeEntry(table_entry);
                }
                break;
            }
        }
        break;

    case MODE_SET_ACTION:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            printf("MODE_SET_ACTION, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);
            switch (table_info->colnum) {
            case COLUMN_WGNAME:
                memcpy( table_entry->old_wgName,
                        table_entry->wgName,
                        sizeof(table_entry->wgName));
                table_entry->old_wgName_len =
                        table_entry->wgName_len;
                memset( table_entry->wgName, 0,
                        sizeof(table_entry->wgName));
                memcpy( table_entry->wgName,
                        request->requestvb->val.string,
                        request->requestvb->val_len);
                table_entry->wgName_len =
                        request->requestvb->val_len;
                printf("MODE_SET_ACTION, wgName %s written to table entry %p\n", \
request->requestvb->val.string, table_entry);  break;
            case COLUMN_WGCHAIR1:
                memcpy( table_entry->old_wgChair1,
                        table_entry->wgChair1,
                        sizeof(table_entry->wgChair1));
                table_entry->old_wgChair1_len =
                        table_entry->wgChair1_len;
                memset( table_entry->wgChair1, 0,
                        sizeof(table_entry->wgChair1));
                memcpy( table_entry->wgChair1,
                        request->requestvb->val.string,
                        request->requestvb->val_len);
                table_entry->wgChair1_len =
                        request->requestvb->val_len;
                printf("MODE_SET_ACTION, wgChair1 %s written to table entry %p\n", \
request->requestvb->val.string, table_entry);  break;
            case COLUMN_WGCHAIR2:
                memcpy( table_entry->old_wgChair2,
                        table_entry->wgChair2,
                        sizeof(table_entry->wgChair2));
                table_entry->old_wgChair2_len =
                        table_entry->wgChair2_len;
                memset( table_entry->wgChair2, 0,
                        sizeof(table_entry->wgChair2));
                memcpy( table_entry->wgChair2,
                        request->requestvb->val.string,
                        request->requestvb->val_len);
                table_entry->wgChair2_len =
                        request->requestvb->val_len;
                printf("MODE_SET_ACTION, wgChair2 %s written to table entry %p\n", \
request->requestvb->val.string, table_entry);  break;
            case COLUMN_WGPRIO:
                table_entry->old_wgPrio = table_entry->wgPrio;
                table_entry->wgPrio     = *request->requestvb->val.integer;
                printf("MODE_SET_ACTION, wgPrio %ld written to table entry %p\n", \
*request->requestvb->val.integer, table_entry);  break;
            }
        }
        break;

    case MODE_SET_UNDO:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            printf("MODE_SET_UNDO, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);
            switch (table_info->colnum) {
            case COLUMN_WGNAME:
                if ( table_entry && !table_entry->valid ) {
                    wgTable_removeEntry(table_entry );
                } else {
                    memcpy( table_entry->wgName,
                            table_entry->old_wgName,
                            sizeof(table_entry->wgName));
                    memset( table_entry->old_wgName, 0,
                            sizeof(table_entry->wgName));
                    table_entry->wgName_len =
                            table_entry->old_wgName_len;
                }
                break;
            case COLUMN_WGCHAIR1:
                if ( table_entry && !table_entry->valid ) {
                    wgTable_removeEntry(table_entry);
                } else {
                    memcpy( table_entry->wgChair1,
                            table_entry->old_wgChair1,
                            sizeof(table_entry->wgChair1));
                    memset( table_entry->old_wgChair1, 0,
                            sizeof(table_entry->wgChair1));
                    table_entry->wgChair1_len =
                            table_entry->old_wgChair1_len;
                }
                break;
            case COLUMN_WGCHAIR2:
                if ( table_entry && !table_entry->valid ) {
                    wgTable_removeEntry(table_entry);
                } else {
                    memcpy( table_entry->wgChair2,
                            table_entry->old_wgChair2,
                            sizeof(table_entry->wgChair2));
                    memset( table_entry->old_wgChair2, 0,
                            sizeof(table_entry->wgChair2));
                    table_entry->wgChair2_len =
                            table_entry->old_wgChair2_len;
                }
                break;
            case COLUMN_WGPRIO:
                if ( table_entry && !table_entry->valid ) {
                    wgTable_removeEntry(table_entry);
                } else {
                    table_entry->wgPrio     = table_entry->old_wgPrio;
                    table_entry->old_wgPrio = 0;
                }
                break;
            }
        }
        break;

    case MODE_SET_COMMIT:
        for (request=requests; request; request=request->next) {
            table_entry = (struct wgTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);

            printf("MODE_SET_COMMIT, table_entry %p with request %p, \
requestvb->val.string %s\n",  table_entry, request, request->requestvb->val.string);
            switch (table_info->colnum) {
            case COLUMN_WGNAME:
            case COLUMN_WGCHAIR1:
            case COLUMN_WGCHAIR2:
            case COLUMN_WGPRIO:
                if ( table_entry && !table_entry->valid ) {
                    table_entry->valid = 1;
                }
            }
        }
        break;
    }
    return SNMP_ERR_NOERROR;
}

long init_wgTable_from_file(const char* initFile) {
    FILE* pFile;
    char line[MAX_LINE_SIZE];
    memset(line, '\0', MAX_LINE_SIZE);
    pFile = fopen(initFile, "r");
    if (pFile == NULL) {
        return MFD_RESOURCE_UNAVAILABLE;
    }

    struct wgTable_entry* wgEntry = NULL;
    char* textLine;
    while (1) {
        do {
            if (!fgets(line, sizeof(line), pFile)) {
                fclose(pFile);
                pFile = NULL;
            }
        } while (pFile && line[0] == '\n');

        //check for end of data
        if (pFile == NULL) break;
        //printf("line found : %s\n", line);
        //parse the line based on spaces and store it in the temp wgTable_entry
        textLine = line;
        char* token = strtok(textLine, "\n");
        token = strtok(token, " ");
        uint32_t tokenIndex = 0;
        long tableIndex = 0;
        while ( token != NULL ) {
            switch (tokenIndex) {
                case 0 :
                    tableIndex = strtol(token, NULL, 10);
                    if (tableIndex > 0) {
                        wgEntry = wgTable_createEntry(tableIndex);
                        if (wgEntry) {
                            //wgEntry->wgIndex = tableIndex; //already initialized in \
                the wgTable_createEntry
                            printf("tokenIndex %u : wgIndex %ld\n", tokenIndex, \
wgEntry->wgIndex);  }
                        else {
                            printf("ERROR: cannot create wgTable entry: Resource \
unavailable\n");  return MFD_RESOURCE_UNAVAILABLE;
                        }
                    }
                    else {
                        printf("Invalid table index %ld\n", tableIndex);
                        return MFD_ERROR;
                    }
                    break;

                case 1 :
                    if (wgEntry) {
                        strcpy(wgEntry->wgName, token);
                        wgEntry->wgName_len = strlen(token);
                        printf("tokenIndex %u : wgName %s wgName_len, \n", \
tokenIndex, wgEntry->wgName);  }
                    break;

                case 2 :
                    if (wgEntry) {
                        strcpy(wgEntry->wgChair1, token);
                        wgEntry->wgChair1_len = strlen(token);
                        printf("tokenIndex %u : wgChair1 %s\n", tokenIndex, \
wgEntry->wgChair1);  }
                    break;

                case 3 :
                    if (wgEntry) {
                        strcpy(wgEntry->wgChair2, token);
                        wgEntry->wgChair2_len = strlen(token);
                        printf("tokenIndex %u : wgChair2 %s\n", tokenIndex, \
wgEntry->wgChair2);  }
                    break;

                case 4 :
                    if (wgEntry) {
                        wgEntry->wgPrio = strtol(token, NULL, 10);
                        printf("tokenIndex %u : wgPrio %ld\n", tokenIndex, \
wgEntry->wgPrio);  }
                    break;

                default :
                    printf("token %u : %s\n", tokenIndex, token);
                    break;
            }
            ++tokenIndex;
            token = strtok(NULL, " ");
        }

        //insert the entry in the iterator
        if (wgEntry) {
            //netsnmp_insert_iterator_context( NULL, wgEntry );
            //printf("INIT TABLE, table_entry %p inserted in the iterator \
context\n\n", wgEntry);  }
        else {
            printf("ERROR: cannot insert wgTable entry: Resource unavailable\n");
            return MFD_RESOURCE_UNAVAILABLE;
        }
    }

    return MFD_SUCCESS;
}





_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic