Monday, July 12, 2010

Ber encoding an SNMP Oid

The following steps will let you encode numbers that are greater than 127 in your SNMP Oid string. For instance in the Oid 1.3.6.1.4.1.19865.1.2.1.6.0 the value 19865 is greater than 127 so it must be Ber encoded. Please refer to this PDF document when following the steps below. Why you need to encode values greater than 127 in this way is beyond the scope of this post. I had a hard time finding information for a dummy like me to understand. I found plenty of RFC's but none of them got it to click in my head. Now that I feel I have a firm grasp on to calculate these values properly I am sharing my knowledge.

For this example I am using the value 19865. You may find it easier to have the flowchart PDF I linked to above open next to your browser window when following the steps below. In my opinion, it's much easier to visualize what is happening.


Step 1. Convert the decimal value into its Hexadecimal format of 4D99

Step 2. Split the value 4D99 "in half" in to two separate values of 4D and 99

Step 3. Convert the Hexadecimal value of 99 into its equivalent binary string format of "10011001"

Step 4. Convert the Hexadecimal value of 4D into its equivalent binary string format of “01001101”

Step 5. Combine these two binary strings into one long 16 character binary string of “0100110110011001”

Step 6. Starting from the far right of the string take 7 characters. You now have a 7 digit string “0011001”

Step 7. Add a “0” to this new string. You now have an 8 digit string “00011001”. This will be the last number of the encoded value.

Step 8. Starting from the 8th number from the right take 7 characters. You now have a 7 digit string “0011011”.

Step 9. Add a “1” to this new string. You now have an 8 digit string “10011011”. This will be the middle number of the encoded value. The 1 at the beginning of the number indicates that more numbers will follow it. This is referred to as the "high bit".

Step 10. Take the remaining 2 numbers from the string. Add five zeros to the front of these two numbers and then a “1” to the front of that. This will now give you the string “10000001”.

Step 11. Convert all three of these new binary strings into their Hexadecimal values of 81 9B 19. These three Hex values are what you will use to represent numbers of more than 127 in your oid string.

The fully encoded Oid that would be sent to the SNMP host would be 2B 06 01 04 01 81 9B 19 01 02 01 06 00

You may have noticed that the start of the Oid is "2B" instead of "01 03". This is because the first two values of the Oid are encoded differently than the rest. Right now I won’t go into the reason why it works this way. I will explain this later.

If this information helps you please take a second to give me a shout out. It'd be nice to know that I didn't waste my time documenting this. If I made a mistake or typo please let me know.

9 comments:

  1. Anonymous6:20 AM

    cheer's helped me out. I was searching for ages for this. Ta!!

    ReplyDelete
  2. Thanks Chris, your site was the only place I could find that explained how to do the BER encoding. I and my company's Microchip PIC project thank you.

    ReplyDelete
  3. Thanks Chris!

    In step 10, it is mentioned that we should add "six zeros" to the front of the remaining two digits. Here the remaining 2 digits are "01". Adding 6 zeros & 1 = 1+000000+01 = 100000001. This different to what is mentioned in here & in PDF.
    Is this a typo or I'm missing out something?

    ReplyDelete
    Replies
    1. Good catch. Thanks. The PDF is correct. I will have to go back and correct the blog post.

      Delete
  4. Anonymous8:12 AM

    Thanks a lot buddy... specially for the attached pdf

    ReplyDelete
  5. Anonymous7:25 PM

    Wow!! Great description. The pdf really help me figured it out. Thanks Chris!!

    ReplyDelete
  6. Anonymous3:09 AM

    great explanation, cool diagram! It helped me a lot! Thanks dude!

    ReplyDelete
  7. Anonymous6:01 AM

    You're a LEGEND !
    I spent nearly an hour trying to work that out by other means. Thanks mate

    ReplyDelete
  8. Anonymous3:43 AM

    Great!
    Finally a clear and proper explanation.
    Many Thanks!

    ReplyDelete