?? snmpmain.c
字號:
#include "VxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "taskLib.h"
#include "msgQLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "ioLib.h"
#include "fioLib.h"
#include "snmp.h"
#include "Mib2Func.h"
U32 g_u32ClientIP;
char data[4096]; //最大SNMP包接收4k字節,超出的話,error_status = toobig
MSG_Q_ID trap_rx;
extern logParam logList[MAXLOG],*logHead;
void snmpStart()
{
TRAPMSG tx_msg;
trap_rx = msgQCreate(50,sizeof(TRAPMSG),MSG_Q_FIFO );
taskSpawn("snmpAgent",100,0,1024*4,snmpAgent,0,0,0,0,0,0,0,0,0,0);
taskSpawn("snmpTrap",100,0,1024*4,snmpTrap,0,0,0,0,0,0,0,0,0,0);
taskSpawn("snmpTask",100,0,1024*4,TrapTask,0,0,0,0,0,0,0,0,0,0);
tx_msg.msg = MSG_SYS_UP;
tx_msg.snmp = NULL;
msgQSend(trap_rx,(char *)&tx_msg,sizeof(TRAPMSG),WAIT_FOREVER,MSG_PRI_NORMAL);
}
int snmpAgent(void)
{
struct sockaddr_in serverSock; //服務器socket地址
int sockAddrSize; //socket地址的數據結構大小
int ServerSocketFd; //socket文件描述符
u_long count = 0;
//printf("\n<<_SNMP>> SNMP start!!!!!");
sockAddrSize = sizeof(struct sockaddr_in);
bzero((char *)&serverSock,sockAddrSize);
serverSock.sin_family = AF_INET;
serverSock.sin_len = (u_char)sockAddrSize;
serverSock.sin_port = htons(SNMP_UDP);
serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
//創建基于TCP的socket
if((ServerSocketFd = socket(AF_INET,SOCK_DGRAM,0)) == ERROR)
{
printf("\n <<_SNMP>> Socket create error!");
return (ERROR);
}
//將socket綁定在本地地址上
if(bind(ServerSocketFd,(struct sockaddr*)&serverSock, sockAddrSize) == ERROR)
{
printf("\n <<_SNMP>> Socket bind error!");
close(ServerSocketFd);
return (ERROR);
}
m2MyInit();
//循環監聽端口數據
while(1)
{
count++;
if(Exam_startdata(ServerSocketFd,count) == ERROR)
{
printf("\n <<_SNMP>> %d turn ERROR\n\n",count);
}
}
m2MyUninit();
}
/************************************************************
每個return ERROR都要添加freesnmpPDU(snmp);
以后添加
*************************************************************/
int Exam_startdata(int sFd,int count)
{
struct sockaddr_in clientAddr; //客戶端socket地址
int sockAddrSize; /*socket地址的數據結構大小*/
int buflen,snmplen,pdulen,asnlen;
char *buf,*backup;
char type;
char **packet;
int packetlen;
char address[20];
SNMP_PDU *snmp;
snmp = snmpMalloc(sizeof(SNMP_PDU));
initSNMP(snmp);
sockAddrSize = sizeof(struct sockaddr_in);
memset(data,0,4096);
if((buflen = recvfrom(sFd, data, 4096, 0, (struct sockaddr *)&clientAddr, &sockAddrSize)) == ERROR)
return ERROR;
//_ADD_DEBUG
g_u32ClientIP = clientAddr.sin_addr.s_addr;
inet_ntoa_b(clientAddr.sin_addr,address);
//printf("\n <<_SNMP>> NMS = %s",address);
//hexDump(data,buflen);
if((buf = (char *)asnGetType(data,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_SEQUENCE)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
snmplen = asnlen;
//VERSION TAG:INTEGER--0x2 | LENGTH -- 0x1 | VALUE -- (usually 0)
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_INTERGER)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR
|| asnlen != 1)
return ERROR;
if((buf = (char *)asnGetVersion(snmp,buf,(int *)&buflen)) == (char *)ERROR)
return ERROR;
//CommunityTAG:OCTET STRING -- 0x4 | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_OCTET)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetCommuName(snmp,buf,(int *)&buflen,&asnlen)) == (char *)ERROR)
return ERROR;
//PDU field TAG -- 0xa(n) | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetPduCommand(snmp,buf,(int *)&buflen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
//剩下的應該全部屬于PDU的內容,所以剩余長度應該等于PDU數據長度
if(buflen != asnlen)
return ERROR;
pdulen = asnlen;
//RequestID TAG:INTERGER -- 0x2 | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_INTERGER)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetRequestID(snmp,buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
//ErrorStatus TAG: INTERGER |LENGTH = 1| VALUE = 0
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_INTERGER)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetErrorStatus(snmp,buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
//ErrorIndex TAG: INTERGER | LENGTH = 1 | VALUE = 0
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_INTERGER)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetErrorIndex(snmp,buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
//VarBindList TAG 0x30 | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_SEQUENCE)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if(buflen != asnlen)
return ERROR;
while(buflen > 0)
{
//Bind Variable TAG 0x30 | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR)
return ERROR;
switch(type)
{
case TAG_SEQUENCE:
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
//Variable Value TAG 0x6 | LENGTH -- varies | VALUE -- varies
if((buf = (char *)asnGetType(buf,(int *)&buflen,&type)) == (char *)ERROR
||type != TAG_OBJIDT)
return ERROR;
if((buf = (char *)asnGetLen(buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
if((buf = (char *)asnGetOID(snmp,buf,(int *)&buflen,(int *)&asnlen)) == (char *)ERROR)
return ERROR;
default:
break;
}
}//循環,直到所有數據,所有OID處理完畢
ExecSnmp(snmp);
packet = snmpMalloc(sizeof(char *));
if(PrepareRC(snmp,packet,(int *)&packetlen) == ERROR)
{
if((*packet) != NULL)
snmpFree(*packet);
snmpFree(packet);
freesnmpPDU(snmp);
return ERROR;
}
//hexDump((*packet),packetlen);
buflen = sendto(sFd, (*packet), packetlen, 0, (struct sockaddr *)&clientAddr, sockAddrSize);
snmpFree(*packet);
snmpFree(packet);
freesnmpPDU(snmp);
return TRUE;
}
int ExecSnmp(SNMP_PDU *snmp)
{
int oid_len;
VARLIST *vartemp;
vartemp = snmp->variables;
if(vartemp->oid_len == 0)
return ERROR;
snmp->errstat = OK;
if(snmp->command == TAG_GETPDU||snmp->command == TAG_GETNEXT)
{
ScanMib2Oid(snmp);
for(;vartemp != NULL;vartemp = vartemp->next)
{
if (ERROR == GetOidValue(snmp,vartemp))
{
printf("\n <<_SNMP>> GetOidValue ERROR\n\n");
}
}
}
else if(snmp->command == TAG_SETPDU)
{
for(;vartemp != NULL;vartemp = vartemp->next)
{
if (ERROR == SetOidValue(snmp,vartemp))
{
printf("\n <<_SNMP>> SetOidValue ERROR\n\n");
}
}
}
//printf("\nsnmp->errstat = %d", snmp->errstat);
return OK;
}
void initSNMP(SNMP_PDU *snmp)
{
snmp->version = 0;
snmp->community = NULL;
snmp->community_len = 0;
snmp->command = 0;
snmp->reqid = 0;
snmp->errstat = 0;
snmp->errindex = 0;
snmp->variables = NULL;
}
int getLongSize(long len)
{
if((len&0xff000000) != 0)
return (4);
if((len&0x00ff0000) != 0)
return (3);
if((len&0x0000ff00) != 0)
return (2);
return 1;
}
void freesnmpPDU(SNMP_PDU *snmp)
{
if(snmp == NULL)
return;
if(snmp->community != NULL)
snmpFree(snmp->community);
if(snmp->variables != NULL)
{
VARLIST *temp,*swap;
temp = snmp->variables->next;
if(snmp->variables->value != NULL)
snmpFree(snmp->variables->value);
snmpFree(snmp->variables);
for( ;temp != NULL; )
{
swap = temp->next;
if(temp->value != NULL)
snmpFree(temp->value);
snmpFree(temp);
temp = swap;
}
}
snmpFree(snmp);
}
int zeroLocation(char *temp)
{
int n;
for(n = 0;;n++)
{
if(temp[n] == 0)
return n;
}
}
int get_tempstring(char *buf,int *cbNeeded,int *cbUsed)
{
if( (*cbNeeded) < 8)
{
(*cbNeeded) = 8;
return (3);
}
if(buf == NULL)
return ERROR;
memcpy(buf,"12345678",8);
(*cbUsed) = (*cbNeeded) = 8;
return OK;
}
int get_tempint(unsigned long *temp)
{
(*temp) = 66;
HTONL(*temp);
return OK;
}
void *snmpMalloc(int nBytes){ return malloc(nBytes); }
void snmpFree(void *pBlock){ free(pBlock); }
/*
* U32 n,m;
* if(getJoblist("LPT1",&n,&m) == OK)
* ...
* else if(getJoblist("USB1",&n,&m) == OK)
* ...
*/
int getJoblist(char *type, U32 *total, U32 *cnt)
{
logParam *templog;
templog = logHead;
(*total) = (*cnt) = 0;
if( templog->next == NULL && templog->prev == NULL)
{
return FALSE;
}
while(strcmp(templog->dest,type) == 0)
{
(*cnt) = (*cnt) + 1;
(*total)+= templog->pagecnt;
templog = templog->prev;
if(templog == NULL || templog->next == NULL)
break;
}
return OK;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -