2 // Copyright 2013 Stephen Vickers <stephen.vickers.sv@gmail.com>
4 var ber = require ("asn1").Ber;
5 var dgram = require ("dgram");
6 var events = require ("events");
7 var util = require ("util");
9 /*****************************************************************************
13 function _expandConstantObject (object) {
17 for (var i = 0; i < keys.length; i++)
18 object[object[keys[i]]] = parseInt (keys[i]);
34 12: "InconsistentValue",
35 13: "ResourceUnavailable",
38 16: "AuthorizationError",
40 18: "InconsistentName"
43 _expandConstantObject (ErrorStatus);
58 129: "NoSuchInstance",
62 _expandConstantObject (ObjectType);
64 ObjectType.Integer32 = ObjectType.Integer;
65 ObjectType.Counter32 = ObjectType.Counter;
66 ObjectType.Gauge32 = ObjectType.Gauge;
67 ObjectType.Unsigned32 = ObjectType.Gauge32;
71 161: "GetNextRequest",
75 165: "GetBulkRequest",
81 _expandConstantObject (PduType);
88 4: "AuthenticationFailure",
90 6: "EnterpriseSpecific"
93 _expandConstantObject (TrapType);
98 /*****************************************************************************
99 ** Exception class definitions
102 function ResponseInvalidError (message) {
103 this.name = "ResponseInvalidError";
104 this.message = message;
105 Error.captureStackTrace(this, ResponseInvalidError);
107 util.inherits (ResponseInvalidError, Error);
109 function RequestInvalidError (message) {
110 this.name = "RequestInvalidError";
111 this.message = message;
112 Error.captureStackTrace(this, RequestInvalidError);
114 util.inherits (RequestInvalidError, Error);
116 function RequestFailedError (message, status) {
117 this.name = "RequestFailedError";
118 this.message = message;
119 this.status = status;
120 Error.captureStackTrace(this, RequestFailedError);
122 util.inherits (RequestFailedError, Error);
124 function RequestTimedOutError (message) {
125 this.name = "RequestTimedOutError";
126 this.message = message;
127 Error.captureStackTrace(this, RequestTimedOutError);
129 util.inherits (RequestTimedOutError, Error);
131 /*****************************************************************************
132 ** OID and varbind helper functions
135 function isVarbindError (varbind) {
136 if (varbind.type == ObjectType.NoSuchObject
137 || varbind.type == ObjectType.NoSuchInstance
138 || varbind.type == ObjectType.EndOfMibView)
144 function varbindError (varbind) {
145 return (ObjectType[varbind.type] || "NotAnError") + ": " + varbind.oid;
148 function oidFollowsOid (oidString, nextString) {
149 var oid = {str: oidString, len: oidString.length, idx: 0};
150 var next = {str: nextString, len: nextString.length, idx: 0};
151 var dotCharCode = ".".charCodeAt (0);
153 function getNumber (item) {
155 if (item.idx >= item.len)
157 while (item.idx < item.len) {
158 var charCode = item.str.charCodeAt (item.idx++);
159 if (charCode == dotCharCode)
161 n = (n ? (n * 10) : n) + (charCode - 48);
167 var oidNumber = getNumber (oid);
168 var nextNumber = getNumber (next);
170 if (oidNumber !== null) {
171 if (nextNumber !== null) {
172 if (nextNumber > oidNumber) {
174 } else if (nextNumber < oidNumber) {
186 function oidInSubtree (oidString, nextString) {
187 var oid = oidString.split (".");
188 var next = nextString.split (".");
190 if (oid.length > next.length)
193 for (var i = 0; i < oid.length; i++) {
194 if (next[i] != oid[i])
202 ** Some SNMP agents produce integers on the wire such as 00 ff ff ff ff.
203 ** The ASN.1 BER parser we use throws an error when parsing this, which we
204 ** believe is correct. So, we decided not to bother the "asn1" developer(s)
205 ** with this, instead opting to work around it here.
207 ** If an integer is 5 bytes in length we check if the first byte is 0, and if so
208 ** simply drop it and parse it like it was a 4 byte integer, otherwise throw
209 ** an error since the integer is too large.
212 function readInt (buffer) {
213 return readUint (buffer, true);
216 function readUint (buffer, isSigned) {
218 var length = buffer.readByte ();
221 throw new RangeError ("Integer too long '" + length + "'");
222 } else if (length == 5) {
223 if (buffer.readByte () !== 0)
224 throw new RangeError ("Integer too long '" + length + "'");
228 value = 0, signedBitSet = false;
230 for (var i = 0; i < length; i++) {
232 value += buffer.readByte ();
234 if (isSigned && i <= 0) {
235 if ((value & 0x80) == 0x80)
241 value -= (1 << (i * 8));
246 function readUint64 (buffer) {
247 var value = buffer.readString (ObjectType.Counter64, true);
249 if (value.length > 8)
250 throw new RequestInvalidError ("64 bit unsigned integer too long '"
251 + value.length + "'")
256 function readVarbinds (buffer, varbinds) {
257 buffer.readSequence ();
260 buffer.readSequence ();
261 var oid = buffer.readOID ();
262 var type = buffer.peek ();
269 if (type == ObjectType.Boolean) {
270 value = buffer.readBoolean ();
271 } else if (type == ObjectType.Integer) {
272 value = readInt (buffer);
273 } else if (type == ObjectType.OctetString) {
274 value = buffer.readString (null, true);
275 } else if (type == ObjectType.Null) {
279 } else if (type == ObjectType.OID) {
280 value = buffer.readOID ();
281 } else if (type == ObjectType.IpAddress) {
282 var bytes = buffer.readString (ObjectType.IpAddress, true);
283 if (bytes.length != 4)
284 throw new ResponseInvalidError ("Length '" + bytes.length
285 + "' of IP address '" + bytes.toString ("hex")
287 value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3];
288 } else if (type == ObjectType.Counter) {
289 value = readUint (buffer);
290 } else if (type == ObjectType.Gauge) {
291 value = readUint (buffer);
292 } else if (type == ObjectType.TimeTicks) {
293 value = readUint (buffer);
294 } else if (type == ObjectType.Opaque) {
295 value = buffer.readString (ObjectType.Opaque, true);
296 } else if (type == ObjectType.Counter64) {
297 value = readUint64 (buffer);
298 } else if (type == ObjectType.NoSuchObject) {
302 } else if (type == ObjectType.NoSuchInstance) {
306 } else if (type == ObjectType.EndOfMibView) {
311 throw new ResponseInvalidError ("Unknown type '" + type
323 function writeUint (buffer, type, value) {
324 var b = new Buffer (4);
325 b.writeUInt32BE (value, 0);
326 buffer.writeBuffer (b, type);
329 function writeUint64 (buffer, value) {
330 if (value.length > 8)
331 throw new RequestInvalidError ("64 bit unsigned integer too long '"
332 + value.length + "'")
333 buffer.writeBuffer (value, ObjectType.Counter64);
336 function writeVarbinds (buffer, varbinds) {
337 buffer.startSequence ();
338 for (var i = 0; i < varbinds.length; i++) {
339 buffer.startSequence ();
340 buffer.writeOID (varbinds[i].oid);
342 if (varbinds[i].type && varbinds[i].hasOwnProperty("value")) {
343 var type = varbinds[i].type;
344 var value = varbinds[i].value;
346 if (type == ObjectType.Boolean) {
347 buffer.writeBoolean (value ? true : false);
348 } else if (type == ObjectType.Integer) { // also Integer32
349 buffer.writeInt (value);
350 } else if (type == ObjectType.OctetString) {
351 if (typeof value == "string")
352 buffer.writeString (value);
354 buffer.writeBuffer (value, ObjectType.OctetString);
355 } else if (type == ObjectType.Null) {
357 } else if (type == ObjectType.OID) {
358 buffer.writeOID (value);
359 } else if (type == ObjectType.IpAddress) {
360 var bytes = value.split (".");
361 if (bytes.length != 4)
362 throw new RequestInvalidError ("Invalid IP address '"
364 buffer.writeBuffer (new Buffer (bytes), 64);
365 } else if (type == ObjectType.Counter) { // also Counter32
366 writeUint (buffer, ObjectType.Counter, value);
367 } else if (type == ObjectType.Gauge) { // also Gauge32 & Unsigned32
368 writeUint (buffer, ObjectType.Gauge, value);
369 } else if (type == ObjectType.TimeTicks) {
370 writeUint (buffer, ObjectType.TimeTicks, value);
371 } else if (type == ObjectType.Opaque) {
372 buffer.writeBuffer (value, ObjectType.Opaque);
373 } else if (type == ObjectType.Counter64) {
374 writeUint64 (buffer, value);
376 throw new RequestInvalidError ("Unknown type '" + type
383 buffer.endSequence ();
385 buffer.endSequence ();
388 /*****************************************************************************
389 ** PDU class definitions
392 var SimplePdu = function (id, varbinds, options) {
394 this.varbinds = varbinds;
395 this.options = options || {};
398 SimplePdu.prototype.toBuffer = function (buffer) {
399 buffer.startSequence (this.type);
401 buffer.writeInt (this.id);
402 buffer.writeInt ((this.type == PduType.GetBulkRequest)
403 ? (this.options.nonRepeaters || 0)
405 buffer.writeInt ((this.type == PduType.GetBulkRequest)
406 ? (this.options.maxRepetitions || 0)
409 writeVarbinds (buffer, this.varbinds);
411 buffer.endSequence ();
414 var GetBulkRequestPdu = function () {
415 this.type = PduType.GetBulkRequest;
416 GetBulkRequestPdu.super_.apply (this, arguments);
419 util.inherits (GetBulkRequestPdu, SimplePdu);
421 var GetNextRequestPdu = function () {
422 this.type = PduType.GetNextRequest;
423 GetNextRequestPdu.super_.apply (this, arguments);
426 util.inherits (GetNextRequestPdu, SimplePdu);
428 var GetResponsePdu = function (buffer) {
429 this.type = PduType.GetResponse;
431 buffer.readSequence (this.type);
433 this.id = buffer.readInt ();
435 this.errorStatus = buffer.readInt ();
436 this.errorIndex = buffer.readInt ();
440 readVarbinds (buffer, this.varbinds);
443 var GetRequestPdu = function () {
444 this.type = PduType.GetRequest;
445 GetRequestPdu.super_.apply (this, arguments);
448 util.inherits (GetRequestPdu, SimplePdu);
450 var InformRequestPdu = function () {
451 this.type = PduType.InformRequest;
452 InformRequestPdu.super_.apply (this, arguments);
455 util.inherits (InformRequestPdu, SimplePdu);
457 var SetRequestPdu = function () {
458 this.type = PduType.SetRequest;
459 SetRequestPdu.super_.apply (this, arguments);
462 util.inherits (SetRequestPdu, SimplePdu);
464 var TrapPdu = function (typeOrOid, varbinds, options) {
465 this.type = PduType.Trap;
467 this.agentAddr = options.agentAddr || "127.0.0.1";
468 this.upTime = options.upTime;
470 if (typeof typeOrOid == "string") {
471 this.generic = TrapType.EnterpriseSpecific;
472 this.specific = parseInt (typeOrOid.match (/\.(\d+)$/)[1]);
473 this.enterprise = typeOrOid.replace (/\.(\d+)$/, "");
475 this.generic = typeOrOid;
477 this.enterprise = "1.3.6.1.4.1";
480 this.varbinds = varbinds;
483 TrapPdu.prototype.toBuffer = function (buffer) {
484 buffer.startSequence (this.type);
486 buffer.writeOID (this.enterprise);
487 buffer.writeBuffer (new Buffer (this.agentAddr.split (".")),
488 ObjectType.IpAddress);
489 buffer.writeInt (this.generic);
490 buffer.writeInt (this.specific);
491 writeUint (buffer, ObjectType.TimeTicks,
492 this.upTime || Math.floor (process.uptime () * 100));
494 writeVarbinds (buffer, this.varbinds);
496 buffer.endSequence ();
499 var TrapV2Pdu = function () {
500 this.type = PduType.TrapV2;
501 TrapV2Pdu.super_.apply (this, arguments);
504 util.inherits (TrapV2Pdu, SimplePdu);
506 /*****************************************************************************
507 ** Message class definitions
510 var RequestMessage = function (version, community, pdu) {
511 this.version = version;
512 this.community = community;
516 RequestMessage.prototype.toBuffer = function () {
520 var writer = new ber.Writer ();
522 writer.startSequence ();
524 writer.writeInt (this.version);
525 writer.writeString (this.community);
527 this.pdu.toBuffer (writer);
529 writer.endSequence ();
531 this.buffer = writer.buffer;
536 var ResponseMessage = function (buffer) {
537 var reader = new ber.Reader (buffer);
539 reader.readSequence ();
541 this.version = reader.readInt ();
542 this.community = reader.readString ();
544 var type = reader.peek ();
546 if (type == PduType.GetResponse) {
547 this.pdu = new GetResponsePdu (reader);
549 throw new ResponseInvalidError ("Unknown PDU type '" + type
554 /*****************************************************************************
555 ** Session class definition
558 var Session = function (target, community, options) {
559 this.target = target || "127.0.0.1";
560 this.community = community || "public";
562 this.version = (options && options.version)
566 this.transport = (options && options.transport)
569 this.port = (options && options.port )
572 this.trapPort = (options && options.trapPort )
576 this.retries = (options && (options.retries || options.retries == 0))
579 this.timeout = (options && options.timeout)
583 this.sourceAddress = (options && options.sourceAddress )
584 ? options.sourceAddress
586 this.sourcePort = (options && options.sourcePort )
587 ? parseInt(options.sourcePort)
593 this.dgram = dgram.createSocket (this.transport);
597 this.dgram.on ("message", me.onMsg.bind (me));
598 this.dgram.on ("close", me.onClose.bind (me));
599 this.dgram.on ("error", me.onError.bind (me));
601 if (this.sourceAddress || this.sourcePort)
602 req.dgram.bind (this.sourcePort, this.sourceAddress);
605 util.inherits (Session, events.EventEmitter);
607 Session.prototype.close = function () {
612 Session.prototype.cancelRequests = function (error) {
613 for (id in this.reqs) {
614 var req = this.reqs[id];
615 this.unregisterRequest (req.id);
616 req.responseCb (error);
620 function _generateId () {
621 return Math.floor (Math.random () + Math.random () * 10000000)
624 Session.prototype.get = function (oids, responseCb) {
625 function feedCb (req, message) {
626 var pdu = message.pdu;
629 if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
630 req.responseCb (new ResponseInvalidError ("Requested OIDs do not "
631 + "match response OIDs"));
633 for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
634 if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
635 req.responseCb (new ResponseInvalidError ("OID '"
636 + req.message.pdu.varbinds[i].oid
637 + "' in request at positiion '" + i + "' does not "
638 + "match OID '" + pdu.varbinds[i].oid + "' in response "
639 + "at position '" + i + "'"));
642 varbinds.push (pdu.varbinds[i]);
646 req.responseCb (null, varbinds);
650 var pduVarbinds = [];
652 for (var i = 0; i < oids.length; i++) {
656 pduVarbinds.push (varbind);
659 this.simpleGet (GetRequestPdu, feedCb, pduVarbinds, responseCb);
664 Session.prototype.getBulk = function () {
665 var oids, nonRepeaters, maxRepetitions, responseCb;
667 if (arguments.length >= 4) {
669 nonRepeaters = arguments[1];
670 maxRepetitions = arguments[2];
671 responseCb = arguments[3];
672 } else if (arguments.length >= 3) {
674 nonRepeaters = arguments[1];
676 responseCb = arguments[2];
681 responseCb = arguments[1];
684 function feedCb (req, message) {
685 var pdu = message.pdu;
689 // first walk through and grab non-repeaters
690 if (pdu.varbinds.length < nonRepeaters) {
691 req.responseCb (new ResponseInvalidError ("Varbind count in "
692 + "response '" + pdu.varbinds.length + "' is less than "
693 + "non-repeaters '" + nonRepeaters + "' in request"));
695 for ( ; i < nonRepeaters; i++) {
696 if (isVarbindError (pdu.varbinds[i])) {
697 varbinds.push (pdu.varbinds[i]);
698 } else if (! oidFollowsOid (req.message.pdu.varbinds[i].oid,
699 pdu.varbinds[i].oid)) {
700 req.responseCb (new ResponseInvalidError ("OID '"
701 + req.message.pdu.varbinds[i].oid + "' in request at "
702 + "positiion '" + i + "' does not precede "
703 + "OID '" + pdu.varbinds[i].oid + "' in response "
704 + "at position '" + i + "'"));
707 varbinds.push (pdu.varbinds[i]);
712 var repeaters = req.message.pdu.varbinds.length - nonRepeaters;
714 // secondly walk through and grab repeaters
715 if (pdu.varbinds.length % (repeaters)) {
716 req.responseCb (new ResponseInvalidError ("Varbind count in "
717 + "response '" + pdu.varbinds.length + "' is not a "
718 + "multiple of repeaters '" + repeaters
719 + "' plus non-repeaters '" + nonRepeaters + "' in request"));
721 while (i < pdu.varbinds.length) {
722 for (var j = 0; j < repeaters; j++, i++) {
723 var reqIndex = nonRepeaters + j;
726 if (isVarbindError (pdu.varbinds[respIndex])) {
727 if (! varbinds[reqIndex])
728 varbinds[reqIndex] = [];
729 varbinds[reqIndex].push (pdu.varbinds[respIndex]);
730 } else if (! oidFollowsOid (
731 req.message.pdu.varbinds[reqIndex].oid,
732 pdu.varbinds[respIndex].oid)) {
733 req.responseCb (new ResponseInvalidError ("OID '"
734 + req.message.pdu.varbinds[reqIndex].oid
735 + "' in request at positiion '" + (reqIndex)
736 + "' does not precede OID '"
737 + pdu.varbinds[respIndex].oid
738 + "' in response at position '" + (respIndex) + "'"));
741 if (! varbinds[reqIndex])
742 varbinds[reqIndex] = [];
743 varbinds[reqIndex].push (pdu.varbinds[respIndex]);
749 req.responseCb (null, varbinds);
752 var pduVarbinds = [];
754 for (var i = 0; i < oids.length; i++) {
758 pduVarbinds.push (varbind);
762 nonRepeaters: nonRepeaters,
763 maxRepetitions: maxRepetitions
766 this.simpleGet (GetBulkRequestPdu, feedCb, pduVarbinds, responseCb,
772 Session.prototype.getNext = function (oids, responseCb) {
773 function feedCb (req, message) {
774 var pdu = message.pdu;
777 if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
778 req.responseCb (new ResponseInvalidError ("Requested OIDs do not "
779 + "match response OIDs"));
781 for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
782 if (isVarbindError (pdu.varbinds[i])) {
783 varbinds.push (pdu.varbinds[i]);
784 } else if (! oidFollowsOid (req.message.pdu.varbinds[i].oid,
785 pdu.varbinds[i].oid)) {
786 req.responseCb (new ResponseInvalidError ("OID '"
787 + req.message.pdu.varbinds[i].oid + "' in request at "
788 + "positiion '" + i + "' does not precede "
789 + "OID '" + pdu.varbinds[i].oid + "' in response "
790 + "at position '" + i + "'"));
793 varbinds.push (pdu.varbinds[i]);
797 req.responseCb (null, varbinds);
801 var pduVarbinds = [];
803 for (var i = 0; i < oids.length; i++) {
807 pduVarbinds.push (varbind);
810 this.simpleGet (GetNextRequestPdu, feedCb, pduVarbinds, responseCb);
815 Session.prototype.inform = function () {
816 var typeOrOid = arguments[0];;
817 var varbinds, options = {}, responseCb;
820 ** Support the following signatures:
822 ** typeOrOid, varbinds, options, callback
823 ** typeOrOid, varbinds, callback
824 ** typeOrOid, options, callback
825 ** typeOrOid, callback
827 if (arguments.length >= 4) {
828 varbinds = arguments[1];
829 options = arguments[2];
830 responseCb = arguments[3];
831 } else if (arguments.length >= 3) {
832 if (arguments[1].constructor != Array) {
834 options = arguments[1];
835 responseCb = arguments[2];
837 varbinds = arguments[1];
838 responseCb = arguments[2];
842 responseCb = arguments[1];
845 function feedCb (req, message) {
846 var pdu = message.pdu;
849 if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
850 req.responseCb (new ResponseInvalidError ("Inform OIDs do not "
851 + "match response OIDs"));
853 for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
854 if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
855 req.responseCb (new ResponseInvalidError ("OID '"
856 + req.message.pdu.varbinds[i].oid
857 + "' in inform at positiion '" + i + "' does not "
858 + "match OID '" + pdu.varbinds[i].oid + "' in response "
859 + "at position '" + i + "'"));
862 varbinds.push (pdu.varbinds[i]);
866 req.responseCb (null, varbinds);
870 if (typeof typeOrOid != "string")
871 typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
875 oid: "1.3.6.1.2.1.1.3.0",
876 type: ObjectType.TimeTicks,
877 value: options.upTime || Math.floor (process.uptime () * 100)
880 oid: "1.3.6.1.6.3.1.1.4.1.0",
881 type: ObjectType.OID,
886 for (var i = 0; i < varbinds.length; i++) {
888 oid: varbinds[i].oid,
889 type: varbinds[i].type,
890 value: varbinds[i].value
892 pduVarbinds.push (varbind);
895 options.port = this.trapPort;
897 this.simpleGet (InformRequestPdu, feedCb, pduVarbinds, responseCb, options);
902 Session.prototype.onClose = function () {
903 this.cancelRequests (new Error ("Socket forcibly closed"));
907 Session.prototype.onError = function (error) {
911 Session.prototype.onMsg = function (buffer, remote) {
913 var message = new ResponseMessage (buffer);
915 var req = this.unregisterRequest (message.pdu.id);
920 if (message.version != req.message.version) {
921 req.responseCb (new ResponseInvalidError ("Version in request '"
922 + req.message.version + "' does not match version in "
923 + "response '" + message.version));
924 } else if (message.community != req.message.community) {
925 req.responseCb (new ResponseInvalidError ("Community '"
926 + req.message.community + "' in request does not match "
927 + "community '" + message.community + "' in response"));
928 } else if (message.pdu.type == PduType.GetResponse) {
929 req.onResponse (req, message);
931 req.responseCb (new ResponseInvalidError ("Unknown PDU type '"
932 + message.pdu.type + "' in response"));
935 req.responseCb (error);
938 this.emit("error", error);
942 Session.prototype.onSimpleGetResponse = function (req, message) {
943 var pdu = message.pdu;
945 if (pdu.errorStatus > 0) {
946 var statusString = ErrorStatus[pdu.errorStatus]
947 || ErrorStatus.GeneralError;
948 var statusCode = ErrorStatus[statusString]
949 || ErrorStatus[ErrorStatus.GeneralError];
951 if (pdu.errorIndex <= 0 || pdu.errorIndex > pdu.varbinds.length) {
952 req.responseCb (new RequestFailedError (statusString, statusCode));
954 var oid = pdu.varbinds[pdu.errorIndex - 1].oid;
955 var error = new RequestFailedError (statusString + ": " + oid,
957 req.responseCb (error);
960 req.feedCb (req, message);
964 Session.prototype.registerRequest = function (req) {
965 if (! this.reqs[req.id]) {
966 this.reqs[req.id] = req;
967 if (this.reqCount <= 0)
972 req.timer = setTimeout (function () {
973 if (req.retries-- > 0) {
976 me.unregisterRequest (req.id);
977 req.responseCb (new RequestTimedOutError (
978 "Request timed out"));
983 Session.prototype.send = function (req, noWait) {
987 var buffer = req.message.toBuffer ();
989 this.dgram.send (buffer, 0, buffer.length, req.port, this.target,
990 function (error, bytes) {
992 req.responseCb (error);
995 req.responseCb (null);
997 me.registerRequest (req);
1002 req.responseCb (error);
1008 Session.prototype.set = function (varbinds, responseCb) {
1009 function feedCb (req, message) {
1010 var pdu = message.pdu;
1013 if (req.message.pdu.varbinds.length != pdu.varbinds.length) {
1014 req.responseCb (new ResponseInvalidError ("Requested OIDs do not "
1015 + "match response OIDs"));
1017 for (var i = 0; i < req.message.pdu.varbinds.length; i++) {
1018 if (req.message.pdu.varbinds[i].oid != pdu.varbinds[i].oid) {
1019 req.responseCb (new ResponseInvalidError ("OID '"
1020 + req.message.pdu.varbinds[i].oid
1021 + "' in request at positiion '" + i + "' does not "
1022 + "match OID '" + pdu.varbinds[i].oid + "' in response "
1023 + "at position '" + i + "'"));
1026 varbinds.push (pdu.varbinds[i]);
1030 req.responseCb (null, varbinds);
1034 var pduVarbinds = [];
1036 for (var i = 0; i < varbinds.length; i++) {
1038 oid: varbinds[i].oid,
1039 type: varbinds[i].type,
1040 value: varbinds[i].value
1042 pduVarbinds.push (varbind);
1045 this.simpleGet (SetRequestPdu, feedCb, pduVarbinds, responseCb);
1050 Session.prototype.simpleGet = function (pduClass, feedCb, varbinds,
1051 responseCb, options) {
1055 var id = _generateId ();
1056 var pdu = new pduClass (id, varbinds, options);
1057 var message = new RequestMessage (this.version, this.community, pdu);
1062 responseCb: responseCb,
1063 retries: this.retries,
1064 timeout: this.timeout,
1065 onResponse: this.onSimpleGetResponse,
1067 port: (options && options.port) ? options.port : this.port
1073 req.responseCb (error);
1077 function subtreeCb (req, varbinds) {
1080 for (var i = varbinds.length; i > 0; i--) {
1081 if (! oidInSubtree (req.baseOid, varbinds[i - 1].oid)) {
1087 if (varbinds.length > 0)
1088 req.feedCb (varbinds);
1094 Session.prototype.subtree = function () {
1096 var oid = arguments[0];
1097 var maxRepetitions, feedCb, doneCb;
1099 if (arguments.length < 4) {
1100 maxRepetitions = 20;
1101 feedCb = arguments[1];
1102 doneCb = arguments[2];
1104 maxRepetitions = arguments[1];
1105 feedCb = arguments[2];
1106 doneCb = arguments[3];
1112 maxRepetitions: maxRepetitions,
1116 this.walk (oid, maxRepetitions, subtreeCb.bind (me, req), doneCb);
1121 function tableColumnsResponseCb (req, error) {
1123 req.responseCb (error);
1124 } else if (req.error) {
1125 req.responseCb (req.error);
1127 if (req.columns.length > 0) {
1128 var column = req.columns.pop ();
1130 this.subtree (req.rowOid + column, req.maxRepetitions,
1131 tableColumnsFeedCb.bind (me, req),
1132 tableColumnsResponseCb.bind (me, req));
1134 req.responseCb (null, req.table);
1139 function tableColumnsFeedCb (req, varbinds) {
1140 for (var i = 0; i < varbinds.length; i++) {
1141 if (isVarbindError (varbinds[i])) {
1142 req.error = new RequestFailedError (varbindError (varbind[i]));
1146 var oid = varbinds[i].oid.replace (req.rowOid, "")
1147 if (oid && oid != varbinds[i].oid) {
1148 var match = oid.match (/^(\d+)\.(.+)$/);
1149 if (match && match[1] > 0) {
1150 if (! req.table[match[2]])
1151 req.table[match[2]] = {};
1152 req.table[match[2]][match[1]] = varbinds[i].value;
1158 Session.prototype.tableColumns = function () {
1161 var oid = arguments[0];
1162 var columns = arguments[1];
1163 var maxRepetitions, responseCb;
1165 if (arguments.length < 4) {
1166 responseCb = arguments[2];
1167 maxRepetitions = 20;
1169 maxRepetitions = arguments[2];
1170 responseCb = arguments[3];
1174 responseCb: responseCb,
1175 maxRepetitions: maxRepetitions,
1177 rowOid: oid + ".1.",
1178 columns: columns.slice(0),
1182 if (req.columns.length > 0) {
1183 var column = req.columns.pop ();
1184 this.subtree (req.rowOid + column, maxRepetitions,
1185 tableColumnsFeedCb.bind (me, req),
1186 tableColumnsResponseCb.bind (me, req));
1192 function tableResponseCb (req, error) {
1194 req.responseCb (error);
1196 req.responseCb (req.error);
1198 req.responseCb (null, req.table);
1201 function tableFeedCb (req, varbinds) {
1202 for (var i = 0; i < varbinds.length; i++) {
1203 if (isVarbindError (varbinds[i])) {
1204 req.error = new RequestFailedError (varbindError (varbind[i]));
1208 var oid = varbinds[i].oid.replace (req.rowOid, "")
1209 if (oid && oid != varbinds[i].oid) {
1210 var match = oid.match (/^(\d+)\.(.+)$/);
1211 if (match && match[1] > 0) {
1212 if (! req.table[match[2]])
1213 req.table[match[2]] = {};
1214 req.table[match[2]][match[1]] = varbinds[i].value;
1220 Session.prototype.table = function () {
1223 var oid = arguments[0];
1224 var maxRepetitions, responseCb;
1226 if (arguments.length < 3) {
1227 responseCb = arguments[1];
1228 maxRepetitions = 20;
1230 maxRepetitions = arguments[1];
1231 responseCb = arguments[2];
1235 responseCb: responseCb,
1236 maxRepetitions: maxRepetitions,
1238 rowOid: oid + ".1.",
1242 this.subtree (oid, maxRepetitions, tableFeedCb.bind (me, req),
1243 tableResponseCb.bind (me, req));
1248 Session.prototype.trap = function () {
1252 var typeOrOid = arguments[0];
1253 var varbinds, options = {}, responseCb;
1256 ** Support the following signatures:
1258 ** typeOrOid, varbinds, options, callback
1259 ** typeOrOid, varbinds, agentAddr, callback
1260 ** typeOrOid, varbinds, callback
1261 ** typeOrOid, agentAddr, callback
1262 ** typeOrOid, options, callback
1263 ** typeOrOid, callback
1265 if (arguments.length >= 4) {
1266 varbinds = arguments[1];
1267 if (typeof arguments[2] == "string") {
1268 options.agentAddr = arguments[2];
1269 } else if (arguments[2].constructor != Array) {
1270 options = arguments[2];
1272 responseCb = arguments[3];
1273 } else if (arguments.length >= 3) {
1274 if (typeof arguments[1] == "string") {
1276 options.agentAddr = arguments[1];
1277 } else if (arguments[1].constructor != Array) {
1279 options = arguments[1];
1281 varbinds = arguments[1];
1284 responseCb = arguments[2];
1287 responseCb = arguments[1];
1290 var pdu, pduVarbinds = [];
1292 for (var i = 0; i < varbinds.length; i++) {
1294 oid: varbinds[i].oid,
1295 type: varbinds[i].type,
1296 value: varbinds[i].value
1298 pduVarbinds.push (varbind);
1301 var id = _generateId ();
1303 if (this.version == Version2c) {
1304 if (typeof typeOrOid != "string")
1305 typeOrOid = "1.3.6.1.6.3.1.1.5." + (typeOrOid + 1);
1307 pduVarbinds.unshift (
1309 oid: "1.3.6.1.2.1.1.3.0",
1310 type: ObjectType.TimeTicks,
1311 value: options.upTime || Math.floor (process.uptime () * 100)
1314 oid: "1.3.6.1.6.3.1.1.4.1.0",
1315 type: ObjectType.OID,
1320 pdu = new TrapV2Pdu (id, pduVarbinds, options);
1322 pdu = new TrapPdu (typeOrOid, pduVarbinds, options);
1325 var message = new RequestMessage (this.version, this.community, pdu);
1330 responseCb: responseCb,
1334 this.send (req, true);
1337 req.responseCb (error);
1343 Session.prototype.unregisterRequest = function (id) {
1344 var req = this.reqs[id];
1346 delete this.reqs[id];
1347 clearTimeout (req.timer);
1350 if (this.reqCount <= 0)
1358 function walkCb (req, error, varbinds) {
1363 if (error instanceof RequestFailedError) {
1364 if (error.status != ErrorStatus.NoSuchName) {
1368 // signal the version 1 walk code below that it should stop
1377 if (this.version == Version2c) {
1378 for (var i = varbinds[0].length; i > 0; i--) {
1379 if (varbinds[0][i - 1].type == ObjectType.EndOfMibView) {
1384 if (req.feedCb (varbinds[0]))
1387 oid = varbinds[0][varbinds[0].length - 1].oid;
1390 if (req.feedCb (varbinds)) {
1393 oid = varbinds[0].oid;
1401 this.walk (oid, req.maxRepetitions, req.feedCb, req.doneCb,
1405 Session.prototype.walk = function () {
1407 var oid = arguments[0];
1408 var maxRepetitions, feedCb, doneCb, baseOid;
1410 if (arguments.length < 4) {
1411 maxRepetitions = 20;
1412 feedCb = arguments[1];
1413 doneCb = arguments[2];
1415 maxRepetitions = arguments[1];
1416 feedCb = arguments[2];
1417 doneCb = arguments[3];
1421 maxRepetitions: maxRepetitions,
1426 if (this.version == Version2c)
1427 this.getBulk ([oid], 0, maxRepetitions,
1428 walkCb.bind (me, req));
1430 this.getNext ([oid], walkCb.bind (me, req));
1435 /*****************************************************************************
1439 exports.Session = Session;
1441 exports.createSession = function (target, community, version, options) {
1442 return new Session (target, community, version, options);
1445 exports.isVarbindError = isVarbindError;
1446 exports.varbindError = varbindError;
1448 exports.Version1 = Version1;
1449 exports.Version2c = Version2c;
1451 exports.ErrorStatus = ErrorStatus;
1452 exports.TrapType = TrapType;
1453 exports.ObjectType = ObjectType;
1455 exports.ResponseInvalidError = ResponseInvalidError;
1456 exports.RequestInvalidError = RequestInvalidError;
1457 exports.RequestFailedError = RequestFailedError;
1458 exports.RequestTimedOutError = RequestTimedOutError;
1461 ** We've added this for testing.
1463 exports.ObjectParser = {