📄 decodesnmpmessage.java
字号:
//DecodeSNMPMessage.java
//*"*************************************************************************
// 此类实现了对5类不同的SNMP消息的解封装,它的功能有: *
// 1.解封装GetRequestPDU *
// 2.解封装GetNextRequestPDU *
// 3.解封装SetRequestPDU *
// 4.解封装GetResponsePDU *
// 5.解封装TrapPDU *
//..........................................................................*
// Creted by: ZhuSiFeng(朱思峰) *
// Email him: ZhuSiFeng2003@yahoo.com.cn *
//*"***************************************************"*********************
import java.io.*;
import java.net.*;
//import java.lang.*;
public class DecodeSNMPMessage
{
//*"*****************************定义公用方法,供其它方法调用*************"*********************
//将String类型的result打印出来
void PrintBytes(String result)
{
byte temp1[] = result.getBytes();
int chInt;
for(int i = 0;i<temp1.length;i++)
{
chInt = temp1[i];
if(temp1[i]<0)
{
chInt += 256;
}
int temp3 = chInt/16;
int temp4 = chInt-temp3*16;
char ch,ch1;
if( temp3 < 10 )
ch =(char)(temp3 + 48);
else
ch =(char) (temp3 + 55);
if( temp4 < 10 )
ch1 = (char)(temp4 + 48);
else
ch1 = (char)(temp4 + 55);
System.out.print(ch);
System.out.print(ch1);
System.out.print(" ");
}
}
//将byte类型数转换为int类型数,认为byte是无符号数
int ByteToInt(byte temp1)
{
int chInt = temp1;
if( temp1 < 0 )
{
chInt += 256;
}
return chInt;
}
//将byte数组temp1中所有元素打印出来(以16进制的形式)
void PrintBytes(byte temp1[])
{
int chInt;
for(int i = 0;i<temp1.length;i++)
{ chInt = temp1[i];
if(temp1[i]<0)
{chInt += 256;}
int temp3 = chInt/16;
int temp4 = chInt-temp3*16;
char ch,ch1;
if( temp3 < 10 )
ch =(char)(temp3 + 48);
else
ch =(char) (temp3 + 55);
if( temp4 < 10 )
ch1 = (char)(temp4 + 48);
else
ch1 = (char)(temp4 + 55);
System.out.print(ch);
System.out.print(ch1);
System.out.print(" ");
}
}
//将byte数组temp1中的前nLength长度的元素打印出来(以16进制的形式)
void PrintBytes(byte temp1[],int nLength)
{
int chInt;
for(int i = 0;i<nLength;i++)
{
chInt = temp1[i];
if(temp1[i]<0)
{ chInt += 256;}
int temp3 = chInt/16;
int temp4 = chInt-temp3*16;
char ch,ch1;
if( temp3 < 10 )
ch =(char)(temp3 + 48);
else
ch =(char) (temp3 + 55);
if( temp4 < 10 )
ch1 = (char)(temp4 + 48);
else
ch1 = (char)(temp4 + 55);
System.out.print(ch);
System.out.print(ch1);
System.out.print(" ");
}
}
//将temp0字符串转换为相应的数
int StringToInt(String temp0)
{
if (temp0.length() == 0)
return 0;
int retValue = 0;
for (int i = 0; i < temp0.length();i++)
{
char temp1 = temp0.charAt(i);
retValue =retValue*10+(int)temp1 - 0x30;
}
return retValue;
}
//*"*****************************定义主要方法*************"*********************
//该方法的功能: 1. 解码并打印解码结果 2.通过数组返回解码结果
public void DecodePDU(byte message[],byte outcommunity[],byte outrequestID[],byte outvarBind[])
{
//说明: 该函数完成下面的任务:
//0.解析出版本信息;
//1.解析出团体字符串commnunitty信息到community[]中;
//2.解析出消息的PDU-TYPE编码
//3.解析出request-id信息到requestID[]中;
//4.解析出error-status信息;
//5.解析出error-index信息;
//6.解析出Variable-bindings信息到varBind[]中;
int messageLength;//消息体长度
int commLength; //团体字符串长度
int requestIDLength;//Request_ID长度
int varBindLength; //整个varBindList的长度,从sequence of 开始,初值为2
byte version;
byte community[] = new byte[10];//团体字符串临时存储
byte PDu_type;
byte error_status;
byte error_index;
byte requestID[] = new byte[10]; //RequestID临时存储
byte varBind[] = new byte[1024]; //绑定变量表临时存储
int i;
int index=1;//用于指示数组message中的某个位置
//------------------------------------------------------------------------------------
//解码过程
messageLength = ByteToInt(message[index]);
index= index+3;//index=4
//0.解析出版本信息到version[]中;
version= message[index];
index=index+2;
//1.解析出团体字符串commnunitty信息到ocommunity[]中;
commLength= ByteToInt(message[index]);
index++;
for(i=0;i<=commLength-1;i++)
community[i] = message[i+index];
index= index+commLength;
//2.解析出GetRequest的PDU-TYPE编码
PDu_type = message[index];
index = index+3;
//3.解析出request-id信息到requestID[]中;
requestIDLength= ByteToInt(message[index]);
index++;
for(i=0;i<=requestIDLength-1;i++)
requestID[i] = message[index +i];
index = index+ requestIDLength +2;
//4.解析出error-status信息;
error_status = message[index];
index = index+3;
//5.解析出error-index信息中;
error_index = message[index];
index= index +1;
//6.解析出Variable-bindings信息到varBind[]中;
varBindLength = ByteToInt(message[index+1]);
for(i=0;i<=varBindLength-1+2;i++)
varBind[i] = message[index+i];
//------------------------------------------------------------------------------------
//打印解码结果
System.out.println("\n 解码结果如下(Decode result as follow): ");
System.out.print(" 消息体长度(messageLength): ");
System.out.println(messageLength);
System.out.print(" SNMP版本信息(SNMP Version): ");
System.out.println(ByteToInt(version));
System.out.print(" 团体字符串(Community): ");
PrintBytes(community);
System.out.print("\n PDU类型(PDU_TYPE): ");
byte tmp[]= new byte[1];
tmp[0]=PDu_type;
PrintBytes(tmp);
System.out.print("\n 请求编号(RequestID): ");
PrintBytes(requestID);
System.out.print("\n 错误类型(error_status): ");
tmp[0]=error_status;
PrintBytes(tmp);
System.out.print("\n 错误位置(error_index): ");
tmp[0]=error_index;
PrintBytes(tmp);
System.out.print("\n 变量绑定(Variable_bindings): ");
PrintBytes(varBind);
//------------------------------------------------------------------------------------
//通过数组返回解码结果
for(i=0;i<= commLength-1;i++)
outcommunity[i]=community[i];
for(i=0;i<=requestIDLength-1; i++)
outrequestID[i] = requestID[i];
for(i=0;i<=varBindLength-1;i++)
outvarBind[i] = varBind[i];
return;
}//end_of_DecodePDU()
}//end_of_class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -