奇怪的SNMP换档操作 [英] Strange SNMP shifting operation
问题描述
以下是从书C#网络Programmingm由理查德·布卢姆:
The following is from the book C# Network Programmingm by Richard Blum:
public byte[] get(string request, string host, string community, string_ mibstring)
{
byte[] packet = new byte[1024];
byte[] mib = new byte[1024];
int snmplen;
int comlen = community.Length;
string[] mibvals = mibstring.Split('.');
int miblen = mibvals.Length;
int cnt = 0, temp, i;
int orgmiblen = miblen;
int pos = 0;
// Convert the string MIB into a byte array of integer values
// Unfortunately, values over 128 require multiple bytes
// which also increases the MIB length
for (i = 0; i < orgmiblen; i++)
{
temp = Convert.ToInt16(mibvals[i]);
if (temp > 127)
{
mib[cnt] = Convert.ToByte(128 + (temp / 128));
mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
cnt += 2;
miblen++;
}
else
{
mib[cnt] = Convert.ToByte(temp);
cnt++;
}
}
snmplen = 29 + comlen + miblen - 1; //Length of entire SNMP packet
//The SNMP sequence start
packet[pos++] = 0x30; //Sequence start
packet[pos++] = Convert.ToByte(snmplen - 2); //sequence size
//SNMP version
packet[pos++] = 0x02; //Integer type
packet[pos++] = 0x01; //length
packet[pos++] = 0x00; //SNMP version 1
//Community name
packet[pos++] = 0x04; // String type
packet[pos++] = Convert.ToByte(comlen); //length
//Convert community name to byte array
byte[] data = Encoding.ASCII.GetBytes(community);
for (i = 0; i < data.Length; i++)
{
packet[pos++] = data[i];
}
}
我不明白以下code:
I didn't understand the following code:
for (i = 0; i < orgmiblen; i++)
{
temp = Convert.ToInt16(mibvals[i]);
if (temp > 127)
{
mib[cnt] = Convert.ToByte(128 + (temp / 128));
mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
cnt += 2;
miblen++;
}
else
{
mib[cnt] = Convert.ToByte(temp);
cnt++;
}
}
我不明白这是为了将分为两个字节,如果临时更大然后一个字节。但是,什么是计算正在做128+(温度/ 128),然后在第二个字节:TEMP-(温度/ 128)* 128,这就是我不明白
I do understand this is for putting down into two bytes if the temp is larger then one byte. But what is the calculation being done 128+(temp/128) and then for the second byte: temp- (temp/128)*128, this is what i don't understand.
请帮忙,谢谢你。
推荐答案
下面是引用的了解SNMP的MIB 的394页,因为这本书描述的技术细节,比别人好,
Below is quoted from Understanding SNMP MIBs page 394, as this book describes the technical details better than any others,
这是连接codeD对象标识符是由每个子标识符原值EN codeD和级联的。每个子标识符是烯$ C $光盘作为一系列八比特组,即如下,
An encoded OBJECT IDENTIFIER consists of each sub-identifier in the original value encoded and concatenated. Each sub-identifier is encoded as a series of octets, which is as follows,
- 在每个八位位组的位8表示如果是副识别符的最后一个字节由该位设置为0
- 位7至1的八位位组,当串联,形成分标识符 的价值
- 在子标识符的第一个字节可以不具有值80(十六进制)。这确保了最小数目的八位位组被用于编码。 80(十六进制)的值将指示多个字节跟随,但该值的位(7-1)被设置为零。
- Bit 8 in each octet indicates if it is the last octet of the sub-identifier by setting this bit to 0
- Bits 7 through 1 in the octets, when concatenated, form the value of the sub-identifier
- The first octet in the sub-identifier may not have the value 80 (in HEX). This ensures that the smallest number of octets are used for the encoding. A value of 80 (in HEX) would indicate that more octets follow, but the value bits (7-1) are set to zero.
粘贴的源$ C $ C在体内的问题,其实遵循的规则来解析字节。因此,如果你理解了规则,你可以了解code。
The pasted source code in the question body in fact follows the rules to parse the bytes. Thus, if you understand the rules, you can understand the code.
(已更新的规则来自国际电联-T X.690,ISO / IEC 8825-1 。)
(Updated: The rules come from ITU-T X.690, ISO/IEC 8825-1.)
这篇关于奇怪的SNMP换档操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!