snmp_mib.c
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:9k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /*
  2.  *  SNMPv1 MIB group implementation - snmp.c
  3.  *
  4.  */
  5. #include <config.h>
  6. #include <sys/types.h>
  7. #if HAVE_STRING_H
  8. #include <string.h>
  9. #else
  10. #include <strings.h>
  11. #endif
  12. #if HAVE_NETINET_IN_H
  13. #include <netinet/in.h>
  14. #endif
  15. #if HAVE_WINSOCK_H
  16. #ifdef OS_VXWORKS
  17. #include <sys/times.h>
  18. #endif
  19. #include <ip/socket.h>
  20. #endif
  21. #include "mibincl.h"
  22. #include "system.h"
  23. #include "util_funcs.h"
  24. #include "snmp_mib.h"
  25. #include "sysORTable.h"
  26. #ifdef OS_VXWORKS
  27. #include "asn1.h"
  28. #endif
  29. #include "config_struct.h"
  30. /*********************
  31.  *
  32.  *  Kernel & interface information,
  33.  *   and internal forward declarations
  34.  *
  35.  *********************/
  36. extern int snmp_enableauthentraps;
  37.        int old_snmp_enableauthentraps;
  38. /*********************
  39.  *
  40.  *  Initialisation & common implementation functions
  41.  *
  42.  *********************/
  43. /* define the structure we're going to ask the agent to register our
  44.    information at */
  45. struct variable2 snmp_variables[] = {
  46.     {SNMPINPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {1}},
  47.     {SNMPOUTPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {2}},
  48.     {SNMPINBADVERSIONS, ASN_COUNTER, RONLY, var_snmp, 1, {3}},
  49.     {SNMPINBADCOMMUNITYNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {4}},
  50.     {SNMPINBADCOMMUNITYUSES, ASN_COUNTER, RONLY, var_snmp, 1, {5}},
  51.     {SNMPINASNPARSEERRORS, ASN_COUNTER, RONLY, var_snmp, 1, {6}},
  52.     {SNMPINTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {8}},
  53.     {SNMPINNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {9}},
  54.     {SNMPINBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {10}},
  55.     {SNMPINREADONLYS, ASN_COUNTER, RONLY, var_snmp, 1, {11}},
  56.     {SNMPINGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {12}},
  57.     {SNMPINTOTALREQVARS, ASN_COUNTER, RONLY, var_snmp, 1, {13}},
  58.     {SNMPINTOTALSETVARS, ASN_COUNTER, RONLY, var_snmp, 1, {14}},
  59.     {SNMPINGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {15}},
  60.     {SNMPINGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {16}},
  61.     {SNMPINSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {17}},
  62.     {SNMPINGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {18}},
  63.     {SNMPINTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {19}},
  64.     {SNMPOUTTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {20}},
  65.     {SNMPOUTNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {21}},
  66.     {SNMPOUTBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {22}},
  67.     {SNMPOUTGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {24}},
  68.     {SNMPOUTGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {25}},
  69.     {SNMPOUTGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {26}},
  70.     {SNMPOUTSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {27}},
  71.     {SNMPOUTGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {28}},
  72.     {SNMPOUTTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {29}},
  73.     {SNMPENABLEAUTHENTRAPS, ASN_INTEGER, RWRITE, var_snmp, 1, {30}},
  74.     {SNMPSILENTDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {31}},
  75.     {SNMPPROXYDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {32}}
  76. };
  77. /* Define the OID pointer to the top of the mib tree that we're
  78.    registering underneath */
  79. oid snmp_variables_oid[] = { SNMP_OID_MIB2,11 };
  80. #ifdef USING_MIBII_SYSTEM_MIB_MODULE
  81. extern oid system_module_oid[];
  82. extern int system_module_oid_len;
  83. extern int system_module_count;
  84. #endif
  85. void
  86. init_snmp_mib(void) {
  87.   /* register ourselves with the agent to handle our mib tree */
  88.   REGISTER_MIB("mibII/snmp", snmp_variables, variable2, snmp_variables_oid);
  89. #ifdef USING_MIBII_SYSTEM_MIB_MODULE
  90.   if ( ++system_module_count == 3 )
  91. REGISTER_SYSOR_TABLE( system_module_oid, system_module_oid_len,
  92. "The MIB module for SNMPv2 entities");
  93. #endif
  94. }
  95. void show_snmp(void)
  96. {
  97. vty_printf("%d SNMP packets inputn",
  98. snmp_get_statistic (SNMPINPKTS+STAT_SNMP_STATS_START - 1));
  99. vty_printf("  %d Bad SNMP version errorsn",
  100. snmp_get_statistic (SNMPINBADVERSIONS+STAT_SNMP_STATS_START - 1));
  101. vty_printf("  %d Unknown community namen",
  102. snmp_get_statistic (SNMPINBADCOMMUNITYNAMES+STAT_SNMP_STATS_START - 1));
  103. vty_printf("  %d Illegal operation for community name suppliedn",
  104. snmp_get_statistic (SNMPINBADCOMMUNITYUSES+STAT_SNMP_STATS_START - 1));
  105. vty_printf("  %d Encoding errorsn",
  106. snmp_get_statistic (SNMPINASNPARSEERRORS+STAT_SNMP_STATS_START - 1));
  107. vty_printf("  %d Number of requested variablesn",
  108. snmp_get_statistic (SNMPINTOTALREQVARS+STAT_SNMP_STATS_START - 1));
  109. vty_printf("  %d Number of altered variablesn",
  110. snmp_get_statistic (SNMPINTOTALSETVARS+STAT_SNMP_STATS_START - 1));
  111. vty_printf("  %d Get-request PDUsn",
  112. snmp_get_statistic (SNMPINGETREQUESTS+STAT_SNMP_STATS_START - 1));
  113. vty_printf("  %d Get-next PDUsn",
  114. snmp_get_statistic (SNMPINGETNEXTS+STAT_SNMP_STATS_START - 1));
  115. vty_printf("  %d Set-request PDUsn",
  116. snmp_get_statistic (SNMPINSETREQUESTS+STAT_SNMP_STATS_START - 1));
  117. vty_printf("%d SNMP packets outputn",
  118. snmp_get_statistic (SNMPOUTPKTS+STAT_SNMP_STATS_START - 1));
  119. vty_printf("  %d Too big errors (Maximum packet size %d)n",
  120. snmp_get_statistic (SNMPOUTTOOBIGS+STAT_SNMP_STATS_START - 1), snmp_para.packetsize);
  121. vty_printf("  %d No such name errorsn",
  122. snmp_get_statistic (SNMPOUTNOSUCHNAMES+STAT_SNMP_STATS_START - 1));
  123. vty_printf("  %d Bad values errorsn",
  124. snmp_get_statistic (SNMPOUTBADVALUES+STAT_SNMP_STATS_START - 1));
  125. vty_printf("  %d General errorsn",
  126. snmp_get_statistic (SNMPOUTGENERRS+STAT_SNMP_STATS_START - 1));
  127. vty_printf("  %d Response PDUsn",
  128. snmp_get_statistic (SNMPOUTGETRESPONSES+STAT_SNMP_STATS_START - 1));
  129. vty_printf("  %d Trap PDUsn",
  130. snmp_get_statistic (SNMPOUTTRAPS+STAT_SNMP_STATS_START - 1));
  131. vty_printf_end(1);
  132. }
  133. /*
  134.   header_snmp(...
  135.   Arguments:
  136.   vp   IN      - pointer to variable entry that points here
  137.   name    IN/OUT  - IN/name requested, OUT/name found
  138.   length  IN/OUT  - length of IN/OUT oid's 
  139.   exact   IN      - TRUE if an exact match was requested
  140.   var_len OUT     - length of variable or 0 if function returned
  141.   write_method
  142.   
  143. */
  144. /*********************
  145.  *
  146.  *  System specific implementation functions
  147.  * (actually common!)
  148.  *
  149.  *********************/
  150. u_char *
  151. var_snmp(struct variable *vp,
  152.  oid *name,
  153.  int *length,
  154.  int exact,
  155.  int *var_len,
  156.  WriteMethod **write_method)
  157. {
  158.   static long long_ret;
  159.   /* *write_method = 0;          assume it isnt writable for the time being. yang mod 2005-08-24*/
  160.   *var_len = sizeof(long_ret); /* assume an integer and change later if not */
  161.   if (header_generic(vp, name, length, exact, var_len, write_method)
  162.       == MATCH_FAILED)
  163.     return NULL;
  164.     /* this is where we do the value assignments for the mib results. */
  165.   if (vp->magic == SNMPENABLEAUTHENTRAPS) {
  166.     *write_method = write_snmp;
  167.     long_return = snmp_enableauthentraps;
  168.     return (u_char *) &long_return;
  169.   } else if ( (vp->magic >= 1)
  170.        && (vp->magic <= (STAT_SNMP_STATS_END - STAT_SNMP_STATS_START + 1)) ) {
  171.     long_ret = snmp_get_statistic(vp->magic + STAT_SNMP_STATS_START - 1);
  172.     return (unsigned char *) &long_ret;
  173.   }
  174.   return NULL;
  175. }
  176. /*
  177.  * only for snmpEnableAuthenTraps:
  178.  */
  179. int
  180. write_snmp (int action,
  181.     u_char *var_val,
  182.     u_char var_val_type,
  183.     int var_val_len,
  184.     u_char *statP,
  185.     oid *name,
  186.     int name_len)
  187. {
  188.     long intval = *((long *) var_val);
  189.     switch ( action ) {
  190. case RESERVE1: /* Check values for acceptability */
  191. if (var_val_type != ASN_INTEGER){
  192. DEBUGMSGTL(("mibII/snmp_mib", "%x not integer type", var_val_type));
  193. return SNMP_ERR_WRONGTYPE;
  194. }
  195. if (intval != 1 && intval != 2) {
  196. DEBUGMSGTL(("mibII/snmp_mib", "not valid %xn", intval));
  197. return SNMP_ERR_WRONGVALUE;
  198. }
  199. if (var_val_len > sizeof(long)){
  200. DEBUGMSGTL(("mibII/snmp_mib","write to snmp: bad lengthn"));
  201. return SNMP_ERR_WRONGLENGTH;
  202. }
  203.     break;
  204. case RESERVE2: /* Allocate memory and similar resources */
  205. /* Using static variables, so nothing needs to be done */
  206.     break;
  207. case ACTION: /* Perform the SET action (if reversible) */
  208. /* Save the old value, in case of UNDO */
  209.     old_snmp_enableauthentraps = snmp_enableauthentraps;
  210.     snmp_enableauthentraps = intval;
  211.     break;
  212. case UNDO: /* Reverse the SET action and free resources */
  213.     snmp_enableauthentraps = old_snmp_enableauthentraps;
  214.     break;
  215. case COMMIT: /* Confirm the SET, performing any irreversible actions,
  216. and free resources */
  217.     /* save_into_conffile ("authentraps:", intval == 1 ? "yes" : "no"); */
  218.     break;
  219. case ACTION_FREE: /* Free any resources allocated */
  220.     break;
  221.     }
  222.     return SNMP_ERR_NOERROR;
  223. }
  224. /*********************
  225.  *
  226.  *  Internal implementation functions
  227.  *
  228.  *********************/