⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpmain.c

📁 VxWorks操作系统下的SNMP协议以及MIB协议的实现源码,并且实现了HP标准打印类的MIB框架.
💻 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 + -