system_mib.c
资源名称:snmp.src.rar [点击查看]
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:9k
源码类别:
SNMP编程
开发平台:
C/C++
- /*
- * System MIB group implementation - system.c
- *
- */
- #include <ctype.h>
- #include <config.h>
- #if HAVE_STDLIB_H
- #include <stdlib.h>
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #if HAVE_STRING_H
- #include <string.h>
- #else
- #include <strings.h>
- #endif
- #if HAVE_WINSOCK_H
- #ifdef OS_VXWORKS
- #include <sys/times.h>
- #endif
- #include <ip/socket.h>
- #endif
- #ifdef HAVE_SYS_TIME_H
- #include <time.h>
- #endif
- #if HAVE_UTSNAME_H
- #include <utsname.h>
- #else
- #if HAVE_SYS_UTSNAME_H
- #include <sys/utsname.h>
- #endif
- #endif
- #if HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #include <libres/resource.h>
- #if HAVE_DMALLOC_H
- #include <dmalloc.h>
- #endif
- #include "asn1.h"
- #include "config_struct.h"
- #include "mibincl.h"
- #include "system_mib.h"
- #include "../struct.h"
- #include "../util_funcs.h"
- #include "read_config.h"
- #include "agent_read_config.h"
- #include "system.h"
- #include "sysORTable.h"
- #include "assert.h"
- #ifdef SWITCH
- #include <sys_conf.h> /* for hostname */
- #include <bspfuncs.h> /* for get_sys_info() */
- #endif
- /*********************
- *
- * Kernel & interface information,
- * and internal forward declarations
- *
- *********************/
- static u_char *sysContact;
- static u_char *sysName;
- static u_char *sysLocation;
- static int writeSystem (int, u_char *,u_char, int, u_char *,oid*, int);
- #ifndef SWITCH
- int header_system (struct variable *,oid *, int *, int, int *, WriteMethod **write_method);
- extern unsigned long get_sys_info(unsigned char* output_buf,unsigned long buf_len);/*** sun define 2000.07.28 ***/
- static char old_sys_version[128];
- #endif
- static char old_sys_contact[SNMP_MAX_SYSCON_LEN];
- static char old_sys_location[SNMP_MAX_SYSLOC_LEN];
- static char old_host_name[HOSTNAME_MAX_LEN];
- /*** sun define 2000.03.14 ***/
- struct variable2 system_variables[] = {
- {SYSDESCR, ASN_OCTET_STR, RONLY, var_system, 1, {SYSDESCR}},
- {SYSOBJECTID, ASN_OBJECT_ID, RONLY, var_system, 1, {SYSOBJECTID}},
- {SYSUPTIME, ASN_TIMETICKS, RONLY, var_system, 1, {SYSUPTIME}},
- {SYSCONTACT, ASN_OCTET_STR, RWRITE, var_system, 1, {SYSCONTACT}},
- {SYSNAME, ASN_OCTET_STR, RWRITE, var_system, 1, {SYSNAME}},
- {SYSLOCATION, ASN_OCTET_STR, RWRITE, var_system, 1, {SYSLOCATION}},
- {SYSSERVICES, ASN_INTEGER, RONLY, var_system, 1, {SYSSERVICES}}
- };
- /* Define the OID pointer to the top of the mib tree that we're
- registering underneath */
- oid system_variables_oid[] ={ MIB, 1 };
- void init_system_mib(void)
- {
- /* register ourselves with the agent to handle our mib tree */
- REGISTER_MIB("mibII/system", system_variables, variable2,
- system_variables_oid);
- }
- /*********************
- *
- * Initialisation & common implementation functions
- *
- *********************/
- void init_mib_system()
- {
- int rc;
- SNMP_DEBUG_ROUTINES("init_mib_system()");
- sysContact = snmp_para.syscontact;
- rc = resource_read (RESOURCE_TYPE_HOSTNAME, RESOURCE_INDEX_TYPE_NONE,
- NULL,(unsigned long)&sysName, 0, 0, 0);
- if (!rc) {
- /* get the pointer to hostname here. */
- }
- sysLocation = snmp_para.syslocation;
- }
- #define SNMP_SERVICE_BITMASK_PHY 0x01
- #define SNMP_SERVICE_BITMASK_DL 0x02
- #define SNMP_SERVICE_BITMASK_INET 0x04
- #define SNMP_SERVICE_BITMASK_E2E 0x08
- #define SNMP_SERVICE_BITMASK_APP 0x40
- #define SNMP_SERVICE_BITS 0
- /*根据产品不同设置成不同的值*/
- #ifdef SWITCH
- #ifdef L2SWITCH
- #undef SNMP_SERVICE_BITS
- #define SNMP_SERVICE_BITS SNMP_SERVICE_BITMASK_DL
- #else
- #undef SNMP_SERVICE_BITS
- #define SNMP_SERVICE_BITS SNMP_SERVICE_BITMASK_DL|SNMP_SERVICE_BITMASK_INET
- #endif
- #else
- /*router*/
- #undef SNMP_SERVICE_BITS
- #define SNMP_SERVICE_BITS SNMP_SERVICE_BITMASK_DL|SNMP_SERVICE_BITMASK_INET|SNMP_SERVICE_BITMASK_E2E|SNMP_SERVICE_BITMASK_APP
- #endif
- /*********************
- *
- * System specific implementation functions
- * (actually common!)
- *
- *********************/
- u_char *
- var_system(struct variable *vp,
- oid *name,
- int *length,
- int exact,
- int *var_len,
- WriteMethod **write_method)
- {
- static u_char sys_desc[SNMP_MAX_DESCR_LEN+1];
- u_char *p_char;
- SNMP_DEBUG_ROUTINES("var_system()");
- if (header_generic(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )
- return NULL;
- switch (vp->magic)
- {
- case SYSDESCR:
- *var_len = strlen(version_descr);
- /*RFC规定这个值 SIZE ( 0 .. 255 ), 现在超出255 */
- if (*var_len > SNMP_MAX_DESCR_LEN)
- {
- p_char = (u_char *)&sys_desc;
- strncpy(p_char, version_descr, SNMP_MAX_DESCR_LEN);
- *var_len = SNMP_MAX_DESCR_LEN;
- return (u_char *)p_char;
- }
- return (u_char *)version_descr;
- case SYSOBJECTID:
- {
- int i = sizeof(oid)*default_enterprise_len;
- #if 0
- /*Some NMS(nateks,Nov 5,2007) doesn't work with OID followed by ".0" which is necessary for BD-NMS!!!*/
- int j;
- for(j = sizeof(default_enterprise) / sizeof(default_enterprise[0]) - 1; j > 0; j--)
- {
- if (default_enterprise[j] == 0)
- i -= sizeof(default_enterprise[0]);
- else
- break;
- }
- #endif
- *var_len = i;
- return (u_char *)default_enterprise;
- }
- case SYSUPTIME:
- long_return = get_uptime();
- return ((u_char *) &long_return);
- case SYSCONTACT:
- *write_method = writeSystem;
- if (snmp_para.syscontact[0])
- {
- *var_len = strlen(snmp_para.syscontact);
- return (u_char *)(snmp_para.syscontact);
- }
- else
- {
- *var_len = strlen(def_syscontact);
- return (u_char *)(def_syscontact);
- }
- case SYSNAME:
- *var_len = strlen(sysName);
- *write_method = writeSystem;
- return (u_char *)sysName;
- case SYSLOCATION:
- if (snmp_para.syslocation[0])
- p_char = snmp_para.syslocation;
- else
- p_char = def_syslocation;
- *var_len = strlen(p_char);
- *write_method = writeSystem;
- return (u_char *)(p_char);
- case SYSSERVICES:
- long_return = SNMP_SERVICE_BITS;
- return (u_char *)&long_return;
- default:
- ERROR_MSG("");
- }
- return NULL;
- }
- static int
- writeSystem(
- int action,
- u_char *var_val,
- u_char var_val_type,
- int var_val_len,
- u_char *statP,
- oid *name,
- int name_len)
- {
- u_char buf[sizeof(version_descr)], *cp;
- int count, size;
- long rc;
- char hostname[64];
- size = sizeof(buf);
- size = size > var_val_len?var_val_len:size;
- strncpy (buf, var_val, size);
- for(cp = var_val, count = 0; count < var_val_len; count++, cp++){
- if (!isprint(*cp)){
- if(SNMP_TRACE(SNMP_DEBUG_EVENT))
- snmp_trace("not print %xn", *cp);
- return SNMP_ERR_WRONGVALUE;
- }
- }
- buf[size] = 0;
- switch (action){
- case RESERVE1:
- SNMP_DEBUG_ROUTINES("writeSystem()");
- if (var_val_type != ASN_OCTET_STR){
- if(SNMP_TRACE(SNMP_DEBUG_EVENT))
- snmp_trace("not stringn");
- return SNMP_ERR_WRONGTYPE;
- }
- switch((char)name[7]){
- case SYSCONTACT:
- if (var_val_len > SNMP_MAX_SYSCON_LEN-1){
- if(SNMP_TRACE(SNMP_DEBUG_EVENT))
- snmp_trace("bad lengthn");
- return SNMP_ERR_WRONGLENGTH;
- }
- break;
- case SYSNAME:
- if (var_val_len > MAX_NAME_LEN-1){
- if(SNMP_TRACE(SNMP_DEBUG_EVENT))
- snmp_trace("bad lengthn");
- return SNMP_ERR_WRONGLENGTH;
- }
- break;
- case SYSLOCATION:
- if (var_val_len > SNMP_MAX_SYSLOC_LEN-1){
- if(SNMP_TRACE(SNMP_DEBUG_EVENT))
- snmp_trace("bad lengthn");
- return SNMP_ERR_WRONGLENGTH;
- }
- break;
- }
- /** old
- if (var_val_len > sizeof(version_descr)-1){
- snmp_trace("bad lengthn");
- return SNMP_ERR_WRONGLENGTH;
- }
- **/
- break;
- case RESERVE2:
- break;
- case ACTION:
- switch((char)name[7]){
- case SYSCONTACT:
- strncpy (old_sys_contact, sysContact, SNMP_MAX_SYSCON_LEN - 1);
- old_sys_contact[SNMP_MAX_SYSCON_LEN - 1] =' ';
- strcpy(sysContact, (char *) buf);
- break;
- case SYSNAME:
- strncpy (old_host_name, sysContact, HOSTNAME_MAX_LEN);
- old_host_name[HOSTNAME_MAX_LEN - 1] =' ';
- strncpy(hostname, (char *) buf, HOSTNAME_MAX_LEN);
- hostname[HOSTNAME_MAX_LEN - 1]=' ';
- rc = resource_write (RESOURCE_TYPE_HOSTNAME, RESOURCE_INDEX_TYPE_NONE, NULL, (UINT32)hostname, 0, 0, 0);
- if (rc != 0)
- return SNMP_ERR_RESOURCEUNAVAILABLE;
- break;
- case SYSLOCATION:
- strncpy (old_sys_location, sysContact, SNMP_MAX_SYSLOC_LEN - 1);
- old_sys_location[SNMP_MAX_SYSLOC_LEN - 1] =' ';
- strcpy(sysLocation, (char *) buf);
- break;
- default:
- break;
- }
- break;
- case UNDO:
- switch((char)name[7]){
- case SYSCONTACT:
- strcpy(sysContact, old_sys_contact);
- break;
- case SYSNAME:
- strncpy(hostname, (char *) old_host_name, HOSTNAME_MAX_LEN);
- hostname[HOSTNAME_MAX_LEN - 1]=' ';
- rc = resource_write (RESOURCE_TYPE_HOSTNAME, RESOURCE_INDEX_TYPE_NONE, NULL, (UINT32)hostname, 0, 0, 0);
- if (rc != 0)
- return SNMP_ERR_UNDOFAILED;
- break;
- case SYSLOCATION:
- strcpy(sysLocation, (char *) old_sys_location);
- break;
- default:
- break;
- }
- break;
- case COMMIT:
- break;
- case ACTION_FREE:
- break;
- default:
- break;
- }
- return SNMP_ERR_NOERROR;
- } /* end of writeSystem */
- /*********************
- *
- * Internal implementation functions - None
- *
- *********************/
- void hostname_callback (RESOURCE_ID id, unsigned long arg1, unsigned long arg2, unsigned long param1, unsigned long param2)
- {
- sysName =(char *)param1;
- return ;
- }
- long snmp_get_sysUpTime(void)
- {
- return get_uptime();
- }