mib2c.container.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.container.conf,v 1.3 2004/10/14 12:57:33 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.container.conf,v 1.3 2004/10/14 12:57:33 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_container               *container;
  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.     container  = netsnmp_container_find( "table_container" );
  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_container_table_register( reg, table_info, container, 0 );
  76.     /* Initialise the contents of the table here */
  77. }
  78.     /* Typical data structure for a row entry */
  79. struct ${i}_entry {
  80.     netsnmp_index oid_index;
  81.     /* Index values */
  82.     @foreach $idx index@
  83.     $idx.decl $idx;
  84.     @end@
  85.     /* Column values */
  86.     @foreach $c column@
  87.     @if $c.readable@
  88.     $c.decl $c;
  89.      @if $c.settable@
  90.       @if !$c.rowstatus@
  91.     $c.decl old_$c;
  92.       @end@
  93.      @end@
  94.     @end@
  95.     @end@
  96.     int   valid;
  97. };
  98. /* create a new row in the table */
  99. struct ${i}_entry *
  100. ${i}_createEntry(netsnmp_container *container, 
  101.   @foreach $idx index@
  102.                  $idx.decl  $idx,
  103.   @end@
  104.                 ) {
  105.     struct ${i}_entry *entry;
  106.     entry = SNMP_TYPEDEF_MALLOC(struct ${i}_entry);
  107.     if (!entry)
  108.         return NULL;
  109.   @foreach $idx index@
  110.     entry->$idx = $idx;
  111.   @end@
  112.     entry->oid_index.len  = XXX;
  113.     entry->oid_index.oids = YYY;
  114.     CONTAINER_INSERT( container, entry );
  115.     return entry;
  116. }
  117. /* remove a row from the table */
  118. void
  119. ${i}_removeEntry(netsnmp_container *container, 
  120.                  struct ${i}_entry *entry) {
  121.     if (!entry)
  122.         return;    /* Nothing to remove */
  123.     CONTAINER_REMOVE( container, entry );
  124.     if (entry)
  125.         SNMP_FREE( entry );   /* XXX - release any other internal resources */
  126. }
  127. /** handles requests for the $i table */
  128. int
  129. ${i}_handler(
  130.     netsnmp_mib_handler               *handler,
  131.     netsnmp_handler_registration      *reginfo,
  132.     netsnmp_agent_request_info        *reqinfo,
  133.     netsnmp_request_info              *requests) {
  134.     netsnmp_request_info       *request;
  135.     netsnmp_table_request_info *table_info;
  136.     netsnmp_table_data         *table_data;
  137.     netsnmp_container          *container;
  138.     struct ${i}_entry          *table_entry;
  139.     switch (reqinfo->mode) {
  140.         /*
  141.          * Read-support (also covers GetNext requests)
  142.          */
  143.     case MODE_GET:
  144.         for (request=requests; request; request=request->next) {
  145.             table_entry = (struct ${i}_entry *)
  146.                               netsnmp_container_table_extract_context(request);
  147.             table_info  =     netsnmp_extract_table_info(request);
  148.     
  149.             switch (table_info->colnum) {
  150.             @foreach $c column@
  151.             @if $c.readable@
  152.             case COLUMN_$c.uc:
  153.                 snmp_set_var_typed_value( request->requestvb, $c.type,
  154.                                           table_entry->$c,
  155.                                           sizeof(table_entry->$c));
  156.                 break;
  157.             @end@
  158.             @end@
  159.             }
  160.         }
  161.         break;
  162. @if $i.settable@
  163.         /*
  164.          * Write-support
  165.          */
  166.     case MODE_SET_RESERVE1:
  167.         for (request=requests; request; request=request->next) {
  168.             table_entry = (struct ${i}_entry *)
  169.                               netsnmp_container_table_extract_context(request);
  170.             table_info  =     netsnmp_extract_table_info(request);
  171.     
  172.             switch (table_info->colnum) {
  173.             @foreach $c column@
  174.             @if $c.settable@
  175.             case COLUMN_$c.uc:
  176.                 if ( request->requestvb->type != $c.type ) {
  177.                     netsnmp_set_request_error( reqinfo, request,
  178.                                                SNMP_ERR_WRONGTYPE );
  179.                     return SNMP_ERR_NOERROR;
  180.                 }
  181.                 /* Also may need to check size/value */
  182.             @if $c.rowstatus@
  183.                 switch (*request->requestvb->val.integer) {
  184.                 case RS_ACTIVE:
  185.                 case RS_NOTINSERVICE:
  186.                     if (!table_entry) {
  187.                         netsnmp_set_request_error( reqinfo, request,
  188.                                                    SNMP_ERR_INCONSISTENTVALUE );
  189.                         return SNMP_ERR_NOERROR;
  190.                     }
  191.                     break;
  192.                 case RS_CREATEANDGO:
  193.                 case RS_CREATEANDWAIT:
  194.                     if (table_entry) {
  195.                         netsnmp_set_request_error( reqinfo, request,
  196.                                                    SNMP_ERR_INCONSISTENTVALUE );
  197.                         return SNMP_ERR_NOERROR;
  198.                     }
  199.                     break;
  200.                 case RS_DESTROY:
  201.                     /* Valid in all circumstances */
  202.                     break;
  203.                 case RS_NOTREADY:
  204.                 default:
  205.                     netsnmp_set_request_error( reqinfo, request,
  206.                                                SNMP_ERR_WRONGVALUE );
  207.                     return SNMP_ERR_NOERROR;
  208.                     break;
  209.                 }
  210.             @end@
  211.                 break;
  212.             @end@
  213.             @end@
  214.             default:
  215.                 netsnmp_set_request_error( reqinfo, request,
  216.                                            SNMP_ERR_NOTWRITABLE );
  217.                 return SNMP_ERR_NOERROR;
  218.             }
  219.         }
  220.         break;
  221.     case MODE_SET_RESERVE2:
  222. @if $i.creatable@
  223.         for (request=requests; request; request=request->next) {
  224.             container   =     netsnmp_container_table_extract(request);
  225.             table_entry = (struct ${i}_entry *)
  226.                               netsnmp_container_table_extract_context(request);
  227.             table_info  =     netsnmp_extract_table_info(request);
  228.     
  229.             switch (table_info->colnum) {
  230. @if $i.rowstatus@
  231.             @foreach $c column@
  232.             @if $c.rowstatus@
  233.             case COLUMN_$c.uc:
  234.                 switch (*request->requestvb->val.integer) {
  235.                 case RS_CREATEANDGO:
  236.                 case RS_CREATEANDWAIT:
  237.                     table_entry = ${i}_createEntry(container
  238.   @foreach $idx index@
  239.                         , table_info->indexes->val.YYY
  240.   @end@
  241.                         );
  242.                     if (table_entry) {
  243.                         netsnmp_container_table_insert_row( request, table_entry );
  244.                     } else {
  245.                         netsnmp_set_request_error( reqinfo, request,
  246.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  247.                         return SNMP_ERR_NOERROR;
  248.                     }
  249.                 }
  250.             @end@
  251.             @end@
  252. @else@
  253.             @foreach $c column@
  254.             @if $c.creatable@
  255.             case COLUMN_$c.uc:
  256.             @end@
  257.             @end@
  258.                 if ( !table_entry ) {
  259.                     table_entry = ${i}_createEntry(container
  260.   @foreach $idx index@
  261.                         , table_info->indexes->val.YYY
  262.   @end@
  263.                         );
  264.                     if (table_entry) {
  265.                         netsnmp_container_table_insert_row( request, table_entry );
  266.                     } else {
  267.                         netsnmp_set_request_error( reqinfo, request,
  268.                                                    SNMP_ERR_RESOURCEUNAVAILABLE );
  269.                         return SNMP_ERR_NOERROR;
  270.                     }
  271.                 }
  272.                 break;
  273. @end@
  274.             }
  275.         }
  276. @end@
  277.         break;
  278.     case MODE_SET_FREE:
  279. @if $i.creatable@
  280.         for (request=requests; request; request=request->next) {
  281.             container   =     netsnmp_container_table_extract(request);
  282.             table_entry = (struct ${i}_entry *)
  283.                               netsnmp_container_table_extract_context(request);
  284.             table_info  =     netsnmp_extract_table_info(request);
  285.     
  286.             switch (table_info->colnum) {
  287. @if $i.rowstatus@
  288.             @foreach $c column@
  289.             @if $c.rowstatus@
  290.             case COLUMN_$c.uc:
  291.                 switch (*request->requestvb->val.integer) {
  292.                 case RS_CREATEANDGO:
  293.                 case RS_CREATEANDWAIT:
  294.                     if (table_entry && !table_entry->valid) {
  295.                         ${i}_removeEntry(container, table_entry );
  296.                     }
  297.                 }
  298.             @end@
  299.             @end@
  300. @else@
  301.             @foreach $c column@
  302.             @if $c.creatable@
  303.             case COLUMN_$c.uc:
  304.             @end@
  305.             @end@
  306.                 if ( table_entry && !table_entry->valid ) {
  307.                     ${i}_removeEntry(container, table_entry );
  308.                 }
  309.                 break;
  310. @end@
  311.             }
  312.         }
  313. @end@
  314.         break;
  315.     case MODE_SET_ACTION:
  316.         for (request=requests; request; request=request->next) {
  317.             table_entry = (struct ${i}_entry *)
  318.                               netsnmp_container_table_extract_context(request);
  319.             table_info  =     netsnmp_extract_table_info(request);
  320.     
  321.             switch (table_info->colnum) {
  322.             @foreach $c column@
  323.             @if $c.settable@
  324.             @if !$c.rowstatus@
  325.             case COLUMN_$c.uc:
  326.                 /* Need to save old 'table_entry->$c' value.
  327.                    May need to use 'memcpy' */
  328.                 table_entry->old_$c = table_entry->$c;
  329.                 table_entry->$c     = request->requestvb->val.YYY;
  330.                 break;
  331.             @end@
  332.             @end@
  333.             @end@
  334.             }
  335.         }
  336. @if $i.rowstatus@
  337.         /* Check the internal consistency of an active row */
  338.         for (request=requests; request; request=request->next) {
  339.             table_entry = (struct ${i}_entry *)
  340.                               netsnmp_container_table_extract_context(request);
  341.             table_info  =     netsnmp_extract_table_info(request);
  342.     
  343.             switch (table_info->colnum) {
  344.             @foreach $c column@
  345.             @if $c.rowstatus@
  346.             case COLUMN_$c.uc:
  347.                 switch (*request->requestvb->val.integer) {
  348.                 case RS_ACTIVE:
  349.                 case RS_CREATEANDGO:
  350.                     if (/* XXX */) {
  351.                         netsnmp_set_request_error( reqinfo, request,
  352.                                                    SNMP_ERR_INCONSISTENTVALUE );
  353.                         return SNMP_ERR_NOERROR;
  354.                     }
  355.                 }
  356.             @end@
  357.             @end@
  358.             }
  359.         }
  360. @end@
  361.         break;
  362.     case MODE_SET_UNDO:
  363.         for (request=requests; request; request=request->next) {
  364.             container   =     netsnmp_container_table_extract(request);
  365.             table_entry = (struct ${i}_entry *)
  366.                               netsnmp_container_table_extract_context(request);
  367.             table_info  =     netsnmp_extract_table_info(request);
  368.     
  369.             switch (table_info->colnum) {
  370.             @foreach $c column@
  371.             @if $c.settable@
  372.             case COLUMN_$c.uc:
  373. @if $i.rowstatus@
  374.   @if $c.rowstatus@
  375.                 switch (*request->requestvb->val.integer) {
  376.                 case RS_CREATEANDGO:
  377.                 case RS_CREATEANDWAIT:
  378.                     if (table_entry && !table_entry->valid) {
  379.                         ${i}_removeEntry(container, table_entry );
  380.                     }
  381.                 }
  382.   @else@
  383.                 /* Need to restore old 'table_entry->$c' value.
  384.                    May need to use 'memcpy' */
  385.                 table_entry->$c = table_entry->old_$c;
  386.   @end@
  387. @else@
  388.   @if $c.creatable@
  389.                 if ( table_entry && !table_entry->valid ) {
  390.                     ${i}_removeEntry(container, table_entry );
  391.                 } else {
  392.                     /* Need to restore old 'table_entry->$c' value.
  393.                        May need to use 'memcpy' */
  394.                     table_entry->$c = table_entry->old_$c;
  395.                 }
  396.   @else@
  397.                 /* Need to restore old 'table_entry->$c' value.
  398.                    May need to use 'memcpy' */
  399.                 table_entry->$c = table_entry->old_$c;
  400.   @end@
  401. @end@
  402.                 break;
  403.             @end@
  404.             @end@
  405.             }
  406.         }
  407.         break;
  408.     case MODE_SET_COMMIT:
  409. @if $i.creatable@
  410.         for (request=requests; request; request=request->next) {
  411.             container   =     netsnmp_container_table_extract(request);
  412.             table_entry = (struct ${i}_entry *)
  413.                               netsnmp_container_table_extract_context(request);
  414.             table_info  =     netsnmp_extract_table_info(request);
  415.     
  416.             switch (table_info->colnum) {
  417. @if $i.rowstatus@
  418.             @foreach $c column@
  419.             @if $c.rowstatus@
  420.             case COLUMN_$c.uc:
  421.                 switch (*request->requestvb->val.integer) {
  422.                 case RS_CREATEANDGO:
  423.                     table_entry->valid = 1;
  424.                     /* Fall-through */
  425.                 case RS_ACTIVE:
  426.                     table_entry->$c = RS_ACTIVE;
  427.                     break;
  428.                 case RS_CREATEANDWAIT:
  429.                     table_entry->valid = 1;
  430.                     /* Fall-through */
  431.                 case RS_NOTINSERVICE:
  432.                     table_entry->$c = RS_NOTINSERVICE;
  433.                     break;
  434.                 case RS_DESTROY:
  435.                     ${i}_removeEntry(container, table_entry );
  436.                 }
  437.             @end@
  438.             @end@
  439. @else@
  440.             @foreach $c column@
  441.             @if $c.creatable@
  442.             case COLUMN_$c.uc:
  443.             @end@
  444.             @end@
  445.                 if ( table_entry && !table_entry->valid ) {
  446.                     table_entry->valid = 1;
  447.                 }
  448. @end@
  449.             }
  450.         }
  451. @end@
  452.         break;
  453. @end@
  454.     }
  455.     return SNMP_ERR_NOERROR;
  456. }
  457. @end@