snmp_main.c
资源名称:snmp.src.rar [点击查看]
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:15k
源码类别:
SNMP编程
开发平台:
C/C++
- #ifdef OS_VXWORKS
- #include <libsys/vos/vos_rtos.h>
- #include <libsys/vos/vos_msgq.h>
- #endif
- #include "config.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <libcmd/cmdparse.h>
- #include <sys/types.h>
- #include <ip/socket.h>
- #include <ip/in.h>
- #include <ip/inet.h>
- #include <ip/msg.h>
- #include <libsys/misc.h>
- #ifdef SWITCH
- #include <sys_conf.h>
- #else
- #include <../bsps/pub/bsps.h>
- #endif
- #include "asn1.h"
- #include "snmp_api.h"
- #include "snmp_impl.h"
- #include "system.h"
- #include "read_config.h"
- #include "snmp.h"
- #include "mib.h"
- #include "m2m.h"
- #include "snmp_vars.h"
- #include "agent_read_config.h"
- #include "snmpv3.h"
- #include "callback.h"
- #include "snmp_alarm.h"
- #include "default_store.h"
- #include "mib_module_config.h"
- #include "snmpd.h"
- #include "snmp_main.h"
- #include "config_struct.h"
- #include "snmp_debug.h"
- #include "snmp_timer.h"/*** sun define 2000.03.25 ***/
- #include "libres/resource.h"
- #include "var_struct.h"
- #include <objects.h>
- #include <boot/convert.h>
- #include <bspfuncs.h>
- #include <libsysverctl.h>
- #define sockadd_in soaddr_in
- #define sockaddr soaddr
- uint32 snmp_qid;/*snmp main queue identifier*/
- struct SNMPtimer t,*snmptimer=&t;/*** sun define 2000.03.25 ***/
- SNMP_PARA snmp_para;
- ULONG snmp_source_ip;
- void snmpd_timeout(void);
- extern void hostname_callback (RESOURCE_ID id, uint32 arg1, uint32 arg2, uint32 param1, uint32 param2);
- extern int receive(void);
- int register_snmp(unsigned long mibgourp,void (*init_mib)());
- extern int snmpd_init(void);
- void dispatch_voip_oid(void);
- void snmp_version_register(void);
- void snmp_enterprise_oid_set(void);
- /*Function prototype*/
- /********************************************************
- *
- * Routine: snmp
- *
- * Description:
- * This is the main loop of the SNMP module.
- *
- * Arguments:
- *
- * Return code:
- *
- * Side effects:
- *
- ********************************************************/
- void snmp_main()
- {
- #ifdef OS_VXWORKS /*SWITCH by yangyuhua 2005/7/22*/
- snmp_trace("snmp initn");
- snmpd_init();
- #endif
- snmp_trace("snmp startn");
- #ifndef OS_VXWORKS
- start_SNMPtimer(snmptimer);
- #endif
- #ifdef OLD_SNMP
- while (1){
- if ((rc = q_receive(snmp_qid, Q_WAIT, 0, msg)) != 0){
- vty_console_output("SNMP:q_receive() error (rc %d)n",rc);
- continue;
- }
- msg_type = msg[0];
- switch(msg_type){
- /*收到数据*/
- case SOCKET_DATARCVD:
- socketd = msg[1];
- sd_length = msg[2];
- receive(socketd,sd_length);
- break;
- /*连接建立*/
- case SOCKET_CONNECTED:
- /*连接正在断开*/
- case SOCKET_DISCONNECTING:
- /*连接已经断开*/
- case SOCKET_DISCONNECTED:
- /*socket不能再发送数据*/
- case SOCKET_CANTSENDMORE:
- /*socket不能再接收数据*/
- case SOCKET_CANTRCVMORE:
- break;
- case SNMP_TIMEOUT:
- break;
- default :
- break;
- }
- }
- #endif
- receive();
- }
- #include "snmp_vars.h"
- #include "mibgroup/mibII/system_mib.h"
- #include "mibgroup/mibII/snmp_mib.h"
- #ifndef OS_VXWORKS
- #include <boot/convert.h>
- #endif
- extern int do_snmp(int argc, char *argv[], struct user *u);
- static struct topcmds snmp_topcmd =
- { "snmp-server", cmdPref(PF_CMDNO, 0, 0) | cmdPriv(0, PV_SUBCMDSET, 0, 0), IF_ANY, ~FG_CFG_ST, IF_NULL, FG_CONFIG,
- do_snmp, NULL, NULL, 2, 0,
- "snmp-server -- Modify SNMP parameters",
- "snmp-server -- 设置 SNMP 参数 ",
- NULLCHAR, NULLCHAR
- };
- extern long snmp_showrunning(DEVICE_ID diID);
- extern struct cmds debug_snmp_cmds[];
- extern struct cmds snmp_show_cmds[];
- #ifdef SWITCH
- #define DEFAULT_PRODUCT_ID 100 /* switch product id is based on this value */
- #else
- #define DEFAULT_PRODUCT_ID 32 /* router product id is based on this value */
- #endif
- oid default_enterprise[10] = {1, 3, 6, 1, 4, 1, ENTERPRISE_NUMBER, PRODUCEID, DEFAULT_PRODUCT_ID, 0};
- size_t default_enterprise_len = 10;
- unsigned char def_syscontact[SNMP_MAX_SYSCON_LEN];
- unsigned char def_syslocation[SNMP_MAX_SYSLOC_LEN];
- char version_descr[2048] = VERS_DESC;
- void snmp_module_init()
- {
- int rc;
- RESOURCE_ID id;
- registercmd(&snmp_topcmd);
- interface_set_showrunning_service(MODULE_TYPE_SNMP,(INT32 (*)(DEVICE_ID))snmp_showrunning);
- register_snmp(1,init_system_mib);
- register_snmp(11,init_snmp_mib);
- rc =register_subcmd_tab("debug", cmdPref(PF_CMDNO, 0, 0), IF_NULL, FG_ENABLE,debug_snmp_cmds,1);
- assert(rc==1);
- rc = register_subcmd_tab("show", 0, IF_NULL, FG_ENABLE, snmp_show_cmds, 1);
- assert(rc == 1);
- memset(&snmp_para,0,sizeof(SNMP_PARA));
- snmp_para.packetsize = SNMP_DEFAULT_PACKETSIZE;
- snmp_para.queuelen = SNMP_DEFAULT_TRAP_QUEUELEN;
- snmp_para.timeout = SNMP_DEFAULT_TRAP_TIMEOUT;
- /* Initialize timer
- snmptimer->func = (void (*)(void*))snmpd_timeout;
- set_SNMPtimer(snmptimer, SNMPTIMER_TIMEOUT);
- create_SNMPtimer(snmptimer);*/
- /* start_SNMPtimer(snmptimer);
- stop_SNMPtimer(snmptimer);*/
- #ifdef OLD_SNMP
- if ((rc = q_create(SNMP_QUEUE_NAME, SNMP_QUEUE_LEN, Q_FIFO | Q_LIMIT, &snmp_qid)) != NOERR)
- vty_console_output("SNMP: q_create() error (rc %d)n",rc);
- #endif
- #ifndef OS_VXWORKS
- snmp_trace("snmp initn");/*add by yangyuhua, 2005-7-22*/
- snmpd_init();
- #endif
- rc = resource_register_synchronizer_callback (&id, RESOURCE_TYPE_HOSTNAME,
- RESOURCE_INDEX_TYPE_NONE, NULL, hostname_callback, 0, 0);
- assert(rc==0);
- #ifdef SWITCH
- sys_task_delay(1);
- #endif
- #ifdef OS_VXWORKS
- /*finally, spawn snmp_main*/
- sys_task_spawn(TASK_NAME_SNMP, SYS_TASK_PRI_NORMAL, 0,1024*64, snmp_main, NULL, 0);
- #endif
- snmp_version_register();
- }
- /********************************************************
- *
- * Routine: snmp_wait_reset
- *
- * Description:
- * Wait until the SNMP module is initialized.
- * Loop until SNMP module is initialized:
- * Discard any message received until a RESET
- * action request is received and executed
- * sucessfully.
- *
- * Arguments:
- *
- * Return code:
- *
- * Side effects:
- *
- ********************************************************/
- void snmpd_timeout(void)
- {
- snmp_timeout();
- /*由于使用了循环定时器,不用每次超时重新启动定时器,孙希2003.1.8
- start_SNMPtimer(snmptimer);*/
- }
- #include "snmp/snmpreg.h"
- struct snmp_list *SnmpMibList=NULL;
- long subtrees_enable = FALSE;/*** sun define 2000.07.27 ***/
- int register_snmp(unsigned long mibgourp,void (*init_mib)(void))
- {
- struct snmp_list *vp;
- if(subtrees_enable){/*** sun define 2000.07.27 ***/
- (*init_mib)();
- return 0;
- }
- if((vp = (struct snmp_list *)sys_mem_malloc(sizeof(struct snmp_list)))==NULL){
- return 0;
- }
- memset((void *)vp, 0, sizeof(struct snmp_list));
- vp->mibgroup = mibgourp;
- vp->init_mib = init_mib;
- vp->next = SnmpMibList;
- SnmpMibList = vp;
- return 0;
- }
- void snmp_miblist_init(void)
- {
- SnmpMibList=NULL;
- subtrees_enable = FALSE;/*** sun define 2000.07.27 ***/
- snmp_enterprise_oid_set();
- }
- #ifndef SWITCH
- /*add by zhaoyonggang, 2004-12-14*/
- void dispatch_voip_oid(void)
- {
- USHORT t;
- char router_series[20];
- t = sys_get_router_type();
- if(get_extend_info_string(INFO_ROUTER_SERIES,router_series))
- strcpy(router_series,"");
- if(t==ROUTER_TYPE_V100)
- default_enterprise[8] = 251; /*IP phone V100*/
- else if (t==ROUTER_TYPE_V200)
- default_enterprise[8] = 252; /*IP phone V200*/
- else if (t==ROUTER_TYPE_V300)
- default_enterprise[8] = 253; /*IP phone V300*/
- else if (t==ROUTER_TYPE_V500)
- default_enterprise[8] = 254; /*IP phone V500*/
- else
- {
- if(strcmp(router_series, "SuperB GW300") == 0)
- default_enterprise[8] = 260;
- else if(strcmp(router_series, "SuperB GW500") == 0)
- default_enterprise[8] = 261;
- else if(strcmp(router_series, "SuperB GW204") == 0)
- default_enterprise[8] = 262;
- else if(strcmp(router_series, "SuperB GW232") == 0)
- default_enterprise[8] = 263;
- else if(strcmp(router_series, "SuperB MINI") == 0)
- default_enterprise[8] = 270;
- else if(strcmp(router_series, "SuperB GW1000") == 0)
- default_enterprise[8] = 280;
- else if(strcmp(router_series, "SuperB GW2000") == 0)
- default_enterprise[8] = 281;
- else if(strcmp(router_series, "SuperB GK300") == 0)
- default_enterprise[8] = 290;
- else if(strcmp(router_series, "SuperB GK1000") == 0)
- default_enterprise[8] = 291;
- else if(strcmp(router_series, "SuperB PROXY300") == 0)
- default_enterprise[8] = 300;
- else if(strcmp(router_series, "SuperB PROXY1000") == 0)
- default_enterprise[8] = 301;
- /* else
- default_enterprise[8] = 0;*/
- }
- }
- #endif
- /************************************************************************/
- /* DATE: 2006/1/13 */
- /* PURPOSE: rmon version */
- /* */
- /************************************************************************/
- void snmp_version_register(void)
- {
- struct version_list ver;
- /*register module cmd version*/
- ver.module_type = MODULE_TYPE_SNMP;
- ver.version = 0x00010003;
- strcpy(ver.module_name,"SNMP");
- sprintf(ver.module_description,"Simple Network Management Protocol(May23.2008)");
- ver.next = NULL;
- register_module_version(&ver);
- }
- void snmp_enterprise_oid_old_set(void)
- {
- unsigned char short_company_name[30];
- unsigned short switch_type;
- #ifdef SWITCH
- switch_type = sys_get_switch_type();
- if(switch_type < SWITCH_TYPE_BASE)
- switch_type = SWITCH_TYPE_BASE;
- switch_type -= SWITCH_TYPE_BASE;
- switch_type += DEFAULT_PRODUCT_ID;
- default_enterprise[8] = (oid) switch_type;
- #ifdef S3424
- if (!get_extend_info_string(INFO_SHORT_COMPANY_NAME, short_company_name)
- && stricmp(short_company_name, KYLAND_SHORT_VENDOR_NAME) == 0)
- {
- default_enterprise[6] = KYLAND_ENTERPRISE_NUMBER;
- default_enterprise[7] = 6;
- default_enterprise[8] = 5;
- default_enterprise_len = 9;
- memcpy(def_syslocation, "No 95 Building,Southeast Corner of XisanqiBridge,Haidian,Beijing,china", SNMP_MAX_SYSLOC_LEN);
- memcpy(def_syscontact, "+86-10-82900771", SNMP_MAX_SYSCON_LEN);
- memcpy(version_descr, "KYLAND", 10);
- }
- #endif
- #else
- switch(sys_get_router_type()){
- case ROUTER_TYPE_1760:
- default_enterprise[8]=83;
- break;
- case ROUTER_TYPE_1750:
- default_enterprise[8]=82;
- break;
- case ROUTER_TYPE_2750:
- default_enterprise[8]=20;
- break;
- case ROUTER_TYPE_3700:
- default_enterprise[8]=30;
- break;
- case ROUTER_TYPE_3720:
- default_enterprise[8]=32;
- break;
- case ROUTER_TYPE_5000:
- default_enterprise[8]=50;
- break;
- case ROUTER_TYPE_V100:
- default_enterprise[8]=71;
- break;
- case ROUTER_TYPE_V200:
- default_enterprise[8]=72;
- break;
- case ROUTER_TYPE_V300:
- default_enterprise[8]=73;
- break;
- case ROUTER_TYPE_V500:
- default_enterprise[8]=75;
- break;
- case ROUTER_TYPE_3660:
- default_enterprise[8]=93;
- break;
- case ROUTER_TYPE_3680:
- default_enterprise[8]=98;
- break;
- case ROUTER_TYPE_2640:
- default_enterprise[8]=64;
- break;
- case ROUTER_TYPE_2621:
- {
- EPPROM_INFO *rom_info;
- rom_info = (EPPROM_INFO *)(sys_get_epprom_infoaddr()+0x40);/*by xujiasheng(a) 2006-7-19*/
- if (rom_info->hw_version == 0x10)
- {
- default_enterprise[8]=60; /*this is 2621*/
- break;
- }
- if (rom_info->hw_version == 0x20)
- {
- default_enterprise[8]=61; /*this is 2611*/
- break;
- }
- }
- break;
- case ROUTER_TYPE_2650:
- case ROUTER_TYPE_2650B:
- default_enterprise[8]=88;
- break;
- case ROUTER_TYPE_1720:
- default_enterprise[8]=81;
- break;
- case ROUTER_TYPE_1721:
- {
- EPPROM_INFO *rom_info;
- rom_info = (EPPROM_INFO *)sys_get_epprom_infoaddr();
- if ((rom_info->hw_version == 0x10)||(rom_info->hw_version == 0x30))
- {
- default_enterprise[8]=84; /*this is 1721*/
- break;
- }
- if ((rom_info->hw_version == 0x20)||(rom_info->hw_version == 0x40))
- {
- default_enterprise[8]=85; /*this is 1710*/
- break;
- }
- }
- break;
- case ROUTER_TYPE_2630:
- default_enterprise[8]=62;
- break;
- default:
- default_enterprise[8]=sys_get_router_type();
- break;
- }
- memset (short_company_name, 0, sizeof(short_company_name));
- if (!get_extend_info_string(INFO_SHORT_COMPANY_NAME, short_company_name))
- {
- if (stricmp(short_company_name, DIGITAL_CHINA_SHORT_VENDOR_NAME) == 0) {
- default_enterprise[6] = DIGITAL_CHINA_ENTERPRISE_NUMBER;
- }
- else if (stricmp(short_company_name, DLINK_SHORT_VENDOR_NAME) == 0) {
- default_enterprise[6] = DLINK_ENTERPRISE_NUMBER;
- default_enterprise[7] = 10;
- default_enterprise[8] = 56;
- switch(sys_get_router_type()) {
- case ROUTER_TYPE_1750:
- default_enterprise[9] = 1;
- break;
- case ROUTER_TYPE_2630:
- default_enterprise[9] = 2;
- break;
- case ROUTER_TYPE_3660:
- default_enterprise[9] = 3;
- break;
- default:
- default_enterprise[9] = 0;
- break;
- }
- }
- else if (stricmp(short_company_name, LEGEND_SHORT_VENDOR_NAME) == 0)
- {
- default_enterprise[6] = LEGEND_ENTERPRISE_NUMBER;
- switch(sys_get_router_type())
- {
- case ROUTER_TYPE_1750:
- default_enterprise[8] = 1705;
- break;
- default:
- default_enterprise[8] = 0;
- break;
- }
- }
- }
- #ifndef SWITCH
- /*add by zhaoyonggang, 2004-12-14*/
- dispatch_voip_oid();
- #endif
- #endif
- }
- void snmp_enterprise_oid_set(void)
- {
- oid sys_id[10];
- int rc=0;
- int i=0;
- if (!eprom_extend())
- {
- get_sys_info(version_descr, SNMP_MAX_DESCR_LEN);
- snmp_enterprise_oid_old_set();
- return;
- }
- else
- {
- get_extend_info_string(INFO_COMPANY_SNMP_SYSLOCATION,def_syslocation);
- get_extend_info_string(INFO_COMPANY_SNMP_SYSCONTACT,def_syscontact);
- if (get_extend_info_string(INFO_COMPANY_SNMP_SYSDESCR,version_descr))
- {
- get_sys_info(version_descr, SNMP_MAX_DESCR_LEN);
- }
- }
- rc = eprom_system_id((unsigned long *)sys_id);
- if (rc > 10)
- {
- Print("ERROR :snmp trace system_id, length =%dn",rc);
- rc =10;
- }
- if (rc > 0)
- {
- for (i=0;i<rc;i++)
- {
- default_enterprise[i]=sys_id[i];
- }
- for (i=rc; i<10; i++)
- {
- default_enterprise[i]=0;
- }
- default_enterprise_len = rc;
- }
- else
- {
- rc = eprom_vendor_id();
- if (rc >0)
- default_enterprise[6] = rc;/*otherwise use the default value(3320)*/
- rc = eprom_product_id();
- if (rc >0)
- default_enterprise[8] = rc;
- else
- snmp_enterprise_oid_old_set();
- }
- }