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

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_access.conf,v 1.11 2004/08/31 10:23:16 dts12 Exp $
  9.  */
  10. #ifndef $name.uc_H
  11. #define $name.uc_H
  12. /** other required module components */
  13. config_require(${name}_access)
  14. config_require(${name}_checkfns)
  15. /* function declarations */
  16. void init_$name(void);
  17. @foreach $i table@
  18. void initialize_table_$i(void);
  19. Netsnmp_Node_Handler ${i}_handler;
  20. @end@
  21. @foreach $i table@
  22. /* column number definitions for table $i */
  23. #include "${name}_columns.h"
  24. @run mib2c.column_defines.conf@
  25. /* enum definions */
  26. #include "${name}_enums.h"
  27. @run mib2c.column_enums.conf@
  28. @end@
  29. #endif /** $name.uc_H */
  30. ######################################################################
  31. ## Do the .c file
  32. ######################################################################
  33. @open ${name}.c@
  34. /*
  35.  * Note: this file originally auto-generated by mib2c using
  36.  *        $Id: mib2c.iterate_access.conf,v 1.11 2004/08/31 10:23:16 dts12 Exp $
  37.  */
  38. #include <net-snmp/net-snmp-config.h>
  39. #include <net-snmp/net-snmp-includes.h>
  40. #include <net-snmp/agent/net-snmp-agent-includes.h>
  41. #include "${name}.h"
  42. #include "${name}_checkfns.h"
  43. #include "${name}_access.h"
  44. static netsnmp_oid_stash_node *undoStorage = NULL;
  45. static netsnmp_oid_stash_node *commitStorage = NULL;
  46. struct undoInfo {
  47.    void *ptr;
  48.    size_t len;
  49. };
  50. struct commitInfo {
  51.    void *data_context;
  52.    int have_committed;
  53.    int new_row;
  54. };
  55. void
  56. ${name}_free_undoInfo(void *vptr) {
  57.     struct undoInfo *ui = vptr;
  58.     if (!ui)
  59.         return;
  60.     SNMP_FREE(ui->ptr);
  61.     SNMP_FREE(ui);
  62. }
  63. @foreach $i table@
  64. /** Initialize the $i table by defining its contents and how it's structured */
  65. void
  66. initialize_table_$i(void)
  67. {
  68.     static oid ${i}_oid[] = {$i.commaoid};
  69.     netsnmp_table_registration_info *table_info;
  70.     netsnmp_handler_registration *my_handler;
  71.     netsnmp_iterator_info *iinfo;
  72.     /** create the table registration information structures */
  73.     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  74.     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
  75.     my_handler = netsnmp_create_handler_registration("$i",
  76.                                              ${i}_handler,
  77.                                              ${i}_oid,
  78.                                              OID_LENGTH(${i}_oid),
  79. @if $i.settable@
  80.                                              HANDLER_CAN_RWRITE
  81. @else@
  82.                                              HANDLER_CAN_RONLY
  83. @end@
  84.                                              );
  85.             
  86.     if (!my_handler || !table_info || !iinfo) {
  87.         snmp_log(LOG_ERR, "malloc failed in initialize_table_$i");
  88.         return; /** Serious error. */
  89.     }
  90.     /***************************************************
  91.      * Setting up the table's definition
  92.      */
  93.     netsnmp_table_helper_add_indexes(table_info,
  94.     @foreach $idx index@
  95.                                   $idx.type, /** index: $idx */
  96.     @end@
  97.                              0);
  98.     /** Define the minimum and maximum accessible columns.  This
  99.         optimizes retrival. */
  100.     @eval $minv = 0xffffffff@
  101.     @eval $maxv = 0@
  102.     @foreach $c column@
  103.         @if $c.access =~ /(Read|Create)/@
  104.           @eval $minv = min($minv, $c.subid)@
  105.           @eval $maxv = max($maxv, $c.subid)@
  106.         @end@
  107.     @end@
  108.     table_info->min_column = $minv;
  109.     table_info->max_column = $maxv;
  110.     /** iterator access routines */
  111.     iinfo->get_first_data_point = ${i}_get_first_data_point;
  112.     iinfo->get_next_data_point = ${i}_get_next_data_point;
  113.     /** you may wish to set these as well */
  114. #ifdef MAYBE_USE_THESE
  115.     iinfo->make_data_context = ${i}_context_convert_function;
  116.     iinfo->free_data_context = ${i}_data_free;
  117.     /** pick *only* one of these if you use them */
  118.     iinfo->free_loop_context = ${i}_loop_free;
  119.     iinfo->free_loop_context_at_end = ${i}_loop_free;
  120. #endif
  121.     /** tie the two structures together */
  122.     iinfo->table_reginfo = table_info;
  123.     /***************************************************
  124.      * registering the table with the master agent
  125.      */
  126.     DEBUGMSGTL(("initialize_table_$i",
  127.                 "Registering table $i as a table iteratorn"));  
  128.     netsnmp_register_table_iterator(my_handler, iinfo);
  129. }
  130. @end@
  131. /** Initializes the $name module */
  132. void
  133. init_$name(void)
  134. {
  135.   /** here we initialize all the tables we're planning on supporting */
  136.   @foreach $i table@
  137.     initialize_table_$i();
  138.   @end@
  139. }
  140. @foreach $i table@
  141. /** handles requests for the $i table, if anything else needs to be done */
  142. int
  143. ${i}_handler(
  144.     netsnmp_mib_handler               *handler,
  145.     netsnmp_handler_registration      *reginfo,
  146.     netsnmp_agent_request_info        *reqinfo,
  147.     netsnmp_request_info              *requests) {
  148.     netsnmp_request_info *request;
  149.     netsnmp_table_request_info *table_info;
  150.     netsnmp_variable_list *var;
  151.     struct commitInfo *ci = NULL;
  152.     void *data_context = NULL;
  153.     oid *suffix;
  154.     size_t suffix_len;
  155.     /** column and row index encoded portion */
  156.     suffix = requests->requestvb->name + reginfo->rootoid_len + 1;
  157.     suffix_len = requests->requestvb->name_length -
  158.         (reginfo->rootoid_len + 1);
  159.     
  160.     for(request = requests; request; request = request->next) {
  161.         var = request->requestvb;
  162.         if (request->processed != 0)
  163.             continue;
  164.         switch (reqinfo->mode) {
  165.         case MODE_GET:
  166.             data_context =  netsnmp_extract_iterator_context(request);
  167.             if (data_context == NULL) {
  168.                 netsnmp_set_request_error(reqinfo, request,
  169.                                           SNMP_NOSUCHINSTANCE);
  170.                 continue;
  171.             }
  172.             break;
  173. @if $i.settable@
  174.         case MODE_SET_RESERVE1:
  175.             data_context =  netsnmp_extract_iterator_context(request);
  176. @if !$i.creatable@
  177.             if (data_context == NULL) {
  178.                 netsnmp_set_request_error(reqinfo, request,
  179.                                           SNMP_ERR_NOCREATION);
  180.                 continue;
  181.             }
  182. @end@
  183.             break;
  184.         default: /* == the other SET modes */
  185.             ci = netsnmp_oid_stash_get_data(commitStorage,
  186.                                             suffix+1, suffix_len-1);
  187.             break;
  188. @end@
  189.         }
  190.         /** extracts the information about the table from the request */
  191.         table_info = netsnmp_extract_table_info(request);
  192.         /** table_info->colnum contains the column number requested */
  193.         /** table_info->indexes contains a linked list of snmp variable
  194.            bindings for the indexes of the table.  Values in the list
  195.            have been set corresponding to the indexes of the
  196.            request */
  197.         if (table_info == NULL) {
  198.             continue;
  199.         }
  200.         switch(reqinfo->mode) {
  201.             case MODE_GET:
  202.                 switch(table_info->colnum) {
  203.                     @foreach $c column@
  204.                         @if $c.access =~ /(Read|Create)/@
  205.                     case COLUMN_$c.uc:
  206.                             {
  207.                                 $c.decl *retval;
  208.                                 size_t retval_len = 0;
  209.                                 retval = get_$c(data_context, &retval_len);
  210.                                 if (retval)
  211.                                     snmp_set_var_typed_value(var, $c.type,
  212.                                                          (const u_char *) retval,
  213.                                                          retval_len);
  214.                             }
  215.                         break;
  216.                         @end@
  217.                     @end@
  218.                     default:
  219.                 /** We shouldn't get here */
  220.                         snmp_log(LOG_ERR, "problem encountered in ${i}_handler: unknown columnn");
  221.                 }
  222.                 break;
  223. @if $i.settable@
  224.             case MODE_SET_RESERVE1:
  225.                 ci = netsnmp_oid_stash_get_data(commitStorage,
  226.                                                 suffix+1, suffix_len-1);
  227.                 
  228.                 if (!ci) {
  229.                     /** create the commit storage info */
  230.                     ci = SNMP_MALLOC_STRUCT(commitInfo);
  231.                     if (!data_context) {
  232.                         ci->data_context = ${i}_create_data_context(table_info->indexes);
  233.                         ci->new_row = 1;
  234.                     } else {
  235.                         ci->data_context = data_context;
  236.                     }
  237.                     netsnmp_oid_stash_add_data(&commitStorage,
  238.                                                suffix+1, suffix_len-1, ci);
  239.                 }
  240.             break;
  241.                 
  242.             case MODE_SET_RESERVE2:
  243.                 switch(table_info->colnum) {
  244.                     @foreach $c column@
  245.                         @if $c.access =~ /(Write|Create)/@
  246.                           case COLUMN_$c.uc:
  247.                             {
  248.                                 $c.decl *retval;
  249.                                 size_t retval_len = 0;
  250.                                 struct undoInfo *ui = NULL;
  251.                                 int ret;
  252.                                 
  253.                     /** first, get the old value */
  254.                                 retval = get_$c(ci->data_context, &retval_len);
  255.                                 if (retval) {
  256.                                     ui = SNMP_MALLOC_STRUCT(undoInfo);
  257.                                     ui->len = retval_len;
  258.                                     memdup((u_char **) &ui->ptr,
  259.                                            (u_char *) retval,
  260.                                            ui->len);
  261.                                 }
  262.                     /** check the new value, possibly against the
  263.                         older value for a valid state transition */
  264.                                 ret = check_$c(request->requestvb->type,
  265.                                                    ($c.decl *) request->requestvb->val.string,
  266.                                                    request->requestvb->val_len,
  267.                                                    retval, retval_len);
  268.                                 if (ret != 0) {
  269.                                     netsnmp_set_request_error(reqinfo, request,
  270.                                                               ret);
  271.                                     ${name}_free_undoInfo(ui);
  272.                                 } else if (ui) {
  273.                         /** remember information for undo purposes later */
  274.                                     netsnmp_oid_stash_add_data(&undoStorage,
  275.                                                                suffix,
  276.                                                                suffix_len,
  277.                                                                ui);
  278.                                 }
  279.                                 
  280.                             }
  281.                             break;
  282.                          @end@
  283.                     @end@
  284.                     default:
  285.                        netsnmp_set_request_error(reqinfo, request,
  286.                                                  SNMP_ERR_NOTWRITABLE);
  287.                        break;
  288.                  }
  289.                 break;
  290.             case MODE_SET_ACTION:
  291.             /** save a variable copy */
  292.                 switch(table_info->colnum) {
  293.                     @foreach $c column@
  294.                         @if $c.access =~ /(Write|Create)/@
  295.                           case COLUMN_$c.uc:
  296.                             {
  297.                                 int ret;
  298.                                 ret = set_$c(ci->data_context,
  299.                                              ($c.decl *) request->requestvb->val.string,
  300.                                              request->requestvb->val_len);
  301.                                 if (ret) {
  302.                                     netsnmp_set_request_error(reqinfo, request,
  303.                                                               ret);
  304.                                 }
  305.                                 @if $c.syntax eq "RowStatus"@
  306.                                   if (*request->requestvb->val.integer ==
  307.                                       RS_DESTROY) {
  308.                                           ci->new_row = -1;
  309.                                   }
  310.                                 @end@
  311.                             }
  312.                             break;
  313.                          @end@
  314.                     @end@
  315.                  }
  316.                 break;
  317.             case MODE_SET_COMMIT:
  318.                 if (!ci->have_committed) {
  319.                     /** do this once per row only */
  320.                     ${i}_commit_row(&ci->data_context, ci->new_row);
  321.                     ci->have_committed = 1;
  322.                 }
  323.                 break;
  324.             case MODE_SET_UNDO:
  325.              /** save a variable copy */
  326.                 switch(table_info->colnum) {
  327.                     @foreach $c column@
  328.                         @if $c.access =~ /(Write|Create)/@
  329.                           case COLUMN_$c.uc:
  330.                             {
  331.                                 int retval;
  332.                                 struct undoInfo *ui;
  333.                                 ui = netsnmp_oid_stash_get_data(undoStorage,
  334.                                                                 suffix,
  335.                                                                 suffix_len);
  336.                                 retval = set_$c(ci->data_context, ui->ptr,
  337.                                                 ui->len);
  338.                                 if (retval) {
  339.                                     netsnmp_set_request_error(reqinfo, request,
  340.                                                               SNMP_ERR_UNDOFAILED);
  341.                                 }
  342.                             }
  343.                             break;
  344.                         @end@
  345.                     @end@
  346.                 }
  347.                 break;
  348.                 
  349.             case MODE_SET_FREE:
  350.                 break;
  351. @end@
  352.             default:
  353.                 snmp_log(LOG_ERR, "problem encountered in ${i}_handler: unsupported moden");
  354.         }
  355.     }
  356. @if $i.settable@
  357.     /** clean up after all requset processing has ended */
  358.     switch(reqinfo->mode) {
  359.     case MODE_SET_UNDO:
  360.     case MODE_SET_FREE:
  361.     case MODE_SET_COMMIT:
  362.         /** clear out the undo cache */
  363.         netsnmp_oid_stash_free(&undoStorage, ${name}_free_undoInfo);
  364.         netsnmp_oid_stash_free(&commitStorage, netsnmp_oid_stash_no_free);
  365.     }
  366. @end@
  367.     return SNMP_ERR_NOERROR;
  368. }
  369. @end@
  370. @run mib2c.check_values.conf@
  371. @run mib2c.access_functions.conf@
  372. @open -@
  373. **********************************************************************
  374. NOTE:  The only files you MUST modify should be the following:
  375.   ${name}_access.c
  376.   ${name}_access.h
  377.   ${name}_checkfns_local.h
  378.   ${name}_checkfns_local.c
  379. **********************************************************************