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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *Copyright(c)2004,Cisco URP imburses and Network Information Center in Beijing University of Posts and Telecommunications researches.
  3.  *
  4.  *All right reserved
  5.  *
  6.  *File Name:lookupResultsTable.c
  7.  *File Description:Rows of the lookupResultsTable MIB read.
  8.  *
  9.  *Current Version:1.0
  10.  *Author:ChenJing
  11.  *Date:2004.8.20
  12.  */
  13. /*
  14.  * This should always be included first before anything else 
  15.  */
  16. #if HAVE_STDLIB_H
  17. #include <stdlib.h>
  18. #endif
  19. #if HAVE_STRING_H
  20. #include <string.h>
  21. #else
  22. #include <strings.h>
  23. #endif
  24. #ifdef HAVE_LIMITS_H
  25. #include <limits.h>
  26. #endif
  27. #include <net-snmp/net-snmp-config.h>
  28. #include <net-snmp/net-snmp-includes.h>
  29. #include <net-snmp/agent/net-snmp-agent-includes.h>
  30. #include "lookupCtlTable.h"
  31. #include "lookupResultsTable.h"
  32. #include "header_complex.h"
  33. oid             lookupResultsTable_variables_oid[] =
  34.     { 1, 3, 6, 1, 2, 1, 82, 1, 4 };
  35. struct variable2 lookupResultsTable_variables[] = {
  36.     {COLUMN_LOOKUPRESULTSADDRESSTYPE, ASN_INTEGER, RONLY, var_lookupResultsTable, 2, {1, 2}},
  37.     {COLUMN_LOOKUPRESULTSADDRESS,   ASN_OCTET_STR, RONLY, var_lookupResultsTable, 2, {1, 3}}
  38. };
  39. /*
  40.  * global storage of our data, saved in and configured by header_complex() 
  41.  */
  42. extern struct header_complex_index *lookupCtlTableStorage;
  43. extern struct header_complex_index *lookupResultsTableStorage;
  44. void
  45. lookupResultsTable_cleaner(struct header_complex_index *thestuff)
  46. {
  47.     struct header_complex_index *hciptr = NULL;
  48.     struct lookupResultsTable_data *StorageDel = NULL;
  49.     DEBUGMSGTL(("lookupResultsTable", "cleanerout  "));
  50.     for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
  51.         StorageDel =
  52.             header_complex_extract_entry(&lookupResultsTableStorage,
  53.                                          hciptr);
  54.         if (StorageDel != NULL) {
  55.             SNMP_FREE(StorageDel->lookupCtlOwnerIndex);
  56.             SNMP_FREE(StorageDel->lookupCtlOperationName);
  57.             SNMP_FREE(StorageDel->lookupResultsAddress);
  58.             SNMP_FREE(StorageDel);
  59.         }
  60.         DEBUGMSGTL(("lookupResultsTable", "cleaner  "));
  61.     }
  62. }
  63. void
  64. init_lookupResultsTable(void)
  65. {
  66.     DEBUGMSGTL(("lookupResultsTable", "initializing...  "));
  67.     /*
  68.      * register ourselves with the agent to handle our mib tree 
  69.      */
  70.     REGISTER_MIB("lookupResultsTable", lookupResultsTable_variables,
  71.                  variable2, lookupResultsTable_variables_oid);
  72.     /*
  73.      * register our config handler(s) to deal with registrations 
  74.      */
  75.     snmpd_register_config_handler("lookupResultsTable",
  76.                                   parse_lookupResultsTable, NULL, NULL);
  77.     /*
  78.      * we need to be called back later to store our data 
  79.      */
  80.     snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
  81.                            store_lookupResultsTable, NULL);
  82.     DEBUGMSGTL(("lookupResultsTable", "done.n"));
  83. }
  84. /*
  85.  * parse_mteObjectsTable():
  86.  *   parses .conf file entries needed to configure the mib.
  87.  */
  88. void
  89. parse_lookupResultsTable(const char *token, char *line)
  90. {
  91.     size_t          tmpint;
  92.     struct lookupResultsTable_data *StorageTmp =
  93.         SNMP_MALLOC_STRUCT(lookupResultsTable_data);
  94.     DEBUGMSGTL(("lookupResultsTable", "parsing config...  "));
  95.     if (StorageTmp == NULL) {
  96.         config_perror("malloc failure");
  97.         return;
  98.     }
  99.     line =
  100.         read_config_read_data(ASN_OCTET_STR, line,
  101.                               &StorageTmp->lookupCtlOwnerIndex,
  102.                               &StorageTmp->lookupCtlOwnerIndexLen);
  103.     if (StorageTmp->lookupCtlOwnerIndex == NULL) {
  104.         config_perror("invalid specification for lookupCtlOwnerIndex");
  105.         return;
  106.     }
  107.     line =
  108.         read_config_read_data(ASN_OCTET_STR, line,
  109.                               &StorageTmp->lookupCtlOperationName,
  110.                               &StorageTmp->lookupCtlOperationNameLen);
  111.     if (StorageTmp->lookupCtlOperationName == NULL) {
  112.         config_perror("invalid specification for lookupCtlOperationName");
  113.         return;
  114.     }
  115.     line =
  116.         read_config_read_data(ASN_UNSIGNED, line,
  117.                               &StorageTmp->lookupResultsIndex, &tmpint);
  118.     line =
  119.         read_config_read_data(ASN_INTEGER, line,
  120.                               &StorageTmp->lookupResultsAddressType,
  121.                               &tmpint);
  122.     line =
  123.         read_config_read_data(ASN_OCTET_STR, line,
  124.                               &StorageTmp->lookupResultsAddress,
  125.                               &StorageTmp->lookupResultsAddressLen);
  126.     if (StorageTmp->lookupResultsAddress == NULL) {
  127.         config_perror("invalid specification for lookupResultsAddress");
  128.         return;
  129.     }
  130.     lookupResultsTable_inadd(StorageTmp);
  131.     /* lookupResultsTable_cleaner(lookupResultsTableStorage); */
  132.     DEBUGMSGTL(("lookupResultsTable", "done.n"));
  133. }
  134. /*
  135.  * store_lookupResultsTable():
  136.  *   stores .conf file entries needed to configure the mib.
  137.  */
  138. int
  139. store_lookupResultsTable(int majorID, int minorID, void *serverarg,
  140.                          void *clientarg)
  141. {
  142.     char            line[SNMP_MAXBUF];
  143.     char           *cptr = NULL;
  144.     size_t          tmpint;
  145.     struct lookupResultsTable_data *StorageTmp = NULL;
  146.     struct header_complex_index *hcindex = NULL;
  147.     DEBUGMSGTL(("lookupResultsTable", "storing data...  "));
  148.     for (hcindex = lookupResultsTableStorage; hcindex != NULL;
  149.          hcindex = hcindex->next) {
  150.         StorageTmp = (struct lookupResultsTable_data *) hcindex->data;
  151.         if (StorageTmp->storagetype != ST_READONLY) {
  152.             memset(line, 0, sizeof(line));
  153.             strcat(line, "lookupResultsTable ");
  154.             cptr = line + strlen(line);
  155.             cptr =
  156.                 read_config_store_data(ASN_OCTET_STR, cptr,
  157.                                        &StorageTmp->lookupCtlOwnerIndex,
  158.                                        &StorageTmp->
  159.                                        lookupCtlOwnerIndexLen);
  160.             cptr =
  161.                 read_config_store_data(ASN_OCTET_STR, cptr,
  162.                                        &StorageTmp->lookupCtlOperationName,
  163.                                        &StorageTmp->
  164.                                        lookupCtlOperationNameLen);
  165.             cptr =
  166.                 read_config_store_data(ASN_UNSIGNED, cptr,
  167.                                        &StorageTmp->lookupResultsIndex,
  168.                                        &tmpint);
  169.             cptr =
  170.                 read_config_store_data(ASN_INTEGER, cptr,
  171.                                        &StorageTmp->
  172.                                        lookupResultsAddressType, &tmpint);
  173.             cptr =
  174.                 read_config_store_data(ASN_OCTET_STR, cptr,
  175.                                        &StorageTmp->lookupResultsAddress,
  176.                                        &StorageTmp->
  177.                                        lookupResultsAddressLen);
  178.             snmpd_store_config(line);
  179.         }
  180.     }
  181.     DEBUGMSGTL(("lookupResultsTable", "done.n"));
  182.     return SNMPERR_SUCCESS;
  183. }
  184. int
  185. lookupResultsTable_inadd(struct lookupResultsTable_data *thedata)
  186. {
  187.     netsnmp_variable_list *vars_list = NULL;
  188.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->lookupCtlOwnerIndex, thedata->lookupCtlOwnerIndexLen);      /* lookupCtlOwnerIndex */
  189.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->lookupCtlOperationName, thedata->lookupCtlOperationNameLen);        /* lookupCtlOperationName */
  190.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_UNSIGNED, (char *) &thedata->lookupResultsIndex, sizeof(thedata->lookupResultsIndex));   /* lookupResultsIndex */
  191.     /*
  192.      * XXX: fill in default row values here into StorageNew 
  193.      * 
  194.      */
  195.     DEBUGMSGTL(("lookupResultsTable", "adding data...  "));
  196.     /*
  197.      * add the index variables to the varbind list, which is 
  198.      * used by header_complex to index the data 
  199.      */
  200.     header_complex_add_data(&lookupResultsTableStorage, vars_list,
  201.                             thedata);
  202.     DEBUGMSGTL(("lookupResultsTable", "registered an entryn"));
  203.     DEBUGMSGTL(("lookupResultsTable", "done.n"));
  204.     vars_list = NULL;
  205.     return SNMPERR_SUCCESS;
  206. }
  207. /*
  208.  * var_lookupResultsTable():
  209.  *   Handle this table separately from the scalar value case.
  210.  *   The workings of this are basically the same as for var_lookupResultsTable above.
  211.  */
  212. unsigned char  *
  213. var_lookupResultsTable(struct variable *vp,
  214.                        oid * name,
  215.                        size_t *length,
  216.                        int exact,
  217.                        size_t *var_len, WriteMethod ** write_method)
  218. {
  219.     struct lookupResultsTable_data *StorageTmp = NULL;
  220.     *write_method = NULL;
  221.     /*
  222.      * this assumes you have registered all your data properly
  223.      */
  224.     if ((StorageTmp =
  225.          header_complex(lookupResultsTableStorage, vp, name, length, exact,
  226.                         var_len, write_method)) == NULL) {
  227.         return NULL;
  228.     }
  229.     /*
  230.      * this is where we do the value assignments for the mib results.
  231.      */
  232.     switch (vp->magic) {
  233.     case COLUMN_LOOKUPRESULTSADDRESSTYPE:
  234.         *var_len = sizeof(StorageTmp->lookupResultsAddressType);
  235.         return (u_char *) & StorageTmp->lookupResultsAddressType;
  236.     case COLUMN_LOOKUPRESULTSADDRESS:
  237.         *var_len = (StorageTmp->lookupResultsAddressLen);
  238.         return (u_char *) StorageTmp->lookupResultsAddress;
  239.     default:
  240.         ERROR_MSG("");
  241.     }
  242.     return NULL;
  243. }