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

📄 rrsnmp.c

📁 用于嵌入式系统的TCP/IP协议栈及若干服务
💻 C
字号:
/**            Copyright (c) 1998-2000 by NETsilicon Inc.**  This software is copyrighted by and is the sole property of*  NETsilicon.  All rights, title, ownership, or other interests*  in the software remain the property of NETsilicon.  This*  software may only be used in accordance with the corresponding*  license agreement.  Any unauthorized use, duplication, transmission,*  distribution, or disclosure of this software is expressly forbidden.**  This Copyright notice may not be removed or modified without prior*  written consent of NETsilicon.**  NETsilicon, reserves the right to modify this software*  without notice.**  NETsilicon*  411 Waverley Oaks Road                  USA 781.647.1234*  Suite 227                               http://www.netsilicon.com*  Waltham, MA 02452                       AmericaSales@netsilicon.com***************************************************************************  $Name: Fusion 6.52 Fusion 6.51 $*  $Date: 2001/12/21 15:25:32 $*  $Source: M:/psisrc/routing/common/rcs/rrsnmp.c $*  $Revision: 1.4 $*************************************************************************/#include "rrport.h"#include "rrtypes.h"#include "rrsnmp.h"rrSnmpBlk rrSnmpGlob; /* Global SNMP control Block *//* slow but effective equivalent to macros, works all cases */static word net2_u16(byte *add)	{	word res;	res = (((*(byte *)(add)) <<8)&0xff00);	res |= (*((byte *)(add) +1));	return(res);}static dword net2_u32(byte *add){	dword res;	res =   ((  (dword)*((byte *)add) <<24)&0xff000000);	res |= (( (dword)*((byte *)add +1)<<16)&0xff0000);	res |= (( (dword)*((byte *)add +2)<<8)&0xff00);	res |= (  (dword)*((byte *)add +3));	return(res);}/*************************************************rrSnmpDecode:turn a byte, word, triple byte or long atgiven address into a long. Assumes MSB is first.Reference: MIB definitions- introduction****************************************************/long rrSnmpDecode(TLV *tlv){	byte *add= tlv->t_val;	int len= tlv->t_len;	long val;	/* switch on len of req */	switch( len ){		case 1:			return( *add );		case 2:			val = (long)net2_u16(add);			return(val);		case 3:			val= (*add) << 16;			val |= (*(add +1)) << 8;			val |= *(add +2);			return( val);		case 4:			return( ((dword)net2_u32(add)) );		default:			return(0);	}}/************************************************rrSnmpStr:process get or set of octet string.Reference: MIB definitions- introduction************************************************/void rrSnmpStr(SNMP_CB *Ir_cmd, byte *str,int len){	int i;	switch( Ir_cmd->sc_type ){			case GetReq:		case GetNextReq:			/* some protocols had built strings on stack,			   must take care of here for commonality */			if(len > RRSNMP_MAX_STR){				rrSnmpCmdErr(Ir_cmd,tooBig);				return;			}			rrCopyMem(snmp_res_str,str,len);			Ir_cmd->sc_tlv.t_type= T_OCTSTR;			Ir_cmd->sc_tlv.t_len= len;			Ir_cmd->sc_tlv.t_val= snmp_res_str;			break;		case SetReq:			for(i = 0; i < len;i++){				*(str +i) = *((byte *)Ir_cmd->sc_tlv.t_val + i);			}			break;		default:			rrSnmpCmdErr(Ir_cmd,genErr);			break;	}}/*****************************************rrSnmpInt:process get or set for type integer or counter.This could be 16 or 32 bits on a localmachine.Access control, min and max have alreadybeen validated.Reference: MIB definitions- introduction*****************************************/void rrSnmpInt(SNMP_CB *Ir_cmd, int test, int *valp,int type){	int so,amet;		switch( Ir_cmd->sc_type ){			case GetReq:		case GetNextReq:			Ir_cmd->sc_tlv.t_type = T_INTEGER;			/* type determines len of data */			switch( type ){				case T_BYTE:					snmp_res_byte = *(byte *)valp;					Ir_cmd->sc_tlv.t_len= 1;					Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_byte;					break;				case T_WORD:					snmp_res_word = net2_u16((byte *)valp);					Ir_cmd->sc_tlv.t_len= 2;					Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;					break;				case T_LONG:					snmp_res_dword = net2_u32((byte *)valp);					Ir_cmd->sc_tlv.t_len= 4;					Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;					break;				case T_INTEGER:					Ir_cmd->sc_tlv.t_len= sizeof(int);					so= sizeof(int);					if(so == 2){						snmp_res_word = net2_u16((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;					}					else{						snmp_res_dword = net2_u32((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;					}					break;                case T_METRIC:                    amet = *(metric *)valp;                    amet &= 0x7f;                    valp = &amet;                    Ir_cmd->sc_tlv.t_len= sizeof(int);                    so= sizeof(int);                    if(so == 2){                        snmp_res_word = net2_u16((byte *)valp);                        Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;                    }                    else{                        snmp_res_dword = net2_u32((byte *)valp);                        Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;                    }                    break;				case T_COUNTER:					Ir_cmd->sc_tlv.t_type = T_COUNTER;					Ir_cmd->sc_tlv.t_len= sizeof(counter);					so= sizeof(counter);					if(so == 2){						snmp_res_word = net2_u16((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;					}					else{						snmp_res_dword = net2_u32((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;					}					break;				case T_GAUGE:					Ir_cmd->sc_tlv.t_type = T_GAUGE;					Ir_cmd->sc_tlv.t_len= sizeof(gauge);					so= sizeof(gauge);					if(so == 2){						snmp_res_word = net2_u16((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;					}					else{						snmp_res_dword = net2_u32((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;					}					break;				case T_BOOLEAN:					if(*(boolean *)valp){						valp= &SNMP_ON;					}					else{						valp= &SNMP_OFF;					}					Ir_cmd->sc_tlv.t_len= sizeof(int);					so= sizeof(int);					if(so == 2){						snmp_res_word = net2_u16((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_word;					}					else{						snmp_res_dword = net2_u32((byte *)valp);						Ir_cmd->sc_tlv.t_val= (unsigned char *)&snmp_res_dword;					}					break;				default:					rrSnmpCmdErr(Ir_cmd,genErr);					return;			}			break;			case SetReq:			if(test) return;			/* type determines len of data */			switch( type ){				case T_BYTE:					*(byte *)valp= (byte)rrSnmpDecode(&Ir_cmd->sc_tlv);					break;				case T_INTEGER:					/* can truncate in case larger					   because have already verified					   range.					*/					*(int *)valp= (int)rrSnmpDecode(&Ir_cmd->sc_tlv);					break;                case T_METRIC:                    *(int *)valp= (int)rrSnmpDecode(&Ir_cmd->sc_tlv);                    if(*(int *)valp == 0){                        *(int *)valp = 0x80;					}                    break;				case T_COUNTER:					*(counter *)valp= (counter)rrSnmpDecode(&Ir_cmd->sc_tlv);					break;				case T_BOOLEAN:					if((boolean)rrSnmpDecode(&Ir_cmd->sc_tlv)== 2){						*(boolean *)valp= 1;					}					else{						*(boolean *)valp= 0;					}					break;				default:					rrSnmpCmdErr(Ir_cmd,genErr);					return;			}			break;			default:			rrSnmpCmdErr(Ir_cmd,genErr);			return;	}	Ir_cmd->sc_res= noError;}/******************************************rrSnmpValid:validates access, type, min/max.returns object type on success, 0 on err.Reference: MIB definitions- introduction******************************************/int rrSnmpValid(SNMP_CB *Ir_cmd, IRMIB *obj){	int type = obj->m_type;	/* validate access */	switch( Ir_cmd->sc_type){			case GetReq:				if( !(obj->m_acc & A_GET)){				rrSnmpCmdErr(Ir_cmd,genErr);				return(0);			}			break;			case GetNextReq:			if( !(obj->m_acc & A_GET)){				rrSnmpCmdErr(Ir_cmd,genErr);				return(0);			}			break;			case SetReq:			if( !(obj->m_acc & A_SET)){				rrSnmpCmdErr(Ir_cmd,readOnly);				return(0);			}			break;			default:			rrSnmpCmdErr(Ir_cmd,genErr);			return(0);		}	/* validate type for set req */	if( Ir_cmd->sc_type == SetReq ){			/* validate min/max for set int type */		if( type == T_INTEGER || type == T_BOOLEAN ||		    type == T_BYTE ){				if( rrSnmpDecode(&Ir_cmd->sc_tlv) < (int)obj->m_min){							rrSnmpCmdErr(Ir_cmd,badValue);				return(0);			}			if( rrSnmpDecode(&Ir_cmd->sc_tlv) > (int)obj->m_max){					rrSnmpCmdErr(Ir_cmd,badValue);				return(0);			}		} 		/* validate string len for T_OCTSTR */		if( type == T_OCTSTR ){			if( Ir_cmd->sc_tlv.t_len < obj->m_min){							rrSnmpCmdErr(Ir_cmd,badValue);				return(0);			}			if( Ir_cmd->sc_tlv.t_len > obj->m_max){							rrSnmpCmdErr(Ir_cmd,badValue);				return(0);			}		}	}	/* success */	return( type );	}/*******************************************rrSnmpCmdErr:returns an error response to client.Reference: MIB definitions- introduction*******************************************/void rrSnmpCmdErr(SNMP_CB *Ir_cmd, int err_code){	Ir_cmd->sc_res= err_code;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -