net-snmp 3.20.1 → 3.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1663,7 +1663,7 @@ an object, possibly empty, and can contain the following fields:
1663
1663
  * `sockets` - an array of objects containing triples of `transport`, `address` and `port` that
1664
1664
  can be used to specify multiple socket listeners. This option overrides any individual
1665
1665
  `transport`, `address` and `port` options.
1666
- * `mibOptions` - an MIB options object that is passed to the `Mib` instance - see the MIB section
1666
+ * `mibOptions` - a MIB options object that is passed to the `Mib` instance - see the MIB section
1667
1667
  for further details on this - defaults to the empty object.
1668
1668
 
1669
1669
  The `mib` parameter is optional, and sets the agent's singleton `Mib` instance.
@@ -2689,7 +2689,9 @@ var options = {
2689
2689
  master: "localhost",
2690
2690
  masterPort: 705,
2691
2691
  timeout: 0,
2692
- description: "Node net-snmp AgentX sub-agent"
2692
+ description: "Node net-snmp AgentX sub-agent",
2693
+ mibOptions: {},
2694
+ mib: undefined
2693
2695
  };
2694
2696
 
2695
2697
  subagent = snmp.createSubagent (options);
@@ -2704,6 +2706,12 @@ The `options` parameter is a mandatory object, possibly empty, and can contain t
2704
2706
  * `timeout` - set the session-wide timeout on the master agent - defaults to 0, which
2705
2707
  means no session-wide timeout is set.
2706
2708
  * `description` - a textual description of the subagent.
