snmp_mib.c
资源名称:snmp.src.rar [点击查看]
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:9k
源码类别:
SNMP编程
开发平台:
C/C++
- /*
- * SNMPv1 MIB group implementation - snmp.c
- *
- */
- #include <config.h>
- #include <sys/types.h>
- #if HAVE_STRING_H
- #include <string.h>
- #else
- #include <strings.h>
- #endif
- #if HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #if HAVE_WINSOCK_H
- #ifdef OS_VXWORKS
- #include <sys/times.h>
- #endif
- #include <ip/socket.h>
- #endif
- #include "mibincl.h"
- #include "system.h"
- #include "util_funcs.h"
- #include "snmp_mib.h"
- #include "sysORTable.h"
- #ifdef OS_VXWORKS
- #include "asn1.h"
- #endif
- #include "config_struct.h"
- /*********************
- *
- * Kernel & interface information,
- * and internal forward declarations
- *
- *********************/
- extern int snmp_enableauthentraps;
- int old_snmp_enableauthentraps;
- /*********************
- *
- * Initialisation & common implementation functions
- *
- *********************/
- /* define the structure we're going to ask the agent to register our
- information at */
- struct variable2 snmp_variables[] = {
- {SNMPINPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {1}},
- {SNMPOUTPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {2}},
- {SNMPINBADVERSIONS, ASN_COUNTER, RONLY, var_snmp, 1, {3}},
- {SNMPINBADCOMMUNITYNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {4}},
- {SNMPINBADCOMMUNITYUSES, ASN_COUNTER, RONLY, var_snmp, 1, {5}},
- {SNMPINASNPARSEERRORS, ASN_COUNTER, RONLY, var_snmp, 1, {6}},
- {SNMPINTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {8}},
- {SNMPINNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {9}},
- {SNMPINBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {10}},
- {SNMPINREADONLYS, ASN_COUNTER, RONLY, var_snmp, 1, {11}},
- {SNMPINGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {12}},
- {SNMPINTOTALREQVARS, ASN_COUNTER, RONLY, var_snmp, 1, {13}},
- {SNMPINTOTALSETVARS, ASN_COUNTER, RONLY, var_snmp, 1, {14}},
- {SNMPINGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {15}},
- {SNMPINGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {16}},
- {SNMPINSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {17}},
- {SNMPINGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {18}},
- {SNMPINTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {19}},
- {SNMPOUTTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {20}},
- {SNMPOUTNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {21}},
- {SNMPOUTBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {22}},
- {SNMPOUTGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {24}},
- {SNMPOUTGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {25}},
- {SNMPOUTGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {26}},
- {SNMPOUTSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {27}},
- {SNMPOUTGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {28}},
- {SNMPOUTTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {29}},
- {SNMPENABLEAUTHENTRAPS, ASN_INTEGER, RWRITE, var_snmp, 1, {30}},
- {SNMPSILENTDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {31}},
- {SNMPPROXYDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {32}}
- };
- /* Define the OID pointer to the top of the mib tree that we're
- registering underneath */
- oid snmp_variables_oid[] = { SNMP_OID_MIB2,11 };
- #ifdef USING_MIBII_SYSTEM_MIB_MODULE
- extern oid system_module_oid[];
- extern int system_module_oid_len;
- extern int system_module_count;
- #endif
- void
- init_snmp_mib(void) {
- /* register ourselves with the agent to handle our mib tree */
- REGISTER_MIB("mibII/snmp", snmp_variables, variable2, snmp_variables_oid);
- #ifdef USING_MIBII_SYSTEM_MIB_MODULE
- if ( ++system_module_count == 3 )
- REGISTER_SYSOR_TABLE( system_module_oid, system_module_oid_len,
- "The MIB module for SNMPv2 entities");
- #endif
- }
- void show_snmp(void)
- {
- vty_printf("%d SNMP packets inputn",
- snmp_get_statistic (SNMPINPKTS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Bad SNMP version errorsn",
- snmp_get_statistic (SNMPINBADVERSIONS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Unknown community namen",
- snmp_get_statistic (SNMPINBADCOMMUNITYNAMES+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Illegal operation for community name suppliedn",
- snmp_get_statistic (SNMPINBADCOMMUNITYUSES+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Encoding errorsn",
- snmp_get_statistic (SNMPINASNPARSEERRORS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Number of requested variablesn",
- snmp_get_statistic (SNMPINTOTALREQVARS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Number of altered variablesn",
- snmp_get_statistic (SNMPINTOTALSETVARS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Get-request PDUsn",
- snmp_get_statistic (SNMPINGETREQUESTS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Get-next PDUsn",
- snmp_get_statistic (SNMPINGETNEXTS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Set-request PDUsn",
- snmp_get_statistic (SNMPINSETREQUESTS+STAT_SNMP_STATS_START - 1));
- vty_printf("%d SNMP packets outputn",
- snmp_get_statistic (SNMPOUTPKTS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Too big errors (Maximum packet size %d)n",
- snmp_get_statistic (SNMPOUTTOOBIGS+STAT_SNMP_STATS_START - 1), snmp_para.packetsize);
- vty_printf(" %d No such name errorsn",
- snmp_get_statistic (SNMPOUTNOSUCHNAMES+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Bad values errorsn",
- snmp_get_statistic (SNMPOUTBADVALUES+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d General errorsn",
- snmp_get_statistic (SNMPOUTGENERRS+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Response PDUsn",
- snmp_get_statistic (SNMPOUTGETRESPONSES+STAT_SNMP_STATS_START - 1));
- vty_printf(" %d Trap PDUsn",
- snmp_get_statistic (SNMPOUTTRAPS+STAT_SNMP_STATS_START - 1));
- vty_printf_end(1);
- }
- /*
- header_snmp(...
- Arguments:
- vp IN - pointer to variable entry that points here
- name IN/OUT - IN/name requested, OUT/name found
- length IN/OUT - length of IN/OUT oid's
- exact IN - TRUE if an exact match was requested
- var_len OUT - length of variable or 0 if function returned
- write_method
- */
- /*********************
- *
- * System specific implementation functions
- * (actually common!)
- *
- *********************/
- u_char *
- var_snmp(struct variable *vp,
- oid *name,
- int *length,
- int exact,
- int *var_len,
- WriteMethod **write_method)
- {
- static long long_ret;
- /* *write_method = 0; assume it isnt writable for the time being. yang mod 2005-08-24*/
- *var_len = sizeof(long_ret); /* assume an integer and change later if not */
- if (header_generic(vp, name, length, exact, var_len, write_method)
- == MATCH_FAILED)
- return NULL;
- /* this is where we do the value assignments for the mib results. */
- if (vp->magic == SNMPENABLEAUTHENTRAPS) {
- *write_method = write_snmp;
- long_return = snmp_enableauthentraps;
- return (u_char *) &long_return;
- } else if ( (vp->magic >= 1)
- && (vp->magic <= (STAT_SNMP_STATS_END - STAT_SNMP_STATS_START + 1)) ) {
- long_ret = snmp_get_statistic(vp->magic + STAT_SNMP_STATS_START - 1);
- return (unsigned char *) &long_ret;
- }
- return NULL;
- }
- /*
- * only for snmpEnableAuthenTraps:
- */
- int
- write_snmp (int action,
- u_char *var_val,
- u_char var_val_type,
- int var_val_len,
- u_char *statP,
- oid *name,
- int name_len)
- {
- long intval = *((long *) var_val);
- switch ( action ) {
- case RESERVE1: /* Check values for acceptability */
- if (var_val_type != ASN_INTEGER){
- DEBUGMSGTL(("mibII/snmp_mib", "%x not integer type", var_val_type));
- return SNMP_ERR_WRONGTYPE;
- }
- if (intval != 1 && intval != 2) {
- DEBUGMSGTL(("mibII/snmp_mib", "not valid %xn", intval));
- return SNMP_ERR_WRONGVALUE;
- }
- if (var_val_len > sizeof(long)){
- DEBUGMSGTL(("mibII/snmp_mib","write to snmp: bad lengthn"));
- return SNMP_ERR_WRONGLENGTH;
- }
- break;
- case RESERVE2: /* Allocate memory and similar resources */
- /* Using static variables, so nothing needs to be done */
- break;
- case ACTION: /* Perform the SET action (if reversible) */
- /* Save the old value, in case of UNDO */
- old_snmp_enableauthentraps = snmp_enableauthentraps;
- snmp_enableauthentraps = intval;
- break;
- case UNDO: /* Reverse the SET action and free resources */
- snmp_enableauthentraps = old_snmp_enableauthentraps;
- break;
- case COMMIT: /* Confirm the SET, performing any irreversible actions,
- and free resources */
- /* save_into_conffile ("authentraps:", intval == 1 ? "yes" : "no"); */
- break;
- case ACTION_FREE: /* Free any resources allocated */
- break;
- }
- return SNMP_ERR_NOERROR;
- }
- /*********************
- *
- * Internal implementation functions
- *
- *********************/