mib2c.iterate.conf
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:18k
源码类别:

SNMP编程

开发平台:

Unix_Linux

  1. ## -*- c -*-
  2. ######################################################################
  3. ## Do the .h file
  4. ######################################################################
  5. @open ${name}.h@
  6. /*
  7.  * Note: this file originally auto-generated by mib2c using
  8.  *  $Id: mib2c.iterate.conf,v 5.14.2.2 2005/05/09 08:13:01 dts12 Exp $
  9.  */
  10. #ifndef $name.uc_H
  11. #define $name.uc_H
  12. /* function declarations */
  13. void init_$name(void);
  14. @foreach $i table@
  15. void initialize_table_$i(void);
  16. Netsnmp_Node_Handler ${i}_handler;
  17. Netsnmp_First_Data_Point  ${i}_get_first_data_point;
  18. Netsnmp_Next_Data_Point   ${i}_get_next_data_point;
  19. @if "$cache" ne "" @
  20. NetsnmpCacheLoad ${i}_load;
  21. NetsnmpCacheFree ${i}_free;
  22. #define $i.uc_TIMEOUT  60
  23. @end@
  24. @end@
  25. @foreach $i table@
  26. /* column number definitions for table $i */
  27.     @foreach $c column@
  28.        #define COLUMN_$c.uc $c.subid
  29.     @end@
  30. @end@
  31. #endif /* $name.uc_H */
  32. ######################################################################
  33. ## Do the .c file
  34. ######################################################################
  35. @open ${name}.c@
  36. /*
  37.  * Note: this file originally auto-generated by mib2c using
  38.  *  $Id: mib2c.iterate.conf,v 5.14.2.2 2005/05/09 08:13:01 dts12 Exp $
  39.  */
  40. #include <net-snmp/net-snmp-config.h>
  41. #include <net-snmp/net-snmp-includes.h>
  42. #include <net-snmp/agent/net-snmp-agent-includes.h>
  43. #include "${name}.h"
  44. /** Initializes the $name module */
  45. void
  46. init_$name(void)
  47. {
  48.   /* here we initialize all the tables we're planning on supporting */
  49.   @foreach $i table@
  50.     initialize_table_$i();
  51.   @end@
  52. }
  53. @foreach $i table@
  54. /** Initialize the $i table by defining its contents and how it's structured */
  55. void
  56. initialize_table_$i(void)
  57. {
  58.     static oid ${i}_oid[] = {$i.commaoid};
  59.     size_t ${i}_oid_len   = OID_LENGTH(${i}_oid);
  60.     netsnmp_handler_registration    *reg;
  61.     netsnmp_iterator_info           *iinfo;
  62.     netsnmp_table_registration_info *table_info;
  63.     reg = netsnmp_create_handler_registration(
  64.               "$i",     ${i}_handler,
  65.               ${i}_oid, ${i}_oid_len,
  66. @if $i.settable@
  67.               HANDLER_CAN_RWRITE
  68. @else@
  69.               HANDLER_CAN_RONLY
  70. @end@
  71.               );
  72.     table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
  73.     netsnmp_table_helper_add_indexes(table_info,
  74.     @foreach $idx index@
  75.                            $idx.type,  /* index: $idx */
  76.     @end@
  77.                            0);
  78.     table_info->min_column = XXX;
  79.     table_info->max_column = YYY;
  80.     
  81.     iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
  82.     iinfo->get_first_data_point = ${i}_get_first_data_point;
  83.     iinfo->get_next_data_point  = ${i}_get_next_data_point;
  84.     iinfo->table_reginfo        = table_info;
  85.     
  86.     netsnmp_register_table_iterator( reg, iinfo );
  87. @if "$cache" ne "" @
  88.     netsnmp_inject_handler_before( reg,
  89.         netsnmp_get_cache_handler($i.uc_TIMEOUT,
  90.                                   ${i}_load, ${i}_free,
  91.                                   ${i}_oid, ${i}_oid_len),
  92.             TABLE_ITERATOR_NAME);
  93. @end@
  94.     /* Initialise the contents of the table here */
  95. }
  96.     /* Typical data structure for a row entry */
  97. struct ${i}_entry {
  98.     /* Index values */
  99.     @foreach $idx index@
  100.     $idx.decl $idx;
  101.     @end@
  102.     /* Column values */
  103.     @foreach $c column@
  104.     @if $c.readable@
  105.     $c.decl $c;
  106.      @if $c.settable@
  107.       @if !$c.rowstatus@
  108.     $c.decl old_$c;
  109.       @end@
  110.      @end@
  111.     @end@
  112.     @end@
  113.     /* Illustrate using a simple linked list */
  114.     int   valid;
  115.     struct ${i}_entry *next;
  116. };
  117. struct ${i}_entry  *${i}_head;
  118. /* create a new row in the (unsorted) table */
  119. struct ${i}_entry *
  120. ${i}_createEntry(
  121.   @foreach $idx index@
  122.                  $idx.decl  $idx,
  123.   @end@
  124.                 ) {
  125.     struct ${i}_entry *entry;
  126.     entry = SNMP_TYPEDEF_MALLOC(struct ${i}_entry);
  127.     if (!entry)
  128.         return NULL;
  129.   @foreach $idx index@
  130.     entry->$idx = $idx;
  131.   @end@
  132.     entry->next = ${i}_head;
  133.     ${i}_head = entry;
  134.     return entry;
  135. }
  136. /* remove a row from the table */
  137. void
  138. ${i}_removeEntry( struct ${i}_entry *entry ) {
  139.     struct ${i}_entry *ptr, *prev;
  140.     if (!entry)
  141.         return;    /* Nothing to remove */
  142.     for ( ptr  = ${i}_head, prev = NULL;
  143.           ptr != NULL;
  144.           prev = ptr, ptr = ptr->next ) {
  145.         if ( ptr == entry )
  146.             break;
  147.     }
  148.     if ( !ptr )
  149.         return;    /* Can't find it */
  150.     if ( prev == NULL )
  151.         ${i}_head = ptr->next;
  152.     else
  153.         prev->next = ptr->next;
  154.     SNMP_FREE( entry );   /* XXX - release any other internal resources */
  155. }
  156. @if "$cache" ne "" @
  157. /* Example cache handling - set up linked list from a suitable file */
  158. int
  159. ${i}_load( netsnmp_cache *cache, void *vmagic ) {
  160.     FILE *fp;
  161.     struct ${i}_entry *this;
  162.     char buf[STRMAX];
  163.     fp = fopen( "/data/for/${i}", "r" );
  164.     while ( fgets( buf, STRMAX, fp )) {
  165.         this = SNMP_MALLOC_TYPEDEF( struct ${i}_entry );
  166.         /* Unpick 'buf' and populate 'this' */
  167.         this->next = ${i}_head;
  168.         ${i}_head = this;    /* Iterate helper is fine with unordered lists! */+    }
  169.     fclose(fp);
  170. }
  171. int
  172. ${i}_free( netsnmp_cache *cache, void *vmagic ) {
  173.     struct ${i}_entry *this, *that;
  174.     for ( this = ${i}_head; this; this=that ) {
  175.         that = this->next;
  176.         SNMP_FREE( this );   /* XXX - release any other internal resources */
  177.     }
  178.     ${i}_head = NULL;
  179. }
  180. @end@
  181. /* Example iterator hook routines - using 'get_next' to do most of the work */
  182. netsnmp_variable_list *
  183. ${i}_get_first_data_point(void **my_loop_context,
  184.                           void **my_data_context,
  185.                           netsnmp_variable_list *put_index_data,
  186.                           netsnmp_iterator_info *mydata)
  187. {
  188.     *my_loop_context = ${i}_head;
  189.     return ${i}_get_next_data_point(my_loop_context, my_data_context,
  190.                                     put_index_data,  mydata );
  191. }
  192. netsnmp_variable_list *
  193. ${i}_get_next_data_point(void **my_loop_context,
  194.                           void **my_data_context,
  195.                           netsnmp_variable_list *put_index_data,
  196.                           netsnmp_iterator_info *mydata)
  197. {
  198.     struct ${i}_entry *entry = (struct ${i}_entry *)*my_loop_context;
  199.     netsnmp_variable_list *idx = put_index_data;
  200.     if ( entry ) {
  201.       @foreach $idx index@
  202.         snmp_set_var_value( idx, entry->${idx}, sizeof(entry->${idx}) );
  203.         idx = idx->next_variable;
  204.       @end@
  205.         *my_data_context = (void *)entry;
  206.         *my_loop_context = (void *)entry->next;
  207.     } else {
  208.         return NULL;
  209.     }
  210. }
  211. /** handles requests for the $i table */
  212. int
  213. ${i}_handler(
  214.     netsnmp_mib_handler               *handler,
  215.     netsnmp_handler_registration      *reginfo,
  216.     netsnmp_agent_request_info        *reqinfo,
  217.     netsnmp_request_info              *requests) {
  218.     netsnmp_request_info       *request;
  219.     netsnmp_table_request_info *table_info;
  220.     struct ${i}_entry          *table_entry;
  221.     switch (reqinfo->mode) {
  222.         /*
  223.          * Read-support (also covers GetNext requests)
  224.          */
  225.     case MODE_GET:
  226.         for (request=requests; request; request=request->next) {
  227.             table_entry = (struct ${i}_entry *)
  228.                               netsnmp_extract_iterator_context(request);
  229.             table_info  =     netsnmp_extract_table_info(      request);
  230.     
  231.             switch (table_info->colnum) {
  232.             @foreach $c column@
  233.             @if $c.readable@
  234.             case COLUMN_$c.uc:
  235.                 snmp_set_var_typed_value( request->requestvb, $c.type,
  236.                                           table_entry->$c,
  237.                                           sizeof(table_entry->$c));
  238.                 break;
  239.             @end@
  240.             @end@
  241.             }
  242.         }
  243.         break;
  244. @if $i.settable@
  245.         /*
  246.          * Write-support
  247.          */
  248.     case MODE_SET_RESERVE1:
  249.         for (request=requests; request; request=request->next) {
  250.             table_entry = (struct ${i}_entry *)
  251.                               netsnmp_extract_iterator_context(request);
  252.             table_info  =     netsnmp_extract_table_info(      request);
  253.     
  254.             switch (table_info->colnum) {
  255.             @foreach $c column@
  256.             @if $c.settable@
  257.             case COLUMN_$c.uc:
  258.                 if ( request->requestvb->type != $c.type ) {
  259.                     netsnmp_set_request_error( reqinfo, request,
  260.                                                SNMP_ERR_WRONGTYPE );
  261.                     return SNMP_ERR_NOERROR;
  262.                 }
  263.                 /* Also may need to check size/value */
  264.             @if $c.rowstatus@
  265.                 switch (*request->requestvb->val.integer) {
  266.                 case RS_ACTIVE:
  267.                 case RS_NOTINSERVICE:
  268.                     if (!table_entry) {
  269.                         netsnmp_set_request_error( reqinfo, request,
  270.                                                    SNMP_ERR_INCONSISTENTVALUE );
  271.                         return SNMP_ERR_NOERROR;
  272.                     }
  273.                     break;
  274.                 case RS_CREATEANDGO:
  275.                 case RS_CREATEANDWAIT:
  276.                     if (table_entry) {
  277.                         netsnmp_set_request_error( reqinfo, request,
  278.                                                    SNMP_ERR_INCONSISTENTVALUE );
  279.                         return SNMP_ERR_NOERROR;
  280.                     }
  281.                     break;
  282.                 case RS_DESTROY:
  283.                     /* Valid in all circumstances */
  284.                     break;
  285.                 case RS_NOTREADY:
  286.                 default:
  287.                     netsnmp_set_request_error( reqinfo, request,
  288.                                                SNMP_ERR_WRONGVALUE );
  289.                     return SNMP_ERR_NOERROR;
  290.                     break;
  291.                 }
  292.             @end@
  293.                 break;
  294.             @end@
  295.             @end@
  296.             default:
  297.                 netsnmp_set_request_error( reqinfo, request,
  298.                                            SNMP_ERR_NOTWRITABLE );
  299.                 return SNMP_ERR_NOERROR;
  300.             }
  301.         }
  302.         break;
  303.     case MODE_SET_RESERVE2:
  304. @if $i.creatable@
  305.         for (request=requests; request; request=request->next) {
  306.             table_entry = (struct ${i}_entry *)
  307.                               netsnmp_extract_iterator_context(request);
  308.             table_info  =     netsnmp_extract_table_info(      request);
  309.     
  310.             switch (table_info->colnum) {
  311. @if $i.rowstatus@
  312.             @foreach $c column@
  313.             @if $c.rowstatus@
  314.             case COLUMN_$c.uc:
  315.                 switch (*request->requestvb->val.integer) {
  316.                 case RS_CREATEANDGO:
  317.                 case RS_CREATEANDWAIT:
  318.                     table_row = ${i}_createEntry(
  319.   @foreach $idx index@
  320.                         , table_info->indexes->val.YYY
  321.   @end@
  322.                         );
  323.                     if (table_row) {
  324.                         netsnmp_insert_table_row( request, table_row );
  325.                     } else {
  326.                         netsnmp_set_request_error( reqinfo, request,
  327.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  328.                         return SNMP_ERR_NOERROR;
  329.                     }
  330.                 }
  331.             @end@
  332.             @end@
  333. @else@
  334.             @foreach $c column@
  335.             @if $c.creatable@
  336.             case COLUMN_$c.uc:
  337.             @end@
  338.             @end@
  339.                 if ( !table_row ) {
  340.                     table_row = ${i}_createEntry(
  341.   @foreach $idx index@
  342.                         , table_info->indexes->val.YYY
  343.   @end@
  344.                         );
  345.                     if (table_row) {
  346.                         netsnmp_insert_table_row( request, table_row );
  347.                     } else {
  348.                         netsnmp_set_request_error( reqinfo, request,
  349.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  350.                         return SNMP_ERR_NOERROR;
  351.                     }
  352.                 }
  353.                 break;
  354. @end@
  355.             }
  356.         }
  357. @end@
  358.         break;
  359.     case MODE_SET_FREE:
  360. @if $i.creatable@
  361.         for (request=requests; request; request=request->next) {
  362.             table_entry = (struct ${i}_entry *)
  363.                               netsnmp_extract_iterator_context(request);
  364.             table_info  =     netsnmp_extract_table_info(      request);
  365.     
  366.             switch (table_info->colnum) {
  367. @if $i.rowstatus@
  368.             @foreach $c column@
  369.             @if $c.rowstatus@
  370.             case COLUMN_$c.uc:
  371.                 switch (*request->requestvb->val.integer) {
  372.                 case RS_CREATEANDGO:
  373.                 case RS_CREATEANDWAIT:
  374.                     if (table_entry && !table_entry->valid) {
  375.                         ${i}_removeEntry(table_data, table_row );
  376.                     }
  377.                 }
  378.             @end@
  379.             @end@
  380. @else@
  381.             @foreach $c column@
  382.             @if $c.creatable@
  383.             case COLUMN_$c.uc:
  384.             @end@
  385.             @end@
  386.                 if ( table_entry && !table_entry->valid ) {
  387.                     ${i}_removeEntry(table_data, table_row );
  388.                 }
  389.                 break;
  390. @end@
  391.             }
  392.         }
  393. @end@
  394.         break;
  395.     case MODE_SET_ACTION:
  396.         for (request=requests; request; request=request->next) {
  397.             table_entry = (struct ${i}_entry *)
  398.                               netsnmp_extract_iterator_context(request);
  399.             table_info  =     netsnmp_extract_table_info(      request);
  400.     
  401.             switch (table_info->colnum) {
  402.             @foreach $c column@
  403.             @if $c.settable@
  404.             @if !$c.rowstatus@
  405.             case COLUMN_$c.uc:
  406.                 /* Need to save old 'table_entry->$c' value.
  407.                    May need to use 'memcpy' */
  408.                 table_entry->old_$c = table_entry->$c;
  409.                 table_entry->$c     = request->requestvb->val.YYY;
  410.                 break;
  411.             @end@
  412.             @end@
  413.             @end@
  414.             }
  415.         }
  416. @if $i.rowstatus@
  417.         /* Check the internal consistency of an active row */
  418.         for (request=requests; request; request=request->next) {
  419.             table_entry = (struct ${i}_entry *)
  420.                               netsnmp_extract_iterator_context(request);
  421.             table_info  =     netsnmp_extract_table_info(      request);
  422.     
  423.             switch (table_info->colnum) {
  424.             @foreach $c column@
  425.             @if $c.rowstatus@
  426.             case COLUMN_$c.uc:
  427.                 switch (*request->requestvb->val.integer) {
  428.                 case RS_ACTIVE:
  429.                 case RS_CREATEANDGO:
  430.                     if (/* XXX */) {
  431.                         netsnmp_set_request_error( reqinfo, request,
  432.                                                    SNMP_ERR_INCONSISTENTVALUE );
  433.                         return SNMP_ERR_NOERROR;
  434.                     }
  435.                 }
  436.             @end@
  437.             @end@
  438.             }
  439.         }
  440. @end@
  441.         break;
  442.     case MODE_SET_UNDO:
  443.         for (request=requests; request; request=request->next) {
  444.             table_entry = (struct ${i}_entry *)
  445.                               netsnmp_extract_iterator_context(request);
  446.             table_info  =     netsnmp_extract_table_info(      request);
  447.     
  448.             switch (table_info->colnum) {
  449.             @foreach $c column@
  450.             @if $c.settable@
  451.             case COLUMN_$c.uc:
  452. @if $i.rowstatus@
  453.   @if $c.rowstatus@
  454.                 switch (*request->requestvb->val.integer) {
  455.                 case RS_CREATEANDGO:
  456.                 case RS_CREATEANDWAIT:
  457.                     if (table_entry && !table_entry->valid) {
  458.                         ${i}_removeEntry(table_data, table_row );
  459.                     }
  460.                 }
  461.   @else@
  462.                 /* Need to restore old 'table_entry->$c' value.
  463.                    May need to use 'memcpy' */
  464.                 table_entry->$c = table_entry->old_$c;
  465.   @end@
  466. @else@
  467.   @if $c.creatable@
  468.                 if ( table_entry && !table_entry->valid ) {
  469.                     ${i}_removeEntry(table_data, table_row );
  470.                 } else {
  471.                     /* Need to restore old 'table_entry->$c' value.
  472.                        May need to use 'memcpy' */
  473.                     table_entry->$c = table_entry->old_$c;
  474.                 }
  475.   @else@
  476.                 /* Need to restore old 'table_entry->$c' value.
  477.                    May need to use 'memcpy' */
  478.                 table_entry->$c = table_entry->old_$c;
  479.   @end@
  480. @end@
  481.                 break;
  482.             @end@
  483.             @end@
  484.             }
  485.         }
  486.         break;
  487.     case MODE_SET_COMMIT:
  488. @if $i.creatable@
  489.         for (request=requests; request; request=request->next) {
  490.             table_entry = (struct ${i}_entry *)
  491.                               netsnmp_extract_iterator_context(request);
  492.             table_info  =     netsnmp_extract_table_info(      request);
  493.     
  494.             switch (table_info->colnum) {
  495. @if $i.rowstatus@
  496.             @foreach $c column@
  497.             @if $c.rowstatus@
  498.             case COLUMN_$c.uc:
  499.                 switch (*request->requestvb->val.integer) {
  500.                 case RS_CREATEANDGO:
  501.                     table_entry->valid = 1;
  502.                     /* Fall-through */
  503.                 case RS_ACTIVE:
  504.                     table_entry->$c = RS_ACTIVE;
  505.                     break;
  506.                 case RS_CREATEANDWAIT:
  507.                     table_entry->valid = 1;
  508.                     /* Fall-through */
  509.                 case RS_NOTINSERVICE:
  510.                     table_entry->$c = RS_NOTINSERVICE;
  511.                     break;
  512.                 case RS_DESTROY:
  513.                     ${i}_removeEntry(table_data, table_row );
  514.                 }
  515.             @end@
  516.             @end@
  517. @else@
  518.             @foreach $c column@
  519.             @if $c.creatable@
  520.             case COLUMN_$c.uc:
  521.             @end@
  522.             @end@
  523.                 if ( table_entry && !table_entry->valid ) {
  524.                     table_entry->valid = 1;
  525.                 }
  526. @end@
  527.             }
  528.         }
  529. @end@
  530.         break;
  531. @end@
  532.     }
  533.     return SNMP_ERR_NOERROR;
  534. }
  535. @end@