This commit is contained in:
zhangsz
2025-03-03 11:01:26 +08:00
parent 5f1710dc22
commit dae6fc93f7
1057 changed files with 519829 additions and 0 deletions

98
plat/snmp/Makefile Normal file
View File

@@ -0,0 +1,98 @@
##----------------------------------------------------------##
## ##
## Universal Makefile for module template : V1.6.3 ##
## ##
## Created : Wei Liu 07/04/11 ##
## Revision: [Last]Wei Liu 07/07/07 ##
## ##
##----------------------------------------------------------##
##---------------------------------------------------------------------##
##--------------------------------------
##
## Project correlation(Customer define)
##
##--------------------------------------
## MODULE= [Module Name]
## TYPE = app/plat => Module Type
## DBUG_FLAGS_ADD = [Module Define Gcc Flags for Debug ]
## DBUG_FLAGS_ADD = [Module Define Gcc Flags for Release]
## BUILD = lib/exef => Output file format
## CFG = debug/release => Build Configuration
## SRC_PATH = [Source file path]
## INC_PATH = [Include file path]
## APP_PATH = [App Module path]
## PLT_PATH = [Plat Module path]
## PLT_LIB = [Needed plat lib for Link] => just for test or wxc2main
## APP_LIB = [Needed app lib for Link] => just for test or wxc2main
## LIB_ADD = [Needed Extend lib for Link] => just for test or wxc2main
## PLT_LIB e.g. = haepub fsm mng proto kernel aif mgc mgcp sip rtp \
## 8ecp bicc smpp xapp tcap mtp3 m2ua \
## snmp iptrans debug sccp public
##
## APP_LIB e.g. = msc vlr ssf hlr ae pps mnp smsc vms aas
## LIB_ADD e.g. = -liba3a8 -lm
## OBJ_ADD = [Extend third party object files needed]
## TEST_OBJ_PATH = [module object files Path for test ] => just for test
##---------------------------------------------------------------------##
MODULE = snmp
TYPE = plat
DBUG_FLAGS_ADD = -D_CDMA_SNMP
RELS_FLAGS_ADD = -D_CDMA_SNMP
##Default commonly as below
BUILD = lib
CFG = debug
PLT_LIB =
APP_LIB =
LIB_ADD =
SRC_PATH = ./src
INC_PATH = ./src/include
PLT_PATH = ../../plat
APP_PATH = ../../mss
OBJ_ADD =
TEST_OBJ_PATH =
PREPROC_CMD =
POSTPROC_CMD =
##---------------------------------------------------------------------##
##--------------------------------------
##
## Make configuration(Customer define)
##
##--------------------------------------
## CCFLAG_SWITCH = on/off => gcc flag show on/off
## COVER_NEED = yes/no => PTF cover report needed
## COVER_REPORT_PATH = [path ] => PTF cover report path
CCFLAG_SWITCH = off
COVER_NEED = no
COVER_REPORT_PATH = ./output
##---------------------------------------------------------------------##
##--------------------------------------
##
## include makefile.rules (Do not change)
##
##--------------------------------------
include Makefile.rules

315
plat/snmp/src/heartbeat.c Normal file
View File

@@ -0,0 +1,315 @@
/*****************************************************/
/*Title: heartbeat.c */
/*Descr: Generate flatform's heartbeart */
/*Author: Liang Hanxi */
/*Create: 2002-06-24 */
/*Modify: */
/*****************************************************/
#include "./include/heartbeat.h"
// fix multiple definition====================================
DWORD ExistIP[64];
DWORD ExistIPNum;
//
heartbeat_shm *HbShm=NULL;
static DWORD ObjID[20]=FLATFORM_HB_OID;
static snmp_addr HBAddr;
static BYTE InitOnce=0;
static DWORD latestAlmCmp=0;
static BYTE latestAlmCod=0;
static void send_alarm();
static void send_heartbeat();
void heartbeat_init(u_short sys_id)
{
int shmid;
struct shmid_ds shm_info;
if(InitOnce == 0)
{
ExistIPNum = 0;
HBAddr.local_port = HBAddr.remote_port = HB_PORT;
HBAddr.remote_ip = inet_addr(HB_IP);
HBAddr.broadcast = 1;
sysctrl_init();
}
if(HbShm==NULL)
{
/*open share memory*/
if((shmid=shmget(HEARTBEAT_SHM_KEY+wxc2_get_shm_offset(),
sizeof(heartbeat_shm),HEARTBEAT_SHM_PERM))==-1)
{
if((shmid=shmget(HEARTBEAT_SHM_KEY+wxc2_get_shm_offset(),
sizeof(heartbeat_shm),HEARTBEAT_SHM_PERM|IPC_CREAT))==-1)
{
printf("fail to open share memory");
exit(1);
}
}
if((HbShm=(heartbeat_shm *)shmat(shmid,0,0))==(heartbeat_shm *)-1)
{
printf("fail to attach share memory");
exit(1);
}
shmctl(shmid,IPC_STAT,&shm_info);
if(shm_info.shm_nattch==1)
{
memset(HbShm,0,sizeof(heartbeat_shm));
HbShm->msg.sys_id=0xFF;
HbShm->msg.subsys_id=0xFF;
}
}
if((sys_id & 0xFF)!=0xFF)
{
HbShm->msg.sys_id =HbShm->sys_id=sys_id>>8;
HbShm->msg.subsys_id =HbShm->subsys_id=sys_id & 0xFF;
snmp_debug_hb(2);
}
InitOnce=1;
}
void heartbeat_timer() //called per 20ms
{
static time_t l_oldtime = 0,l_time;
static int counter=0;
l_time = time(NULL);
if(l_time - l_oldtime >= 10)
counter = 500;
system_report();
switch(counter)
{
case 500:
send_heartbeat();
counter=0;
l_oldtime = l_time;
break;
default:
if(HbShm->alarm_num >0)
{
//system_report();
send_alarm();
counter=0;
}
else
counter++;
break;
}
}
void send_heartbeat()
{
int i;
short len,msglen=0;
snmp_pdu pdu;
u_long curtime;
curtime=htonl(time(NULL));
memcpy(HbShm->msg.timestamp,(BYTE *)&curtime,4);
for(i=0;i<8;i++)
HbShm->msg.led_color[i]=(HbShm->led[2*i]<<4) | HbShm->led[2*i+1];
for(i=0;i<COMPONENT_NUM;i++)
{
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=0;
#endif
HbShm->msg.info[msglen++]=i;
len=HbShm->component[i].status_len;
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=len >> 8;
#endif
HbShm->msg.info[msglen++]=len & 255;
memcpy(HbShm->msg.info+msglen,HbShm->component[i].status,len);
msglen+=len;
}
#if _CDMA_SNMP
HbShm->msg.length[0] = msglen >> 8;
HbShm->msg.length[1] = msglen & 255;
msglen++;
msglen+=19;
#else
HbShm->msg.length =msglen;
msglen+=18;
#endif
#if _CDMA_SNMP
/* daniel modify on 2005-7-24 */
HbShm->msg.component_id[0]=latestAlmCmp >> 8;
HbShm->msg.component_id[1]=latestAlmCmp & 255;
/* daniel modify on 2005-7-24 */
#else
HbShm->msg.component_id=latestAlmCmp;
#endif
HbShm->msg.alarm_code =latestAlmCod;
/*send out---->*/
pdu.error_index =pdu.error_status =0;
pdu.pdu_type =PDU_TRAP;
pdu.request_id =0;
memcpy(pdu.var[0].msg,(BYTE *)&HbShm->msg,msglen);
pdu.var_num=1;
pdu.var[0].msglen =msglen;
pdu.var[0].vartype =4;
memcpy(pdu.var[0].oid,ObjID,14*sizeof(DWORD));
pdu.var[0].oidlen =14;
HBAddr.local_port =HBAddr.remote_port =HB_PORT;
HBAddr.remote_ip =inet_addr(HB_IP);
for(i = 0; i < ExistIPNum; i++)
{
HBAddr.remote_ip = ExistIP[i];
HBAddr.broadcast = 0;
snmp_plat_send(&pdu, &HBAddr);
}
}
void send_alarm()
{
int i;
short len,msglen=0;
snmp_pdu pdu;
u_long curtime;
curtime=htonl(time(NULL));
memcpy(HbShm->msg.timestamp,(BYTE *)&curtime,4);
for(i=0;i<8;i++)
HbShm->msg.led_color[i]=(HbShm->led[2*i]<<4) | HbShm->led[2*i+1];
if(HbShm->alarm_num<=0)
return;
for(i=0;i<ALARM_COMPONENT_NUM;i++)
{
if(HbShm->alarm[i].flag)
{
#if _CDMA_SNMP
HbShm->msg.component_id[0] = i >> 8;
HbShm->msg.component_id[1] = i & 255;
#else
HbShm->msg.component_id= i;
#endif
HbShm->msg.alarm_code =HbShm->alarm[i].alarm_code;
HbShm->alarm[i].flag =0;
/* daniel add on 2005-7-24 */
latestAlmCmp = i;
latestAlmCod = HbShm->msg.alarm_code;
/* daniel add on 2005-7-24 */
break;
}
}
for(i=0;i<COMPONENT_NUM;i++)
{
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=0;
#endif
HbShm->msg.info[msglen++]=i;
len=HbShm->component[i].status_len;
#if _CDMA_SNMP
HbShm->msg.info[msglen++]=len >> 8;
#endif
HbShm->msg.info[msglen++]=len&255;
memcpy(HbShm->msg.info+msglen,HbShm->component[i].status,len);
msglen+=len;
}
#if _CDMA_SNMP
HbShm->msg.length[0] = msglen >> 8;
HbShm->msg.length[1] = msglen & 255;
msglen++;
msglen+=19;
#else
HbShm->msg.length =msglen;
msglen+=18;
#endif
if(HbShm->alarm_num >=1)
HbShm->alarm_num--;
/*send out---->*/
pdu.error_index =pdu.error_status =0;
pdu.pdu_type =PDU_TRAP;
pdu.request_id =0;
memcpy(pdu.var[0].msg,(BYTE *)&HbShm->msg,msglen);
pdu.var_num=1;
pdu.var[0].msglen =msglen;
pdu.var[0].vartype =4;
memcpy(pdu.var[0].oid,ObjID,14*sizeof(DWORD));
pdu.var[0].oidlen =14;
HBAddr.local_port = 4950; /* to test if 4950 works */
HBAddr.remote_port =HB_PORT;
HBAddr.remote_ip =inet_addr(HB_IP);
for(i = 0; i < ExistIPNum; i++)
{
HBAddr.broadcast = 0;
HBAddr.remote_ip = ExistIP[i];
snmp_plat_send(&pdu, &HBAddr);
}
}
void set_led(BYTE nled,BYTE ncolor)
{
if(HbShm==NULL)
heartbeat_init(0xFF);
HbShm->led[nled]=ncolor;
}
void set_alarm(WORD component_id,BYTE alarm_code)
{
if(HbShm==NULL)
heartbeat_init(0xFF);
if(component_id>ALARM_COMPONENT_NUM)
{
snmp_debug(SNMPDB_ALARM | SNMPDB_ERR, "Fail to send Alarm:No Such component id(%d)",component_id);
return;
}
snmp_debug(SNMPDB_ALARM, "set alarm.component id=%d,alarm_code=%d",component_id,alarm_code);
if(HbShm->alarm[component_id].alarm_code !=alarm_code)
{
HbShm->alarm[component_id].flag=1;
HbShm->alarm_num ++;
}
HbShm->alarm[component_id].alarm_code =alarm_code;
}
void set_status(BYTE component_id,int info_len,BYTE *info)
{
if(HbShm == NULL)
heartbeat_init(0xFF);
if(component_id > COMPONENT_NUM)
{
snmp_debug(SNMPDB_ERR, "Fail to set status of component %d:No Such component id",component_id);
return;
}
/*
if(info_len>=256)
{
snmp_debug(SNMPDB_ERR, "Fail to set status of component %d:the length(%d) of status part exceed limit(256Byte)",component_id,info_len);
return;
}*/
if(info_len > 512)
info_len=512;
HbShm->component[component_id].status_len =info_len;
memcpy(HbShm->component[component_id].status,info,info_len);
}

View File

