netSnmpHostsTable.c
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:23k
源码类别:

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  * Note: this file originally auto-generated by mib2c using
  3.  *        : mib2c.iterate_access.conf,v 1.4 2003/07/01 00:15:11 hardaker Exp $
  4.  */
  5. #include <net-snmp/net-snmp-config.h>
  6. #include <net-snmp/net-snmp-includes.h>
  7. #include <net-snmp/agent/net-snmp-agent-includes.h>
  8. #include "netSnmpHostsTable.h"
  9. #include "netSnmpHostsTable_checkfns.h"
  10. #include "netSnmpHostsTable_access.h"
  11. static netsnmp_oid_stash_node *undoStorage = NULL;
  12. static netsnmp_oid_stash_node *commitStorage = NULL;
  13. struct undoInfo {
  14.     void           *ptr;
  15.     size_t          len;
  16. };
  17. struct commitInfo {
  18.     void           *data_context;
  19.     int             have_committed;
  20.     int             new_row;
  21. };
  22. void
  23. netSnmpHostsTable_free_undoInfo(void *vptr)
  24. {
  25.     struct undoInfo *ui = vptr;
  26.     if (!ui)
  27.         return;
  28.     SNMP_FREE(ui->ptr);
  29.     SNMP_FREE(ui);
  30. }
  31. /** Initialize the netSnmpHostsTable table by defining its contents and how it's structured */
  32. void
  33. initialize_table_netSnmpHostsTable(void)
  34. {
  35.     static oid      netSnmpHostsTable_oid[] =
  36.         { 1, 3, 6, 1, 4, 1, 8072, 2, 2, 2 };
  37.     netsnmp_table_registration_info *table_info;
  38.     netsnmp_handler_registration *my_handler;
  39.     netsnmp_iterator_info *iinfo;
  40.     /** create the table registration information structures */
  41.     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  42.     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
  43.     /** if your table is read only, it's easiest to change the
  44.         HANDLER_CAN_RWRITE definition below to HANDLER_CAN_RONLY */
  45.     my_handler = netsnmp_create_handler_registration("netSnmpHostsTable",
  46.                                                      netSnmpHostsTable_handler,
  47.                                                      netSnmpHostsTable_oid,
  48.                                                      OID_LENGTH
  49.                                                      (netSnmpHostsTable_oid),
  50.                                                      HANDLER_CAN_RWRITE);
  51.     if (!my_handler || !table_info || !iinfo) {
  52.         snmp_log(LOG_ERR,
  53.                  "malloc failed in initialize_table_netSnmpHostsTable");
  54.         return; /** Serious error. */
  55.     }
  56.     /***************************************************
  57.      * Setting up the table's definition
  58.      */
  59.     netsnmp_table_helper_add_indexes(table_info, ASN_OCTET_STR,
  60.                                                  /** index: netSnmpHostName */
  61.                                      0);
  62.     /** Define the minimum and maximum accessible columns.  This
  63.         optimizes retrival. */
  64.     table_info->min_column = 2;
  65.     table_info->max_column = 5;
  66.     /** iterator access routines */
  67.     iinfo->get_first_data_point = netSnmpHostsTable_get_first_data_point;
  68.     iinfo->get_next_data_point = netSnmpHostsTable_get_next_data_point;
  69.     /** you may wish to set these as well */
  70.     iinfo->make_data_context = netSnmpHostsTable_context_convert_function;
  71.     iinfo->free_data_context = netSnmpHostsTable_data_free;
  72.     iinfo->free_loop_context_at_end = netSnmpHostsTable_loop_free;
  73.     /** tie the two structures together */
  74.     iinfo->table_reginfo = table_info;
  75.     /***************************************************
  76.      * registering the table with the master agent
  77.      */
  78.     DEBUGMSGTL(("initialize_table_netSnmpHostsTable",
  79.                 "Registering table netSnmpHostsTable as a table iteratorn"));
  80.     netsnmp_register_table_iterator(my_handler, iinfo);
  81. }
  82. /** Initializes the netSnmpHostsTable module */
  83. void
  84. init_netSnmpHostsTable(void)
  85. {
  86.   /** here we initialize all the tables we're planning on supporting */
  87.     initialize_table_netSnmpHostsTable();
  88. }
  89. /** handles requests for the netSnmpHostsTable table, if anything else needs to be done */
  90. int
  91. netSnmpHostsTable_handler(netsnmp_mib_handler *handler,
  92.                           netsnmp_handler_registration *reginfo,
  93.                           netsnmp_agent_request_info *reqinfo,
  94.                           netsnmp_request_info *requests)
  95. {
  96.     netsnmp_request_info *request;
  97.     netsnmp_table_request_info *table_info;
  98.     netsnmp_variable_list *var;
  99.     struct commitInfo *ci = NULL;
  100.     void           *data_context = NULL;
  101.     oid            *suffix;
  102.     size_t          suffix_len;
  103.     for (request = requests; request; request = request->next) {
  104.         /* column and row index encoded portion */
  105.         var = request->requestvb;
  106.         suffix     = var->name + reginfo->rootoid_len + 1;
  107.         suffix_len = var->name_length - (reginfo->rootoid_len + 1);
  108.         if (request->processed != 0)
  109.             continue;
  110.         switch (reqinfo->mode) {
  111.         case MODE_GET:
  112.         case MODE_SET_RESERVE1:
  113.             data_context = netsnmp_extract_iterator_context(request);
  114.             if (data_context == NULL) {
  115.                 if (reqinfo->mode == MODE_GET) {
  116.                     netsnmp_set_request_error(reqinfo, request,
  117.                                               SNMP_NOSUCHINSTANCE);
  118.                     continue;
  119.                 }
  120.             }
  121.             break;
  122.         default:               /* == the other SET modes */
  123.             ci = netsnmp_oid_stash_get_data(commitStorage,
  124.                                             suffix + 1, suffix_len - 1);
  125.             break;
  126.         }
  127.         /** extracts the information about the table from the request */
  128.         table_info = netsnmp_extract_table_info(request);
  129.         /** table_info->colnum contains the column number requested */
  130.         /** table_info->indexes contains a linked list of snmp variable
  131.            bindings for the indexes of the table.  Values in the list
  132.            have been set corresponding to the indexes of the
  133.            request */
  134.         if (table_info == NULL) {
  135.             continue;
  136.         }
  137.         switch (reqinfo->mode) {
  138.         case MODE_GET:
  139.             switch (table_info->colnum) {
  140.             case COLUMN_NETSNMPHOSTADDRESSTYPE:
  141.                 {
  142.                     long           *retval;
  143.                     size_t          retval_len = 0;
  144.                     retval =
  145.                         get_netSnmpHostAddressType(data_context,
  146.                                                    &retval_len);
  147.                     snmp_set_var_typed_value(var, ASN_INTEGER,
  148.                                              (const u_char *) retval,
  149.                                              retval_len);
  150.                 }
  151.                 break;
  152.             case COLUMN_NETSNMPHOSTADDRESS:
  153.                 {
  154.                     char           *retval;
  155.                     size_t          retval_len = 0;
  156.                     retval =
  157.                         get_netSnmpHostAddress(data_context, &retval_len);
  158.                     snmp_set_var_typed_value(var, ASN_OCTET_STR,
  159.                                              (const u_char *) retval,
  160.                                              retval_len);
  161.                 }
  162.                 break;
  163.             case COLUMN_NETSNMPHOSTSTORAGE:
  164.                 {
  165.                     long           *retval;
  166.                     size_t          retval_len = 0;
  167.                     retval =
  168.                         get_netSnmpHostStorage(data_context, &retval_len);
  169.                     snmp_set_var_typed_value(var, ASN_INTEGER,
  170.                                              (const u_char *) retval,
  171.                                              retval_len);
  172.                 }
  173.                 break;
  174.             case COLUMN_NETSNMPHOSTROWSTATUS:
  175.                 {
  176.                     long           *retval;
  177.                     size_t          retval_len = 0;
  178.                     retval =
  179.                         get_netSnmpHostRowStatus(data_context,
  180.                                                  &retval_len);
  181.                     snmp_set_var_typed_value(var, ASN_INTEGER,
  182.                                              (const u_char *) retval,
  183.                                              retval_len);
  184.                 }
  185.                 break;
  186.             default:
  187.                 /** We shouldn't get here */
  188.                 snmp_log(LOG_ERR,
  189.                          "problem encountered in netSnmpHostsTable_handler: unknown columnn");
  190.             }
  191.             break;
  192.         case MODE_SET_RESERVE1:
  193.             ci = netsnmp_oid_stash_get_data(commitStorage,
  194.                                             suffix + 1, suffix_len - 1);
  195.             if (!ci) {
  196.                     /** create the commit storage info */
  197.                 ci = SNMP_MALLOC_STRUCT(commitInfo);
  198.                 if (!data_context) {
  199.                     ci->data_context =
  200.                         netSnmpHostsTable_create_data_context(table_info->
  201.                                                               indexes);
  202.                     ci->new_row = 1;
  203.                 } else {
  204.                     ci->data_context = data_context;
  205.                 }
  206.                 netsnmp_oid_stash_add_data(&commitStorage,
  207.                                            suffix + 1, suffix_len - 1, ci);
  208.             }
  209.             break;
  210.         case MODE_SET_RESERVE2:
  211.             switch (table_info->colnum) {
  212.             case COLUMN_NETSNMPHOSTADDRESSTYPE:
  213.                 {
  214.                     long           *retval;
  215.                     size_t          retval_len = 0;
  216.                     struct undoInfo *ui = NULL;
  217.                     int             ret;
  218.                     /** first, get the old value */
  219.                     retval =
  220.                         get_netSnmpHostAddressType(ci->data_context,
  221.                                                    &retval_len);
  222.                     if (retval) {
  223.                         ui = SNMP_MALLOC_STRUCT(undoInfo);
  224.                         ui->len = retval_len;
  225.                         memdup((u_char **) & ui->ptr,
  226.                                (u_char *) retval, ui->len);
  227.                     }
  228.                     /** check the new value, possibly against the
  229.                         older value for a valid state transition */
  230.                     ret =
  231.                         check_netSnmpHostAddressType(request->requestvb->
  232.                                                      type,
  233.                                                      (long *) request->
  234.                                                      requestvb->val.string,
  235.                                                      request->requestvb->
  236.                                                      val_len, retval,
  237.                                                      retval_len);
  238.                     if (ret != 0) {
  239.                         netsnmp_set_request_error(reqinfo, request, ret);
  240.                         netSnmpHostsTable_free_undoInfo(ui);
  241.                     } else if (ui) {
  242.                         /** remember information for undo purposes later */
  243.                         netsnmp_oid_stash_add_data(&undoStorage,
  244.                                                    suffix, suffix_len, ui);
  245.                     }
  246.                 }
  247.                 break;
  248.             case COLUMN_NETSNMPHOSTADDRESS:
  249.                 {
  250.                     char           *retval;
  251.                     size_t          retval_len = 0;
  252.                     struct undoInfo *ui = NULL;
  253.                     int             ret;
  254.                     /** first, get the old value */
  255.                     retval =
  256.                         get_netSnmpHostAddress(ci->data_context,
  257.                                                &retval_len);
  258.                     if (retval) {
  259.                         ui = SNMP_MALLOC_STRUCT(undoInfo);
  260.                         ui->len = retval_len;
  261.                         memdup((u_char **) & ui->ptr,
  262.                                (u_char *) retval, ui->len);
  263.                     }
  264.                     /** check the new value, possibly against the
  265.                         older value for a valid state transition */
  266.                     ret =
  267.                         check_netSnmpHostAddress(request->requestvb->type,
  268.                                                  (char *) request->
  269.                                                  requestvb->val.string,
  270.                                                  request->requestvb->
  271.                                                  val_len, retval,
  272.                                                  retval_len);
  273.                     if (ret != 0) {
  274.                         netsnmp_set_request_error(reqinfo, request, ret);
  275.                         netSnmpHostsTable_free_undoInfo(ui);
  276.                     } else if (ui) {
  277.                         /** remember information for undo purposes later */
  278.                         netsnmp_oid_stash_add_data(&undoStorage,
  279.                                                    suffix, suffix_len, ui);
  280.                     }
  281.                 }
  282.                 break;
  283.             case COLUMN_NETSNMPHOSTSTORAGE:
  284.                 {
  285.                     long           *retval;
  286.                     size_t          retval_len = 0;
  287.                     struct undoInfo *ui = NULL;
  288.                     int             ret;
  289.                     /** first, get the old value */
  290.                     retval =
  291.                         get_netSnmpHostStorage(ci->data_context,
  292.                                                &retval_len);
  293.                     if (retval) {
  294.                         ui = SNMP_MALLOC_STRUCT(undoInfo);
  295.                         ui->len = retval_len;
  296.                         memdup((u_char **) & ui->ptr,
  297.                                (u_char *) retval, ui->len);
  298.                     }
  299.                     /** check the new value, possibly against the
  300.                         older value for a valid state transition */
  301.                     ret =
  302.                         check_netSnmpHostStorage(request->requestvb->type,
  303.                                                  (long *) request->
  304.                                                  requestvb->val.string,
  305.                                                  request->requestvb->
  306.                                                  val_len, retval,
  307.                                                  retval_len);
  308.                     if (ret != 0) {
  309.                         netsnmp_set_request_error(reqinfo, request, ret);
  310.                         netSnmpHostsTable_free_undoInfo(ui);
  311.                     } else if (ui) {
  312.                         /** remember information for undo purposes later */
  313.                         netsnmp_oid_stash_add_data(&undoStorage,
  314.                                                    suffix, suffix_len, ui);
  315.                     }
  316.                 }
  317.                 break;
  318.             case COLUMN_NETSNMPHOSTROWSTATUS:
  319.                 {
  320.                     long           *retval;
  321.                     size_t          retval_len = 0;
  322.                     struct undoInfo *ui = NULL;
  323.                     int             ret;
  324.                     /** first, get the old value */
  325.                     retval =
  326.                         get_netSnmpHostRowStatus(ci->data_context,
  327.                                                  &retval_len);
  328.                     if (retval) {
  329.                         ui = SNMP_MALLOC_STRUCT(undoInfo);
  330.                         ui->len = retval_len;
  331.                         memdup((u_char **) & ui->ptr,
  332.                                (u_char *) retval, ui->len);
  333.                     }
  334.                     /** check the new value, possibly against the
  335.                         older value for a valid state transition */
  336.                     ret =
  337.                         check_netSnmpHostRowStatus(request->requestvb->
  338.                                                    type,
  339.                                                    (long *) request->
  340.                                                    requestvb->val.string,
  341.                                                    request->requestvb->
  342.                                                    val_len, retval,
  343.                                                    retval_len);
  344.                     if (ret != 0) {
  345.                         netsnmp_set_request_error(reqinfo, request, ret);
  346.                         netSnmpHostsTable_free_undoInfo(ui);
  347.                     } else if (ui) {
  348.                         /** remember information for undo purposes later */
  349.                         netsnmp_oid_stash_add_data(&undoStorage,
  350.                                                    suffix, suffix_len, ui);
  351.                     }
  352.                 }
  353.                 break;
  354.             default:
  355.                 netsnmp_set_request_error(reqinfo, request,
  356.                                           SNMP_ERR_NOTWRITABLE);
  357.                 break;
  358.             }
  359.             break;
  360.         case MODE_SET_ACTION:
  361.             /** save a variable copy */
  362.             switch (table_info->colnum) {
  363.             case COLUMN_NETSNMPHOSTADDRESSTYPE:
  364.                 {
  365.                     int             ret;
  366.                     ret = set_netSnmpHostAddressType(ci->data_context,
  367.                                                      (long *) request->
  368.                                                      requestvb->val.string,
  369.                                                      request->requestvb->
  370.                                                      val_len);
  371.                     if (ret) {
  372.                         netsnmp_set_request_error(reqinfo, request, ret);
  373.                     }
  374.                 }
  375.                 break;
  376.             case COLUMN_NETSNMPHOSTADDRESS:
  377.                 {
  378.                     int             ret;
  379.                     ret = set_netSnmpHostAddress(ci->data_context,
  380.                                                  (char *) request->
  381.                                                  requestvb->val.string,
  382.                                                  request->requestvb->
  383.                                                  val_len);
  384.                     if (ret) {
  385.                         netsnmp_set_request_error(reqinfo, request, ret);
  386.                     }
  387.                 }
  388.                 break;
  389.             case COLUMN_NETSNMPHOSTSTORAGE:
  390.                 {
  391.                     int             ret;
  392.                     ret = set_netSnmpHostStorage(ci->data_context,
  393.                                                  (long *) request->
  394.                                                  requestvb->val.string,
  395.                                                  request->requestvb->
  396.                                                  val_len);
  397.                     if (ret) {
  398.                         netsnmp_set_request_error(reqinfo, request, ret);
  399.                     }
  400.                 }
  401.                 break;
  402.             case COLUMN_NETSNMPHOSTROWSTATUS:
  403.                 {
  404.                     int             ret;
  405.                     ret = set_netSnmpHostRowStatus(ci->data_context,
  406.                                                    (long *) request->
  407.                                                    requestvb->val.string,
  408.                                                    request->requestvb->
  409.                                                    val_len);
  410.                     if (ret) {
  411.                         netsnmp_set_request_error(reqinfo, request, ret);
  412.                     }
  413.                     if (*request->requestvb->val.integer == RS_DESTROY) {
  414.                         ci->new_row = -1;
  415.                     }
  416.                 }
  417.                 break;
  418.             }
  419.             break;
  420.         case MODE_SET_COMMIT:
  421.             if (!ci->have_committed) {
  422.                     /** do this once per row only */
  423.                 netSnmpHostsTable_commit_row(&ci->data_context,
  424.                                              ci->new_row);
  425.                 ci->have_committed = 1;
  426.             }
  427.             break;
  428.         case MODE_SET_UNDO:
  429.              /** save a variable copy */
  430.             switch (table_info->colnum) {
  431.             case COLUMN_NETSNMPHOSTADDRESSTYPE:
  432.                 {
  433.                     int             retval;
  434.                     struct undoInfo *ui;
  435.                     ui = netsnmp_oid_stash_get_data(undoStorage,
  436.                                                     suffix, suffix_len);
  437.                     retval =
  438.                         set_netSnmpHostAddressType(ci->data_context,
  439.                                                    ui->ptr, ui->len);
  440.                     if (retval) {
  441.                         netsnmp_set_request_error(reqinfo, request,
  442.                                                   SNMP_ERR_UNDOFAILED);
  443.                     }
  444.                 }
  445.                 break;
  446.             case COLUMN_NETSNMPHOSTADDRESS:
  447.                 {
  448.                     int             retval;
  449.                     struct undoInfo *ui;
  450.                     ui = netsnmp_oid_stash_get_data(undoStorage,
  451.                                                     suffix, suffix_len);
  452.                     retval =
  453.                         set_netSnmpHostAddress(ci->data_context, ui->ptr,
  454.                                                ui->len);
  455.                     if (retval) {
  456.                         netsnmp_set_request_error(reqinfo, request,
  457.                                                   SNMP_ERR_UNDOFAILED);
  458.                     }
  459.                 }
  460.                 break;
  461.             case COLUMN_NETSNMPHOSTSTORAGE:
  462.                 {
  463.                     int             retval;
  464.                     struct undoInfo *ui;
  465.                     ui = netsnmp_oid_stash_get_data(undoStorage,
  466.                                                     suffix, suffix_len);
  467.                     retval =
  468.                         set_netSnmpHostStorage(ci->data_context, ui->ptr,
  469.                                                ui->len);
  470.                     if (retval) {
  471.                         netsnmp_set_request_error(reqinfo, request,
  472.                                                   SNMP_ERR_UNDOFAILED);
  473.                     }
  474.                 }
  475.                 break;
  476.             case COLUMN_NETSNMPHOSTROWSTATUS:
  477.                 {
  478.                     int             retval;
  479.                     struct undoInfo *ui;
  480.                     ui = netsnmp_oid_stash_get_data(undoStorage,
  481.                                                     suffix, suffix_len);
  482.                     retval =
  483.                         set_netSnmpHostRowStatus(ci->data_context, ui->ptr,
  484.                                                  ui->len);
  485.                     if (retval) {
  486.                         netsnmp_set_request_error(reqinfo, request,
  487.                                                   SNMP_ERR_UNDOFAILED);
  488.                     }
  489.                 }
  490.                 break;
  491.             }
  492.             break;
  493.         case MODE_SET_FREE:
  494.             break;
  495.         default:
  496.             snmp_log(LOG_ERR,
  497.                      "problem encountered in netSnmpHostsTable_handler: unsupported moden");
  498.         }
  499.     }
  500.     /** clean up after all requset processing has ended */
  501.     switch (reqinfo->mode) {
  502.     case MODE_SET_UNDO:
  503.     case MODE_SET_FREE:
  504.     case MODE_SET_COMMIT:
  505.         /** clear out the undo cache */
  506.         netsnmp_oid_stash_free(&undoStorage,
  507.                                netSnmpHostsTable_free_undoInfo);
  508.         netsnmp_oid_stash_free(&commitStorage, netsnmp_oid_stash_no_free);
  509.     }
  510.     return SNMP_ERR_NOERROR;
  511. }