5 Basic Encoding Rules (BRE)
In a specific system, we need to use specific encoding rules to convert abstract data represented by ASN.1 into specific bit streams. The encoding method used by SNMP is BER (Basic Encoding rule ). The data of BER is composed of three fields: Tag, length, and value ).
5.1 The encoding rules for tag fields indicate the data type, occupying 1 byte. Common types include bool (0x01) and INT (0x02 ); octstr (0x04); null (0x05); objid (0x06); Enum (0x0a); seq (0x30); setof (0x31 ); ipaddr (0x40); counter (0x41); gauge (0x42); timeticks (0x43); opaque (0x44 ).
The length of the encoding rule for a 5.2-length domain indicates the length of the value range. The length is generally one to three bytes. The short format can be divided into short format (the value range below does not exceed 127 long) and long format, as shown in the following short format representation: 0 (1 bit) length (7 bit) representation of long format: 1 (1 bit) K (7 bit) K eight-bit group length (K byte) Example: length = 30 => 1E (30 does not exceed 127, the length field is 0001 1110) Length = 169 => 81 A9 (169 more than 127, the length field is 1000 0001 1010 1001,169 is the value of the last 9 bits, the first one of the first eight bits indicates that this is a long format representation. The last seven bits indicate the number of bytes after the first eight bits. Here, it is 0001, there is a byte that represents the actual length, 1010 1001 is 169, and the following value is 169 bytes long .) Length = 1500 => 82 05 DC (1000 0010 0000 0101 1101 1100, first look at the first byte, indicating the long format, followed by two bytes representing the length, these two bytes are 0000 0101 1101 1100 represents 1500)
5.3 value encoding rules
5.3.1 integer encoding INTEGER: = 0x02 length {byte) or one (negative ). The highest value is a sign. Example: 1500 => 02 02 05 DC 21500 => 02 02 fa 24
5.3.2 all management information library (MIB) objects maintained by the objectid encoding SNMP server are represented by objectid. For example, 1.3.6.1.2.1.1.1 indicates that the device in the MIB library describes the sysdesc variable. The encoding rules are as follows: objectid :: = 0x06 length {subidentifier} * (1) subidentifier :::={ leadingbyte} * lastbyte (2) leadingbyte: = 1 7 bitvalue (3) lastbyte :: = 0 7 bitvalue (4) the first two IDs are merged into one byte x 40 + Y (5). Although there are many rules, most sub-object IDs are ~ 127, only by rules (1), (5); when the sub-Object ID is greater than 127, by rules (2), (3), (4) it is divided into multiple bytes, the last byte is zero, and the rest of the byte is one. For example: 1.3.6.1.810.1, according to Rule (5), the first two sub-object IDs 1.3 are merged into 2B (1 3 40 + 3 = 43); the sub-object IDs 810 exceed 127, according to Rule (2), (3), (4) split it into two bytes 86 2a (810 = 11 0010 1010 => 1000 0110 0010 1010); the entire MiB is encoded: 0x06 0x06 0x2b 0x06 0x01 0x86 0x2a 0x01.
5.3.3 sequence combination encoding sequence: = 0x30 length {asndata} * For example: 30 05 02 01 10 05 00 represents a sequence structure with two members, one of them is an integer and the other is null ).
5.3.4 null encoding: NULL: = 0x05 0x00
5.3.5 string type encoding string: = 0x04 length {byte} * example: 04 06 70 75 62 6C 69 63 represents the string public
Instance: 30 3C 02 01 00 04 06 70 75 62 6C 69 63 A2 2f 02 01 10 02 01 01 01 00 00 30 24 30 22 06 08 2B 06 01 01 01 01 01 01 00 04 16 53 54 41 52 20 53 77 69 74 63 68 20 53 32 38 30 30 20 56 34 2E 30
Based on section 2nd BER encoding rules and section 3.1 SNMP datagram format, we can break down the data:
SNMP length = 60 <= 30 3C
Version = 1 <= 02 01 00
Community = Public <= 04 06 70 75 62 6C 69 63
PDU type = Get Response length = 2f <= a2 2f Request ID = 6 <= 02 01 10
Error status = (no error) <= 02 01 00
Error Index = 0 <= 02 01 00
Variable list length = 24 <= 30 24
Item1 length = 22 <= 30 22
Object = {1.3.6.1.2.1.1.1.0} <= 06 08 2B 06 01 01 01 01 00
Value = Star switch s2800 v4.0 <= 04 16 53 54 41 52 20...
The meaning of the entire message is SNMP getreply sysdescr = Star switch s2800 v4.0