@@ -0,0 +1,88 @@
/*************************************************/
/*Title: heartbeat.h */
/*Descr: heartbeat processor(Head File) */
/*Author: Liang Hanxi */
/*Create: 2002-4-5 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef __HEART_BEAT__H
#define __HEART_BEAT__H
#include "snmp.h"
#include "time.h"
#include "sysctrl.h"
#define HEARTBEAT_SHM_KEY 0x00112233
#define HEARTBEAT_SHM_PERM 0666
#define COMPONENT_NUM 10
#define ALARM_COMPONENT_NUM 1024
#define LED_NUM 16
#define FLATFORM_HB_OID {1,3,6,1,4,1,1373,2,3,2,0}
#define FLATFORM_HB_OIDLEN 11
#define HB_PORT 4957
#define HB_IP "172.18.255.255"
#define INFO_LEN 1024
#define MasterSubSysID 0
#define MasterSysID 0
// fix multiple definition====================================
extern DWORD ExistIP[64];
extern DWORD ExistIPNum;
//
typedef struct component_buf{
BOOL flag;
BYTE alarm_code;
int status_len;
BYTE status[512];
}component_buf;
typedef struct alarm_buf{
BOOL flag;
BYTE alarm_code;
}alarm_buf;
typedef struct heartbeat_msg{
BYTE sys_id;
BYTE subsys_id;
BYTE timestamp[4];
BYTE led_color[8];
#if _CDMA_SNMP
BYTE component_id[2];
#else
BYTE component_id;
#endif
BYTE alarm_code;
#if _CDMA_SNMP
BYTE length[2];
#else
BYTE reserved;
BYTE length;
#endif
BYTE info[INFO_LEN];
}heartbeat_msg;
typedef struct heartbeat_shm{
BYTE sys_id;
BYTE subsys_id;
BYTE led[LED_NUM];
component_buf component[COMPONENT_NUM];
alarm_buf alarm[ALARM_COMPONENT_NUM];
BYTE alarm_num;
heartbeat_msg msg;
}heartbeat_shm;
void set_led(BYTE nled,BYTE ncolor);
void set_alarm(WORD component_id,BYTE alarm_code);
void set_status(BYTE component_id,int info_len,BYTE *info);
void heartbeat_init(u_short sys_id);
void heartbeat_timer();
#endif

View File

@@ -0,0 +1,100 @@
#ifndef _SNMP_MACRO_H
#define _SNMP_MACRO_H
#define SyntaxInteger 0x02
#define SyntaxOctetString 0x04
#define SyntaxIpAddress 0x40
#define SyntaxCounter 0x41
#define SyntaxTimeTicks 0x43
#define SyntaxOpaque 0x44
#define Create_Object_Cloumn 1
#define Create_Sub_Object_Command 1
#define GET_NULL() do { \
*pdata = 0; \
*vartype = SyntaxInteger; \
datalen = 1; \
} while(0)
#define GET_INTEGER(integer) do { \
*((int *)pdata) = htonl(integer); \
*vartype = SyntaxInteger; \
datalen = 4; \
} while(0)
#define SET_INTEGER(integer) do { \
int i; \
if(datalen > 4) \
return -1; \
for(i = 0, integer = 0; i < datalen; i++) \
{ \
integer = integer << 8; \
integer += pdata[i]; \
} \
} while(0)
#define RETURN_INTEGER(integer) do { \
int i; \
if(datalen > 4) \
datalen = 4; \
for(i = 0; i < datalen; i++) \
{ \
pdata[datalen-i-1] = ((u8 *)&integer)[i]; \
} \
} while(0)
#undef GET_U8 /* undefine the "public" macro */
#define GET_U8(byte) do { \
*pdata = byte; \
*vartype = SyntaxInteger; \
datalen = 1; \
} while(0)
#define SET_U8(byte) do { \
byte = pdata[datalen-1]; \
} while(0)
#define GET_STRING(string) do { \
strcpy((char *)pdata, (char *)string); \
*vartype = SyntaxOctetString; \
datalen = strlen((char *)string); \
} while(0)
#define SET_STRING(string) do { \
memcpy(string, pdata, datalen); \
string[datalen] = 0; \
} while(0)
#define GET_OCTETSTRING(octets, count) do { \
memcpy(pdata, octets, count); \
*vartype = SyntaxOctetString; \
datalen = count; \
} while(0)
#define SET_OCTETSTRING(octets, count) do { \
memcpy(octets, pdata, count); \
} while(0)
#define GET_OPAQUE(octets, count) do { \
memcpy(pdata, octets, count); \
*vartype = SyntaxOpaque; \
datalen = count; \
} while(0)
#define SET_OPAQUE(octets, count) do { \
memcpy(octets, pdata, count); \
} while(0)
#define GET_IP(ip) do { \
memcpy(pdata, ip, 4); \
*vartype = SyntaxIpAddress; \
datalen = 4; \
} while(0)
#define SET_IP(ip) do { \
memcpy(ip, pdata, 4); \
} while(0)
#endif

View File

