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

SNMP编程

开发平台:

Unix_Linux

  1. #include <net-snmp/net-snmp-config.h>
  2. #ifdef HAVE_STDLIB_H
  3. #include <stdlib.h>
  4. #endif
  5. #if HAVE_STRING_H
  6. #include <string.h>
  7. #else
  8. #include <strings.h>
  9. #endif
  10. #include <net-snmp/net-snmp-includes.h>
  11. #include <net-snmp/agent/net-snmp-agent-includes.h>
  12. #include "testhandler.h"
  13. #include <net-snmp/agent/table.h>
  14. #include <net-snmp/agent/instance.h>
  15. #include <net-snmp/agent/table_data.h>
  16. #include <net-snmp/agent/table_dataset.h>
  17. static oid      my_test_oid[4] = { 1, 2, 3, 4 };
  18. static oid      my_table_oid[4] = { 1, 2, 3, 5 };
  19. static oid      my_instance_oid[5] = { 1, 2, 3, 6, 1 };
  20. static oid      my_data_table_oid[4] = { 1, 2, 3, 7 };
  21. static oid      my_data_table_set_oid[4] = { 1, 2, 3, 8 };
  22. static oid      my_data_ulong_instance[4] = { 1, 2, 3, 9 };
  23. u_long          my_ulong = 0;
  24. void
  25. init_testhandler(void)
  26. {
  27.     /*
  28.      * we're registering at .1.2.3.4 
  29.      */
  30.     netsnmp_handler_registration *my_test;
  31.     netsnmp_table_registration_info *table_info;
  32.     u_long          ind1;
  33.     netsnmp_table_data *table;
  34.     netsnmp_table_data_set *table_set;
  35.     netsnmp_table_row *row;
  36.     DEBUGMSGTL(("testhandler", "initializingn"));
  37.     /*
  38.      * basic handler test
  39.      */
  40.     netsnmp_register_handler(netsnmp_create_handler_registration
  41.                              ("myTest", my_test_handler, my_test_oid, 4,
  42.                               HANDLER_CAN_RONLY));
  43.     /*
  44.      * instance handler test
  45.      */
  46.     netsnmp_register_instance(netsnmp_create_handler_registration
  47.                               ("myInstance", my_test_instance_handler,
  48.                                my_instance_oid, 5, HANDLER_CAN_RWRITE));
  49.     netsnmp_register_ulong_instance("myulong",
  50.                                     my_data_ulong_instance, 4,
  51.                                     &my_ulong, NULL);
  52.     /*
  53.      * table helper test
  54.      */
  55.     my_test = netsnmp_create_handler_registration("myTable",
  56.                                                   my_test_table_handler,
  57.                                                   my_table_oid, 4,
  58.                                                   HANDLER_CAN_RONLY);
  59.     if (!my_test)
  60.         return;
  61.     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  62.     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_INTEGER,
  63.                                      0);
  64.     table_info->min_column = 3;
  65.     table_info->max_column = 3;
  66.     netsnmp_register_table(my_test, table_info);
  67.     /*
  68.      * data table helper test
  69.      */
  70.     /*
  71.      * we'll construct a simple table here with two indexes: an
  72.      * integer and a string (why not).  It'll contain only one
  73.      * column so the data pointer is merely the data in that
  74.      * column. 
  75.      */
  76.     table = netsnmp_create_table_data("data_table_test");
  77.     netsnmp_table_data_add_index(table, ASN_INTEGER);
  78.     netsnmp_table_data_add_index(table, ASN_OCTET_STR);
  79.     /*
  80.      * 1 partridge in a pear tree 
  81.      */
  82.     row = netsnmp_create_table_data_row();
  83.     ind1 = 1;
  84.     netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1));
  85.     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "partridge",
  86.                                 strlen("partridge"));
  87.     row->data = (void *) "pear tree";
  88.     netsnmp_table_data_add_row(table, row);
  89.     /*
  90.      * 2 turtle doves 
  91.      */
  92.     row = netsnmp_create_table_data_row();
  93.     ind1 = 2;
  94.     netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1));
  95.     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "turtle",
  96.                                 strlen("turtle"));
  97.     row->data = (void *) "doves";
  98.     netsnmp_table_data_add_row(table, row);
  99.     /*
  100.      * we're going to register it as a normal table too, so we get the
  101.      * automatically parsed column and index information 
  102.      */
  103.     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  104.     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,
  105.                                      ASN_OCTET_STR, 0);
  106.     table_info->min_column = 3;
  107.     table_info->max_column = 3;
  108.     netsnmp_register_read_only_table_data
  109.         (netsnmp_create_handler_registration
  110.          ("12days", my_data_table_handler, my_data_table_oid, 4,
  111.           HANDLER_CAN_RONLY), table, table_info);
  112. }
  113. int
  114. my_test_handler(netsnmp_mib_handler *handler,
  115.                 netsnmp_handler_registration *reginfo,
  116.                 netsnmp_agent_request_info *reqinfo,
  117.                 netsnmp_request_info *requests)
  118. {
  119.     oid             myoid1[] = { 1, 2, 3, 4, 5, 6 };
  120.     static u_long   accesses = 0;
  121.     DEBUGMSGTL(("testhandler", "Got request:n"));
  122.     /*
  123.      * loop through requests 
  124.      */
  125.     while (requests) {
  126.         netsnmp_variable_list *var = requests->requestvb;
  127.         DEBUGMSGTL(("testhandler", "  oid:"));
  128.         DEBUGMSGOID(("testhandler", var->name, var->name_length));
  129.         DEBUGMSG(("testhandler", "n"));
  130.         switch (reqinfo->mode) {
  131.         case MODE_GET:
  132.             if (netsnmp_oid_equals(var->name, var->name_length, myoid1, 6)
  133.                 == 0) {
  134.                 snmp_set_var_typed_value(var, ASN_INTEGER,
  135.                                          (u_char *) & accesses,
  136.                                          sizeof(accesses));
  137.                 return SNMP_ERR_NOERROR;
  138.             }
  139.             break;
  140.         case MODE_GETNEXT:
  141.             if (snmp_oid_compare(var->name, var->name_length, myoid1, 6)
  142.                 < 0) {
  143.                 snmp_set_var_objid(var, myoid1, 6);
  144.                 snmp_set_var_typed_value(var, ASN_INTEGER,
  145.                                          (u_char *) & accesses,
  146.                                          sizeof(accesses));
  147.                 return SNMP_ERR_NOERROR;
  148.             }
  149.             break;
  150.         default:
  151.             netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR);
  152.             break;
  153.         }
  154.         requests = requests->next;
  155.     }
  156.     return SNMP_ERR_NOERROR;
  157. }
  158. /*
  159.  * functionally this is a simply a multiplication table for 12x12
  160.  */
  161. #define MAX_COLONE 12
  162. #define MAX_COLTWO 12
  163. #define RESULT_COLUMN 3
  164. int
  165. my_test_table_handler(netsnmp_mib_handler *handler,
  166.                       netsnmp_handler_registration *reginfo,
  167.                       netsnmp_agent_request_info *reqinfo,
  168.                       netsnmp_request_info *requests)
  169. {
  170.     netsnmp_table_registration_info
  171.      
  172.         
  173.         
  174.         
  175.         
  176.         
  177.         
  178.         
  179.         
  180.         
  181.         
  182.         
  183.         
  184.         
  185.         *handler_reg_info =
  186.         (netsnmp_table_registration_info *) handler->prev->myvoid;
  187.     netsnmp_table_request_info *table_info;
  188.     u_long          result;
  189.     int             x, y;
  190.     while (requests) {
  191.         netsnmp_variable_list *var = requests->requestvb;
  192.         if (requests->processed != 0)
  193.             continue;
  194.         DEBUGMSGTL(("testhandler_table", "Got request:n"));
  195.         DEBUGMSGTL(("testhandler_table", "  oid:"));
  196.         DEBUGMSGOID(("testhandler_table", var->name, var->name_length));
  197.         DEBUGMSG(("testhandler_table", "n"));
  198.         table_info = netsnmp_extract_table_info(requests);
  199.         if (table_info == NULL) {
  200.             requests = requests->next;
  201.             continue;
  202.         }
  203.         switch (reqinfo->mode) {
  204.         case MODE_GETNEXT:
  205.             /*
  206.              * beyond our search range? 
  207.              */
  208.             if (table_info->colnum > RESULT_COLUMN)
  209.                 break;
  210.             /*
  211.              * below our minimum column? 
  212.              */
  213.             if (table_info->colnum < RESULT_COLUMN ||
  214.                 /*
  215.                  * or no index specified 
  216.                  */
  217.                 table_info->indexes->val.integer == 0) {
  218.                 table_info->colnum = RESULT_COLUMN;
  219.                 x = 0;
  220.                 y = 0;
  221.             } else {
  222.                 x = *(table_info->indexes->val.integer);
  223.                 y = *(table_info->indexes->next_variable->val.integer);
  224.             }
  225.             if (table_info->number_indexes ==
  226.                 handler_reg_info->number_indexes) {
  227.                 y++;            /* GETNEXT is basically just y+1 for this table */
  228.                 if (y > MAX_COLTWO) {   /* (with wrapping) */
  229.                     y = 0;
  230.                     x++;
  231.                 }
  232.             }
  233.             if (x <= MAX_COLONE) {
  234.                 result = x * y;
  235.                 *(table_info->indexes->val.integer) = x;
  236.                 *(table_info->indexes->next_variable->val.integer) = y;
  237.                 netsnmp_table_build_result(reginfo, requests,
  238.                                            table_info, ASN_INTEGER,
  239.                                            (u_char *) & result,
  240.                                            sizeof(result));
  241.             }
  242.             break;
  243.         case MODE_GET:
  244.             if (var->type == ASN_NULL) {        /* valid request if ASN_NULL */
  245.                 /*
  246.                  * is it the right column? 
  247.                  */
  248.                 if (table_info->colnum == RESULT_COLUMN &&
  249.                     /*
  250.                      * and within the max boundries? 
  251.                      */
  252.                     *(table_info->indexes->val.integer) <= MAX_COLONE &&
  253.                     *(table_info->indexes->next_variable->val.integer)
  254.                     <= MAX_COLTWO) {
  255.                     /*
  256.                      * then, the result is column1 * column2 
  257.                      */
  258.                     result = *(table_info->indexes->val.integer) *
  259.                         *(table_info->indexes->next_variable->val.integer);
  260.                     snmp_set_var_typed_value(var, ASN_INTEGER,
  261.                                              (u_char *) & result,
  262.                                              sizeof(result));
  263.                 }
  264.             }
  265.             break;
  266.         }
  267.         requests = requests->next;
  268.     }
  269.     return SNMP_ERR_NOERROR;
  270. }
  271. #define TESTHANDLER_SET_NAME "my_test"
  272. int
  273. my_test_instance_handler(netsnmp_mib_handler *handler,
  274.                          netsnmp_handler_registration *reginfo,
  275.                          netsnmp_agent_request_info *reqinfo,
  276.                          netsnmp_request_info *requests)
  277. {
  278.     static u_long   accesses = 0;
  279.     u_long         *accesses_cache = NULL;
  280.     DEBUGMSGTL(("testhandler", "Got instance request:n"));
  281.     switch (reqinfo->mode) {
  282.     case MODE_GET:
  283.         snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
  284.                                  (u_char *) & accesses, sizeof(accesses));
  285.         break;
  286.     case MODE_SET_RESERVE1:
  287.         if (requests->requestvb->type != ASN_UNSIGNED)
  288.             netsnmp_set_request_error(reqinfo, requests,
  289.                                       SNMP_ERR_WRONGTYPE);
  290.         break;
  291.     case MODE_SET_RESERVE2:
  292.         /*
  293.          * store old info for undo later 
  294.          */
  295.         memdup((u_char **) & accesses_cache,
  296.                (u_char *) & accesses, sizeof(accesses));
  297.         if (accesses_cache == NULL) {
  298.             netsnmp_set_request_error(reqinfo, requests,
  299.                                       SNMP_ERR_RESOURCEUNAVAILABLE);
  300.             return SNMP_ERR_NOERROR;
  301.         }
  302.         netsnmp_request_add_list_data(requests,
  303.                                       netsnmp_create_data_list
  304.                                       (TESTHANDLER_SET_NAME,
  305.                                        accesses_cache, free));
  306.         break;
  307.     case MODE_SET_ACTION:
  308.         /*
  309.          * update current 
  310.          */
  311.         accesses = *(requests->requestvb->val.integer);
  312.         DEBUGMSGTL(("testhandler", "updated accesses -> %dn", accesses));
  313.         break;
  314.     case MODE_SET_UNDO:
  315.         accesses =
  316.             *((u_long *) netsnmp_request_get_list_data(requests,
  317.                                                        TESTHANDLER_SET_NAME));
  318.         break;
  319.     case MODE_SET_COMMIT:
  320.     case MODE_SET_FREE:
  321.         /*
  322.          * nothing to do 
  323.          */
  324.         break;
  325.     }
  326.     return SNMP_ERR_NOERROR;
  327. }
  328. int
  329. my_data_table_handler(netsnmp_mib_handler *handler,
  330.                       netsnmp_handler_registration *reginfo,
  331.                       netsnmp_agent_request_info *reqinfo,
  332.                       netsnmp_request_info *requests)
  333. {
  334.     char           *column3;
  335.     netsnmp_table_request_info *table_info;
  336.     netsnmp_table_row *row;
  337.     while (requests) {
  338.         if (requests->processed)
  339.             continue;
  340.         /*
  341.          * extract our stored data and table info 
  342.          */
  343.         row = netsnmp_extract_table_row(requests);
  344.         table_info = netsnmp_extract_table_info(requests);
  345.         if (row)
  346.             column3 = (char *) row->data;
  347.         if (!row || !table_info || !column3)
  348.             continue;
  349.         /*
  350.          * there's only one column, we don't need to check if it's right 
  351.          */
  352.         netsnmp_table_data_build_result(reginfo, reqinfo, requests, row,
  353.                                         table_info->colnum,
  354.                                         ASN_OCTET_STR, column3,
  355.                                         strlen(column3));
  356.         requests = requests->next;
  357.     }
  358.     return SNMP_ERR_NOERROR;
  359. }