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

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.table_data.conf,v 1.3 2004/10/14 12:57:34 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. @end@
  18. @foreach $i table@
  19. /* column number definitions for table $i */
  20.     @foreach $c column@
  21.        #define COLUMN_$c.uc $c.subid
  22.     @end@
  23. @end@
  24. #endif /* $name.uc_H */
  25. ######################################################################
  26. ## Do the .c file
  27. ######################################################################
  28. @open ${name}.c@
  29. /*
  30.  * Note: this file originally auto-generated by mib2c using
  31.  *  $Id: mib2c.table_data.conf,v 1.3 2004/10/14 12:57:34 dts12 Exp $
  32.  */
  33. #include <net-snmp/net-snmp-config.h>
  34. #include <net-snmp/net-snmp-includes.h>
  35. #include <net-snmp/agent/net-snmp-agent-includes.h>
  36. #include "${name}.h"
  37. /** Initializes the $name module */
  38. void
  39. init_$name(void)
  40. {
  41.   /* here we initialize all the tables we're planning on supporting */
  42.   @foreach $i table@
  43.     initialize_table_$i();
  44.   @end@
  45. }
  46. @foreach $i table@
  47. /** Initialize the $i table by defining its contents and how it's structured */
  48. void
  49. initialize_table_$i(void)
  50. {
  51.     static oid ${i}_oid[] = {$i.commaoid};
  52.     size_t ${i}_oid_len   = OID_LENGTH(${i}_oid);
  53.     netsnmp_handler_registration    *reg;
  54.     netsnmp_table_data              *table_data;
  55.     netsnmp_table_registration_info *table_info;
  56.     reg = netsnmp_create_handler_registration(
  57.               "$i",     ${i}_handler,
  58.               ${i}_oid, ${i}_oid_len,
  59. @if $i.settable@
  60.               HANDLER_CAN_RWRITE
  61. @else@
  62.               HANDLER_CAN_RONLY
  63. @end@
  64.               );
  65.     table_data = netsnmp_create_table_data( "$i" );
  66.     table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
  67.     netsnmp_table_helper_add_indexes(table_info,
  68.     @foreach $idx index@
  69.                            $idx.type,  /* index: $idx */
  70.     @end@
  71.                            0);
  72.     table_info->min_column = XXX;
  73.     table_info->min_column = YYY;
  74.     
  75.     netsnmp_register_table_data( reg, table_data, table_info );
  76.     /* Initialise the contents of the table here */
  77. }
  78.     /* Typical data structure for a row entry */
  79. struct ${i}_entry {
  80.     /* Index values */
  81.     @foreach $idx index@
  82.     $idx.decl $idx;
  83.     @end@
  84.     /* Column values */
  85.     @foreach $c column@
  86.     @if $c.readable@
  87.     $c.decl $c;
  88.      @if $c.settable@
  89.       @if !$c.rowstatus@
  90.     $c.decl old_$c;
  91.       @end@
  92.      @end@
  93.     @end@
  94.     @end@
  95.     int   valid;
  96. };
  97. /* create a new row in the table */
  98. netsnmp_table_row *
  99. ${i}_createEntry(netsnmp_table_data *table_data, 
  100.   @foreach $idx index@
  101.                  $idx.decl  $idx,
  102.   @end@
  103.                 ) {
  104.     struct ${i}_entry *entry;
  105.     netsnmp_table_row *row;
  106.     entry = SNMP_TYPEDEF_MALLOC(struct ${i}_entry);
  107.     if (!entry)
  108.         return NULL;
  109.     row = netsnmp_create_table_data_row();
  110.     if (!row) {
  111.         SNMP_FREE(entry);
  112.         return NULL;
  113.     }
  114.     row->data = entry;
  115.   @foreach $idx index@
  116.     entry->$idx = $idx;
  117.     netsnmp_table_row_add_index( row, $idx.type,
  118.                                  &(entry->$idx),
  119.                                  sizeof(entry->$idx));
  120.   @end@
  121.     netsnmp_table_data_add_row( table_data, row );
  122.     return row;
  123. }
  124. /* remove a row from the table */
  125. void
  126. ${i}_removeEntry(netsnmp_table_data *table_data, 
  127.                  netsnmp_table_row  *row) {
  128.     struct ${i}_entry *entry;
  129.     if (!row)
  130.         return;    /* Nothing to remove */
  131.     entry = (struct ${i}_entry)
  132.         netsnmp_table_data_remove_and_delete_row( table_data, row );
  133.     if (entry)
  134.         SNMP_FREE( entry );   /* XXX - release any other internal resources */
  135. }
  136. /** handles requests for the $i table */
  137. int
  138. ${i}_handler(
  139.     netsnmp_mib_handler               *handler,
  140.     netsnmp_handler_registration      *reginfo,
  141.     netsnmp_agent_request_info        *reqinfo,
  142.     netsnmp_request_info              *requests) {
  143.     netsnmp_request_info       *request;
  144.     netsnmp_table_request_info *table_info;
  145.     netsnmp_table_data         *table_data;
  146.     netsnmp_table_row          *table_row;
  147.     struct ${i}_entry          *table_entry;
  148.     switch (reqinfo->mode) {
  149.         /*
  150.          * Read-support (also covers GetNext requests)
  151.          */
  152.     case MODE_GET:
  153.         for (request=requests; request; request=request->next) {
  154.             table_entry = (struct ${i}_entry *)
  155.                               netsnmp_extract_table_row_data(request);
  156.             table_info  =     netsnmp_extract_table_info(    request);
  157.     
  158.             switch (table_info->colnum) {
  159.             @foreach $c column@
  160.             @if $c.readable@
  161.             case COLUMN_$c.uc:
  162.                 snmp_set_var_typed_value( request->requestvb, $c.type,
  163.                                           table_entry->$c,
  164.                                           sizeof(table_entry->$c));
  165.                 break;
  166.             @end@
  167.             @end@
  168.             }
  169.         }
  170.         break;
  171. @if $i.settable@
  172.         /*
  173.          * Write-support
  174.          */
  175.     case MODE_SET_RESERVE1:
  176.         for (request=requests; request; request=request->next) {
  177.             table_entry = (struct ${i}_entry *)
  178.                               netsnmp_extract_table_row_data(request);
  179.             table_info  =     netsnmp_extract_table_info(    request);
  180.     
  181.             switch (table_info->colnum) {
  182.             @foreach $c column@
  183.             @if $c.settable@
  184.             case COLUMN_$c.uc:
  185.                 if ( request->requestvb->type != $c.type ) {
  186.                     netsnmp_set_request_error( reqinfo, request,
  187.                                                SNMP_ERR_WRONGTYPE );
  188.                     return SNMP_ERR_NOERROR;
  189.                 }
  190.                 /* Also may need to check size/value */
  191.             @if $c.rowstatus@
  192.                 switch (*request->requestvb->val.integer) {
  193.                 case RS_ACTIVE:
  194.                 case RS_NOTINSERVICE:
  195.                     if (!table_entry) {
  196.                         netsnmp_set_request_error( reqinfo, request,
  197.                                                    SNMP_ERR_INCONSISTENTVALUE );
  198.                         return SNMP_ERR_NOERROR;
  199.                     }
  200.                     break;
  201.                 case RS_CREATEANDGO:
  202.                 case RS_CREATEANDWAIT:
  203.                     if (table_entry) {
  204.                         netsnmp_set_request_error( reqinfo, request,
  205.                                                    SNMP_ERR_INCONSISTENTVALUE );
  206.                         return SNMP_ERR_NOERROR;
  207.                     }
  208.                     break;
  209.                 case RS_DESTROY:
  210.                     /* Valid in all circumstances */
  211.                     break;
  212.                 case RS_NOTREADY:
  213.                 default:
  214.                     netsnmp_set_request_error( reqinfo, request,
  215.                                                SNMP_ERR_WRONGVALUE );
  216.                     return SNMP_ERR_NOERROR;
  217.                     break;
  218.                 }
  219.             @end@
  220.                 break;
  221.             @end@
  222.             @end@
  223.             default:
  224.                 netsnmp_set_request_error( reqinfo, request,
  225.                                            SNMP_ERR_NOTWRITABLE );
  226.                 return SNMP_ERR_NOERROR;
  227.             }
  228.         }
  229.         break;
  230.     case MODE_SET_RESERVE2:
  231. @if $i.creatable@
  232.         for (request=requests; request; request=request->next) {
  233.             table_row   = (netsnmp_table_row *)
  234.                               netsnmp_extract_table_row( request);
  235.             table_data  =     netsnmp_extract_table(     request);
  236.             table_info  =     netsnmp_extract_table_info(request);
  237.     
  238.             switch (table_info->colnum) {
  239. @if $i.rowstatus@
  240.             @foreach $c column@
  241.             @if $c.rowstatus@
  242.             case COLUMN_$c.uc:
  243.                 switch (*request->requestvb->val.integer) {
  244.                 case RS_CREATEANDGO:
  245.                 case RS_CREATEANDWAIT:
  246.                     table_row = ${i}_createEntry(table_data
  247.   @foreach $idx index@
  248.                         , table_info->indexes->val.YYY
  249.   @end@
  250.                         );
  251.                     if (table_row) {
  252.                         netsnmp_insert_table_row( request, table_row );
  253.                     } else {
  254.                         netsnmp_set_request_error( reqinfo, request,
  255.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  256.                         return SNMP_ERR_NOERROR;
  257.                     }
  258.                 }
  259.             @end@
  260.             @end@
  261. @else@
  262.             @foreach $c column@
  263.             @if $c.creatable@
  264.             case COLUMN_$c.uc:
  265.             @end@
  266.             @end@
  267.                 if ( !table_row ) {
  268.                     table_row = ${i}_createEntry(table_data
  269.   @foreach $idx index@
  270.                         , table_info->indexes->val.YYY
  271.   @end@
  272.                         );
  273.                     if (table_row) {
  274.                         netsnmp_insert_table_row( request, table_row );
  275.                     } else {
  276.                         netsnmp_set_request_error( reqinfo, request,
  277.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  278.                         return SNMP_ERR_NOERROR;
  279.                     }
  280.                 }
  281.                 break;
  282. @end@
  283.             }
  284.         }
  285. @end@
  286.         break;
  287.     case MODE_SET_FREE:
  288. @if $i.creatable@
  289.         for (request=requests; request; request=request->next) {
  290.             table_entry = (struct ${i}_entry *)
  291.                               netsnmp_extract_table_row_data(request);
  292.             table_row   = (netsnmp_table_row *)
  293.                               netsnmp_extract_table_row(     request);
  294.             table_data  =     netsnmp_extract_table(         request);
  295.             table_info  =     netsnmp_extract_table_info(    request);
  296.     
  297.             switch (table_info->colnum) {
  298. @if $i.rowstatus@
  299.             @foreach $c column@
  300.             @if $c.rowstatus@
  301.             case COLUMN_$c.uc:
  302.                 switch (*request->requestvb->val.integer) {
  303.                 case RS_CREATEANDGO:
  304.                 case RS_CREATEANDWAIT:
  305.                     if (table_entry && !table_entry->valid) {
  306.                         ${i}_removeEntry(table_data, table_row );
  307.                     }
  308.                 }
  309.             @end@
  310.             @end@
  311. @else@
  312.             @foreach $c column@
  313.             @if $c.creatable@
  314.             case COLUMN_$c.uc:
  315.             @end@
  316.             @end@
  317.                 if ( table_entry && !table_entry->valid ) {
  318.                     ${i}_removeEntry(table_data, table_row );
  319.                 }
  320.                 break;
  321. @end@
  322.             }
  323.         }
  324. @end@
  325.         break;
  326.     case MODE_SET_ACTION:
  327.         for (request=requests; request; request=request->next) {
  328.             table_entry = (struct ${i}_entry *)
  329.                               netsnmp_extract_table_row_data(request);
  330.             table_info  =     netsnmp_extract_table_info(    request);
  331.     
  332.             switch (table_info->colnum) {
  333.             @foreach $c column@
  334.             @if $c.settable@
  335.             @if !$c.rowstatus@
  336.             case COLUMN_$c.uc:
  337.                 /* Need to save old 'table_entry->$c' value.
  338.                    May need to use 'memcpy' */
  339.                 table_entry->old_$c = table_entry->$c;
  340.                 table_entry->$c     = request->requestvb->val.YYY;
  341.                 break;
  342.             @end@
  343.             @end@
  344.             @end@
  345.             }
  346.         }
  347. @if $i.rowstatus@
  348.         /* Check the internal consistency of an active row */
  349.         for (request=requests; request; request=request->next) {
  350.             table_entry = (struct ${i}_entry *)
  351.                               netsnmp_extract_table_row_data(request);
  352.             table_info  =     netsnmp_extract_table_info(    request);
  353.     
  354.             switch (table_info->colnum) {
  355.             @foreach $c column@
  356.             @if $c.rowstatus@
  357.             case COLUMN_$c.uc:
  358.                 switch (*request->requestvb->val.integer) {
  359.                 case RS_ACTIVE:
  360.                 case RS_CREATEANDGO:
  361.                     if (/* XXX */) {
  362.                         netsnmp_set_request_error( reqinfo, request,
  363.                                                    SNMP_ERR_INCONSISTENTVALUE );
  364.                         return SNMP_ERR_NOERROR;
  365.                     }
  366.                 }
  367.             @end@
  368.             @end@
  369.             }
  370.         }
  371. @end@
  372.         break;
  373.     case MODE_SET_UNDO:
  374.         for (request=requests; request; request=request->next) {
  375.             table_entry = (struct ${i}_entry *)
  376.                               netsnmp_extract_table_row_data(request);
  377.             table_row   = (netsnmp_table_row *)
  378.                               netsnmp_extract_table_row(     request);
  379.             table_data  =     netsnmp_extract_table(         request);
  380.             table_info  =     netsnmp_extract_table_info(    request);
  381.     
  382.             switch (table_info->colnum) {
  383.             @foreach $c column@
  384.             @if $c.settable@
  385.             case COLUMN_$c.uc:
  386. @if $i.rowstatus@
  387.   @if $c.rowstatus@
  388.                 switch (*request->requestvb->val.integer) {
  389.                 case RS_CREATEANDGO:
  390.                 case RS_CREATEANDWAIT:
  391.                     if (table_entry && !table_entry->valid) {
  392.                         ${i}_removeEntry(table_data, table_row );
  393.                     }
  394.                 }
  395.   @else@
  396.                 /* Need to restore old 'table_entry->$c' value.
  397.                    May need to use 'memcpy' */
  398.                 table_entry->$c = table_entry->old_$c;
  399.   @end@
  400. @else@
  401.   @if $c.creatable@
  402.                 if ( table_entry && !table_entry->valid ) {
  403.                     ${i}_removeEntry(table_data, table_row );
  404.                 } else {
  405.                     /* Need to restore old 'table_entry->$c' value.
  406.                        May need to use 'memcpy' */
  407.                     table_entry->$c = table_entry->old_$c;
  408.                 }
  409.   @else@
  410.                 /* Need to restore old 'table_entry->$c' value.
  411.                    May need to use 'memcpy' */
  412.                 table_entry->$c = table_entry->old_$c;
  413.   @end@
  414. @end@
  415.                 break;
  416.             @end@
  417.             @end@
  418.             }
  419.         }
  420.         break;
  421.     case MODE_SET_COMMIT:
  422. @if $i.creatable@
  423.         for (request=requests; request; request=request->next) {
  424.             table_entry = (struct ${i}_entry *)
  425.                               netsnmp_extract_table_row_data(request);
  426.             table_info  =     netsnmp_extract_table_info(    request);
  427.     
  428.             switch (table_info->colnum) {
  429. @if $i.rowstatus@
  430.             @foreach $c column@
  431.             @if $c.rowstatus@
  432.             case COLUMN_$c.uc:
  433.                 switch (*request->requestvb->val.integer) {
  434.                 case RS_CREATEANDGO:
  435.                     table_entry->valid = 1;
  436.                     /* Fall-through */
  437.                 case RS_ACTIVE:
  438.                     table_entry->$c = RS_ACTIVE;
  439.                     break;
  440.                 case RS_CREATEANDWAIT:
  441.                     table_entry->valid = 1;
  442.                     /* Fall-through */
  443.                 case RS_NOTINSERVICE:
  444.                     table_entry->$c = RS_NOTINSERVICE;
  445.                     break;
  446.                 case RS_DESTROY:
  447.                     ${i}_removeEntry(table_data, table_row );
  448.                 }
  449.             @end@
  450.             @end@
  451. @else@
  452.             @foreach $c column@
  453.             @if $c.creatable@
  454.             case COLUMN_$c.uc:
  455.             @end@
  456.             @end@
  457.                 if ( table_entry && !table_entry->valid ) {
  458.                     table_entry->valid = 1;
  459.                 }
  460. @end@
  461.             }
  462.         }
  463. @end@
  464.         break;
  465. @end@
  466.     }
  467.     return SNMP_ERR_NOERROR;
  468. }
  469. @end@