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

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:traceRouteHopsTable.c
  7.  *File Description:Rows of traceRouteHopsTable MIB read.
  8.  *
  9.  *Current Version:1.0
  10.  *Author:ChenJing
  11.  *Date:2004.8.20
  12.  */
  13. #include <net-snmp/net-snmp-config.h>
  14. #if HAVE_STDLIB_H
  15. #include <stdlib.h>
  16. #endif
  17. #if HAVE_STRING_H
  18. #include <string.h>
  19. #else
  20. #include <strings.h>
  21. #endif
  22. #ifdef HAVE_LIMITS_H
  23. #include <limits.h>
  24. #endif
  25. #include <net-snmp/net-snmp-config.h>
  26. #include <net-snmp/net-snmp-includes.h>
  27. #include <net-snmp/agent/net-snmp-agent-includes.h>
  28. #include "traceRouteCtlTable.h"
  29. #include "traceRouteResultsTable.h"
  30. #include "traceRouteProbeHistoryTable.h"
  31. #include "traceRouteHopsTable.h"
  32. #include "header_complex.h"
  33. oid             traceRouteHopsTable_variables_oid[] =
  34.     { 1, 3, 6, 1, 2, 1, 81, 1, 5 };
  35. struct variable2 traceRouteHopsTable_variables[] = {
  36.     {COLUMN_TRACEROUTEHOPSIPTGTADDRESSTYPE, ASN_INTEGER, RONLY, var_traceRouteHopsTable, 2, {1, 2}},
  37.     {COLUMN_TRACEROUTEHOPSIPTGTADDRESS,   ASN_OCTET_STR, RONLY, var_traceRouteHopsTable, 2, {1, 3}},
  38.     {COLUMN_TRACEROUTEHOPSMINRTT,          ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 4}},
  39.     {COLUMN_TRACEROUTEHOPSMAXRTT,          ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 5}},
  40.     {COLUMN_TRACEROUTEHOPSAVERAGERTT,      ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 6}},
  41.     {COLUMN_TRACEROUTEHOPSRTTSUMOFSQUARES, ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 7}},
  42.     {COLUMN_TRACEROUTEHOPSSENTPROBES,      ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 8}},
  43.     {COLUMN_TRACEROUTEHOPSPROBERESPONSES,  ASN_UNSIGNED, RONLY, var_traceRouteHopsTable, 2, {1, 9}},
  44.     {COLUMN_TRACEROUTEHOPSLASTGOODPROBE,  ASN_OCTET_STR, RONLY, var_traceRouteHopsTable, 2, {1, 10}}
  45. };
  46. /*
  47.  * global storage of our data, saved in and configured by header_complex() 
  48.  */
  49. extern struct header_complex_index *traceRouteCtlTableStorage;
  50. extern struct header_complex_index *traceRouteHopsTableStorage;
  51. void
  52. traceRouteHopsTable_cleaner(struct header_complex_index *thestuff)
  53. {
  54.     struct header_complex_index *hciptr = NULL;
  55.     struct traceRouteHopsTable_data *StorageDel = NULL;
  56.     DEBUGMSGTL(("traceRouteHopsTable", "cleanerout  "));
  57.     for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
  58.         StorageDel =
  59.             header_complex_extract_entry(&traceRouteHopsTableStorage,
  60.                                          hciptr);
  61.         if (StorageDel != NULL) {
  62.             free(StorageDel->traceRouteCtlOwnerIndex);
  63.             StorageDel->traceRouteCtlOwnerIndex = NULL;
  64.             free(StorageDel->traceRouteCtlTestName);
  65.             StorageDel->traceRouteCtlTestName = NULL;
  66.             free(StorageDel->traceRouteHopsLastGoodProbe);
  67.             StorageDel->traceRouteHopsLastGoodProbe = NULL;
  68.             free(StorageDel);
  69.             StorageDel = NULL;
  70.         }
  71.         DEBUGMSGTL(("traceRouteHopsTable", "cleaner  "));
  72.     }
  73. }
  74. void
  75. init_traceRouteHopsTable(void)
  76. {
  77.     DEBUGMSGTL(("traceRouteHopsTable", "initializing...  "));
  78.     /*
  79.      * register ourselves with the agent to handle our mib tree 
  80.      */
  81.     REGISTER_MIB("traceRouteHopsTable", traceRouteHopsTable_variables,
  82.                  variable2, traceRouteHopsTable_variables_oid);
  83.     /*
  84.      * register our config handler(s) to deal with registrations 
  85.      */
  86.     snmpd_register_config_handler("traceRouteHopsTable",
  87.                                   parse_traceRouteHopsTable, NULL, NULL);
  88.     /*
  89.      * we need to be called back later to store our data 
  90.      */
  91.     snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
  92.                            store_traceRouteHopsTable, NULL);
  93.     DEBUGMSGTL(("traceRouteHopsTable", "done.n"));
  94. }
  95. /*
  96.  * parse_mteObjectsTable():
  97.  *   parses .conf file entries needed to configure the mib.
  98.  */
  99. void
  100. parse_traceRouteHopsTable(const char *token, char *line)
  101. {
  102.     size_t          tmpint;
  103.     struct traceRouteHopsTable_data *StorageTmp =
  104.         SNMP_MALLOC_STRUCT(traceRouteHopsTable_data);
  105.     DEBUGMSGTL(("traceRouteHopsTable", "parsing config...  "));
  106.     if (StorageTmp == NULL) {
  107.         config_perror("malloc failure");
  108.         return;
  109.     }
  110.     line =
  111.         read_config_read_data(ASN_OCTET_STR, line,
  112.                               &StorageTmp->traceRouteCtlOwnerIndex,
  113.                               &StorageTmp->traceRouteCtlOwnerIndexLen);
  114.     if (StorageTmp->traceRouteCtlOwnerIndex == NULL) {
  115.         config_perror("invalid specification for traceRouteCtlOwnerIndex");
  116.         return;
  117.     }
  118.     line =
  119.         read_config_read_data(ASN_OCTET_STR, line,
  120.                               &StorageTmp->traceRouteCtlTestName,
  121.                               &StorageTmp->traceRouteCtlTestNameLen);
  122.     if (StorageTmp->traceRouteCtlTestName == NULL) {
  123.         config_perror("invalid specification for traceRouteCtlTestName");
  124.         return;
  125.     }
  126.     line =
  127.         read_config_read_data(ASN_UNSIGNED, line,
  128.                               &StorageTmp->traceRouteHopsHopIndex,
  129.                               &tmpint);
  130.     line =
  131.         read_config_read_data(ASN_INTEGER, line,
  132.                               &StorageTmp->traceRouteHopsIpTgtAddressType,
  133.                               &tmpint);
  134.     line =
  135.         read_config_read_data(ASN_OCTET_STR, line,
  136.                               &StorageTmp->traceRouteHopsIpTgtAddress,
  137.                               &StorageTmp->traceRouteHopsIpTgtAddressLen);
  138.     if (StorageTmp->traceRouteHopsIpTgtAddress == NULL) {
  139.         config_perror
  140.             ("invalid specification for traceRouteHopsIpTgtAddress");
  141.         return;
  142.     }
  143.     line =
  144.         read_config_read_data(ASN_UNSIGNED, line,
  145.                               &StorageTmp->traceRouteHopsMinRtt, &tmpint);
  146.     line =
  147.         read_config_read_data(ASN_UNSIGNED, line,
  148.                               &StorageTmp->traceRouteHopsMaxRtt, &tmpint);
  149.     line =
  150.         read_config_read_data(ASN_UNSIGNED, line,
  151.                               &StorageTmp->traceRouteHopsAverageRtt,
  152.                               &tmpint);
  153.     line =
  154.         read_config_read_data(ASN_UNSIGNED, line,
  155.                               &StorageTmp->traceRouteHopsRttSumOfSquares,
  156.                               &tmpint);
  157.     line =
  158.         read_config_read_data(ASN_UNSIGNED, line,
  159.                               &StorageTmp->traceRouteHopsSentProbes,
  160.                               &tmpint);
  161.     line =
  162.         read_config_read_data(ASN_UNSIGNED, line,
  163.                               &StorageTmp->traceRouteHopsProbeResponses,
  164.                               &tmpint);
  165.     line =
  166.         read_config_read_data(ASN_OCTET_STR, line,
  167.                               &StorageTmp->traceRouteHopsLastGoodProbe,
  168.                               &StorageTmp->traceRouteHopsLastGoodProbeLen);
  169.     if (StorageTmp->traceRouteHopsLastGoodProbe == NULL) {
  170.         config_perror
  171.             ("invalid specification for traceRouteHopsLastGoodProbe");
  172.         return;
  173.     }
  174.     traceRouteHopsTable_inadd(StorageTmp);
  175.     /* traceRouteHopsTable_cleaner(traceRouteHopsTableStorage); */
  176.     DEBUGMSGTL(("traceRouteHopsTable", "done.n"));
  177. }
  178. /*
  179.  * store_traceRouteHopsTable():
  180.  *   stores .conf file entries needed to configure the mib.
  181.  */
  182. int
  183. store_traceRouteHopsTable(int majorID, int minorID, void *serverarg,
  184.                           void *clientarg)
  185. {
  186.     char            line[SNMP_MAXBUF];
  187.     char           *cptr = NULL;
  188.     size_t          tmpint;
  189.     struct traceRouteHopsTable_data *StorageTmp = NULL;
  190.     struct header_complex_index *hcindex = NULL;
  191.     DEBUGMSGTL(("traceRouteHopsTable", "storing data...  "));
  192.     for (hcindex = traceRouteHopsTableStorage; hcindex != NULL;
  193.          hcindex = hcindex->next) {
  194.         StorageTmp = (struct traceRouteHopsTable_data *) hcindex->data;
  195.         if (StorageTmp->storageType != ST_READONLY) {
  196.             memset(line, 0, sizeof(line));
  197.             strcat(line, "traceRouteHopsTable ");
  198.             cptr = line + strlen(line);
  199.             cptr =
  200.                 read_config_store_data(ASN_OCTET_STR, cptr,
  201.                                        &StorageTmp->
  202.                                        traceRouteCtlOwnerIndex,
  203.                                        &StorageTmp->
  204.                                        traceRouteCtlOwnerIndexLen);
  205.             cptr =
  206.                 read_config_store_data(ASN_OCTET_STR, cptr,
  207.                                        &StorageTmp->traceRouteCtlTestName,
  208.                                        &StorageTmp->
  209.                                        traceRouteCtlTestNameLen);
  210.             cptr =
  211.                 read_config_store_data(ASN_UNSIGNED, cptr,
  212.                                        &StorageTmp->traceRouteHopsHopIndex,
  213.                                        &tmpint);
  214.             cptr =
  215.                 read_config_store_data(ASN_INTEGER, cptr,
  216.                                        &StorageTmp->
  217.                                        traceRouteHopsIpTgtAddressType,
  218.                                        &tmpint);
  219.             cptr =
  220.                 read_config_store_data(ASN_OCTET_STR, cptr,
  221.                                        &StorageTmp->
  222.                                        traceRouteHopsIpTgtAddress,
  223.                                        &StorageTmp->
  224.                                        traceRouteHopsIpTgtAddressLen);
  225.             cptr =
  226.                 read_config_store_data(ASN_UNSIGNED, cptr,
  227.                                        &StorageTmp->traceRouteHopsMinRtt,
  228.                                        &tmpint);
  229.             cptr =
  230.                 read_config_store_data(ASN_UNSIGNED, cptr,
  231.                                        &StorageTmp->traceRouteHopsMaxRtt,
  232.                                        &tmpint);
  233.             cptr =
  234.                 read_config_store_data(ASN_UNSIGNED, cptr,
  235.                                        &StorageTmp->
  236.                                        traceRouteHopsAverageRtt, &tmpint);
  237.             cptr =
  238.                 read_config_store_data(ASN_UNSIGNED, cptr,
  239.                                        &StorageTmp->
  240.                                        traceRouteHopsRttSumOfSquares,
  241.                                        &tmpint);
  242.             cptr =
  243.                 read_config_store_data(ASN_UNSIGNED, cptr,
  244.                                        &StorageTmp->
  245.                                        traceRouteHopsSentProbes, &tmpint);
  246.             cptr =
  247.                 read_config_store_data(ASN_UNSIGNED, cptr,
  248.                                        &StorageTmp->
  249.                                        traceRouteHopsProbeResponses,
  250.                                        &tmpint);
  251.             cptr =
  252.                 read_config_store_data(ASN_OCTET_STR, cptr,
  253.                                        &StorageTmp->
  254.                                        traceRouteHopsLastGoodProbe,
  255.                                        &StorageTmp->
  256.                                        traceRouteHopsLastGoodProbeLen);
  257.             snmpd_store_config(line);
  258.         }
  259.     }
  260.     DEBUGMSGTL(("traceRouteHopsTable", "done.n"));
  261.     return SNMPERR_SUCCESS;
  262. }
  263. int
  264. traceRouteHopsTable_inadd(struct traceRouteHopsTable_data *thedata)
  265. {
  266.     netsnmp_variable_list *vars_list = NULL;
  267.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->traceRouteCtlOwnerIndex, thedata->traceRouteCtlOwnerIndexLen);      /* traceRouteCtlOwnerIndex */
  268.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->traceRouteCtlTestName, thedata->traceRouteCtlTestNameLen);  /* traceRouteCtlTestName */
  269.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_UNSIGNED, (char *) &thedata->traceRouteHopsHopIndex, sizeof(thedata->traceRouteHopsHopIndex));   /* traceRouteHopsHopIndex */
  270.     DEBUGMSGTL(("traceRouteHopsTable", "adding data...  "));
  271.     /*
  272.      * add the index variables to the varbind list, which is 
  273.      * used by header_complex to index the data 
  274.      */
  275.     header_complex_add_data(&traceRouteHopsTableStorage, vars_list,
  276.                             thedata);
  277.     DEBUGMSGTL(("traceRouteHopsTable", "registered an entryn"));
  278.     DEBUGMSGTL(("traceRouteHopsTable", "done.n"));
  279. }
  280. /*
  281.  * var_traceRouteHopsTable():
  282.  *   Handle this table separately from the scalar value case.
  283.  *   The workings of this are basically the same as for var_traceRouteHopsTable above.
  284.  */
  285. unsigned char  *
  286. var_traceRouteHopsTable(struct variable *vp,
  287.                         oid * name,
  288.                         size_t *length,
  289.                         int exact,
  290.                         size_t *var_len, WriteMethod ** write_method)
  291. {
  292.     struct traceRouteHopsTable_data *StorageTmp = NULL;
  293.     *write_method = NULL;
  294.     /*
  295.      * this assumes you have registered all your data properly
  296.      */
  297.     if ((StorageTmp =
  298.          header_complex(traceRouteHopsTableStorage, vp, name, length,
  299.                         exact, var_len, write_method)) == NULL) {
  300.         return NULL;
  301.     }
  302.     /*
  303.      * this is where we do the value assignments for the mib results.
  304.      */
  305.     switch (vp->magic) {
  306.     case COLUMN_TRACEROUTEHOPSIPTGTADDRESSTYPE:
  307.         *var_len = sizeof(StorageTmp->traceRouteHopsIpTgtAddressType);
  308.         return (u_char *) & StorageTmp->traceRouteHopsIpTgtAddressType;
  309.     case COLUMN_TRACEROUTEHOPSIPTGTADDRESS:
  310.         *var_len = (StorageTmp->traceRouteHopsIpTgtAddressLen);
  311.         return (u_char *) StorageTmp->traceRouteHopsIpTgtAddress;
  312.     case COLUMN_TRACEROUTEHOPSMINRTT:
  313.         *var_len = sizeof(StorageTmp->traceRouteHopsMinRtt);
  314.         return (u_char *) & StorageTmp->traceRouteHopsMinRtt;
  315.     case COLUMN_TRACEROUTEHOPSMAXRTT:
  316.         *var_len = sizeof(StorageTmp->traceRouteHopsMaxRtt);
  317.         return (u_char *) & StorageTmp->traceRouteHopsMaxRtt;
  318.     case COLUMN_TRACEROUTEHOPSAVERAGERTT:
  319.         *var_len = sizeof(StorageTmp->traceRouteHopsAverageRtt);
  320.         return (u_char *) & StorageTmp->traceRouteHopsAverageRtt;
  321.     case COLUMN_TRACEROUTEHOPSRTTSUMOFSQUARES:
  322.         *var_len = sizeof(StorageTmp->traceRouteHopsRttSumOfSquares);
  323.         return (u_char *) & StorageTmp->traceRouteHopsRttSumOfSquares;
  324.     case COLUMN_TRACEROUTEHOPSSENTPROBES:
  325.         *var_len = sizeof(StorageTmp->traceRouteHopsSentProbes);
  326.         return (u_char *) & StorageTmp->traceRouteHopsSentProbes;
  327.     case COLUMN_TRACEROUTEHOPSPROBERESPONSES:
  328.         *var_len = sizeof(StorageTmp->traceRouteHopsProbeResponses);
  329.         return (u_char *) & StorageTmp->traceRouteHopsProbeResponses;
  330.     case COLUMN_TRACEROUTEHOPSLASTGOODPROBE:
  331.         *var_len = (StorageTmp->traceRouteHopsLastGoodProbeLen);
  332.         return (u_char *) StorageTmp->traceRouteHopsLastGoodProbe;
  333.     default:
  334.         ERROR_MSG("");
  335.     }
  336.     return NULL;
  337. }