net-snmp 3.22.0 → 3.23.0

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.cn.md CHANGED
@@ -100,6 +100,7 @@ var session = snmp.createSession ("127.0.0.1", "public", options);
100
100
  * `version` - `snmp.Version1`或`snmp.Version2c`,默认为`snmp.Version1`。
101
101
  * "backwardsGetNexts"----允许进行GetNext操作的布尔值,以检索词法上在前的OIDs。
102
102
  * `idBitsSize` - `16`或`32`,默认为`32`。用来减少生成的id的大小,以便与一些旧设备兼容。
103
+ * `dgramModule` – 一个与 Node.js [`dgram`](https://nodejs.org/api/dgram.html) 模块接口兼容的模块。您可以通过提供自定义或包装的 `dgram` 实现来扩展或覆盖默认的 UDP 套接字行为。
103
104
 
104
105
  当一个会话结束后,应该关闭它。
105
106
 
package/README.md CHANGED
@@ -344,7 +344,7 @@ Actions
344
344
  - `4 - ECouldNotDecrypt`
345
345
  - `5 - EAuthFailure`
346
346
  - `6 - EReqResOidNoMatch`
347
- - `7 - (no longer used)
347
+ - `7 - (no longer used)`
348
348
  - `8 - EOutOfOrder`
349
349
  - `9 - EVersionNoMatch`
350
350
  - `10 - ECommunityNoMatch`
@@ -588,7 +588,8 @@ var options = {
588
588
  version: snmp.Version1,
589
589
  backwardsGetNexts: true,
590
590
  reportOidMismatchErrors: false,
591
- idBitsSize: 32
591
+ idBitsSize: 32,
592
+ dgramModule: dgram
592
593
  };
593
594
 
594
595
  var session = snmp.createSession ("127.0.0.1", "public", options);
@@ -620,6 +621,10 @@ is an object, and can contain the following items:
620
621
  requests and responses, defaults to `false`
621
622
  * `idBitsSize` - Either `16` or `32`, defaults to `32`. Used to reduce the size
622
623
  of the generated id for compatibility with some older devices.
624
+ * `dgramModule` – A module that is interface-compatible with the Node.js [`dgram`](https://nodejs.org/api/dgram.html) module.
625
+ This can be used to extend or override the default UDP socket behavior by supplying
626
+ a custom or wrapped implementation of `dgram`.
627
+
623
628
 
624
629
  When a session has been finished with it should be closed:
625
630
 
@@ -3556,6 +3561,10 @@ Example programs are included under the module's `example` directory.
3556
3561
 
3557
3562
  * Add custom base module list
3558
3563
 
3564
+ # Version 3.23.0 - 20/06/2025
3565
+
3566
+ * Add support for custom dgram module
3567
+
3559
3568
  # License
3560
3569
 
3561
3570
  Copyright (c) 2020 Mark Abrahams <mark@abrahams.co.nz>
package/index.js CHANGED
@@ -2042,7 +2042,8 @@ var Session = function (target, authenticator, options) {
2042
2042
  this.reqs = {};
2043
2043
  this.reqCount = 0;
2044
2044
 
2045
- this.dgram = dgram.createSocket (this.transport);
2045
+ const dgramMod = options.dgramModule || dgram;
2046
+ this.dgram = dgramMod.createSocket (this.transport);
2046
2047
  this.dgram.unref();
2047
2048
 
2048
2049
  var me = this;
@@ -3034,10 +3035,8 @@ Engine.prototype.generateEngineID = function() {
3034
3035
  var Listener = function (options, receiver) {
3035
3036
  this.receiver = receiver;
3036
3037
  this.callback = receiver.onMsg;
3037
- // this.transport = options.transport || 'udp4';
3038
- // this.port = options.port || 161;
3039
- // this.address = options.address;
3040
3038
  this.disableAuthorization = options.disableAuthorization || false;
3039
+ this.dgramModule = options.dgramModule || dgram;
3041
3040
  if ( options.sockets ) {
3042
3041
  this.socketOptions = options.sockets;
3043
3042
  } else {
@@ -3060,7 +3059,8 @@ Listener.prototype.startListening = function () {
3060
3059
  var me = this;
3061
3060
  this.sockets = {};
3062
3061
  for ( const socketOptions of this.socketOptions ) {
3063
- const socket = dgram.createSocket (socketOptions.transport);
3062
+ const dgramMod = this.dgramModule;
3063
+ const socket = dgramMod.createSocket (socketOptions.transport);
3064
3064
  socket.on ("error", me.receiver.callback);
3065
3065
  socket.bind (socketOptions.port, socketOptions.address);
3066
3066
  socket.on ("message", me.callback.bind (me.receiver, socket));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "net-snmp",
3
- "version": "3.22.0",
3
+ "version": "3.23.0",
4
4
  "description": "JavaScript implementation of the Simple Network Management Protocol (SNMP)",
5
5
  "author": "Mark Abrahams <mark@abrahams.co.nz>",
6
6
  "license": "MIT",
@@ -0,0 +1,71 @@
1
+ const assert = require('assert');
2
+ const snmp = require('../');
3
+
4
+ describe('Custom dgram module support', function () {
5
+ it('should use custom dgram module in Session', function (done) {
6
+ let createSocketCalled = false;
7
+ const mockDgram = {
8
+ createSocket: function (transport) {
9
+ createSocketCalled = true;
10
+ assert.equal(transport, 'udp4');
11
+
12
+ // Return a mock socket
13
+ return {
14
+ unref: function () {},
15
+ on: function () {},
16
+ bind: function () {},
17
+ close: function () {}
18
+ };
19
+ }
20
+ };
21
+
22
+ const session = snmp.createSession('127.0.0.1', 'public', {
23
+ dgramModule: mockDgram
24
+ });
25
+
26
+ assert(createSocketCalled, 'Custom dgram module createSocket should have been called');
27
+ session.close();
28
+ done();
29
+ });
30
+
31
+ it('should use custom dgram module in Receiver', function (done) {
32
+ let createSocketCalled = false;
33
+ const mockDgram = {
34
+ createSocket: function (transport) {
35
+ createSocketCalled = true;
36
+ assert.equal(transport, 'udp4');
37
+
38
+ // Return a mock socket
39
+ return {
40
+ on: function () {},
41
+ bind: function () {},
42
+ close: function () {},
43
+ address: function () {
44
+ return { address: '127.0.0.1', family: 'IPv4', port: 162 };
45
+ }
46
+ };
47
+ }
48
+ };
49
+
50
+ const receiver = snmp.createReceiver({
51
+ dgramModule: mockDgram,
52
+ port: 1162
53
+ }, function () {});
54
+
55
+ assert(createSocketCalled, 'Custom dgram module createSocket should have been called');
56
+ receiver.close();
57
+ done();
58
+ });
59
+
60
+ it('should fallback to default dgram when no custom module provided', function (done) {
61
+ // This should not throw an error
62
+ const session = snmp.createSession('127.0.0.1', 'public', {
63
+ // No dgramModule specified
64
+ });
65
+
66
+ // Session should be created successfully
67
+ assert(session);
68
+ session.close();
69
+ done();
70
+ });
71
+ });
@@ -0,0 +1,53 @@
1
+ const assert = require('assert');
2
+ const snmp = require('../index.js');
3
+
4
+ describe('Transport Option Handling in createV3Session', function() {
5
+ const user = {
6
+ name: "testuser",
7
+ level: snmp.SecurityLevel.noAuthNoPriv
8
+ };
9
+
10
+ afterEach(function() {
11
+ // Clean up any open sessions
12
+ // Note: sessions are closed in individual tests
13
+ });
14
+
15
+ it('should use default transport when not specified', function() {
16
+ const session = snmp.createV3Session("127.0.0.1", user);
17
+ assert.strictEqual(session.transport, "udp4");
18
+ session.close();
19
+ });
20
+
21
+ it('should use default transport when options is empty object', function() {
22
+ const session = snmp.createV3Session("127.0.0.1", user, {});
23
+ assert.strictEqual(session.transport, "udp4");
24
+ session.close();
25
+ });
26
+
27
+ it('should preserve explicit udp4 transport', function() {
28
+ const session = snmp.createV3Session("127.0.0.1", user, { transport: "udp4" });
29
+ assert.strictEqual(session.transport, "udp4");
30
+ session.close();
31
+ });
32
+
33
+ it('should preserve udp6 transport', function() {
34
+ const session = snmp.createV3Session("127.0.0.1", user, { transport: "udp6" });
35
+ assert.strictEqual(session.transport, "udp6");
36
+ session.close();
37
+ });
38
+
39
+ it('should handle null transport by using default', function() {
40
+ const session = snmp.createV3Session("127.0.0.1", user, { transport: null });
41
+ assert.strictEqual(session.transport, "udp4");
42
+ session.close();
43
+ });
44
+
45
+ it('should preserve invalid transport values (let Node.js validate)', function() {
46
+ assert.throws(function() {
47
+ snmp.createV3Session("127.0.0.1", user, { transport: "invalid" });
48
+ }, function(err) {
49
+ return err.code === 'ERR_SOCKET_BAD_TYPE';
50
+ });
51
+ });
52
+
53
+ });