2709
+ * `mibOptions` - n MIB options object that is passed to the `Mib` instance - see the MIB section
2710
+ for further details on this - defaults to the empty object.
2711
+ * `mib` - sets the agent's singleton `Mib` instance. If not supplied, the agent creates itself
2712
+ a new empty `Mib` singleton. If supplied, the `Mib` instance needs to be created and populated as
2713
+ per the [Mib Module](#mib-module) section.
2714
+
2707
2715
 
2708
2716
  ## subagent.getMib ()
2709
2717
 
@@ -3509,6 +3517,14 @@ Example programs are included under the module's `example` directory.
3509
3517
 
3510
3518
  * Update documentation with compatibility note on DES and recent Node.js versions
3511
3519
 
3520
+ # Version 3.21.0 - 26/04/2025
3521
+
3522
+ * Add AgentX subagent mib and mibOptions on initialization
3523
+
3524
+ # Version 3.21.1 - 26/04/2025
3525
+
3526
+ * Add better defval type handling, improved debug handling and simple agent example
3527
+
3512
3528
  # License
3513
3529
 
3514
3530
  Copyright (c) 2020 Mark Abrahams <mark@abrahams.co.nz>
@@ -0,0 +1,44 @@
1
+ const snmp = require ("..");
2
+
3
+ // Create a module store, which contains SNMPv2-MIB and other modules
4
+ const store = snmp.createModuleStore ();
5
+
6
+ // Fetch MIB providers for SNMPv2-MIB, create an agent, and register the providers with the agent's MIB
7
+ const providers = store.getProvidersForModule ("SNMPv2-MIB");
8
+ const agentOptions = {
9
+ port: 1161
10
+ };
11
+ const agentCallback = function (error, data) {
12
+ if ( error ) {
13
+ console.error (error);
14
+ } else {
15
+ console.log (JSON.stringify(data, null, 2));
16
+ }
17
+ };
18
+ const agent = snmp.createAgent ( agentOptions, agentCallback );
19
+ const mib = agent.getMib ();
20
+ mib.registerProviders (providers);
21
+
22
+ // Add community and user to agent's authorizer
23
+ const authorizer = agent.getAuthorizer ();
24
+ authorizer.addCommunity ("private");
25
+ authorizer.addUser ({
26
+ name: "wilma",
27
+ level: snmp.SecurityLevel.authPriv,
28
+ authProtocol: snmp.AuthProtocols.sha,
29
+ authKey: "illhavesomeauth",
30
+ privProtocol: snmp.PrivProtocols.aes,
31
+ privKey: "andsomepriv"
32
+ });
33
+
34
+ // Start adding data to the MIB through the registered providers using the `Mib` API calls
35
+ mib.setScalarValue ("sysDescr", "The most powerful system you can think of");
36
+ mib.setScalarValue ("sysObjectID", "1.3.6.1.4.1.8072.3.2.10");
37
+ mib.setScalarValue ("sysContact", "You");
38
+ mib.setScalarValue ("sysName", "megamind");
39
+ mib.setScalarValue ("sysLocation", "Yours");
40
+ mib.setScalarValue ("sysORLastChange", 710);
41
+ mib.addTableRow ("sysOREntry", [1, "1.3.6.1.4.1.47491.42.43.44.45", "I've dreamed up this MIB", 20]);
42
+
43
+ // Dump the resulting MIB to the console
44
+ mib.dump ();
@@ -264,7 +264,7 @@ mib.dump ({
264
264
  // var data = mib.getTableColumnCells ("ifTable", 2);
265
265
  // var data = mib.getTableRowCells ("ifTable", [1]);
266
266
  // mib.setTableSingleCell ("ifTable", 2, [2], "changed!");
267
- mib.setTableSingleCell ("ifTable", 3, [2], 99);
267
+ mib.setTableSingleCell ("ifTable", 3, [2], 24);
268
268
  var data = mib.getTableSingleCell ("ifTable", 3, [2]);
269
269
  // var data = mib.getScalarValue ("sysDescr");
270
270
 
package/index.js CHANGED
@@ -21,9 +21,13 @@ const MAX_UNSIGNED_INT64 = 18446744073709551615;
21
21
 
22
22
  const DES_IMPLEMENTATION = 'library';
23
23
 
24
- function debug (line) {
24
+ // Use test harness trace or debug functions falling back to console.debug in normal mode.
25
+ // eslint-disable-next-line no-undef
26
+ var debugfn = typeof(global.debug) === 'function'? global.trace ?? global.debug : console.debug;
27
+
28
+ function debug () {
25
29
  if ( DEBUG ) {
26
- console.debug (line);
30
+ debugfn.apply (this, arguments);
27
31
  }
28
32
  }
29
33
 
@@ -1400,6 +1404,7 @@ Encryption.encryptPduDes = function (scopedPdu, privProtocol, privPassword, auth
1400
1404
  iv[i] = preIv[i] ^ salt[i];
1401
1405
  }
1402
1406
 
1407
+
1403
1408
  if (scopedPdu.length % des.BLOCK_LENGTH == 0) {
1404
1409
  paddedScopedPdu = scopedPdu;
1405
1410
  } else {
@@ -2030,7 +2035,7 @@ var Session = function (target, authenticator, options) {
2030
2035
  ? options.reportOidMismatchErrors
2031
2036
  : false;
2032
2037
 
2033
- DEBUG = options.debug;
2038
+ DEBUG |= options.debug;
2034
2039
 
2035
2040
  this.engine = new Engine ({
2036
2041
  engineId: options.engineID
@@ -2040,7 +2045,7 @@ var Session = function (target, authenticator, options) {
2040
2045
 
2041
2046
  this.dgram = dgram.createSocket (this.transport);
2042
2047
  this.dgram.unref();
2043
-
2048
+
2044
2049
  var me = this;
2045
2050
  this.dgram.on ("message", me.onMsg.bind (me));
2046
2051
  this.dgram.on ("close", me.onClose.bind (me));
@@ -2268,7 +2273,7 @@ Session.prototype.inform = function () {
2268
2273
 
2269
2274
  /**
2270
2275
  ** Support the following signatures:
2271
- **
2276
+ **
2272
2277
  ** typeOrOid, varbinds, options, callback
2273
2278
  ** typeOrOid, varbinds, callback
2274
2279
  ** typeOrOid, options, callback
@@ -2346,7 +2351,7 @@ Session.prototype.inform = function () {
2346
2351
  };
2347
2352
  pduVarbinds.push (varbind);
2348
2353
  }
2349
-
2354
+
2350
2355
  options.port = this.trapPort;
2351
2356
 
2352
2357
  this.simpleGet (InformRequestPdu, feedCb, pduVarbinds, responseCb, options);
@@ -2466,7 +2471,7 @@ Session.prototype.registerRequest = function (req) {
2466
2471
  Session.prototype.send = function (req, noWait) {
2467
2472
  try {
2468
2473
  var me = this;
2469
-
2474
+
2470
2475
  var buffer = req.message.toBuffer ();
2471
2476
 
2472
2477
  this.dgram.send (buffer, 0, buffer.length, req.port, this.target,
@@ -2484,7 +2489,7 @@ Session.prototype.send = function (req, noWait) {
2484
2489
  } catch (error) {
2485
2490
  req.responseCb (error);
2486
2491
  }
2487
-
2492
+
2488
2493
  return this;
2489
2494
  };
2490
2495
 
@@ -2736,7 +2741,7 @@ Session.prototype.trap = function () {
2736
2741
 
2737
2742
  /**
2738
2743
  ** Support the following signatures:
2739
- **
2744
+ **
2740
2745
  ** typeOrOid, varbinds, options, callback
2741
2746
  ** typeOrOid, varbinds, agentAddr, callback
2742
2747
  ** typeOrOid, varbinds, callback
@@ -2779,7 +2784,7 @@ Session.prototype.trap = function () {
2779
2784
  };
2780
2785
  pduVarbinds.push (varbind);
2781
2786
  }
2782
-
2787
+
2783
2788
  var id = _generateId (this.idBitsSize);
2784
2789
 
2785
2790
  if (this.version == Version2c || this.version == Version3 ) {
@@ -3070,7 +3075,7 @@ Listener.prototype.startListening = function () {
3070
3075
 
3071
3076
  Listener.prototype.send = function (message, rinfo, socket) {
3072
3077
  // var me = this;
3073
-
3078
+
3074
3079
  var buffer = message.toBuffer ();
3075
3080
 
3076
3081
  socket.send (buffer, 0, buffer.length, rinfo.port, rinfo.address, function (error, bytes) {
@@ -3347,7 +3352,7 @@ SimpleAccessControlModel.prototype.isAccessAllowed = function (securityModel, se
3347
3352
  **/
3348
3353
 
3349
3354
  var Receiver = function (options, callback) {
3350
- DEBUG = options.debug;
3355
+ DEBUG |= options.debug;
3351
3356
  this.authorizer = new Authorizer (options);
3352
3357
  this.engine = new Engine ({
3353
3358
  engineId: options.engineID
@@ -3678,7 +3683,7 @@ ModuleStore.prototype.getProvidersForModule = function (moduleName) {
3678
3683
  // (See lib/mibs/SNMPv2-TC.mib#L186.)
3679
3684
  if ( syntax == "RowStatus" &&
3680
3685
  "IMPORTS" in mibModule &&
3681
- Array.isArray(mibModule.IMPORTS["SNMPv2-TC"]) &&
3686
+ Array.isArray(mibModule.IMPORTS["SNMPv2-TC"]) &&
3682
3687
  mibModule.IMPORTS["SNMPv2-TC"].includes("RowStatus") ) {
3683
3688
 
3684
3689
  // Mark this column as being rowStatus
@@ -4027,7 +4032,7 @@ MibNode.prototype.getNextInstanceNode = function () {
4027
4032
  childrenAddresses = Object.keys (node.children).sort ( (a, b) => a - b);
4028
4033
  node = node.children[childrenAddresses[0]];
4029
4034
  if ( ! node ) {
4030
- // unexpected
4035
+ // unexpected
4031
4036
  return null;
4032
4037
  }
4033
4038
  }
@@ -4271,7 +4276,8 @@ Mib.prototype.registerProvider = function (provider) {
4271
4276
  if ( provider.constraints?.enumeration ) {
4272
4277
  scalarValue = ObjectTypeUtil.getEnumerationNumberFromName (provider.constraints.enumeration, provider.defVal);
4273
4278
  } else {
4274
- scalarValue = provider.defVal;
4279
+ // Remove quotes from strings and resolve numeric strings to integers
4280
+ scalarValue = JSON.parse(provider.defVal);
4275
4281
  }
4276
4282
  this.setScalarValue (provider.name, scalarValue);
4277
4283
  }
@@ -4690,7 +4696,7 @@ Mib.prototype.getTableCells = function (table, byRows, includeInstances) {
4690
4696
  } else {
4691
4697
  return data;
4692
4698
  }
4693
-
4699
+
4694
4700
  };
4695
4701
 
4696
4702
  Mib.prototype.getTableSingleCell = function (table, columnNumber, rowIndex) {
@@ -4864,7 +4870,7 @@ MibRequest.prototype.isTabular = function () {
4864
4870
  };
4865
4871
 
4866
4872
  var Agent = function (options, callback, mib) {
4867
- DEBUG = options.debug;
4873
+ DEBUG |= options.debug;
4868
4874
  this.listener = new Listener (options, this);
4869
4875
  this.engine = new Engine ({
4870
4876
  engineId: options.engineID
@@ -4985,7 +4991,7 @@ Agent.prototype.onMsg = function (socket, buffer, rinfo) {
4985
4991
  }
4986
4992
 
4987
4993
  // Request processing
4988
- // debug (JSON.stringify (message.pdu, null, 2));
4994
+ debug (message.pdu);
4989
4995
  if ( message.pdu.contextName && message.pdu.contextName != "" ) {
4990
4996
  this.onProxyRequest (socket, message, rinfo);
4991
4997
  } else if ( message.pdu.type == PduType.GetRequest ) {
@@ -5088,7 +5094,7 @@ Agent.prototype.tryCreateInstance = function (varbind, requestType) {
5088
5094
  typeof rowStatusColumn == "number" &&
5089
5095
  column === rowStatusColumn ) {
5090
5096
 
5091
- if ( (varbind.value === RowStatus["createAndGo"] || varbind.value === RowStatus["createAndWait"]) &&
5097
+ if ( (varbind.value === RowStatus["createAndGo"] || varbind.value === RowStatus["createAndWait"]) &&
5092
5098
  provider.createHandler !== null ) {
5093
5099
 
5094
5100
  // The create handler will return an array
@@ -5572,7 +5578,7 @@ Agent.prototype.getBulkRequest = function (socket, requestMessage, rinfo) {
5572
5578
  }
5573
5579
 
5574
5580
  if ( requestPdu.nonRepeaters < requestVarbinds.length ) {
5575
-
5581
+
5576
5582
  for (var v = requestPdu.nonRepeaters ; v < requestVarbinds.length ; v++ ) {
5577
5583
  startOid.push (requestVarbinds[v].oid);
5578
5584
  }
@@ -5784,7 +5790,7 @@ AgentXPdu.prototype.readHeader = function (buffer) {
5784
5790
  this.version = buffer.readUInt8 ();
5785
5791
  this.pduType = buffer.readUInt8 ();
5786
5792
  this.flags = buffer.readUInt8 ();
5787
- buffer.readUInt8 (); // reserved byte
5793
+ buffer.readUInt8 (); // reserved byte
5788
5794
  this.sessionID = buffer.readUInt32BE ();
5789
5795
  this.transactionID = buffer.readUInt32BE ();
5790
5796
  this.packetID = buffer.readUInt32BE ();
@@ -5854,7 +5860,7 @@ AgentXPdu.createFromVariables = function (vars) {
5854
5860
  + "' in created PDU");
5855
5861
 
5856
5862
  }
5857
-
5863
+
5858
5864
  return pdu;
5859
5865
  };
5860
5866
 
@@ -6089,7 +6095,7 @@ AgentXPdu.packetID = 1;
6089
6095
 
6090
6096
  var Subagent = function (options) {
6091
6097
  DEBUG = options.debug;
6092
- this.mib = new Mib ();
6098
+ this.mib = options?.mib ?? new Mib (options?.mibOptions);
6093
6099
  this.master = options.master || 'localhost';
6094
6100
  this.masterPort = options.masterPort || 705;
6095
6101
  this.timeout = options.timeout || 0;
package/lib/mib.js CHANGED
@@ -655,7 +655,7 @@ const MIB = function (dir) {
655
655
  if (unresolvedObjects.length > 0) {
656
656
  for (const unresolved of unresolvedObjects) {
657
657
  const obj = this.Modules[ModuleName][unresolved];
658
-
658
+
659
659
  const { oidString, nameString, unresolvedObject } = this.getOidAndNamePaths(obj['OBJECT IDENTIFIER'], unresolved, ModuleName);
660
660
  this.Modules[ModuleName][unresolved].NameSpace = nameString;
661
661
  this.Modules[ModuleName][unresolved].OID = oidString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "net-snmp",
3
- "version": "3.20.1",
3
+ "version": "3.21.1",
4
4
  "description": "JavaScript implementation of the Simple Network Management Protocol (SNMP)",
5
5
  "main": "index.js",
6
6
  "directories": {
package/test/mib.test.js CHANGED
@@ -122,7 +122,7 @@ describe('MIB', function () {
122
122
  };
123
123
  mib = snmp.createMib(options);
124
124
  mib.registerProviders(mibProviders);
125
- assert.equal(mib.getScalarValue('testScalarIntegerDefval'), 49);
125
+ assert.strictEqual(mib.getScalarValue('testScalarIntegerDefval'), 49);
126
126
  });
127
127
  });
128
128