calmh / node-snmp-native

Native Javascript SNMP library for Node.js
MIT License
252 stars 65 forks source link

vb.valueHex and vb.valueRaw length problem #34

Closed MortenGuldager closed 8 years ago

MortenGuldager commented 8 years ago

Retrieving multiple values with getAll will return vb.valueHex and vb.valueRaw much longer than the SNMP response justifies. The first values includes header and value of the remaining ones in same packet.

I have tried my luck with a patch here. Somebody experienced with SNMP and javascript should at least review and conduct own tests before we roll it into anything others might eventually run!

*** snmp-native/lib/snmp.js 2015-04-06 21:22:58.000000000 +0200
--- snmp-native-MOGUL/lib/snmp.js   2015-11-12 19:49:58.004650751 +0100
*************** function parse(buf) {
*** 272,280 ****
          // routine. For the SNMPv2c error types, we simply set the
          // value to a text representation of the error and leave handling
          // up to the user.
!         vbhdr = asn1ber.typeAndLength(bvb)
!         bvb = bvb.slice(vbhdr.header + vbhdr.len)
!         vb.type = bvb[0];
          if (vb.type === asn1ber.types.Null) {
              // Null type.
              vb.value = null;
--- 272,281 ----
          // routine. For the SNMPv2c error types, we simply set the
          // value to a text representation of the error and leave handling
          // up to the user.
!         var vb_name_hdr = asn1ber.typeAndLength(bvb)
!         bvb = bvb.slice(vb_name_hdr.header + vb_name_hdr.len)
!         var vb_value_hdr = asn1ber.typeAndLength(bvb);
!         vb.type = vb_value_hdr.type;
          if (vb.type === asn1ber.types.Null) {
              // Null type.
              vb.value = null;
*************** function parse(buf) {
*** 317,323 ****
          }

          // Take the raw octet string value and preseve it as a buffer and hex string.
!         vb.valueRaw = bvb.slice(vbhdr.header);
          vb.valueHex = vb.valueRaw.toString('hex');

          // Add the request id to the varbind (even though it doesn't really belong)
--- 318,324 ----
          }

          // Take the raw octet string value and preseve it as a buffer and hex string.
!         vb.valueRaw = bvb.slice(vb_value_hdr.header, vb_value_hdr.header + vb_value_hdr.len);
          vb.valueHex = vb.valueRaw.toString('hex');

          // Add the request id to the varbind (even though it doesn't really belong)