diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2020-01-22 18:05:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-22 18:05:18 +0300 |
commit | b93aaad43c5861d1ba47df62cd07ca0e3223455a (patch) | |
tree | 2d2c323b87b3dc0f6896f0c493191482c913f6cb /collectors | |
parent | dc3a08b74f93cf0d6b2eb3b4f7bedbe0feac2bc1 (diff) |
node.d/snmp: snmpv3 support (#7802)
* node.d.plugin/node_modules: update net-snmp.js to the latest (snmp v3 support)
* node.d/snmp.node.js: create v3 session if snmp version is 3
* node.d/snmp/README.md: update
Diffstat (limited to 'collectors')
-rw-r--r-- | collectors/node.d.plugin/node_modules/net-snmp.js | 4411 | ||||
-rw-r--r-- | collectors/node.d.plugin/snmp/README.md | 345 | ||||
-rw-r--r-- | collectors/node.d.plugin/snmp/snmp.node.js | 20 |
3 files changed, 3419 insertions, 1357 deletions
diff --git a/collectors/node.d.plugin/node_modules/net-snmp.js b/collectors/node.d.plugin/node_modules/net-snmp.js index 484597dcb0..6b5b754ea4 100644 --- a/collectors/node.d.plugin/node_modules/net-snmp.js +++ b/collectors/node.d.plugin/node_modules/net-snmp.js @@ -1,66 +1,77 @@ - // Copyright 2013 Stephen Vickers <stephen.vickers.sv@gmail.com> // SPDX-License-Identifier: MIT -var ber = require ("asn1-ber").Ber; -var dgram = require ("dgram"); -var events = require ("events"); -var util = require ("util"); +var ber = require("asn1-ber").Ber; +var dgram = require("dgram"); +var events = require("events"); +var util = require("util"); +var crypto = require("crypto"); + +var DEBUG = false; + +var MAX_INT32 = 2147483647; + +function debug(line) { + if (DEBUG) { + console.debug(line); + } +} /***************************************************************************** ** Constants **/ -function _expandConstantObject (object) { - var keys = []; - for (var key in object) - keys.push (key); - for (var i = 0; i < keys.length; i++) - object[object[keys[i]]] = parseInt (keys[i]); + +function _expandConstantObject(object) { + var keys = []; + for (var key in object) + keys.push(key); + for (var i = 0; i < keys.length; i++) + object[object[keys[i]]] = parseInt(keys[i]); } var ErrorStatus = { - 0: "NoError", - 1: "TooBig", - 2: "NoSuchName", - 3: "BadValue", - 4: "ReadOnly", - 5: "GeneralError", - 6: "NoAccess", - 7: "WrongType", - 8: "WrongLength", - 9: "WrongEncoding", - 10: "WrongValue", - 11: "NoCreation", - 12: "InconsistentValue", - 13: "ResourceUnavailable", - 14: "CommitFailed", - 15: "UndoFailed", - 16: "AuthorizationError", - 17: "NotWritable", - 18: "InconsistentName" -}; - -_expandConstantObject (ErrorStatus); + 0: "NoError", + 1: "TooBig", + 2: "NoSuchName", + 3: "BadValue", + 4: "ReadOnly", + 5: "GeneralError", + 6: "NoAccess", + 7: "WrongType", + 8: "WrongLength", + 9: "WrongEncoding", + 10: "WrongValue", + 11: "NoCreation", + 12: "InconsistentValue", + 13: "ResourceUnavailable", + 14: "CommitFailed", + 15: "UndoFailed", + 16: "AuthorizationError", + 17: "NotWritable", + 18: "InconsistentName" +}; + +_expandConstantObject(ErrorStatus); var ObjectType = { - 1: "Boolean", - 2: "Integer", - 4: "OctetString", - 5: "Null", - 6: "OID", - 64: "IpAddress", - 65: "Counter", - 66: "Gauge", - 67: "TimeTicks", - 68: "Opaque", - 70: "Counter64", - 128: "NoSuchObject", - 129: "NoSuchInstance", - 130: "EndOfMibView" -}; - -_expandConstantObject (ObjectType); + 1: "Boolean", + 2: "Integer", + 4: "OctetString", + 5: "Null", + 6: "OID", + 64: "IpAddress", + 65: "Counter", + 66: "Gauge", + 67: "TimeTicks", + 68: "Opaque", + 70: "Counter64", + 128: "NoSuchObject", + 129: "NoSuchInstance", + 130: "EndOfMibView" +}; + +_expandConstantObject(ObjectType); ObjectType.Integer32 = ObjectType.Integer; ObjectType.Counter32 = ObjectType.Counter; @@ -68,132 +79,173 @@ ObjectType.Gauge32 = ObjectType.Gauge; ObjectType.Unsigned32 = ObjectType.Gauge32; var PduType = { - 160: "GetRequest", - 161: "GetNextRequest", - 162: "GetResponse", - 163: "SetRequest", - 164: "Trap", - 165: "GetBulkRequest", - 166: "InformRequest", - 167: "TrapV2", - 168: "Report" + 160: "GetRequest", + 161: "GetNextRequest", + 162: "GetResponse", + 163: "SetRequest", + 164: "Trap", + 165: "GetBulkRequest", + 166: "InformRequest", + 167: "TrapV2", + 168: "Report" }; -_expandConstantObject (PduType); +_expandConstantObject(PduType); var TrapType = { - 0: "ColdStart", - 1: "WarmStart", - 2: "LinkDown", - 3: "LinkUp", - 4: "AuthenticationFailure", - 5: "EgpNeighborLoss", - 6: "EnterpriseSpecific" + 0: "ColdStart", + 1: "WarmStart", + 2: "LinkDown", + 3: "LinkUp", + 4: "AuthenticationFailure", + 5: "EgpNeighborLoss", + 6: "EnterpriseSpecific" +}; + +_expandConstantObject(TrapType); + +var SecurityLevel = { + 1: "noAuthNoPriv", + 2: "authNoPriv", + 3: "authPriv" +}; + +_expandConstantObject(SecurityLevel); + +var AuthProtocols = { + "1": "none", + "2": "md5", + "3": "sha" +}; + +_expandConstantObject(AuthProtocols); + +var PrivProtocols = { + "1": "none", + "2": "des" +}; + +_expandConstantObject(PrivProtocols); + +var MibProviderType = { + "1": "Scalar", + "2": "Table" }; -_expandConstantObject (TrapType); +_expandConstantObject(MibProviderType); var Version1 = 0; var Version2c = 1; +var Version3 = 3; + +var Version = { + "1": Version1, + "2c": Version2c, + "3": Version3 +}; /***************************************************************************** ** Exception class definitions **/ -function ResponseInvalidError (message) { - this.name = "ResponseInvalidError"; - this.message = message; - Error.captureStackTrace(this, ResponseInvalidError); +function ResponseInvalidError(message) { + this.name = "ResponseInvalidError"; + this.message = message; + Error.captureStackTrace(this, ResponseInvalidError); } -util.inherits (ResponseInvalidError, Error); -function RequestInvalidError (message) { - this.name = "RequestInvalidError"; - this.message = message; - Error.captureStackTrace(this, RequestInvalidError); +util.inherits(ResponseInvalidError, Error); + +function RequestInvalidError(message) { + this.name = "RequestInvalidError"; + this.message = message; + Error.captureStackTrace(this, RequestInvalidError); } -util.inherits (RequestInvalidError, Error); -function RequestFailedError (message, status) { - this.name = "RequestFailedError"; - this.message = message; - this.status = status; - Error.captureStackTrace(this, RequestFailedError); +util.inherits(RequestInvalidError, Error); + +function RequestFailedError(message, status) { + this.name = "RequestFailedError"; + this.message = message; + this.status = status; + Error.captureStackTrace(this, RequestFailedError); } -util.inherits (RequestFailedError, Error); -function RequestTimedOutError (message) { - this.name = "RequestTimedOutError"; - this.message = message; - Error.captureStackTrace(this, RequestTimedOutError); +util.inherits(RequestFailedError, Error); + +function RequestTimedOutError(message) { + this.name = "RequestTimedOutError"; + this.message = message; + Error.captureStackTrace(this, RequestTimedOutError); } -util.inherits (RequestTimedOutError, Error); + +util.inherits(RequestTimedOutError, Error); /***************************************************************************** ** OID and varbind helper functions **/ -function isVarbindError (varbind) { - return !!(varbind.type == ObjectType.NoSuchObject - || varbind.type == ObjectType.NoSuchInstance - || varbind.type == ObjectType.EndOfMibView); +function isVarbindError(varbind) { + return !!(varbind.type == ObjectType.NoSuchObject + || varbind.type == ObjectType.NoSuchInstance + || varbind.type == ObjectType.EndOfMibView); } -function varbindError (varbind) { - return (ObjectType[varbind.type] || "NotAnError") + ": " + varbind.oid; +function varbindError(varbind) { + return (ObjectType[varbind.type] || "NotAnError") + ": " + varbind.oid; } -function oidFollowsOid (oidString, nextString) { - var oid = {str: oidString, len: oidString.length, idx: 0}; - var next = {str: nextString, len: nextString.length, idx: 0}; - var dotCharCode = ".".charCodeAt (0); - - function getNumber (item) { - var n = 0; - if (item.idx >= item.len) - return null; - while (item.idx < item.len) { - var charCode = item.str.charCodeAt (item.idx++); - if (charCode == dotCharCode) - return n; - n = (n ? (n * 10) : n) + (charCode - 48); - } - return n; - } - - while (1) { - var oidNumber = getNumber (oid); - var nextNumber = getNumber (next); - - if (oidNumber !== null) { - if (nextNumber !== null) { - if (nextNumber > oidNumber) { - return true; - } else if (nextNumber < oidNumber) { - return false; - } - } else { - return true; - } - } else { - return true; - } - } +function oidFollowsOid(oidString, nextString) { + var oid = {str: oidString, len: oidString.length, idx: 0}; + var next = {str: nextString, len: nextString.length, idx: 0}; + var dotCharCode = ".".charCodeAt(0); + + function getNumber(item) { + var n = 0; + if (item.idx >= item.len) + return null; + while (item.idx < item.len) { + var charCode = item.str.charCodeAt(item.idx++); + if (charCode == dotCharCode) + return n; + n = (n ? (n * 10) : n) + (charCode - 48); + } + return n; + } + + while (1) { + var oidNumber = getNumber(oid); + var nextNumber = getNumber(next); + + if (oidNumber !== null) { + if (nextNumber !== null) { + if (nextNumber > oidNumber) { + return true; + } else if (nextNumber < oidNumber) { + return false; + } + } else { + return true; + } + } else { + return true; + } + } } -function oidInSubtree (oidString, nextString) { - var oid = oidString.split ("."); - var next = nextString.split ("."); +function oidInSubtree(oidString, nextString) { + var oid = oidString.split("."); + var next = nextString.split("."); - if (oid.length > next.length) - return false; + if (oid.length > next.length) + return false; - for (var i = 0; i < oid.length; i++) { - if (next[i] != oid[i]) - return false; - } + for (var i = 0; i < oid.length; i++) { + if (next[i] != oid[i]) + return false; + } - return true; + return true; } /** @@ -207,1228 +259,3138 @@ function oidInSubtree (oidString, nextString) { ** an error since the integer is too large. **/ -function readInt (buffer) { - return readUint (buffer, true); +function readInt(buffer) { + return readUint(buffer, true); +} + +function readIpAddress(buffer) { + var bytes = buffer.readString(ObjectType.IpAddress, true); + if (bytes.length != 4) + throw new ResponseInvalidError("Length '" + bytes.length + + "' of IP address '" + bytes.toString("hex") + + "' is not 4"); + var value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3]; + return value; } -function readUint (buffer, isSigned) { - buffer.readByte (); - var length = buffer.readByte (); - var value = 0; - var signedBitSet = false; - - if (length > 5) { - throw new RangeError ("Integer too long '" + length + "'"); - } else if (length == 5) { - if (buffer.readByte () !== 0) - throw new RangeError ("Integer too long '" + length + "'"); - length = 4; - } - - for (var i = 0; i < length; i++) { - value *= 256; - value += buffer.readByte (); - - if (isSigned && i <= 0) { - if ((value & 0x80) == 0x80) - signedBitSet = true; - } - } - - if (signedBitSet) - value -= (1 << (i * 8)); - - return value; +function readUint(buffer, isSigned) { + buffer.readByte(); + var length = buffer.readByte(); + var value = 0; + var signedBitSet = false; + + if (length > 5) { + throw new RangeError("Integer too long '" + length + "'"); + } else if (length == 5) { + if (buffer.readByte() !== 0) + throw new RangeError("Integer too long '" + length + "'"); + length = 4; + } + + for (var i = 0; i < length; i++) { + value *= 256; + value += buffer.readByte(); + + if (isSigned && i <= 0) { + if ((value & 0x80) == 0x80) + signedBitSet = true; + } + } + + if (signedBitSet) + value -= (1 << (i * 8)); + + return value; } -function readUint64 (buffer) { - var value = buffer.readString (ObjectType.Counter64, true); +function readUint64(buffer) { + var value = buffer.readString(ObjectType.Counter64, true); - return value; + return value; } -function readVarbinds (buffer, varbinds) { - buffer.readSequence (); - - while (1) { - buffer.readSequence (); - var oid = buffer.readOID (); - var type = buffer.peek (); - - if (type == null) - break; - - var value; - - if (type == ObjectType.Boolean) { - value = buffer.readBoolean (); - } else if (type == ObjectType.Integer) { - value = readInt (buffer); - } else if (type == ObjectType.OctetString) { - value = buffer.readString (null, true); - } else if (type == ObjectType.Null) { - buffer.readByte (); - buffer.readByte (); - value = null; - } else if (type == ObjectType.OID) { - value = buffer.readOID (); - } else if (type == ObjectType.IpAddress) { - var bytes = buffer.readString (ObjectType.IpAddress, true); - if (bytes.length != 4) - throw new ResponseInvalidError ("Length '" + bytes.length - + "' of IP address '" + bytes.toString ("hex") - + "' is not 4"); - value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3]; - } else if (type == ObjectType.Counter) { - value = readUint (buffer); - } else if (type == ObjectType.Gauge) { - value = readUint (buffer); - } else if (type == ObjectType.TimeTicks) { - value = readUint (buffer); - } else if (type == ObjectType.Opaque) { - value = buffer.readString (ObjectType.Opaque, true); - } else if (type == ObjectType.Counter64) { - value = readUint64 (buffer); - } else if (type == ObjectType.NoSuchObject) { - buffer.readByte (); - buffer.readByte (); - value = null; - } else if (type == ObjectType.NoSuchInstance) { - buffer.readByte (); - buffer.readByte (); - value = null; - } else if (type == ObjectType.EndOfMibView) { - buffer.readByte (); - buffer.readByte (); - value = null; - } else { - throw new ResponseInvalidError ("Unknown type '" + type - + "' in response"); - } - - varbinds.push ({ - oid: oid, - type: type, - value: value - }); - } +function readVarbinds(buffer, varbinds) { + buffer.readSequence(); + + while (1) { + buffer.readSequence(); + if (buffer.peek() != ObjectType.OID) + break; + var oid = buffer.readOID(); + var type = buffer.peek(); + + if (type == null) + break; + + var value; + + if (type == ObjectType.Boolean) { + value = buffer.readBoolean(); + } else if (type == ObjectType.Integer) { + value = readInt(buffer); + } else if (type == ObjectType.OctetString) { + value = buffer.readString(null, true); + } else if (type == ObjectType.Null) { + buffer.readByte(); + buffer.readByte(); + value = null; + } else if (type == ObjectType.OID) { + value = buffer.readOID(); + } else if (type == ObjectType.IpAddress) { + var bytes = buffer.readString(ObjectType.IpAddress, true); + if (bytes.length != 4) + throw new ResponseInvalidError("Length '" + bytes.length + + "' of IP address '" + bytes.toString("hex") + + "' is not 4"); + value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3]; + } else if (type == ObjectType.Counter) { + value = readUint(buffer); + } else if (type == ObjectType.Gauge) { + value = readUint(buffer); + } else if (type == ObjectType.TimeTicks) { + value = readUint(buffer); + } else if (type == ObjectType.Opaque) { + value = buffer.readString(ObjectType.Opaque, true); + } else if (type == ObjectType.Counter64) { + value = readUint64(buffer); + } else if (type == ObjectType.NoSuchObject) { + buffer.readByte(); + buffer.readByte(); + value = null; + } else if (type == ObjectType.NoSuchInstance) { + buffer.readByte(); + buffer.readByte(); + value = null; + } else if (type == ObjectType.EndOfMibView) { + buffer.readByte(); + buffer.readByte(); + value = null; + } else { + throw new ResponseInvalidError("Unknown type '" + type + + "' in response"); + } + + varbinds.push({ + oid: oid, + type: type, + value: value + }); + } } -function writeUint (buffer, type, value) { - var b = new Buffer (4); - b.writeUInt32BE (value, 0); - buffer.writeBuffer (b, type); +function writeUint(buffer, type, value) { + var b = Buffer.alloc(4); + b.writeUInt32BE(value, 0); + buffer.writeBuffer(b, type); } -function writeUint64 (buffer, value) { - buffer.writeBuffer (value, ObjectType.Counter64); +function writeUint64(buffer, value) { + buffer.writeBuffer(value, ObjectType.Counter64); } -function writeVarbinds (buffer, varbinds) { - buffer.startSequence (); - for (var i = 0; i < varbinds.length; i++) { - buffer.startSequence (); - buffer.writeOID (varbinds[i].oid); - - if (varbinds[i].type && varbinds[i].hasOwnProperty("value")) { - var type = varbinds[i].type; - var value = varbinds[i].value; - - if (type == ObjectType.Boolean) { - buffer.writeBoolean (value ? true : false); - } else if (type == ObjectType.Integer) { // also Integer32 - buffer.writeInt (value); - } else if (type == ObjectType.OctetString) { - if (typeof value == "string") - buffer.writeString (value); - else - buffer.writeBuffer (value, ObjectType.OctetString); - } else if (type == ObjectType.Null) { - buffer.writeNull (); - } else if (type == ObjectType.OID) { - buffer.writeOID (value); - } else if (type == ObjectType.IpAddress) { - var bytes = value.split ("."); - if (bytes.length != 4) - throw new RequestInvalidError ("Invalid IP address '" - + value + "'"); - buffer.writeBuffer (new Buffer (bytes), 64); - } else if (type == ObjectType.Counter) { // also Counter32 - writeUint (buffer, ObjectType.Counter, value); - } else if (type == ObjectType.Gauge) { // also Gauge32 & Unsigned32 - writeUint (buffer, ObjectType.Gauge, value); - } else if (type == ObjectType.TimeTicks) { - writeUint (buffer, ObjectType.TimeTicks, value); - } else if (type == ObjectType.Opaque) { - buffer.writeBuffer (value, ObjectType.Opaque); - } else if (type == ObjectType.Counter64) { - writeUint64 (buffer, value); - } else { - throw new RequestInvalidError ("Unknown type '" + type - + "' in request"); - } - } else { - buffer.writeNull (); - } - - buffer.endSequence (); - } - buffer.endSequence (); +function writeVarbinds(buffer, varbinds) { + buffer.startSequence(); + for (var i = 0; i < varbinds.length; i++) { + buffer.startSequence(); + buffer.writeOID(varbinds[i].oid); + + if (varbinds[i].type && varbinds[i].hasOwnProperty("value")) { + var type = varbinds[i].type; + var value = varbinds[i].value; + + if (type == ObjectType.Boolean) { + buffer.writeBoolean(value ? true : false); + } else if (type == ObjectType.Integer) { // also Integer32 + buffer.writeInt(value); + } else if (type == ObjectType.OctetString) { + if (typeof value == "string") + buffer.writeString(value); + else + buffer.writeBuffer(value, ObjectType.OctetString); + } else if (type == ObjectType.Null) { + buffer.writeNull(); + } else if (type == ObjectType.OID) { + buffer.writeOID(value); + } else if (type == ObjectType.IpAddress) { + var bytes = value.split("."); + if (bytes.length != 4) + throw new RequestInvalidError("Invalid IP address '" + + value + "'"); + buffer.writeBuffer(Buffer.from(bytes), 64); + } else if (type == ObjectType.Counter) { // also Counter32 + writeUint(buffer, ObjectType.Counter, value); + } else if (type == ObjectType.Gauge) { // also Gauge32 & Unsigned32 + writeUint(buffer, ObjectType.Gauge, value); + } else if (type == ObjectType.TimeTicks) { + writeUint(buffer, ObjectType.TimeTicks, value); + } else if (type == ObjectType.Opaque) { + buffer.writeBuffer(value, ObjectType.Opaque); + } else if (type == ObjectType.Counter64) { + writeUint64(buffer, value); + } else if (type == ObjectType.EndOfMibView) { + buffer.writeByte(130); + buffer.writeByte(0); + } else { + throw new RequestInvalidError("Unknown type '" + type + + "' in request"); + } + } else { + buffer.writeNull(); + } + + buffer.endSequence(); + } + buffer.endSequence(); } /***************************************************************************** ** PDU class definitions **/ -var SimplePdu = function (id, varbinds, options) { - this.id = id; - this.varbinds = varbinds; - this.options = options || {}; +var SimplePdu = function () { }; SimplePdu.prototype.toBuffer = function (buffer) { - buffer.startSequence (this.type); + buffer.startSequence(this.type); - buffer.writeInt (this.id); - buffer.writeInt ((this.type == PduType.GetBulkRequest) - ? (this.options.nonRepeaters || 0) - : 0); - buffer.writeInt ((this.type == PduType.GetBulkRequest) - ? (this.options.maxRepetitions || 0) - : 0); + buffer.writeInt(this.id); + buffer.writeInt((this.type == PduType.GetBulkRequest) + ? (this.options.nonRepeaters || 0) + : 0); + buffer.writeInt((this.type == PduType.GetBulkRequest) + ? (this.options.maxRepetitions || 0) + : 0); - writeVarbinds (buffer, this.varbinds); + writeVarbinds(buffer, this.varbinds); - buffer.endSequence (); + buffer.endSequence(); }; -var GetBulkRequestPdu = function () { - this.type = PduType.GetBulkRequest; - GetBulkRequestPdu.super_.apply (this, arguments); -}; +SimplePdu.prototype.initializeFromVariables = function (id, varbinds, options) { + this.id = id; + this.varbinds = varbinds; + this.options = options || {}; + this.contextName = (options && options.context) ? options.context : ""; +} -util.inherits (GetBulkRequestPdu, SimplePdu); +SimplePdu.prototype.initializeFromBuffer = function (reader) { + this.type = reader.peek(); + reader.readSequence(); + + this.id = reader.readInt(); + this.nonRepeaters = reader.readInt(); + this.maxRepetitions = reader.readInt(); + + this.varbinds = []; + readVarbinds(reader, this.varbinds); -var GetNextRequestPdu = function () { - this.type = PduType.GetNextRequest; - GetNextRequestPdu.super_.apply (this, arguments); }; -util.inherits (GetNextRequestPdu, SimplePdu); +SimplePdu.prototype.getResponsePduForRequest = function () { + var responsePdu = GetResponsePdu.createFromVariables(this.id, [], {}); + if (this.contextEngineID) { + responsePdu.contextEngineID = this.contextEngineID; + responsePdu.contextName = this.contextName; + } + return responsePdu; +}; + +SimplePdu.createFromVariables = function (pduClass, id, varbinds, options) { + var pdu = new pduClass(id, varbinds, options); + pdu.id = id; + pdu.varbinds = varbinds; + pdu.options = options || {}; + pdu.contextName = (options && options.context) ? options.context : ""; + return pdu; +}; -var GetResponsePdu = function (buffer) { - this.type = PduType.GetResponse; +var GetBulkRequestPdu = function () { + this.type = PduType.GetBulkRequest; + GetBulkRequestPdu.super_.apply(this, arguments); +}; - buffer.readSequence (this.type); +util.inherits(GetBulkRequestPdu, SimplePdu); - this.id = buffer.readInt (); +GetBulkRequestPdu.createFromBuffer = function (reader) { + var pdu = new GetBulkRequestPdu(); + pdu.initializeFromBuffer(reader); + return pdu; +}; - this.errorStatus = buffer.readInt (); - this.errorIndex = buffer.readInt (); +var GetNextRequestPdu = function () { + this.type = PduType.GetNextRequest; + GetNextRequestPdu.super_.apply(this, arguments); +}; - this.varbinds = []; +util.inherits(GetNextRequestPdu, SimplePdu); - readVarbinds (buffer, this.varbinds); +GetNextRequestPdu.createFromBuffer = function (reader) { + var pdu = new GetNextRequestPdu(); + pdu.initializeFromBuffer(reader); + return pdu; }; var GetRequestPdu = function () { - this.type = PduType.GetRequest; - GetRequestPdu.super_.apply (this, arguments); + this.type = PduType.GetRequest; + GetRequestPdu.super_.apply(this, arguments); +}; + +util.inherits(GetRequestPdu, SimplePdu); + +GetRequestPdu.createFromBuffer = function (reader) { + var pdu = new GetRequestPdu(); + pdu.initializeFromBuffer(reader); + return pdu; }; -util.inherits (GetRequestPdu, SimplePdu); +GetRequestPdu.createFromVariables = function (id, varbinds, options) { + var pdu = new GetRequestPdu(); + pdu.initializeFromVariables(id, varbinds, options); + return pdu; +}; var InformRequestPdu = function () { - this.type = PduType.InformRequest; - InformRequestPdu.super_.apply (this, arguments); + this.type = PduType.InformRequest; + InformRequestPdu.super_.apply(this, arguments); }; -util.inherits (InformRequestPdu, SimplePdu); +util.inherits(InformRequestPdu, SimplePdu); + +InformRequestPdu.createFromBuffer = function (reader) { + var pdu = new InformRequestPdu(); + pdu.initializeFromBuffer(reader); + return pdu; +}; var SetRequestPdu = function () { - this.type = PduType.SetRequest; - SetRequestPdu.super_.apply (this, arguments); + this.type = PduType.SetRequest; + SetRequestPdu.super_.apply(this, arguments); +}; + +util.inherits(SetRequestPdu, SimplePdu); + +SetRequestPdu.createFromBuffer = function (reader) { + var pdu = new SetRequestPdu(); + pdu.initializeFromBuffer(reader); + return pdu; }; -util.inherits (SetRequestPdu, SimplePdu); +var TrapPdu = function () { + this.type = PduType.Trap; +}; -var TrapPdu = function (typeOrOid, varbinds, options) { - this.type = PduType.Trap; +TrapPdu.prototype.toBuffer = function (buffer) { + buffer.startSequence(this.type); - this.agentAddr = options.agentAddr || "127.0.0.1"; - this.upTime = options.upTime; + buffer.writeOID(this.enterprise); + buffer.writeBuffer(Buffer.from(this.agentAddr.split(".")), + ObjectType.IpAddress); + buffer.writeInt(this.generic); + buffer.writeInt(this.specific); + writeUint(buffer, ObjectType.TimeTicks, + this.upTime || Math.floor(process.uptime() * 100)); - if (typeof typeOrOid == "string") { - this.generic = TrapType.EnterpriseSpecific; - this.specific = parseInt (typeOrOid.match (/\.(\d+)$/)[1]); - this.enterprise = typeOrOid.replace (/\.(\d+)$/, ""); - } else { - this.generic = typeOrOid; - this.specific = 0; - this.enterprise = "1.3.6.1.4.1"; - } + writeVarbinds(buffer, this.varbinds); - this.varbinds = varbinds; + buffer.endSequence(); }; -TrapPdu.prototype.toBuffer = function (buffer) { - buffer.startSequence (this.type); +TrapPdu.createFromBuffer = function (reader) { + var pdu = new TrapPdu(); + reader.readSequence(); + + pdu.enterprise = reader.readOID(); + pdu.agentAddr = readIpAddress(reader); + pdu.generic = reader.readInt(); + pdu.specific = reader.readInt(); + pdu.upTime = readUint(reader) + + pdu.varbinds = []; + readVarbinds(reader, pdu.varbinds); + + return pdu; +}; - buffer.writeOID (this.enterprise); - buffer.writeBuffer (new Buffer (this.agentAddr.split (".")), - ObjectType.IpAddress); - buffer.writeInt (this.generic); - buffer.writeInt (this.specific); - writeUint (buffer, ObjectType.TimeTicks, - this.upTime || Math.floor (process.uptime () * 100)); +TrapPdu.createFromVariables = function (typeOrOid, varbinds, options) { + var pdu = new TrapPdu(); + pdu.agentAddr = options.agentAddr || "127.0.0.1"; + pdu.upTime = options.upTime; - writeVarbinds (buffer, this.varbinds); + if (typeof typeOrOid == "string") { + pdu.generic = TrapType.EnterpriseSpecific; + pdu.specific = parseInt(typeOrOid.match(/\.(\d+)$/)[1]); + pdu.enterprise = typeOrOid.replace(/\.(\d+)$/, ""); + } else { + pdu.generic = typeOrOid; + pdu.specific = 0; + pdu.enterprise = "1.3.6.1.4.1"; + } - buffer.endSequence (); + pdu.varbinds = varbinds; + + return pdu; }; var TrapV2Pdu = function () { - this.type = PduType.TrapV2; - TrapV2Pdu.super_.apply (this, arguments); + this.type = PduType.TrapV2; + TrapV2Pdu.super_.apply(this, arguments); +}; + +util.inherits(TrapV2Pdu, SimplePdu); + +TrapV2Pdu.createFromBuffer = function (reader) { + var pdu = new TrapV2Pdu(); + pdu.initializeFromBuffer(reader); + return pdu; +}; + +TrapV2Pdu.createFromVariables = function (id, varbinds, options) { + var pdu = new TrapV2Pdu(); + pdu.initializeFromVariables(id, varbinds, options); + return pdu; +}; + +var SimpleResponsePdu = function () { +}; + +SimpleResponsePdu.prototype.toBuffer = function (writer) { + writer.startSequence(this.type); + + writer.writeInt(this.id); + writer.writeInt(this.errorStatus || 0); + writer.writeInt(this.errorIndex || 0); + writeVarbinds(writer, this.varbinds); + writer.endSequence(); + }; -util.inherits (TrapV2Pdu, SimplePdu); +SimpleResponsePdu.prototype.initializeFromBuffer = function (reader) { + reader.readSequence(this.type); + + this.id = reader.readInt(); + this.errorStatus = reader.readInt(); + this.errorIndex = reader.readInt(); + + this.varbinds = []; + readVarbinds(reader, this.varbinds); +}; + +SimpleResponsePdu.prototype.initializeFromVariables = function (id, varbinds, options) { + this.id = id; + this.varbinds = varbinds; + this.options = options || {}; +}; + +var GetResponsePdu = function () { + this.type = PduType.GetResponse; + GetResponsePdu.super_.apply(this, arguments); +}; + +util.inherits(GetResponsePdu, SimpleResponsePdu); + +GetResponsePdu.createFromBuffer = function (reader) { + var pdu = new GetResponsePdu(); + pdu.initializeFromBuffer(reader); + return pdu; +}; + +GetResponsePdu.createFromVariables = function (id, varbinds, options) { + var pdu = new GetResponsePdu(); + pdu.initializeFromVariables(id, varbinds, options); + return pdu; +}; + +var ReportPdu = function () { + this.type = PduType.Report; + ReportPdu.super_.apply(this, arguments); +}; + +util.inherits(ReportPdu, SimpleResponsePdu); + +ReportPdu.createFromBuffer = function (reader) { + var pdu = new ReportPdu(); + pdu.initializeFromBuffer(reader); + return pdu; +}; + +ReportPdu.createFromVariables = function (id, varbinds, options) { + var pdu = new ReportPdu(); + pdu.initializeFromVariables(id, var |