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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *  Template MIB group implementation - sysORTable.c
  3.  *
  4.  */
  5. #include <net-snmp/net-snmp-config.h>
  6. #if HAVE_STDLIB_H
  7. #include <stdlib.h>
  8. #endif
  9. #include <sys/types.h>
  10. #if TIME_WITH_SYS_TIME
  11. # ifdef WIN32
  12. #  include <sys/timeb.h>
  13. # else
  14. #  include <sys/time.h>
  15. # endif
  16. # include <time.h>
  17. #else
  18. # if HAVE_SYS_TIME_H
  19. #  include <sys/time.h>
  20. # else
  21. #  include <time.h>
  22. # endif
  23. #endif
  24. #if HAVE_STRING_H
  25. #include <string.h>
  26. #else
  27. #include <strings.h>
  28. #endif
  29. #if HAVE_WINSOCK_H
  30. #include <winsock.h>
  31. #endif
  32. #if HAVE_NETINET_IN_H
  33. #include <netinet/in.h>
  34. #endif
  35. #if HAVE_DMALLOC_H
  36. #include <dmalloc.h>
  37. #endif
  38. #include <net-snmp/net-snmp-includes.h>
  39. #include <net-snmp/agent/net-snmp-agent-includes.h>
  40. #include <net-snmp/agent/agent_callbacks.h>
  41. #include "struct.h"
  42. #include "util_funcs.h"
  43. #include "sysORTable.h"
  44. #include "snmpd.h"
  45. #ifdef USING_AGENTX_SUBAGENT_MODULE
  46. #include "agentx/subagent.h"
  47. #include "agentx/client.h"
  48. #endif
  49. static int
  50. _register_sysOR_callback(int majorID, int minorID,
  51.                          void *serverarg, void *clientarg);
  52. static int
  53. _unregister_sysOR_callback(int majorID, int minorID,
  54.                             void *serverarg, void *clientarg);
  55. static int
  56. _unregister_sysOR_by_session_callback(int majorID, int minorID,
  57.                                       void *serverarg, void *clientarg);
  58. struct timeval  sysOR_lastchange;
  59. static struct sysORTable *table = NULL;
  60. static int      numEntries = 0;
  61. /*
  62.  * define the structure we're going to ask the agent to register our
  63.  * information at 
  64.  */
  65. struct variable1 sysORTable_variables[] = {
  66.     {SYSORTABLEID, ASN_OBJECT_ID, RONLY, var_sysORTable, 1, {2}},
  67.     {SYSORTABLEDESCR, ASN_OCTET_STR, RONLY, var_sysORTable, 1, {3}},
  68.     {SYSORTABLEUPTIME, ASN_TIMETICKS, RONLY, var_sysORTable, 1, {4}}
  69. };
  70. /*
  71.  * Define the OID pointer to the top of the mib tree that we're
  72.  * registering underneath 
  73.  */
  74. oid             sysORTable_variables_oid[] = { SNMP_OID_MIB2, 1, 9, 1 };
  75. #ifdef USING_MIBII_SYSTEM_MIB_MODULE
  76. extern oid      system_module_oid[];
  77. extern int      system_module_oid_len;
  78. extern int      system_module_count;
  79. #endif
  80. void
  81. init_sysORTable(void)
  82. {
  83.     /*
  84.      * register ourselves with the agent to handle our mib tree 
  85.      */
  86. #ifdef USING_AGENTX_SUBAGENT_MODULE
  87.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT)
  88.         (void) register_mib_priority("mibII/sysORTable",
  89.                                      (struct variable *)
  90.                                      sysORTable_variables,
  91.                                      sizeof(struct variable1),
  92.                                      sizeof(sysORTable_variables) /
  93.                                      sizeof(struct variable1),
  94.                                      sysORTable_variables_oid,
  95.                                      sizeof(sysORTable_variables_oid) /
  96.                                      sizeof(oid), 1);
  97.     else
  98. #endif
  99.         REGISTER_MIB("mibII/sysORTable", sysORTable_variables, variable1,
  100.                      sysORTable_variables_oid);
  101.     snmp_register_callback(SNMP_CALLBACK_APPLICATION,
  102.                            SNMPD_CALLBACK_REQ_REG_SYSOR,
  103.                            _register_sysOR_callback, NULL);
  104.     snmp_register_callback(SNMP_CALLBACK_APPLICATION,
  105.                            SNMPD_CALLBACK_REQ_UNREG_SYSOR,
  106.                            _unregister_sysOR_callback, NULL);
  107.     snmp_register_callback(SNMP_CALLBACK_APPLICATION,
  108.                            SNMPD_CALLBACK_REQ_UNREG_SYSOR_SESS,
  109.                            _unregister_sysOR_by_session_callback, NULL);
  110. #ifdef USING_MIBII_SYSTEM_MIB_MODULE
  111.     if (++system_module_count == 3)
  112.         REGISTER_SYSOR_TABLE(system_module_oid, system_module_oid_len,
  113.                              "The MIB module for SNMPv2 entities");
  114. #endif
  115.     gettimeofday(&sysOR_lastchange, NULL);
  116. }
  117.         /*********************
  118.  *
  119.  *  System specific implementation functions
  120.  *
  121.  *********************/
  122. u_char         *
  123. var_sysORTable(struct variable *vp,
  124.                oid * name,
  125.                size_t * length,
  126.                int exact, size_t * var_len, WriteMethod ** write_method)
  127. {
  128.     unsigned long   i = 0;
  129.     static unsigned long ret;
  130.     struct sysORTable *ptr = table;
  131.     if (header_simple_table
  132.         (vp, name, length, exact, var_len, write_method, numEntries))
  133.         return NULL;
  134.     for (i = 1; ptr != NULL && i < name[*length - 1]; ptr = ptr->next, i++) {
  135.         DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- %lu != %lun",
  136.                     i, name[*length - 1]));
  137.     }
  138.     if (ptr == NULL) {
  139.         DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- no match: %lun",
  140.                     i));
  141.         return NULL;
  142.     }
  143.     DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- match: %lun", i));
  144.     switch (vp->magic) {
  145.     case SYSORTABLEID:
  146.         *var_len = ptr->OR_oidlen * sizeof(ptr->OR_oid[0]);
  147.         return (u_char *) ptr->OR_oid;
  148.     case SYSORTABLEDESCR:
  149.         *var_len = strlen(ptr->OR_descr);
  150.         return (u_char *) ptr->OR_descr;
  151.     case SYSORTABLEUPTIME:
  152.         ret = netsnmp_timeval_uptime(&ptr->OR_uptime);
  153.         return (u_char *) & ret;
  154.     default:
  155.         DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_sysORTablen",
  156.                     vp->magic));
  157.     }
  158.     return NULL;
  159. }
  160. int
  161. register_sysORTable_sess(oid * oidin,
  162.                          size_t oidlen,
  163.                          const char *descr, netsnmp_session * ss)
  164. {
  165.     struct sysORTable *ptr, **nptr;
  166.     struct register_sysOR_parameters reg_sysOR_parms;
  167.     DEBUGMSGTL(("mibII/sysORTable", "sysORTable registering: "));
  168.     DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
  169.     DEBUGMSG(("mibII/sysORTable", "n"));
  170.     ptr = (struct sysORTable *) malloc(sizeof(struct sysORTable));
  171.     if (ptr == NULL) {
  172.         return SYS_ORTABLE_REGISTRATION_FAILED;
  173.     }
  174.     ptr->OR_descr = (char *) strdup(descr);
  175.     if (ptr->OR_descr == NULL) {
  176.         free(ptr);
  177.         return SYS_ORTABLE_REGISTRATION_FAILED;
  178.     }
  179.     ptr->OR_oidlen = oidlen;
  180.     ptr->OR_oid = (oid *) malloc(sizeof(oid) * oidlen);
  181.     if (ptr->OR_oid == NULL) {
  182.         free(ptr->OR_descr);
  183.         free(ptr);
  184.         return SYS_ORTABLE_REGISTRATION_FAILED;
  185.     }
  186.     memcpy(ptr->OR_oid, oidin, sizeof(oid) * oidlen);
  187.     gettimeofday(&(ptr->OR_uptime), NULL);
  188.     gettimeofday(&(sysOR_lastchange), NULL);
  189.     ptr->OR_sess = ss;
  190.     ptr->next = NULL;
  191.     numEntries++;
  192.     /* add this entry to the end of the chained list */
  193.     nptr = &table;
  194.     while (*nptr != NULL)
  195.         nptr = &((*nptr)->next);
  196.     *nptr = ptr;
  197.     reg_sysOR_parms.name = oidin;
  198.     reg_sysOR_parms.namelen = oidlen;
  199.     reg_sysOR_parms.descr = descr;
  200.     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
  201.                         SNMPD_CALLBACK_REG_SYSOR, &reg_sysOR_parms);
  202.     return SYS_ORTABLE_REGISTERED_OK;
  203. }
  204. int
  205. register_sysORTable(oid * oidin, size_t oidlen, const char *descr)
  206. {
  207.     return register_sysORTable_sess(oidin, oidlen, descr, NULL);
  208. }
  209. int
  210. unregister_sysORTable_sess(oid * oidin,
  211.                            size_t oidlen, netsnmp_session * ss)
  212. {
  213.     struct sysORTable *ptr, *prev = NULL, *next;
  214.     int             found = SYS_ORTABLE_NO_SUCH_REGISTRATION;
  215.     struct register_sysOR_parameters reg_sysOR_parms;
  216.     DEBUGMSGTL(("mibII/sysORTable", "sysORTable unregistering: "));
  217.     DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
  218.     DEBUGMSG(("mibII/sysORTable", "n"));
  219.     for (ptr = table; ptr; ptr = next)
  220.     {
  221.         next = ptr->next;
  222.         if (ptr->OR_sess == ss &&
  223.           (snmp_oid_compare(oidin, oidlen, ptr->OR_oid, ptr->OR_oidlen) == 0))
  224.         {
  225.             if (prev == NULL)
  226.                 table = ptr->next;
  227.             else
  228.                 prev->next = ptr->next;
  229.             free(ptr->OR_oid);
  230.             free(ptr->OR_descr);
  231.             free(ptr);
  232.             numEntries--;
  233.             gettimeofday(&(sysOR_lastchange), NULL);
  234.             found = SYS_ORTABLE_UNREGISTERED_OK;
  235.             break;
  236.         } else
  237.             prev = ptr;
  238.     }
  239.     reg_sysOR_parms.name = oidin;
  240.     reg_sysOR_parms.namelen = oidlen;
  241.     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
  242.                         SNMPD_CALLBACK_UNREG_SYSOR, &reg_sysOR_parms);
  243.     return found;
  244. }
  245. int
  246. unregister_sysORTable(oid * oidin, size_t oidlen)
  247. {
  248.     return unregister_sysORTable_sess(oidin, oidlen, NULL);
  249. }
  250. void
  251. unregister_sysORTable_by_session(netsnmp_session * ss)
  252. {
  253.     struct sysORTable *ptr, *prev = NULL, *next;
  254.     for (ptr = table; ptr; ptr = next)
  255.     {
  256.         next = ptr->next;
  257.         if (((ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess == ss) ||
  258.             (!(ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess &&
  259.              ptr->OR_sess->subsession == ss)) {
  260.             if (prev == NULL)
  261.                 table = next;
  262.             else
  263.                 prev->next = next;
  264.             free(ptr->OR_oid);
  265.             free(ptr->OR_descr);
  266.             free(ptr);
  267.             numEntries--;
  268.             gettimeofday(&(sysOR_lastchange), NULL);
  269.         } else
  270.             prev = ptr;
  271.     }
  272. }
  273. static int
  274. _register_sysOR_callback(int majorID, int minorID, void *serverarg,
  275.                          void *clientarg)
  276. {
  277.     struct sysORTable *parms = (struct sysORTable *) serverarg;
  278.     return register_sysORTable_sess(parms->OR_oid, parms->OR_oidlen,
  279.                                     parms->OR_descr, parms->OR_sess);
  280. }
  281. static int
  282. _unregister_sysOR_by_session_callback(int majorID, int minorID,
  283.                                       void *serverarg, void *clientarg)
  284. {
  285.     netsnmp_session *session = (netsnmp_session *) serverarg;
  286.     unregister_sysORTable_by_session(session);
  287.     return 0;
  288. }
  289. static int
  290. _unregister_sysOR_callback(int majorID, int minorID, void *serverarg,
  291.                        void *clientarg)
  292. {
  293.     struct sysORTable *parms = (struct sysORTable *) serverarg;
  294.     return unregister_sysORTable_sess(parms->OR_oid,
  295.                                       parms->OR_oidlen,
  296.                                       parms->OR_sess);
  297. }