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

SNMP编程

开发平台:

Unix_Linux

  1. #include <net-snmp/net-snmp-config.h>
  2. #if HAVE_STRING_H
  3. #include <string.h>
  4. #else
  5. #include <strings.h>
  6. #endif
  7. #include <net-snmp/net-snmp-includes.h>
  8. #include <net-snmp/agent/net-snmp-agent-includes.h>
  9. #include <net-snmp/agent/table.h>
  10. #include <net-snmp/agent/table_iterator.h>
  11. #include "vacm_context.h"
  12. static oid      vacm_context_oid[] = { 1, 3, 6, 1, 6, 3, 16, 1, 1 };
  13. #define CONTEXTNAME_COLUMN 1
  14. /*
  15.  * return the index data from the first node in the agent's
  16.  * subtree_context_cache list.
  17.  */
  18. netsnmp_variable_list *
  19. get_first_context(void **my_loop_context, void **my_data_context,
  20.                   netsnmp_variable_list * put_data,
  21.                   netsnmp_iterator_info *iinfo)
  22. {
  23.     subtree_context_cache *context_ptr;
  24.     context_ptr = get_top_context_cache();
  25.     if (!context_ptr)
  26.         return NULL;
  27.     *my_loop_context = context_ptr;
  28.     *my_data_context = context_ptr;
  29.     snmp_set_var_value(put_data, context_ptr->context_name,
  30.                        strlen(context_ptr->context_name));
  31.     return put_data;
  32. }
  33. /*
  34.  * return the next index data from the first node in the agent's
  35.  * subtree_context_cache list.
  36.  */
  37. netsnmp_variable_list *
  38. get_next_context(void **my_loop_context,
  39.                  void **my_data_context,
  40.                  netsnmp_variable_list * put_data,
  41.                  netsnmp_iterator_info *iinfo)
  42. {
  43.     subtree_context_cache *context_ptr;
  44.     if (!my_loop_context || !*my_loop_context)
  45.         return NULL;
  46.     context_ptr = (subtree_context_cache *) (*my_loop_context);
  47.     context_ptr = context_ptr->next;
  48.     *my_loop_context = context_ptr;
  49.     *my_data_context = context_ptr;
  50.     if (!context_ptr)
  51.         return NULL;
  52.     snmp_set_var_value(put_data, context_ptr->context_name,
  53.                        strlen(context_ptr->context_name));
  54.     return put_data;
  55. }
  56. void
  57. init_vacm_context(void)
  58. {
  59.     /*
  60.      * table vacm_context
  61.      */
  62.     netsnmp_handler_registration *my_handler;
  63.     netsnmp_table_registration_info *table_info;
  64.     netsnmp_iterator_info *iinfo;
  65.     my_handler = netsnmp_create_handler_registration("vacm_context",
  66.                                                      vacm_context_handler,
  67.                                                      vacm_context_oid,
  68.                                                      sizeof
  69.                                                      (vacm_context_oid) /
  70.                                                      sizeof(oid),
  71.                                                      HANDLER_CAN_RONLY);
  72.     if (!my_handler)
  73.         return;
  74.     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
  75.     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
  76.     if (!table_info || !iinfo)
  77.         return;
  78.     netsnmp_table_helper_add_index(table_info, ASN_OCTET_STR)
  79.         table_info->min_column = 1;
  80.     table_info->max_column = 1;
  81.     iinfo->get_first_data_point = get_first_context;
  82.     iinfo->get_next_data_point = get_next_context;
  83.     iinfo->table_reginfo = table_info;
  84.     netsnmp_register_table_iterator(my_handler, iinfo);
  85. }
  86. /*
  87.  * returns a list of known context names
  88.  */
  89. int
  90. vacm_context_handler(netsnmp_mib_handler *handler,
  91.                      netsnmp_handler_registration *reginfo,
  92.                      netsnmp_agent_request_info *reqinfo,
  93.                      netsnmp_request_info *requests)
  94. {
  95.     subtree_context_cache *context_ptr;
  96.     for(; requests; requests = requests->next) {
  97.         netsnmp_variable_list *var = requests->requestvb;
  98.         if (requests->processed != 0)
  99.             continue;
  100.         context_ptr = (subtree_context_cache *)
  101.             netsnmp_extract_iterator_context(requests);
  102.         if (context_ptr == NULL) {
  103.             snmp_log(LOG_ERR,
  104.                      "vacm_context_handler called without datan");
  105.             continue;
  106.         }
  107.         switch (reqinfo->mode) {
  108.         case MODE_GET:
  109.             /*
  110.              * if here we should have a context_ptr passed in already 
  111.              */
  112.             /*
  113.              * only one column should ever reach us, so don't check it 
  114.              */
  115.             snmp_set_var_typed_value(var, ASN_OCTET_STR,
  116.                                      context_ptr->context_name,
  117.                                      strlen(context_ptr->context_name));
  118.             break;
  119.         default:
  120.             /*
  121.              * We should never get here, getnext already have been
  122.              * handled by the table_iterator and we're read_only 
  123.              */
  124.             snmp_log(LOG_ERR,
  125.                      "vacm_context table accessed as mode=%d.  We're improperly registered!",
  126.                      reqinfo->mode);
  127.             break;
  128.         }
  129.     }
  130.     return SNMP_ERR_NOERROR;
  131. }