Encoding of SNMP Counter64

Hi,

I wonder about the encoding of the SNMP Counter64 value.
The RFC 2578 says it should range between Zero and
2^64-1 (18446744073709551615 decimal).

But then I found this in the OTP code of snmp_pdus.erl:

%% Counter64         
          dec_value([70 | Bytes]) ->
              %% Counter64 is an unsigned 64 but is actually encoded as 
              %% a signed integer 64.

So the maximum value seem to be: 18410715276690587647

3> Val1 = 18410715276690587647.
18410715276690587647
4> snmp_pdus:enc_value('Counter64', Val1).
[70,"\b",[255,127,255,255,255,255,255,255]]

6> Val2 = 18410715276690587648.
18410715276690587648
7> snmp_pdus:enc_value('Counter64', Val2).
[70,[7],[128,0,0,0,0,0,0]]

So my question is if this is correct?

Cheers, Tobbe

3 Likes

Bumping up this question.
I have tested this with net-snmp and it seem to be able to handle the (according to the RFC) max Counter64 value:

he Counter64 type represents a non-negative integer which
   monotonically increases until it reaches a maximum value of 2^64-1
   (18446744073709551615 decimal), when it wraps around and starts
   increasing again from zero.
snmpwalk -v2c -c testing -m +TAILF-TOP-MIB:TAILF-TOBBE-MIB 127.0.0.1 TAILF-TOBBE-MIB::tfTobbeMIB
TAILF-TOBBE-MIB::tfTobbeNumber.0 = Counter64: 18446744073709551615
1 Like

I don’t think there’s a problem. SNMP is encoded using the ASN.1 Basic Encoding Rules (BER). In RFC2578 you’ll find the ASN.1 which specifies:

Counter64 ::=
    [APPLICATION 6]
        IMPLICIT INTEGER (0..18446744073709551615)

So a Counter64 value is BER encoded as an INTEGER however it has a subtype constraint on the value. ASN.1 is a specification language. The above says that an implementation of the protocol should BER encode an INTEGER for transmission but internally limit it’s use to values between 0 and 18446744073709551615.

3 Likes

So then there is a bug in the Erlang implementation, which apparently
rolls over to zero at: 18410715276690587648

2 Likes

That… uh… is not quite 2^64… Where’d that number come from? o.O

1 Like

See the comment in snmp_pdus.erl as shown in my initial post.

Also, if anyone would like to repeat my test with net-snmp, you’ll find it here:

I also created a bug-report at: SNMP Counter64 is encoded wrong !? · Issue #5756 · erlang/otp · GitHub

2 Likes

Yeah I’m just really curious as to the reasoning of why it was done that way…

1 Like