@@ -0,0 +1,415 @@
/*************************************************/
/*Title: snmp.h */
/*Descr: SNMP Agent(Head File) */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef _SNMP_HEAD_FILE
#define _SNMP_HEAD_FILE
#include "../../../debug/src/include/debug.h"
#include "../../../iptrans/src/include/iptrans.h"
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#define MAX_ENGINE 16
#define SNMP_DEBUG_MASK 0
#define SNMP_DEBUG_LOG 0
#define SNMP_VERSION {1, 0, 01}
#define SNMP_V1 0
#define SNMP_V2 1
#define SNMP_V3 3
#define COMMUNITY "public"
#define SNMP_ANY_COMMUNITY 0 //Should be set to 1 if snmp module is invoked in OMC
#define BIND_MAX 50
#define OID_MAXLEN 32
//#define FIX_OID {0x2b,0x06,0x01,0x04,0x01,0x8a,0x5d,0x01,0x01}
#define FIX_OID {1,3,6,1,4,1,1373,1,3}
//{iso(1).org(3).dod(6).internet(1).private(4).enterprises(1).interWAVE(1373).1.1}
#define FIX_OIDLEN 8
#define SNMP_OID {1,3,6,1,4,1,1373,1,3,2,3,3}
#define SNMP_OIDLEN 12
typedef enum usmStats
{
UnsupportedSecLevels = 1,
NotInTimeWindows,
UnknownUserNames,
UnknownEngineIDs,
WrongDigests,
DecryptionErrors
}usmstatsErrors;
#define HEAP_SIZE 500*1024
#define SNMP_PORT 4957
#define SNMP_VAR_MAX 1600
//PDU
#define PDU_GET 0
#define PDU_GETNEXT 1
#define PDU_RSP 2
#define PDU_SET 3
#define PDU_GETBULK 5
#define PDU_INFORM 6
#define PDU_TRAP 7
#define PDU_REPORT 8
//variable type
#define NoSuchObject 0x80
#define NosuchInstance 0x81
#define EndOfMibView 0x82
//Debug
#define SNMPDB_IN 0x00
#define SNMPDB_OUT 0x01
#define SNMPDB_ERR 0x02
#define SNMPDB_NOREG 0x04
#define SNMPDB_VALUE 0x10
#define SNMPDB_ALARM 0x20
#define SNMPDB_MIB 0x40
#define SNMPDB_SYS 0x80
#ifndef _T_BYTE
#define _T_BYTE
typedef unsigned char BYTE;
#endif
#ifndef _T_BOOL
#define _T_BOOL
typedef unsigned char BOOL;
#endif
#ifndef _T_DWORD
#define _T_DWORD
typedef unsigned int DWORD;
#endif
#ifndef _T_WORD
#define _T_WORD
typedef unsigned short WORD;
#endif
extern char SnmpVer[16];
typedef struct var_list{
DWORD oid[OID_MAXLEN];
BYTE oidlen;
BYTE vartype;
short msglen;
BYTE msg[SNMP_VAR_MAX];
}var_list;
typedef struct _HeaderData
{
DWORD seqFlag;
DWORD msgID; //INTEGER (0..2147483647),
DWORD msgMaxSize; //INTEGER (484..2147483647),
char msgFlags;// OCTET STRING (SIZE(1)),
// .... ...1 authFlag
// .... ..1. privFlag
// .... .1.. reportableFlag
DWORD msgSecurityModel;// INTEGER (1..2147483647)
}HeaderData;
typedef struct _ScopedPDU
{
DWORD seqFlag;
char contextEngineID[32];
char contextName[32];
// snmp_pdu data;
}ScopedPDU;
typedef struct _ScopedPduData
{
DWORD optFlag;
ScopedPDU plaintext;
char encryptedPDU[SNMP_VAR_MAX];// OCTET STRING -- encrypted scopedPDU value
}ScopedPduData;
typedef struct _UsmSecurityParameters
{ //global User-based security parameters
DWORD seqFlag;
char msgAuthoritativeEngineID[32];
DWORD msgAuthoritativeEngineBoots;// INTEGER (0..2147483647),
DWORD msgAuthoritativeEngineTime;// INTEGER (0..2147483647),
char msgUserName[32];//OCTET STRING (SIZE(0..32)),
char msgAuthenticationParameters[32];//-- authentication protocol specific parameters
char msgPrivacyParameters[16];//-- privacy protocol specific parameters
}UsmSecurityParameters;
typedef struct _SNMPv3Message
{
DWORD seqFlag;
DWORD msgVersion; //INTEGER ( 0 .. 2147483647 ),
HeaderData msgGlobalData;
char msgSecurityParameters[128];
ScopedPduData msgData;
}SNMPv3Message;
typedef struct snmp_pdu //protocol data unit
{
BYTE pdu_type; //0-get request 2-get(set) respond 3-set request 7-trap
DWORD request_id;
char community[16];
BYTE var_num;
var_list var[BIND_MAX];
BYTE error_status; //0:no error 1:too big 2:nosuchname 3:badvalue 4:readonly 5:generr
BYTE error_index;
//add by yizane start
DWORD non_repeaters; //in getbulk replace error_status
DWORD max_repetitions; //in getbulk replace error_index
//add by yizane end
//v3
DWORD msgVersion; //INTEGER ( 0 .. 2147483647 ),
HeaderData msgGlobalData;
UsmSecurityParameters msgSecurityParameters;
ScopedPduData msgData;
} snmp_pdu;
typedef struct _snmpV3SecurityPara
{
char engineType;//1=client, 0=server, 2=internal mss to ems;
char securityEngineID[32];
char securityUserName[32];
char contextName[32];
char contextEngineID[32];
BYTE securityLevel;//0 no-auth&no-pri, 1auth&no-pri, 3 auth&pri
BYTE authProtocol;
char authPassword[32];
BYTE authKey[16];
BYTE privacyProtocol;
char privacyPassword[32];
BYTE privacyKey[16];
DWORD boots;
DWORD authInTime;
DWORD remote_ip;
WORD remote_port;
char admin_state;
}snmpV3SecurityPara;
typedef struct snmp_addr
{
DWORD remote_ip;
DWORD local_ip;
WORD remote_port;
WORD local_port;
BYTE broadcast;
} snmp_addr;
typedef struct snmp_msg
{
snmp_addr addr;
message_list msglist;
}snmp_msg;
typedef struct snmp_buf
{
snmp_msg msgout[256];
BYTE readsub, writesub;
}snmp_buf;
//snmp_buf snmp_outbuf;
typedef struct snmp_filter
{
BYTE flag;
BYTE type;
BYTE oidlen;
DWORD ip;
DWORD oid[OID_MAXLEN];
short local_port;
short remote_port;
}snmp_filter;
#define MAX_SNMP_RECORD 32
#define SNMP_SHM_KEY 0x60000001
#define SNMP_SHM_PERM 0666
typedef struct snmp_log_info
{
BYTE currentNo; /*from 0-MAX_SNMP_RECORD-1 */
char log_info[MAX_SNMP_RECORD][512];
}snmp_log_info;
/*---------------------------------------*/
typedef void (*trapcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len,snmp_addr *addr);
typedef int (*setcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len);
typedef int (*getcall)(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane start
typedef int (*getnextcall)(BYTE in_oidlen, DWORD *in_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane end
typedef void (*rspcall)(snmp_pdu *pdu,snmp_addr *addr);
typedef int (*snmpcall)(snmp_pdu *pdu,snmp_addr *addr);
typedef struct mib_nod{
DWORD id;
trapcall trap_proc;
setcall set_proc;
getcall get_proc;
//add by yizane start
getnextcall getnext_proc;
//add by yizane end
rspcall rsp_proc;
snmpcall snmp_proc;
struct mib_nod *pnext;
struct mib_nod *psub;
}mib_nod;
typedef struct snmp_csta{
DWORD timestamp;
DWORD trap_in;
DWORD trap_out;
DWORD set_in;
DWORD set_out;
DWORD get_in;
DWORD get_out;
//add by yizane start
DWORD getnext_in;
DWORD getnext_out;
DWORD getbulk_in;
DWORD getbulk_out;
//add by yizane end
DWORD rsp_in;
DWORD rsp_out;
DWORD fail_parse;
DWORD not_register;
DWORD fail_build;
}snmp_csta;
typedef struct snmp_mib{
BYTE sysid[2];
BYTE setpasswd[10];
BYTE getpasswd[10];
DWORD set_oid[64][20];
DWORD get_oid[64][20];
DWORD trap_oid[64][20];
DWORD rsp_oid[64][20];
}snmp_mib;
/********************************************************************/
/* Macros For ASN.1 Encoding */
/********************************************************************/
#define insert_length(buf,pos,len) {\
if(len<0)\
{\
snmp_debug(SNMPDB_ERR,"length should be larger than zero.");\
return 0;\
}\
else if(len<0x80)\
*(buf+pos--)=len;\
else if(len<0x100)\
{\
*(buf+pos--)=len;\
*(buf+pos--)=0x81;\
}\
else if(len<1600)\
{\
*(buf+pos--)=len & 0xFF;\
*(buf+pos--)=len >>8;\
*(buf+pos--)=0x82;\
}\
else\
{\
snmp_debug(SNMPDB_ERR,"Length(%d) exceed limit.varindex=%d",len,var_num);\
return 0;\
}\
}
#define enc_integer(buf,pos,val) {\
if(val<0)\
{\
snmp_debug(SNMPDB_ERR,"integer should larger than zero.val=%d",val);\
return -1;\
}\
else if(val<0x80)\
{\
*(buf+pos--)=(BYTE)val;\
*(buf+pos--)=1;\
}\
else if(val<0x8000)\
{\
*(buf+pos--)=val & 0xFF;\
*(buf+pos--)=val>>8;\
*(buf+pos--)=2;\
}\
else if(val<0x800000)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=val>>16;\
*(buf+pos--)=3;\
}\
else if(val<0x80000000)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=(val & 0xFFFFFF)>>16;\
*(buf+pos--)=val>>24;\
*(buf+pos--)=4;\
}\
else if(val<0xFFFFFFFF)\
{\
*(buf+pos--)= val & 0xFF;\
*(buf+pos--)=(val & 0xFFFF)>>8;\
*(buf+pos--)=(val & 0xFFFFFF)>>16;\
*(buf+pos--)=val>>24;\
*(buf+pos--)=0;\
*(buf+pos--)=5;\
}\
else\
{\
snmp_debug(SNMPDB_ERR,"Integer's value exceed limit.val=%d",val);\
return -1;\
}\
}
void snmp_init(WORD nport);
void snmpmib_init();
void snmp_timer();
int snmp_send(snmp_pdu *msgbuf,snmp_addr *addr);
int snmp_plat_send(snmp_pdu *msgbuf,snmp_addr *addr);
int snmp_receive(snmp_pdu *msgbuf,snmp_addr *addr);
int get_response(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int set_response(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen);
//add by yizane start
int getnext_response(BYTE pr_oidlen, DWORD *pre_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//add by yizane end
void inquire_trapmsg(trapcall trap_proc);
void inquire_setmsg(BYTE oidlen,DWORD *oid,setcall set_proc);
void inquire_getmsg(BYTE oidlen,DWORD *oid,getcall get_proc);
void inquire_rspmsg(BYTE oidlen,DWORD *oid,rspcall rsp_proc);
void inquire_snmpmsg(BYTE oidlen,DWORD *oid,snmpcall snmp_proc);
//add by yizane start
void inquire_getnextmsg(BYTE oidlen,DWORD *oid,getnextcall getnext_proc);
//add by yizane end
void rob_trapmsg(BYTE oidlen,DWORD *oid,trapcall trap_proc);
BYTE encode_integer(BYTE *buf,DWORD val);
DWORD decode_integer(BYTE *buf,BYTE len);
void snmp_log(char *info);
void snmp_debug(DWORD mask, const char *fmt,...);
void snmp_showbuf(DWORD mask, BYTE *buf, int len);
void snmp_logmsg(DWORD mask, snmp_pdu *pdu, snmp_addr *addr);
void snmp_debug_setup();
void snmp_debug_timer();
void snmp_debug_csta(BYTE npage,int instance);
void snmp_debug_hb(BYTE npage);
void snmp_init_serial_number(char *Serial);
void snmp_set_timenosync();//call of this function will tell snmp module do not sync the omc time
#include "table.h"
/********************************************************************/
#endif

View File

@@ -0,0 +1,77 @@
#ifndef snmpv3authcryp_h
#define snmpv3authcryp_h
#include "../../../../plat/public/src/include/public.h"
#define AUTH_KEY_LEN 16
#define MD5_DIGEST_90_LEN 12
extern int snmpV3AuthenticateOutgoingMsg
(/* result >0:success,else fail */
u8 *inAuthKey, /*secret key for authentication*/
u16 inAuthKeyLen,
u8 *inWholeMsg, /* unauthenticated complete message*/
u16 inWholeMsgLen,
u8 *outAuthParameters, /* authenticationParamemeter */
u16 *outAuthParametersLen
);
extern int snmpV3AuthenticateIncomingMsg
(/* result >0:success,else fail */
u8 *inAuthKey, /*secret key for authentication*/
u16 inAuthKeyLen,
u8 *inWholeMsg, /* as received on the wire*/
u16 inWholeMsgLen,
u8 *inAuthParameters, /* as received on the wire*/
u16 inAuthParametersLen
);
extern int snmpV3EncryptData
(/* result >0:success,else fail */
u8 *inEncryptKey, /* secret key for encryption*/
u16 inEncryptKeyLen,
u8 *inDataToEncrypt, /* data to encrypt (scopedPDU)*/
u16 inDataToEncryptLen,
u8 *outEncryptedData, /* encrypted data (encryptedPDU)*/
u16 *outEncryptedDataLen,
u8 *outPrivParameters, /* filled in by service provider*/
u16 *outPrivParametersLen,
ulong runTime
);
extern int snmpV3DecryptData
(/* result >0:success,else fail */
u8 *inDecryptKey, /*secret key for decryption*/
u16 inDecryptKeyLen,
u8 *inPrivParameters, /* as received on the wire*/
u16 inPrivParametersLen,
u8 *inEncryptedData, /* encrypted data (encryptedPDU)*/
u16 inEncryptedDataLen,
u8 *outDecryptedData, /*decrypted data (scopedPDU)*/
u16 *outDecryptedDataLen
);
#endif

View File

@@ -0,0 +1,57 @@
/*************************************************/
/*Title: sysctrl.h */
/*Descr: */
/*Author: Liang Hanxi */
/*Create: 2002-4-5 */
/*Modify: 2002-6-25 */
/*************************************************/
#ifndef __SYSCTRL__H
#define __SYSCTRL__H
#include "time.h"
#include <sys/vfs.h>
#include <errno.h>
#include <mntent.h>
#include <sys/sysinfo.h>
#include "heartbeat.h"
#define SYS_T_report 50*60*1 //1min
#define MAX_PARTITION_NUM 16
typedef struct partition
{
char mountpoint_path[16];
DWORD used_space;
DWORD total_space;
}partition;
typedef struct hb_sysinfo{
DWORD up_time;
BYTE idle_cpu_rate;
DWORD ram_total_used;
DWORD ram_total;
DWORD ram_swap_used;
DWORD ram_swap_total;
DWORD ram_buffers;
DWORD ram_cached;
BYTE pt_count;
partition pt[MAX_PARTITION_NUM];
}hb_sysinfo;
typedef struct mnt_list
{
char mnt_fsname[50];
char mnt_dir[50];
DWORD total;
DWORD free;
DWORD limit;
}mnt_list;
void sysctrl_init();
void system_report();
#endif

View File

@@ -0,0 +1,42 @@
#ifndef _SNMP_TABLE_H
#define _SNMP_TABLE_H
#define MAX_COLUMNAR_OF_TABLE 48
typedef struct table_index {
u8 len;
u32 oid[32];
} table_index;
/* Get an exist instance from the inclusive one pointed to by address 'instance' */
typedef int (*table_get_instance)(u32 *instance);
typedef int (*table_get_resp)(u32 column, u32 *instance, u8 *pdata, u8 *vartype);
typedef int (*table_set_resp)(u32 column, u32 *instance, u8 *pdata, u16 datalen);
struct snmp_table_profile {
char name[32];
table_index index; /* oid of table with entry exclusive */
u32 column[MAX_COLUMNAR_OF_TABLE]; /* define all columns of a table, use '0' indicating the end */
u8 level; /* level of instance in order to identify a row */
table_get_instance get_instance;
table_get_resp get_resp;
table_set_resp set_resp;
};
struct snmp_register {
char name[32];
u8 prefix_len; /* Module prefix len */
u32 prefix_oid[32]; /* Module prefix oid */
u8 num_of_table; /* how many tables being registered, such tables is pointed to by address 'table' */
struct snmp_table_profile *table;
/* In case no table is matched, default callback function is invoked if it is not NULL. Hence,
you can implement get/set in much the same way before, i.e. traveling all branches of MIB */
getcall default_get_resp;
setcall default_set_resp;
};
extern int register_snmp_table(struct snmp_register *reg);
#endif

301
plat/snmp/src/md5.c Normal file
View File

@@ -0,0 +1,301 @@
/*
* This code implements the MD5 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
* written by Colin Plumb in 1993, no copyright is claimed.
* This code is in the public domain; do with it what you wish.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD5Context structure, pass it to SQUID_MD5Init, call SQUID_MD5Update as
* needed on buffers full of bytes, and then call SQUID_MD5Final, which
* will fill a supplied 16-byte array with the digest.
*
* Changed so as no longer to depend on Colin Plumb's `usual.h' header
* definitions; now uses stuff from dpkg's config.h.
* - Ian Jackson <ian@chiark.greenend.org.uk>.
* Still in the public domain.
*
* Changed SQUID_MD5Update to take a void * for easier use and some other
* minor cleanup. - Henrik Nordstrom <henrik@henriknordstrom.net>.
* Still in the public domain.
*
* Prefixed all symbols with "Squid" so they don't collide with
* other libraries. Henrik Nordstrom <henrik@henriknordstrom.net>.
* Still in the public domain.
*/
#include <string.h> /* for memcpy() */
#include <sys/types.h> /* for stupid systems */
#include <netinet/in.h> /* for ntohl() */
#include "md5.h"
/*
* Now that we have several alternatives the MD5 files are
* passed in by default. But a header-selection decides whether
* this provided version is to be built.
* TODO: may obsolete the MSV #if below.
*/
void byteSwap(uint32_t * buf, unsigned words)
{
uint8_t *p = (uint8_t *) buf;
do {
*buf++ = (uint32_t) ((unsigned) p[3] << 8 | p[2]) << 16 |
((unsigned) p[1] << 8 | p[0]);
p += 4;
} while (--words);
}
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void SQUID_MD5Init(struct SquidMD5Context *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[3] = 0x10325476;
ctx->bytes[0] = 0;
ctx->bytes[1] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void SQUID_MD5Update(struct SquidMD5Context *ctx, const void *_buf, unsigned len)
{
uint8_t const *buf = _buf;
uint32_t t;
/* Update byte count */
t = ctx->bytes[0];
if ((ctx->bytes[0] = t + len) < t)
ctx->bytes[1]++; /* Carry from low to high */
t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
if (t > len) {
memcpy((uint8_t *) ctx->in + 64 - t, buf, len);
return;
}
/* First chunk is an odd size */
memcpy((uint8_t *) ctx->in + 64 - t, buf, t);
byteSwap(ctx->in, 16);
SQUID_MD5Transform(ctx->buf, ctx->in); // Squid_MD5Transform(ctx->buf, ctx->in);
buf += t;
len -= t;
/* Process data in 64-byte chunks */
while (len >= 64) {
memcpy(ctx->in, buf, 64);
byteSwap(ctx->in, 16);
SQUID_MD5Transform(ctx->buf, ctx->in);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void SQUID_MD5Final(unsigned char digest[16], struct SquidMD5Context *ctx)
{
int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
uint8_t *p = (uint8_t *) ctx->in + count;
/* Set the first char of padding to 0x80. There is always room. */
*p++ = 0x80;
/* Bytes of padding needed to make 56 bytes (-8..55) */
count = 56 - 1 - count;
if (count < 0) { /* Padding forces an extra block */
memset(p, 0, count + 8);
byteSwap(ctx->in, 16);
SQUID_MD5Transform(ctx->buf, ctx->in);
p = (uint8_t *) ctx->in;
count = 56;
}
memset(p, 0, count);
byteSwap(ctx->in, 14);
/* Append length in bits and transform */
ctx->in[14] = ctx->bytes[0] << 3;
ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
SQUID_MD5Transform(ctx->buf, ctx->in);
byteSwap(ctx->buf, 4);
memcpy(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
}
void md5_calc(uint8_t *output, uint8_t *input, uint32_t inlen)
{
SquidMD5_CTX context;
SQUID_MD5Init(&context);
SQUID_MD5Update(&context, input, inlen);
SQUID_MD5Final(output, &context);
}
void password_to_key_md5(
u_char *password,
u_int passwordlen,
u_char *engineID,
u_int engineLength,
u_char *key)
{
SquidMD5_CTX MD;
u_char *cp, password_buf[64];
u_long password_index = 0;
u_long count = 0, i;
SQUID_MD5Init (&MD); /* initialize MD5 */
/**********************************************/
/* Use while loop until we've done 1 Megabyte */
/**********************************************/
while (count < 1048576) {
cp = password_buf;
for (i = 0; i < 64; i++) {
/*************************************************/
/* Take the next octet of the password, wrapping */
/* to the beginning of the password as necessary.*/
/*************************************************/
*cp++ = password[password_index++ % passwordlen];
}
SQUID_MD5Update (&MD, password_buf, 64);
count += 64;
}
SQUID_MD5Final (key, &MD); /* tell MD5 we're done */
/*****************************************************/
/* Now localize the key with the engineID and pass */
/* through MD5 to produce final key */
/* May want to ensure that engineLength <= 32, */
/* otherwise need to use a buffer larger than 64 */
/*****************************************************/
memcpy(password_buf, key, 16);
memcpy(password_buf+16, engineID, engineLength);
memcpy(password_buf+16+engineLength, key, 16);
SQUID_MD5Init(&MD);
SQUID_MD5Update(&MD, password_buf, 32+engineLength);
SQUID_MD5Final(key, &MD);
return;
}
//#ifndef ASM_MD5
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f,w,x,y,z,in,s) \
(w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. SQUID_MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
void SQUID_MD5Transform(uint32_t buf[4], uint32_t const in[16])
{
register uint32_t a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
//#endif /* !ASM_MD5 */

21
plat/snmp/src/md5.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef SQUID_MD5_H
#define SQUID_MD5_H
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef struct SquidMD5Context {
uint32_t buf[4];
uint32_t bytes[2];
uint32_t in[16];
}SquidMD5_CTX;
void SquidMD5Init(struct SquidMD5Context *context);
void SquidMD5Update(struct SquidMD5Context *context, const void *buf, unsigned len);
void SquidMD5Final(uint8_t digest[16], struct SquidMD5Context *context);
void SQUID_MD5Transform(uint32_t buf[4], uint32_t const in[16]); // void SquidMD5Transform(uint32_t buf[4], uint32_t const in[16]);
#define SQUID_MD5_DIGEST_LENGTH 16
#endif /** SQUID_MD5_H */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,171 @@
/********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib.h
Description: header file of snmp mib parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-9
History:
2007-6-9 v9.0.0 Create
BEWARE: Portions of this file are subject to the copyrights of
open-source project Net-SNMP.See the Net-SNMP's COPYING file
for more details
*********************************************************************/
#ifndef __WXC2_SNMP_MIB_H_
#define __WXC2_SNMP_MIB_H_
#define MAXLABEL 64 /* maximum characters in a label */
#define MAXTOKEN 128 /* maximum characters in a token */
#define MAXQUOTESTR (8192*4) /* maximum characters in a quoted string */
struct variable_list;
/*
* A linked list of tag-value pairs for enumerated integers.
*/
struct enum_list {
struct enum_list *next;
int value;
char *label;
};
/*
* A linked list of ranges
*/
struct range_list {
struct range_list *next;
int low, high;
};
/*
* A linked list of indexes
*/
struct index_list {
struct index_list *next;
char *ilabel;
char isimplied;
};
/*
* A linked list of varbinds
*/
struct varbind_list {
struct varbind_list *next;
char *vblabel;
};
/*
* A tree in the format of the tree structure of the MIB.
*/
struct tree {
struct tree *child_list; /* list of children of this node */
struct tree *next_peer; /* Next node in list of peers */
struct tree *next; /* Next node in hashed list of names */
struct tree *parent;
char *label; /* This node's textual name */
int subid; /* This node's integer subidentifier */
int modid; /* The module containing this node */
int number_modules;
int *module_list; /* To handle multiple modules */
int tc_index; /* index into tclist (-1 if NA) */
int type; /* This node's object type */
int access; /* This nodes access */
int status; /* This nodes status */
struct enum_list *enums; /* (optional) list of enumerated integers */
struct range_list *ranges;
struct index_list *indexes;
char *augments;
struct varbind_list *varbinds;
char *hint;
char *units;
char *description; /* description (a quoted string) */
char *reference; /* references (a quoted string) */
int reported; /* 1=report started in print_subtree... */
char *defaultValue;
int oid[32];
int oid_len;
};
/*
* Information held about each MIB module
*/
struct module_import {
char *label; /* The descriptor being imported */
int modid; /* The module imported from */
};
struct module {
char *name; /* This module's name */
char *file; /* The file containing the module */
struct module_import *imports; /* List of descriptors being imported */
int no_imports; /* The number of such import descriptors */
/*
* -1 implies the module hasn't been read in yet
*/
int modid; /* The index number of this module */
struct module *next; /* Linked list pointer */
};
struct module_compatability {
const char *old_module;
const char *new_module;
const char *tag; /* NULL implies unconditional replacement,
* otherwise node identifier or prefix */
size_t tag_len; /* 0 implies exact match (or unconditional) */
struct module_compatability *next; /* linked list */
};
/*
* non-aggregate types for tree end nodes
*/
#define TYPE_OTHER 0
#define TYPE_OBJID 1
#define TYPE_OCTETSTR 2
#define TYPE_INTEGER 3
#define TYPE_NETADDR 4
#define TYPE_IPADDR 5
#define TYPE_COUNTER 6
#define TYPE_GAUGE 7
#define TYPE_TIMETICKS 8
#define TYPE_OPAQUE 9
#define TYPE_NULL 10
#define TYPE_COUNTER64 11
#define TYPE_BITSTRING 12
#define TYPE_NSAPADDRESS 13
#define TYPE_UINTEGER 14
#define TYPE_UNSIGNED32 15
#define TYPE_INTEGER32 16
#define TYPE_SIMPLE_LAST 16
#define TYPE_TRAPTYPE 20
#define TYPE_NOTIFTYPE 21
#define TYPE_OBJGROUP 22
#define TYPE_NOTIFGROUP 23
#define TYPE_MODID 24
#define TYPE_AGENTCAP 25
#define TYPE_MODCOMP 26
#define TYPE_OBJIDENTITY 27
#define MIB_ACCESS_READONLY 18
#define MIB_ACCESS_READWRITE 19
#define MIB_ACCESS_WRITEONLY 20
#define MIB_ACCESS_NOACCESS 21
#define MIB_ACCESS_NOTIFY 67
#define MIB_ACCESS_CREATE 48
#define MIB_STATUS_MANDATORY 23
#define MIB_STATUS_OPTIONAL 24
#define MIB_STATUS_OBSOLETE 25
#define MIB_STATUS_DEPRECATED 39
#define MIB_STATUS_CURRENT 57
#define ANON "anonymous#"
#define ANON_LEN strlen(ANON)
#endif

View File

@@ -0,0 +1,122 @@
/***********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib_api.c
Description: API for SNMP MIB parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-19
History:
2007-6-9 v9.0.0 Create
***********************************************************************/
#include <stdio.h>
#include <sys/stat.h>
#include "snmp_mib.h"
#include "snmp_mib_api.h"
const char *mib_dir = "./mib";
const char *index_file = "index";
extern struct tree *tree_head;
extern struct module *module_head;
extern void new_module(const char *name, const char *file);
extern struct tree *read_module(const char *name);
extern int get_token(FILE * fp, char *token, int maxtlen);
extern void print_subtree(FILE * f, struct tree *tree, int count);
extern struct tree *find_tree_node(const char *name, int modid);
extern void free_tree(struct tree *Tree);
//return 0 on success
int init_mib()
{
//open dir and check mib files in the dir
{
FILE *ip, *fp;
char tmpstr[300];
int count = 0;
char token[MAXTOKEN];
char newline;
struct stat dir_stat, idx_stat;
char tmpstr1[300];
printf("Scanning directory %s\n", mib_dir);
snprintf(token, sizeof(token), "%s/%s", mib_dir, index_file);
token[ sizeof(token)-1 ] = 0;
if (stat(token, &idx_stat) == 0 && stat(mib_dir, &dir_stat) == 0) {
if ((ip = fopen(token, "r")) != NULL) {
while (fscanf(ip, "%256s%c", tmpstr, &newline) == 2)
{
/*
* If an overflow of the token or tmpstr buffers has been
* found log a message and break out of the while loop,
* thus the rest of the file tokens will be ignored.
*/
if (newline != '\n') {
printf("add_mibdir: strings scanned in from %s/%s " \
"are too large. count = %d\n ", mib_dir,
".index", count);
break;
}
snprintf(tmpstr1, sizeof(tmpstr1), "%s/%s", mib_dir, tmpstr);
tmpstr1[ sizeof(tmpstr1)-1 ] = 0;
fp = fopen(tmpstr1, "r");
if (fp == NULL) {
printf("Can not open mib file: %s\n", tmpstr1);
continue;
}
get_token(fp, token, MAXTOKEN);
fclose(fp);
new_module(token, tmpstr1);
count++;
}
fclose(ip);
}
else
printf("Can't read index\n");
}
else
printf("No index\n");
}
//read all mibs
{
struct module *mp;
for (mp = module_head; mp; mp = mp->next)
if (mp->no_imports == -1)
read_module(mp->name);
}
#ifdef _TEST_
{
//for debug
print_subtree(stdout, tree_head, 0);
}
#endif
return 0;
}
struct tree *getLGCroot()
{
return find_tree_node("lgcNS", -1);
}
void close_mib()
{
free_tree(tree_head);
return;
}
//test----------------------------------------------------------------------------
#ifdef _TEST_
int main(int argc, char *argv[])
{
init_mib();
return 0;
}
#endif
//end of test---------------------------------------------------------------------

View File

@@ -0,0 +1,49 @@
/***********************************************************************
Copyright ?2007 LGC Wireless, Inc. All rights reserved
File Name: snmp_mib.h
Description: header file of snmp mib parser
Version: v9.0.0
Author: Roy Jiang
Create Date: 2007-6-9
History:
2007-6-9 v9.0.0 Create
***********************************************************************/
#ifndef _WXC2_SNMP_MIB_API_H_
#define _WXC2_SNMP_MIB_API_H_
#include "snmp_mib.h"
/*
*Purpose: Init mib parser, read all mib files listed in ./mib/index,
* you should config which mib file you want to parse in the file./mib/index
*Input: None
*Output: None
*/
extern int init_mib();
/*
*Purpose: get the LGC root node of mib tree
*Input: None
*Output: LGC root tree node
*/
extern struct tree *getLGCroot();
/*
*Purpose: Close mib parser and release all resources used in parsing mib files
*Input: None
*Output: None
*/
extern void close_mib();
/*
*Purpose: Get tree node from oid
*Input: const int oid: oid of the tree node to get
int oidlen: the length of the oid
struct tree *subtree: tree node from which the search start
*Output: tree node
*/
extern struct tree *get_tree(const int *oid, int oidlen, struct tree *subtree);
#endif

3319
plat/snmp/src/snmp.c Normal file

File diff suppressed because it is too large Load Diff

503
plat/snmp/src/snmpdebug.c Normal file
View File

@@ -0,0 +1,503 @@
/**************************************************/
/*Title: snmpdebug.c */
/*Descr: Debug Module of SNMP Agent */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: */
/**************************************************/
#include "./include/heartbeat.h"
static char debug_buf[4096];
static char asc_in[256];
static DWORD title_id[20]={1,3,6,1,4,1,1373,1,3,2,3,1,2,13,8,1};
//private function
BYTE snmpDebugFull = 0;
static snmp_filter SnmpFilter[8];
static BYTE SnmpFilterIndex = 0;
static DWORD debug_mask = SNMP_DEBUG_MASK;
extern snmp_csta SnmpCsta[96];
extern heartbeat_shm *HbShm;
static char Type_Name[9][16] = {"Get", "GetNext"
, "Response", "Set", "Trap-V1", "GetBulk", "Inform", "Trap-V2", "Report"};
static char help_info[] = "Commands provided now:\r\n\
help T001\r\n\
log all Output all debug information\r\n\
log none Turn off all information\r\n\
log error on/off Show error information\r\n\
log noreg on/off Alarm if oid is not registered\r\n\
log value on/off Show value party of snmp message\r\n\
log alarm on/off Show alarm information of platform\r\n\
log sys on/off Show system information\r\n\
log type cond1 (and cond2...) Show message due to specified condition\r\n\
type should be snmp/trap/set/get/rsp\r\n\
One or more conditions could be taken:\r\n\
oid=x.x../ip=xx/local port=xx/remote port=xx\r\n\
For example:log trap oid=1.3.6.1.4.1373 and ip=172.18.98.1\r\n\
list register List all registered oid\r\n\
list status List system status\
";
int snmp_filter_msg(snmp_pdu *pMsg, snmp_addr *pAddr);
extern int Str2Oid(DWORD *pOid, char *pStr, BYTE maxLen);
extern void snmp_readpara();
extern void init_engine_para();
void snmp_debug_setup()
{
DWORD asc_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,4};
DWORD ascin_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,3};
DWORD status_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,1};
DWORD name_id[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,2};
static BYTE debug_status=1;
debug_set_response(15,asc_id, (BYTE *)debug_buf,1);
debug_set_response(15,ascin_id, (BYTE *)asc_in,1);
debug_set_response(15,status_id,&debug_status,1);
debug_set_response(15,name_id, (BYTE *)SnmpVer, strlen(SnmpVer));
}
void snmp_log(char *info)
{
if(strlen(debug_buf) + strlen(info) > 4096)
{
snmpDebugFull = 1;
return;
}
strncat(debug_buf, info, 4094);
strcat(debug_buf, "\r\n");
return;
}
int snmp_filter_msg(snmp_pdu *pMsg, snmp_addr *pAddr)
{
int i;
for(i = 0; i < SnmpFilterIndex % 9; i++)
{
if(SnmpFilter[i].type != pMsg->pdu_type && pMsg->pdu_type != 0xFF)
continue;
if(SnmpFilter[i].flag & 0x01)
{
if(SnmpFilter[i].ip != pAddr->remote_ip)
continue;
}
if(SnmpFilter[i].flag & 0x02)
{
if(memcmp(SnmpFilter[i].oid, pMsg->var[0].oid, SnmpFilter[i].oidlen * 4) != 0)
continue;
}
if(SnmpFilter[i].flag & 0x04)
{
if(SnmpFilter[i].local_port != pAddr->local_port)
continue;
}
if(SnmpFilter[i].flag & 0x08)
{
if(SnmpFilter[i].remote_port != pAddr->remote_port)
continue;
}
return 1;
}
return 0;
}
void snmp_debug(DWORD mask, const char *fmt, ...)
{
va_list ap;
char buf[1024];
if((mask & debug_mask) == 0 || snmpDebugFull)
return;
if(mask & SNMPDB_ERR)
strcat(debug_buf, "\33[31m");
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
strcat(buf,"\33[37m");
snmp_log(buf);
}
void snmp_showbuf(DWORD mask, BYTE *buf, int len)
{
int i = 0, j = 0, linemax = 24;
char info[1024];
if((debug_mask & SNMPDB_VALUE) == 0 || (debug_mask & mask) == 0 || snmpDebugFull)
return;
if(len >= 256)
len = 256;
while(i * 24 < len)
{
if((linemax = len - 24 * i) > 24)
linemax = 24;
for(j = 0; j < linemax; j ++)
{
sprintf(info + 3 * j, "%02X ", buf[24 * i + j]);
}
snmp_log(info);
i ++;
}
}
int oid2str(char *pStr, DWORD *oid, BYTE oidlen)
{
int i;
char tmpstr[16];
pStr[0] = 0;
if(oidlen > OID_MAXLEN) oidlen = OID_MAXLEN;
for(i = 0; i < oidlen - 1; i++)
{
sprintf(tmpstr, "%u.", oid[i]);
strcat(pStr, tmpstr);
}
sprintf(tmpstr, "%u", oid[oidlen - 1]);
strcat(pStr, tmpstr);
return 1;
}
void snmp_logvalue(BYTE *msgbuf, short msglen)
{
int len, i = 0, j, linemax;
char info[1024];
len = msglen % 256;
while(i * 24 < len)
{
if((linemax = len - 24 * i) > 24)
linemax = 24;
for(j = 0; j < linemax; j ++)
{
sprintf(info + 3 * j, "%02X ", msgbuf[24 * i + j]);
}
snmp_log(info);
i ++;
}
}
void snmp_logmsg(DWORD mask, snmp_pdu *pdu, snmp_addr *addr)
{
struct in_addr inaddr;
int i = 0;
char info[1024], oidstr[64], desc[64] = "\0";
if((debug_mask & mask) == 0 && snmp_filter_msg(pdu, addr) == 0)
return;
if(snmpDebugFull)
return;
if(mask & SNMPDB_ERR)
strcat(debug_buf, "\33[31m");
inaddr.s_addr = addr->remote_ip;
if(mask & SNMPDB_IN)
sprintf(desc, "\r\nlocal:%d <== %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
else if(mask & SNMPDB_OUT)
sprintf(desc, "\r\nlocal:%d ==> %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
else
sprintf(desc, "\r\nlocal:%d <=> %s:%d ", addr->local_port, inet_ntoa(inaddr), addr->remote_port);
sprintf(info, "%sType=%s, RID=%u, Error=%d, Bind=%d\33[37m", desc, Type_Name[pdu->pdu_type], pdu->request_id
, pdu->error_status, pdu->var_num);
snmp_log(info);
for(i = 0; i < pdu->var_num % 16; i++)
{
oid2str(oidstr, pdu->var[i].oid, pdu->var[i].oidlen);
sprintf(info, "oid[%d]=%s, vartype=%02x, msglen=%d", i, oidstr, pdu->var[i].vartype, pdu->var[i].msglen);
snmp_log(info);
if((debug_mask & SNMPDB_VALUE) == 0)
return;
snmp_logvalue(pdu->var[i].msg, pdu->var[i].msglen);
}
}
static int get_word(char *pWord, char *pInStr, BYTE max)
{
char *pStart = pInStr, *pCh = NULL;
while(*pStart == ' ' || *pStart == '\t')
pStart ++;
strncpy(pWord, pStart, max);
pCh = strchr(pWord, ' ');
if(pCh != NULL)
*pCh = 0;
return 1;
}
static int parsecond(char *cmdstr, BYTE type)
{
char *pKey = cmdstr, *pEQ, info[256], tmpStr[64], valStr[32];
if(SnmpFilterIndex >= 8)
{
snmp_log("only 8 pieces of condition can be set");
return 0;
}
sprintf(info, "type = 0x%02X", type);
SnmpFilter[SnmpFilterIndex].type = type;
SnmpFilter[SnmpFilterIndex].flag = 0;
if((pKey = strstr(cmdstr, "ip")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].ip = inet_addr(valStr);
sprintf(tmpStr, " and ip = %s", valStr);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x01;
}
if((pKey = strstr(cmdstr, "oid")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and oid = %s", valStr);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].oidlen = Str2Oid(SnmpFilter[SnmpFilterIndex].oid, valStr, OID_MAXLEN) % OID_MAXLEN;
SnmpFilter[SnmpFilterIndex].flag |= 0x02;
}
if((pKey = strstr(cmdstr, "local port")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].local_port = atoi(valStr);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and local port = %d", SnmpFilter[SnmpFilterIndex].local_port);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x04;
}
if((pKey = strstr(cmdstr, "remote port")) != NULL && (pEQ = strchr(pKey, '=')) != NULL)
{
get_word(valStr, pEQ + 1, 32);
SnmpFilter[SnmpFilterIndex].remote_port = atoi(valStr);
if(SnmpFilter[SnmpFilterIndex].flag > 0) strcat(info, " and ");
sprintf(tmpStr, " and remote port = %d", SnmpFilter[SnmpFilterIndex].remote_port);
strcat(info, tmpStr);
SnmpFilter[SnmpFilterIndex].flag |= 0x08;
}
SnmpFilterIndex ++;
snmp_log(info);
snmp_log("command OK");
return 1;
}
void snmp_debug_timer()
{
BYTE npage, nline;
char *cmdstr = asc_in + 1;
if(strlen(asc_in) <= 1)
return;
debug_buf[0] = '\0';
npage = asc_in[0] - 1;
if(npage > 0)
{
nline = atoi(asc_in + 1);
switch(npage)
{
case 1: //csta
snmp_debug_csta(1,nline);
break;
}
asc_in[0]='\0';
return;
}
asc_in[0]='\0';
if(strstr(cmdstr, "help") != NULL || strstr(cmdstr, "?") != NULL)
{
snmp_log(help_info);
return;
}
else if(strstr(cmdstr, "log all") != NULL)
{
SnmpFilterIndex = 0;
debug_mask = 0xFFFFFFFF;
}
else if(strstr(cmdstr, "log none") != NULL)
{
SnmpFilterIndex = 0;
debug_mask = 0;
}
else if(strstr(cmdstr, "log error on") != NULL)
debug_mask |= SNMPDB_ERR;
else if(strstr(cmdstr, "log error off") != NULL)
debug_mask &= (~SNMPDB_ERR);
else if(strstr(cmdstr, "log onreg on") != NULL)
debug_mask |= SNMPDB_NOREG;
else if(strstr(cmdstr, "log onreg off") != NULL)
debug_mask &= (~SNMPDB_NOREG);
else if(strstr(cmdstr, "log value on") != NULL)
debug_mask |= SNMPDB_VALUE;
else if(strstr(cmdstr, "log value off") != NULL)
debug_mask &= (~SNMPDB_VALUE);
else if(strstr(cmdstr, "log alarm on") != NULL)
debug_mask |= SNMPDB_ALARM;
else if(strstr(cmdstr, "log alarm off") != NULL)
debug_mask &= (~SNMPDB_ALARM);
else if(strstr(cmdstr, "log mib on") != NULL)
debug_mask |= SNMPDB_MIB;
else if(strstr(cmdstr, "log mib off") != NULL)
debug_mask &= (~SNMPDB_MIB);
else if(strstr(cmdstr, "log sys on") != NULL)
debug_mask |= SNMPDB_SYS;
else if(strstr(cmdstr, "log sys off") != NULL)
debug_mask &= (~SNMPDB_SYS);
else if(strstr(cmdstr, "log snmp") != NULL)
{
parsecond(cmdstr + 8, 0xFF);
return;
}
else if(strstr(cmdstr, "log get") != NULL)
{
parsecond(cmdstr + 7, 0);
return;
}
else if(strstr(cmdstr, "log set") != NULL)
{
parsecond(cmdstr + 7, 3);
return;
}
else if(strstr(cmdstr, "log rsp") != NULL)
{
parsecond(cmdstr + 7, 2);
return;
}
else if(strstr(cmdstr, "log trap") != NULL)
{
parsecond(cmdstr + 8, 7);
return;
}
else if(strstr(cmdstr, "reload") != NULL)
{
snmp_readpara();
init_engine_para();
snmp_log("Command OK");
return;
}
else
{
snmp_log("Invalid command/parameter(s).");
return;
}
snmp_log("Command OK");
return;
}
void snmp_debug_info(BYTE npage, BYTE nline, BYTE *pt, BYTE len)
{
DWORD oid[20]={1,3,6,1,4,1,1373,1,1,2,3,1,2,13,5,2,2};
oid[14] = 5 + npage;
oid[15] = 2 + nline;
debug_set_response(17, oid, pt, len);
}
/*-----------------------------------------------*/
/*-------------------CSTA------------------------*/
/*-----------------------------------------------*/
void snmp_debug_csta(BYTE npage,int instance)
{
char title[256];
snmp_csta *pcsta;
instance=instance%96;
pcsta=&SnmpCsta[instance];
sprintf(title,"\tSNMP-%s\tPage %02d\t\tMIB-CSTA(ID:1 Instance:%d)\r\n\r\n"\
"trap in\r\n"\
"trap out\r\n"\
"set in\r\n"\
"set out\r\n"\
"get in\r\n"\
"get out\r\n"\
"rsp in\r\n"\
"rsp out\r\n"\
"fail to parse\r\n"\
"OID Not registered\r\n"\
"fail to build\r\n", SnmpVer, npage, instance);
title_id[14]=5+npage;
debug_set_response(16,title_id, (BYTE *)title,strlen(title));
snmp_debug_info(npage,0,(BYTE *)&pcsta->trap_in,4);
snmp_debug_info(npage,1,(BYTE *)&pcsta->trap_out,4);
snmp_debug_info(npage,2,(BYTE *)&pcsta->set_in,4);
snmp_debug_info(npage,3,(BYTE *)&pcsta->set_out,4);
snmp_debug_info(npage,4,(BYTE *)&pcsta->get_in,4);
snmp_debug_info(npage,5,(BYTE *)&pcsta->get_out,4);
snmp_debug_info(npage,6,(BYTE *)&pcsta->rsp_in,4);
snmp_debug_info(npage,7,(BYTE *)&pcsta->rsp_out,4);
snmp_debug_info(npage,8,(BYTE *)&pcsta->fail_parse,4);
snmp_debug_info(npage,9,(BYTE *)&pcsta->not_register,4);
snmp_debug_info(npage,10,(BYTE *)&pcsta->fail_build,4);
}
void snmp_debug_hb(BYTE npage)
{
char title[512];
sprintf(title,"\tSNMP-%s\tPage %02d\t\tHeartBeat\r\n"\
"\r\n"\
"subsys id\r\n"\
"LED\r\n"\
"Component 0\r\n"\
"\r\n"\
"Component 1\r\n"\
"\r\n"\
"Component 2\r\n"\
"\r\n"\
"Component 3\r\n"\
"\r\n"\
"Component 4\r\n"\
"\r\n"\
"Component 5\r\n"\
"\r\n"\
"Component 6\r\n"\
"\r\n"\
"alarm num\r\n"\
"HB message\r\n\r\n", SnmpVer, npage);
title_id[14]=5+npage;
debug_set_response(16,title_id, (BYTE *)title,strlen(title));
snmp_debug_info(npage,0,(BYTE *)&HbShm->sys_id,2);
snmp_debug_info(npage,1,(BYTE *)HbShm->led,LED_NUM);
snmp_debug_info(npage,2,(BYTE *)&HbShm->component[0],20);
snmp_debug_info(npage,3,(BYTE *)&HbShm->component[0]+20,20);
snmp_debug_info(npage,4,(BYTE *)&HbShm->component[1],20);
snmp_debug_info(npage,5,(BYTE *)&HbShm->component[1]+20,20);
snmp_debug_info(npage,6,(BYTE *)&HbShm->component[2],20);
snmp_debug_info(npage,7,(BYTE *)&HbShm->component[2]+20,20);
snmp_debug_info(npage,8,(BYTE *)&HbShm->component[3],20);
snmp_debug_info(npage,9,(BYTE *)&HbShm->component[3]+20,20);
snmp_debug_info(npage,10,(BYTE *)&HbShm->component[4],20);
snmp_debug_info(npage,11,(BYTE *)&HbShm->component[4]+20,20);
snmp_debug_info(npage,12,(BYTE *)&HbShm->component[5],20);
snmp_debug_info(npage,13,(BYTE *)&HbShm->component[5]+20,20);
snmp_debug_info(npage,14,(BYTE *)&HbShm->component[6],20);
snmp_debug_info(npage,15,(BYTE *)&HbShm->component[6]+20,20);
snmp_debug_info(npage,16,(BYTE *)&HbShm->alarm_num,1);
snmp_debug_info(npage,17,(BYTE *)&HbShm->msg,20);
snmp_debug_info(npage,18,(BYTE *)&HbShm->msg+20,20);
snmp_debug_info(npage,19,(BYTE *)&HbShm->msg+40,20);
snmp_debug_info(npage,20,(BYTE *)&HbShm->msg+60,20);
snmp_debug_info(npage,21,(BYTE *)&HbShm->msg+80,20);
}

224
plat/snmp/src/snmpmib.c Normal file
View File

@@ -0,0 +1,224 @@
/**************************************************/
/*Title: snmpdebug.c */
/*Descr: Debug Module of SNMP Agent */
/*Author: Liang Hanxi */
/*Create: 2002-4-1 */
/*Modify: */
/**************************************************/
#include "./include/heartbeat.h"
static unsigned long l_starttime;
extern snmp_csta SnmpCsta[96];
extern int snmp_ins;
extern heartbeat_shm *HbShm;
int snmp_setmib(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen);
int snmp_getmib(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int snmp_getsys(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
void time_sync(BYTE oidlen,DWORD *oid,BYTE *pdata,WORD len,snmp_addr *addr);
static void submit_snmpcsta(BYTE *pdword,BYTE *pbyte,int len);
void snmpmib_init()
{
DWORD oid[OID_MAXLEN]=SNMP_OID;
DWORD sysoid[8]={1,3,6,1,2,1,1};
l_starttime=time(NULL);
inquire_getmsg(SNMP_OIDLEN,oid,snmp_getmib);
inquire_setmsg(SNMP_OIDLEN,oid,snmp_setmib);
inquire_getmsg(7,sysoid,snmp_getsys);
inquire_trapmsg(time_sync);
}
int snmp_setmib(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD datalen)
{
return -1;
switch(oid[SNMP_OIDLEN])
{
case 1: //Measure
return -1; //read only
case 2: //Configuration
switch(oid[SNMP_OIDLEN+1])
{
case 1: //SNMP Parameter
switch(oid[SNMP_OIDLEN+2])
{
case 1: //Port
return -1; //not allow
case 2: //password
return -1; //not provide now
}
break;
case 2:
break;
case 3:
break;
}
break;
case 3:
break;
}
}
int snmp_getmib(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
int instance;
snmp_csta *pcsta;
switch(oid[SNMP_OIDLEN])
{
case 1: //csta
if(oidlen!=SNMP_OIDLEN+2)
return -1;
instance=oid[SNMP_OIDLEN+1];
if(instance==snmp_ins || instance>95)
{
*vartype=NosuchInstance;
snmp_debug(SNMPDB_MIB,"csta message not ready.instance=%d",instance);
return -1;
}
pcsta=&SnmpCsta[instance];
if(pcsta->timestamp ==0)
{
snmp_debug(SNMPDB_MIB,"csta message is expired");
*vartype=5;
return 0;
}
*vartype=4;
snmp_debug(SNMPDB_MIB, "Respond csta message");
submit_snmpcsta((BYTE *)&pcsta->timestamp,pdata,sizeof(snmp_csta));
//memcpy(pdata,(BYTE *)&pcsta->timestamp,sizeof(snmp_csta));
return sizeof(snmp_csta);
default:
return -1;
}
return -1;
}
int snmp_getsys(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
unsigned long l_time;
char contact[50]="InterWAVE";
char sysname[50];
switch(oid[oidlen - 1])
{
case 1:
*vartype = 4;
memcpy(pdata, SnmpVer, strlen(SnmpVer));
return strlen(SnmpVer);
case 3:
l_time=time(NULL);
*vartype=0x43;
return encode_integer(pdata,(l_time-l_starttime)*100);
case 4:
*vartype=4;
memcpy(pdata,contact,strlen(contact));
return strlen(contact);
case 5:
*vartype=4;
sprintf(sysname,"SNMP Agent %d.%d",HbShm->sys_id,HbShm->subsys_id);
memcpy(pdata,sysname,strlen(sysname));
return strlen(sysname);
}
return -1;
}
#define SNMP_OMC_HB_OID {1,3,6,1,4,1,1373,2,4,1}
#define SNMP_OMC_HB_OIDLEN 10
#define SNMP_SERNUM_LEN 4
ull Bcd2Ull(BYTE *pBcd, int BcdLen);
extern unsigned long long SNMPSerialNumber;
extern BYTE snmp_synctime_flag;
void time_sync(BYTE oidlen,DWORD *oid,BYTE *pdata,WORD len,snmp_addr *addr)
{
//DWORD ObjID[20]=FLATFORM_HB_OID;
DWORD OMCHBObjID[20] = SNMP_OMC_HB_OID;
heartbeat_msg *pmsg=(heartbeat_msg *)pdata;
struct timeval tv;
struct timezone tz;
long l_time,l_temp;
static long master_host=0,master_timeout;
if (!snmp_synctime_flag)
return;
if(memcmp(&oid[7], &OMCHBObjID[7], (SNMP_OMC_HB_OIDLEN - 7) * 4) == 0)//omc heartbeat
{//automatic add omc ip into ip list,
//if receive omc heartbeat and the omc serial number is equial to the local serial number,
//then the platform will send heartbeat if received omc heartbeat
if ((SNMPSerialNumber==0) ||//support old version, do not have serial number
(Bcd2Ull(&pdata[39],SNMP_SERNUM_LEN)==SNMPSerialNumber) )
{}
else
return;
}
else// if it is not omc heartbeat then return
return;
if(pmsg->subsys_id ==HbShm->subsys_id && pmsg->sys_id ==HbShm->sys_id)
return;
/*
if(memcmp(&oid[9],&ObjID[9],(FLATFORM_HB_OIDLEN-9)*4)!=0)
{
return;
}
*/
/*
if(pmsg->sys_id!=MasterSysID || pmsg->subsys_id!=MasterSubSysID)
return;
*/
if(SNMPSerialNumber!=0){//New version
if(pdata[26]!=5&&pdata[26]!=7)//not the master omc
return;
}else{//Old version
if(pdata[21]!=5&&pdata[21]!=7)//not the master omc
return;
}
memcpy((BYTE *)&l_temp,pmsg->timestamp,4);
l_time=htonl(l_temp); //timestamp in heartbeat
gettimeofday(&tv,&tz); //local time
if(master_host==0 || master_timeout<=0)
{
snmp_debug(SNMPDB_SYS, "change master host to %lX",htonl(addr->remote_ip));
master_host=addr->remote_ip;
master_timeout=8;
}
if(addr->remote_ip != master_host)
{
master_timeout--;
snmp_debug(SNMPDB_SYS, "Found another master,ip=%lX.current master=%lX,timeout=%ld\n",htonl(addr->remote_ip),htonl(master_host),master_timeout);
return;
}
else
master_timeout=8;
if(abs(l_time-tv.tv_sec)<2) //compare
{
return;
}
tv.tv_sec =l_time;
settimeofday(&tv,&tz);
snmp_debug(SNMPDB_SYS, "system's time is changed by %lX\n",htonl(addr->remote_ip));
}
void submit_snmpcsta(BYTE *pdword,BYTE *pbyte,int len)
{
int i;
DWORD *p1,*p2;
for(i=0;i<len/4;i++)
{
p1=(DWORD *)(pdword+4*i);
p2=(DWORD *)(pbyte+4*i);
*p2=htonl(*p1);
}
}

269
plat/snmp/src/snmptable.c Normal file
View File

@@ -0,0 +1,269 @@
/*
* snmp/snmptable.c
*
* Copyright (C) 2008 ADC, Inc
* Written by: Xinyu Yan <xinyu.yan@adc.com>
*
* Description: provide a mechanism to register and maintain snmp table, simplify app programing
*/
#include "./include/snmp.h"
#define MAX_SNMP_REGISTER 32 /* Maximum of modules registering snmp table */
/* It's great chance that next object and current one in same table, so cache improves searching efficiency */
static struct snmp_table_profile *cache;
static struct snmp_register *reg_cache;
static struct snmp_register snmpregister[MAX_SNMP_REGISTER];
//#define PRINT_OID
#ifdef PRINT_OID
static void printoid(u32 *oid, u8 len)
{
int i;
if(len == 0)
printf("OID length is 0!\n");
printf("%ld", oid[0]);
for(i = 1; i < len; i++)
{
printf(".%ld", oid[i]);
}
printf("\n");
}
#endif
/* To check if oid matches oid prefix */
static int oid_match_prefix(u8 prefix_len, u32 *prefix, u8 oid_len, u32 *oid)
{
if(prefix_len == 0)
return 0;
if(oid_len < prefix_len)
return 0;
if(!memcmp(prefix, oid, prefix_len*sizeof(u32)))
return 1;
else
return 0;
}
static struct snmp_register *find_snmp_register(u8 oid_len, u32 *oid)
{
int i;
struct snmp_register *reg;
if(reg_cache != NULL)
{
if(oid_match_prefix(reg_cache->prefix_len, reg_cache->prefix_oid, oid_len, oid) == 1)
return reg_cache;
}
for(i = 0; i < MAX_SNMP_REGISTER; i++)
{
reg = &snmpregister[i];
if(oid_match_prefix(reg->prefix_len, reg->prefix_oid, oid_len, oid) == 1)
{
reg_cache = reg;
return reg;
}
}
return NULL;
}
/* Check cache first, otherwise check all snmp tables by matching oid with table index */
static struct snmp_table_profile *find_snmp_table(u8 oid_len, u32 *oid)
{
int i;
struct snmp_register *reg;
struct snmp_table_profile *t;
if(cache != NULL)
{
if(oid_match_prefix(cache->index.len, cache->index.oid, oid_len, oid) == 1)
return cache;
}
if((reg = find_snmp_register(oid_len, oid)) == NULL)
return NULL;
for(i = 0; i < reg->num_of_table; i++)
{
t = &reg->table[i];
if(oid_match_prefix(t->index.len, t->index.oid, oid_len, oid) == 1)
{
cache = t;
return t;
}
}
return NULL;
}
static u32 find_next_column(u32 current_column, u32 *columns)
{
int i;
for(i = 0; i < MAX_COLUMNAR_OF_TABLE-1; i++)
{
if(current_column == columns[i])
return columns[i+1];
}
return 0;
}
static void point_to_column_and_instance(u32 **column, u32 **instance, u32 *oid, struct snmp_table_profile *t)
{
if(t->level != 0)
{
*column = oid + t->index.len + 1; /* count the entry, hence plus 1 */
}
else
{
*column = oid + t->index.len;
}
*instance = *column + 1;
}
static int table_set_response(u8 oidlen, u32 *oid, u8 *pdata, u16 datalen)
{
struct snmp_table_profile *t;
struct snmp_register *reg;
u32 *column;
u32 *instance;
if((t = find_snmp_table(oidlen, oid)) == NULL)
{
if((reg = find_snmp_register(oidlen, oid)) == NULL)
return -1;
if(reg->default_set_resp == NULL)
return -1;
else
return reg->default_set_resp(oidlen, oid, pdata, datalen);
}
point_to_column_and_instance(&column, &instance, oid, t);
if(t->set_resp == NULL)
return -1;
else
return t->set_resp(*column, instance, pdata, datalen);
}
static int table_get_response(u8 oidlen, u32 *oid, u8 *pdata, u8 *vartype)
{
struct snmp_table_profile *t;
struct snmp_register *reg;
u32 *column;
u32 *instance;
if((t = find_snmp_table(oidlen, oid)) == NULL)
{
if((reg = find_snmp_register(oidlen, oid)) == NULL)
return -1;
if(reg->default_get_resp == NULL)
return -1;
else
return reg->default_get_resp(oidlen, oid, pdata, vartype);
}
point_to_column_and_instance(&column, &instance, oid, t);
if(t->get_resp == NULL)
return -1;
else
return t->get_resp(*column, instance, pdata, vartype);
}
static int get_next_oid(u8 *oidlen, u32 *oid)
{
struct snmp_table_profile *t;
u32 *entry;
u8 entry_oid_len;
u32 *column;
u8 column_oid_len;
u32 *instance;
u8 full_oid_len;
u8 plus_one_suppress = 0;
if((t = find_snmp_table(*oidlen, oid)) == NULL)
return 0;
if(t->level == 0) /* If MIB is not table basis, do not support get next */
return 0;
entry = oid + t->index.len;
entry_oid_len = t->index.len + 1;
column = entry + 1;
column_oid_len = entry_oid_len + 1;
instance = column + 1;
full_oid_len = column_oid_len + t->level;
if(*oidlen < full_oid_len)
{
memset(oid+*oidlen, 0, (full_oid_len-*oidlen)*sizeof(u32));
if(*oidlen < entry_oid_len)
*entry = 1;
if(*oidlen < column_oid_len)
*column = t->column[0];
*oidlen = full_oid_len;
plus_one_suppress = 1;
}
for(; *column != 0; *column = find_next_column(*column, t->column))
{
if(plus_one_suppress == 0)
instance[t->level-1]++;
if(t->get_instance(instance) == 1)
return 1;
else
{
memset(instance, 0, t->level*sizeof(u32));
plus_one_suppress = 1;
}
}
return 0;
}
static int table_getnext_response(u8 prev_oidlen, u32 *prev_oid, u8 *oidlen, u32 *oid, u8 *pdata, u8 *vartype)
{
memcpy(oid, prev_oid, prev_oidlen*sizeof(u32));
*oidlen = prev_oidlen;
#ifdef PRINT_OID
printf("Prev oid: ");
printoid(oid, *oidlen);
#endif
if(get_next_oid(oidlen, oid) == 1)
{
#ifdef PRINT_OID
printf("Next oid: ");
printoid(oid, *oidlen);
#endif
return table_get_response(*oidlen, oid, pdata, vartype);
}
else
return -1;
}
int register_snmp_table(struct snmp_register *reg)
{
int i;
struct snmp_register *r;
/* TODO: check table validation */
for(i = 0; i < MAX_SNMP_REGISTER; i++)
{
r = &snmpregister[i];
if(r->prefix_len == 0)
{
memcpy(r, reg, sizeof(struct snmp_register));
inquire_getmsg(r->prefix_len, r->prefix_oid, table_get_response);
inquire_setmsg(r->prefix_len, r->prefix_oid, table_set_response);
inquire_getnextmsg(r->prefix_len, r->prefix_oid, table_getnext_response);
return 1;
}
}
return 0;
}

254
plat/snmp/src/snmptest.c Normal file
View File

@@ -0,0 +1,254 @@
#include "./include/snmp.h"
#include "./include/heartbeat.h"
static struct itimerval itimer,old_itimer;
int intval;
char strval[50]="hello!";
static void On_Timer();
static void SetTimer();
static int get_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,BYTE *vartype);
static int getnext_rsp(BYTE in_oidlen, DWORD in_oid[], BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
static int set_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,WORD len);
static void pro_rsp(snmp_pdu *pdata,snmp_addr *addr);
//static int atoi_hex(char* input);
static DWORD oid[10] ={1,3,6,1,4,1,12189,2,2,50};
//static DWORD oid[11] ={1,3,6,1,4,1,12189,2,1,50,6};
static int table_len = 9;
static int row_len = 11;
static int table_id = 50;
void showbuf(BYTE *buf,int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%02X",buf[i]);
if(i>0 && i%25==0)
printf("\n");
else
printf(" ");
}
printf("\n");
}
static void showid(DWORD *buf,int len)
{
int i;
for(i=0;i<len;i++)
{
printf("%1u.",buf[i]);
if(i>0 && i%25==0)
printf("\n");
}
printf("\n");
}
static void trap_callback(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len,snmp_addr *addr)
{
//printf("Receive trap message,oid:");
//showid(oid,oidlen);
//showbuf(pdata,len);
}
int main()
{
intval=123456;
snmp_init(4961);
heartbeat_init(0x1234);
SetTimer();
inquire_trapmsg(trap_callback);
inquire_setmsg(1,oid,set_rsp);
inquire_getmsg(1,oid,get_rsp);
inquire_rspmsg(10,oid,pro_rsp);
inquire_getnextmsg(10,oid,getnext_rsp);
//cls;
set_led(0,8);
set_alarm(0,1);
{
DWORD request_id;
snmp_pdu request_msg;
snmp_addr request_addr;
// iptrans_init();
iptrMainInit();
request_msg.pdu_type = PDU_GETBULK;
request_msg.max_repetitions = 199900;
request_msg.non_repeaters = 0;
request_msg.request_id = 10;
request_msg.var_num = 1;
sprintf(request_msg.community, "public");
memcpy(request_msg.var[0].oid, oid, 4 * 10);
request_msg.var[0].oidlen = 10;
request_addr.remote_ip =inet_addr("127.0.0.1");
request_addr.local_ip =inet_addr("172.48.234.128");
request_addr.remote_port =4961;
request_addr.local_port =4961;
request_addr.broadcast =1;
request_id = snmp_send(&request_msg, &request_addr);
printf("%u\n",request_id);
sleep(2);
}
while(1)
{
usleep(50);
}
return 1;
}
int getnext_rsp(BYTE in_oidlen, DWORD *in_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
memcpy(oid,in_oid,in_oidlen*sizeof(DWORD));
//printf("receive command\n");
if(in_oidlen<12)
{
oid[10]=1;
oid[11]=1;
oid[12]=0;
in_oidlen=13;
}
else
{
oid[in_oidlen-1] =in_oid[in_oidlen-1]+1;
}
if(oid[in_oidlen-1]>12)
{
oid[in_oidlen-1]=0;
oid[in_oidlen-2]=in_oid[in_oidlen-2]+1;
if(oid[in_oidlen-2]>20)
return -1;
}
pdata[0] = oid[in_oidlen-1];
*vartype=2;
*oidlen = in_oidlen;
return 1;
}
void pro_rsp(snmp_pdu *pdata,snmp_addr *addr)
{
int i,getflag,value=0;
static int old_oid=0;
DWORD request_id;
snmp_pdu request_msg;
snmp_addr request_addr;
getflag = 1;
for(i=0;i<pdata->var_num;i++)
{
if(pdata->var[i].oid[table_len]!=table_id||pdata->var[i].vartype==EndOfMibView)
{
printf("\n");
getflag = 0;
break;
}
if(pdata->var[i].oid[row_len]!=old_oid)
printf("\n");
old_oid = pdata->var[i].oid[row_len];
value = decode_integer(pdata->var[i].msg,pdata->var[i].msglen);
printf("%-5d",value);
}
if(getflag)
{
request_msg.pdu_type = PDU_GETBULK;
request_msg.max_repetitions = 10;
request_msg.non_repeaters = 0;
request_msg.var_num = 1;
sprintf(request_msg.community, "public");
memcpy(request_msg.var[0].oid, pdata->var[pdata->var_num-1].oid, 4 * pdata->var[pdata->var_num-1].oidlen);
request_msg.var[0].oidlen = pdata->var[pdata->var_num-1].oidlen;
request_addr.remote_ip =addr->remote_ip;
request_addr.remote_port =addr->remote_port;
request_addr.local_port =addr->local_port;
request_id = snmp_send(&request_msg, &request_addr);
}
return;
}
void On_Timer()
{
snmp_timer();
iptrans_timer();
//heartbeat_timer();
}
void SetTimer()
{
struct sigaction act;
act.sa_handler=On_Timer;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
if(sigaction(SIGALRM,&act,NULL)<0)
{
perror("Produce Sigaction");
exit(1);
}
itimer.it_interval.tv_sec=0;
itimer.it_interval.tv_usec=1000;
itimer.it_value.tv_sec=0;
itimer.it_value.tv_usec=1000;
if (setitimer(ITIMER_REAL,&itimer,&old_itimer) !=0 )
{
printf("Setting Timer error! \n");
exit(1);
}
}
int get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
printf("I Receive get message,oid:");
showid(oid,oidlen);
/*
if(object_id[1]==2)
{
memcpy(data_ptr,(BYTE *)&intval,4);
return 4;
}
else
{
memcpy(data_ptr,(BYTE *)&strval,strlen(strval));
return strlen(strval);
}
*/
return -1;
}
int set_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len)
{
printf("I Receive set message,oid:");
showid(oid,oidlen);
showbuf(pdata,len);
/*
if(object_id[1]==2)
{
memcpy((BYTE *)&intval,data_ptr,4);
return 4;
}
else
{
memcpy((BYTE *)&strval,data_ptr,len);
return len;
}
*/
return -1;
}

View File

@@ -0,0 +1,278 @@
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <netinet/in.h>
#include "include/snmpv3AuthCry.h"
extern void md5_calc(
unsigned char *output,
unsigned char *input, /* input block */
unsigned int inlen /* length of input block */
);
extern void des(unsigned char *output, const unsigned char *input, const unsigned char *key, int direction);
#define EXTEND_AUTH_LEN 64
#define MD5_DIGEST_LEN 16
#define MAX_SNMP_LEN 1500
#define CRYPTIPN_BLOCK_LEN 8
static const u8 IPAD[EXTEND_AUTH_LEN]=
{
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36
};
static const u8 OPAD[EXTEND_AUTH_LEN]=
{
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C
};
static void XORFunction(u8 *input0, const u8 *input1,u8 *output,u8 len)
{
int i;
for(i=0;i<len;i++)
output[i] = input0[i] ^ input1[i];
return;
}
static long getCurrentTime()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz );
return tv.tv_sec;
}
int snmpV3AuthenticateOutgoingMsg
(/* result >0:success,else fail */
u8 *inAuthKey, /*secret key for authentication*/
u16 inAuthKeyLen,
u8 *inWholeMsg, /* unauthenticated complete message*/
u16 inWholeMsgLen,
u8 *outAuthParameters, /* authenticationParamemeter */
u16 *outAuthParametersLen
)
{
int i;
u8 extendedAuthKey[EXTEND_AUTH_LEN];
u8 K1[EXTEND_AUTH_LEN],K2[EXTEND_AUTH_LEN];
u8 snmpData[MAX_SNMP_LEN];
u8 digest1[MD5_DIGEST_LEN],digest2[MD5_DIGEST_LEN];
if(inAuthKeyLen != AUTH_KEY_LEN)
return -1;
memset(outAuthParameters,0,MD5_DIGEST_90_LEN);
memset(extendedAuthKey,0,EXTEND_AUTH_LEN);
memcpy(extendedAuthKey,inAuthKey,AUTH_KEY_LEN);
XORFunction(extendedAuthKey,IPAD,K1,EXTEND_AUTH_LEN);
XORFunction(extendedAuthKey,OPAD,K2,EXTEND_AUTH_LEN);
memcpy(snmpData,K1,EXTEND_AUTH_LEN);
memcpy(snmpData+EXTEND_AUTH_LEN,inWholeMsg,inWholeMsgLen);
md5_calc(digest1,snmpData,EXTEND_AUTH_LEN+inWholeMsgLen);
memcpy(snmpData,K2,EXTEND_AUTH_LEN);
memcpy(snmpData+EXTEND_AUTH_LEN,digest1,MD5_DIGEST_LEN);
md5_calc(digest2,snmpData,EXTEND_AUTH_LEN+MD5_DIGEST_LEN);
*outAuthParametersLen = MD5_DIGEST_90_LEN;
for(i=0;i<MD5_DIGEST_90_LEN;i++)/* take the first 12 octets */
outAuthParameters[i] = digest2[i];
return 1;
}
int snmpV3AuthenticateIncomingMsg
(/* result >0:success,else fail */
u8 *inAuthKey, /*secret key for authentication*/
u16 inAuthKeyLen,
u8 *inWholeMsg, /* as received on the wire*/
u16 inWholeMsgLen,
u8 *inAuthParameters, /* as received on the wire*/
u16 inAuthParametersLen
)
{
int i;
u8 extendedAuthKey[EXTEND_AUTH_LEN];
u8 K1[EXTEND_AUTH_LEN],K2[EXTEND_AUTH_LEN];
u8 snmpData[MAX_SNMP_LEN];
u8 digest1[MD5_DIGEST_LEN],digest2[MD5_DIGEST_LEN],oldMAC[MD5_DIGEST_90_LEN],newMAC[MD5_DIGEST_90_LEN];
if(inAuthKeyLen != AUTH_KEY_LEN)
return -1;
if(inAuthParametersLen != MD5_DIGEST_90_LEN)
return -1;
memcpy(oldMAC,inAuthParameters,MD5_DIGEST_90_LEN);
memset(inAuthParameters,0,MD5_DIGEST_90_LEN);
memset(extendedAuthKey,0,EXTEND_AUTH_LEN);
memcpy(extendedAuthKey,inAuthKey,AUTH_KEY_LEN);
XORFunction(extendedAuthKey,IPAD,K1,EXTEND_AUTH_LEN);
XORFunction(extendedAuthKey,OPAD,K2,EXTEND_AUTH_LEN);
memcpy(snmpData,K1,EXTEND_AUTH_LEN);
memcpy(snmpData+EXTEND_AUTH_LEN,inWholeMsg,inWholeMsgLen);
md5_calc(digest1,snmpData,EXTEND_AUTH_LEN+inWholeMsgLen);
memcpy(snmpData,K2,EXTEND_AUTH_LEN);
memcpy(snmpData+EXTEND_AUTH_LEN,digest1,MD5_DIGEST_LEN);
md5_calc(digest2,snmpData,EXTEND_AUTH_LEN+MD5_DIGEST_LEN);
for(i=0;i<MD5_DIGEST_90_LEN;i++)/* take the first 12 octets */
newMAC[i] = digest2[i];
memcpy(inAuthParameters,oldMAC,MD5_DIGEST_90_LEN);
if(memcmp(oldMAC,newMAC,MD5_DIGEST_90_LEN))
return -1;
return 1;
}
static void snmpV3CryptInitVector
(
u32 runTime, /* the running period of the wxc2main*/
u8 *snmpV3Salt
)
{
ulong currentTime;
static ulong count =0;
//u8 inSalt[8]={0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x21};
memcpy(snmpV3Salt,&runTime,4);
currentTime = getCurrentTime() + count++;
memcpy(snmpV3Salt+4,&currentTime,4);
//memcpy(snmpV3Salt,inSalt,8);
return;
}
int snmpV3EncryptData
(/* result >0:success,else fail */
u8 *inEncryptKey, /* secret key for encryption*/
u16 inEncryptKeyLen,
u8 *inDataToEncrypt, /* data to encrypt (scopedPDU)*/
u16 inDataToEncryptLen,
u8 *outEncryptedData, /* encrypted data (encryptedPDU)*/
u16 *outEncryptedDataLen,
u8 *outPrivParameters, /* filled in by service provider*/
u16 *outPrivParametersLen,
ulong runTime
)
{
int i,blockCount;
u8 modValue,preIV[CRYPTIPN_BLOCK_LEN],iv[CRYPTIPN_BLOCK_LEN];
u8 /*plainText[CRYPTIPN_BLOCK_LEN],*/XORedText[CRYPTIPN_BLOCK_LEN],CipherText[CRYPTIPN_BLOCK_LEN];
u8 *pPlainText,*pCipherText;
u8 snmpV3Salt[CRYPTIPN_BLOCK_LEN];
if(inEncryptKeyLen != AUTH_KEY_LEN)
return -1;
modValue = inDataToEncryptLen % CRYPTIPN_BLOCK_LEN;
if(modValue)
{/* pad 0 to the rear */
for(i=0;i<CRYPTIPN_BLOCK_LEN-modValue;i++)
inDataToEncrypt[inDataToEncryptLen+i] = 0;
}
snmpV3CryptInitVector(runTime,snmpV3Salt);
blockCount = modValue?(inDataToEncryptLen / CRYPTIPN_BLOCK_LEN)+1:(inDataToEncryptLen / CRYPTIPN_BLOCK_LEN);
memcpy(preIV,inEncryptKey+CRYPTIPN_BLOCK_LEN,CRYPTIPN_BLOCK_LEN);
XORFunction(snmpV3Salt,preIV,iv,CRYPTIPN_BLOCK_LEN);
memcpy(CipherText,iv,CRYPTIPN_BLOCK_LEN);
pPlainText = inDataToEncrypt;
pCipherText = outEncryptedData;
for(i=0;i<blockCount;i++)
{
XORFunction(pPlainText,CipherText,XORedText,CRYPTIPN_BLOCK_LEN);
des(pCipherText,XORedText,inEncryptKey,0);
pPlainText += CRYPTIPN_BLOCK_LEN;
memcpy(CipherText,pCipherText,CRYPTIPN_BLOCK_LEN);
pCipherText += CRYPTIPN_BLOCK_LEN;
}
*outEncryptedDataLen = CRYPTIPN_BLOCK_LEN*blockCount;
memcpy(outPrivParameters,snmpV3Salt,CRYPTIPN_BLOCK_LEN);
*outPrivParametersLen = CRYPTIPN_BLOCK_LEN;
return 1;
}
int snmpV3DecryptData
(/* result >0:success,else fail */
u8 *inDecryptKey, /*secret key for decryption*/
u16 inDecryptKeyLen,
u8 *inPrivParameters, /* as received on the wire*/
u16 inPrivParametersLen,
u8 *inEncryptedData, /* encrypted data (encryptedPDU)*/
u16 inEncryptedDataLen,
u8 *outDecryptedData, /*decrypted data (scopedPDU)*/
u16 *outDecryptedDataLen
)
{
int i,blockCount;
u8 desKey[CRYPTIPN_BLOCK_LEN],preIV[CRYPTIPN_BLOCK_LEN];
u8 CipherText[CRYPTIPN_BLOCK_LEN],/*XORedText[CRYPTIPN_BLOCK_LEN],*/previousCipherText[CRYPTIPN_BLOCK_LEN];
u8 *pPlainText,*pCipherText;
u8 initationVector[CRYPTIPN_BLOCK_LEN];
if(inDecryptKeyLen != AUTH_KEY_LEN)
return -1;
if(inEncryptedDataLen % CRYPTIPN_BLOCK_LEN)
return -1;
blockCount = inEncryptedDataLen / CRYPTIPN_BLOCK_LEN;
memcpy(desKey,inDecryptKey,CRYPTIPN_BLOCK_LEN);
memcpy(preIV,inDecryptKey+CRYPTIPN_BLOCK_LEN,CRYPTIPN_BLOCK_LEN);
XORFunction(preIV,inPrivParameters,initationVector,CRYPTIPN_BLOCK_LEN);
memcpy(previousCipherText,initationVector,CRYPTIPN_BLOCK_LEN);
pPlainText = outDecryptedData;
pCipherText = inEncryptedData;
for(i=0;i<blockCount;i++)
{
des(CipherText,pCipherText,desKey,1);
XORFunction(CipherText,previousCipherText,pPlainText,CRYPTIPN_BLOCK_LEN);
pPlainText += CRYPTIPN_BLOCK_LEN;
memcpy(previousCipherText,pCipherText,CRYPTIPN_BLOCK_LEN);
pCipherText += CRYPTIPN_BLOCK_LEN;
}
*outDecryptedDataLen = inEncryptedDataLen;
return 1;
}

329
plat/snmp/src/sysctrl.c Normal file
View File

@@ -0,0 +1,329 @@
/*****************************************************/
/*Title: sysctrl.c */
/*Descr: report operation system status */
/*Author: Liang Hanxi */
/*Create: 2002-06-24 */
/*Modify: */
/*****************************************************/
#include "./include/sysctrl.h"
#define MAX_MNT_FILESYS 32 //in CC client >16
//static mnt_list MntInfo[MAX_MNT_FILESYS];
//static BYTE MntNum=0;
static hb_sysinfo SysInfo;
extern int errno;
static BYTE get_idlecpu();
static int parse_infoline(char *str,char (*row)[64],int maxrow);
static int get_raminfo(hb_sysinfo *info);
static int get_ptinfo(hb_sysinfo *info);
static int change_hb_byte(hb_sysinfo *info, BYTE *output, BYTE *outlen);
static BYTE snmp_hb_byte[512];
static BYTE snmp_hb_len;
void sysctrl_init()
{
memset(&SysInfo,0,sizeof(SysInfo));
SysInfo.up_time=time(NULL);
SysInfo.idle_cpu_rate=get_idlecpu();
get_raminfo(&SysInfo);
get_ptinfo(&SysInfo);
change_hb_byte(&SysInfo,snmp_hb_byte,&snmp_hb_len);
}
void system_report()
{
static int m_state=0;
//struct sysinfo info;
//unsigned long totaldisk,freedisk;
switch(m_state)
{
case 50*60*10: //10min
m_state=0;
get_ptinfo(&SysInfo);
change_hb_byte(&SysInfo,snmp_hb_byte,&snmp_hb_len);
set_status(8,snmp_hb_len,(BYTE*)snmp_hb_byte);
break;
default:
m_state++;
if(m_state%(50*30)==0) //30s
{
SysInfo.idle_cpu_rate=get_idlecpu();
get_raminfo(&SysInfo);
change_hb_byte(&SysInfo,snmp_hb_byte,&snmp_hb_len);
set_status(8,snmp_hb_len,(BYTE*)snmp_hb_byte);
}
break;
}
}
BYTE get_idlecpu()
{
char buf[128],row[5][64];
unsigned long timeofuser,timeofsys,timeofnice,timeofidle;
static unsigned long oldusertime=0,oldsystime=0,oldnicetime=0,oldidletime=0;
unsigned long idlecpu,totalcpu;
unsigned short idlerate;
FILE *stat_fp=NULL;
if((stat_fp=fopen("/proc/stat","r"))==NULL)
{
printf("Fail to open /proc/stat\n");
exit(1);
}
if(stat_fp == NULL)
return 0;
if(fseek(stat_fp,0,SEEK_SET)!=0)
{
return 0;
}
if(fgets(buf,128,stat_fp)==NULL)
{
return 0;
}
if(parse_infoline(buf,row,5)<5)
return 0;
timeofuser=strtoul(row[1],NULL,10);
timeofnice=strtoul(row[2],NULL,10);
timeofsys=strtoul(row[3],NULL,10);
timeofidle=strtoul(row[4],NULL,10);
totalcpu=timeofuser - oldusertime + timeofnice - oldnicetime + timeofsys - oldsystime + timeofidle - oldidletime;
if(totalcpu==0)
{
fclose(stat_fp);
return 0;
}
idlecpu=timeofidle-oldidletime;
idlerate=(unsigned short)(((double)idlecpu/totalcpu)*100);
//store current time
oldusertime=timeofuser;
oldnicetime=timeofnice;
oldsystime=timeofsys;
oldidletime=timeofidle;
if(idlerate > 100)
idlerate=100;
fclose(stat_fp);
return (BYTE)idlerate;
}
int get_raminfo(hb_sysinfo *info)
{
int rn;
unsigned char rb,prb;
int field_id=0;
int field_id2=0;
char field_value[256][256];
FILE *mem_fp=NULL;
if((mem_fp=fopen("/proc/meminfo","r"))==NULL)
{
printf("Fail to open /proc/meminfo\n");
exit(1);
}
if(mem_fp == NULL)
return 0;
memset(field_value,0,sizeof(field_value));
if(fseek(mem_fp,0,SEEK_SET)!=0)
{
fclose(mem_fp);
return 0;
}
while((rn = fread(&rb,1,1,mem_fp)))
{
if(rb != 0x0A)
field_value[field_id][field_id2]=rb;
if(!(prb == 0x20 && rb == 0x20))
field_id2++;
if((prb != 0x20 && rb == 0x20) || (rb == 0x0A))
{
field_id2=0;
field_id++;
}
prb=rb;
}
info->ram_total_used=atol(field_value[1])-atol(field_value[4]);
info->ram_total=atol(field_value[1]);
info->ram_swap_used=atol(field_value[34])-atol(field_value[37]);
info->ram_swap_total=atol(field_value[34]);
info->ram_buffers=atol(field_value[7]);
info->ram_cached=atol(field_value[10]);
fclose(mem_fp);
return 1;
}
int get_ptinfo(hb_sysinfo *info)
{
FILE *handle;
char line[256];
char path[256];
char *pc;
struct statfs fsbuf;
handle=fopen("/etc/mtab","r");
if(handle == NULL)
return 0;
info->pt_count=0;
while(fgets(line,256,handle))
{
if(strncmp(line,"/dev",4) == 0)
{
strtok(line," ");
pc=strtok(NULL," ");
if(pc == NULL)
continue;
sprintf(path,"%s",pc);
if(statfs(path,&fsbuf) == 0)
{
sprintf(info->pt[info->pt_count].mountpoint_path,"%s",path);
info->pt[info->pt_count].used_space=(fsbuf.f_blocks - fsbuf.f_bfree) * (fsbuf.f_bsize / 1024);
info->pt[info->pt_count].total_space=fsbuf.f_blocks * (fsbuf.f_bsize / 1024);
//printf("%s\t%ld\t%ld\n",path,info->pt[info->pt_count].total_space,info->pt[info->pt_count].used_space);
info->pt_count++;
if(info->pt_count == MAX_PARTITION_NUM)
{
fclose(handle);
return 1;
}
}
}
}
fclose(handle);
return 1;
}
int change_hb_byte(hb_sysinfo *info, BYTE *output, BYTE *outlen)
{
DWORD *pd;
BYTE *pb;
BYTE *copy;
int i;
BYTE len;
copy=output;
pd=(DWORD*)copy;
*pd=htonl(info->up_time);
copy+=sizeof(DWORD);
pb=(BYTE*)copy;
*pb=(BYTE)(info->idle_cpu_rate);
copy+=sizeof(BYTE);
pd=(DWORD*)copy;
*pd=htonl(info->ram_total_used);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->ram_total);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->ram_swap_used);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->ram_swap_total);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->ram_buffers);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->ram_cached);
copy+=sizeof(DWORD);
pb=(BYTE*)copy;
*pb=(BYTE)(info->pt_count);
copy+=sizeof(BYTE);
for(i=0;i<info->pt_count;i++)
{
len=strlen(info->pt[i].mountpoint_path);
pb=(BYTE*)copy;
*pb=(BYTE)len;
copy+=sizeof(BYTE);
memcpy(copy,info->pt[i].mountpoint_path,len);
copy+=len;
pd=(DWORD*)copy;
*pd=htonl(info->pt[i].used_space);
copy+=sizeof(DWORD);
pd=(DWORD*)copy;
*pd=htonl(info->pt[i].total_space);
copy+=sizeof(DWORD);
}
*outlen=copy - output;
return 1;
}
int parse_infoline(char *str,char (*row)[64],int maxrow)
{
char *pstr=str;
int len,index=0,i;
BOOL black=0;
len=strlen(str);
for(i=0;i<len && index<maxrow;i++)
{
if(isspace(str[i]))
{
if(black==0)
pstr=str+i+1;
else
{
str[i]='\0';
if(strlen(pstr)>64)
pstr[63]=0;
sprintf(row[index++],"%s",pstr);
pstr=str+i+1;
}
black=0;
}
else
black=1;
}
if(black==1)
{
if(strlen(pstr)>64)
pstr[63]=0;
sprintf(row[index++],"%s",pstr);
}
return index;
}