Skip to content

Commit

Permalink
fixed > v3 Discovery can be initialized with a contextName
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-bernard committed Jan 3, 2025
1 parent 1894e10 commit e67d498
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
10 changes: 6 additions & 4 deletions SharpSnmpLib/Messaging/Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public sealed partial class Discoverer
/// <param name="broadcastAddress">The broadcast address.</param>
/// <param name="community">The community.</param>
/// <param name="interval">The discovering time interval, in milliseconds.</param>
/// <param name="contextName">The optional Context Name.</param>
/// <remarks><paramref name="broadcastAddress"/> must be configured to a valid multicast address when IPv6 is used. For example, "[ff02::1]:161"</remarks>
public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval)
public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval, OctetString? contextName = null)
{
if (broadcastAddress == null)
{
Expand All @@ -75,7 +76,7 @@ public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetStri
_requestId = Messenger.NextRequestId;
if (version == VersionCode.V3)
{
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize);
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize, contextName);
bytes = discovery.ToBytes();
}
else
Expand Down Expand Up @@ -282,8 +283,9 @@ private void HandleMessage(byte[] buffer, int count, IPEndPoint remote)
/// <param name="broadcastAddress">The broadcast address.</param>
/// <param name="community">The community.</param>
/// <param name="interval">The discovering time interval, in milliseconds.</param>
/// <param name="contextName">The optional context name.</param>
/// <remarks><paramref name="broadcastAddress"/> must be an IPv4 address. IPv6 is not yet supported here.</remarks>
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval)
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval, OctetString? contextName = null)
{
if (broadcastAddress == null)
{
Expand All @@ -300,7 +302,7 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress
_requestId = Messenger.NextRequestId;
if (version == VersionCode.V3)
{
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize);
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize, contextName);
bytes = discovery.ToBytes();
}
else
Expand Down
24 changes: 16 additions & 8 deletions SharpSnmpLib/Messaging/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public sealed partial class Discovery
/// <param name="requestId">The request id.</param>
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">The max size of message.</param>
public Discovery(int messageId, int requestId, int maxMessageSize)
/// <param name="contextName">The optional Context Name.</param>
public Discovery(int messageId, int requestId, int maxMessageSize, OctetString? contextName = null)
{
if (contextName == null)
contextName = OctetString.Empty;

_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
Expand All @@ -68,7 +72,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize)
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
Expand All @@ -81,8 +85,12 @@ public Discovery(int messageId, int requestId, int maxMessageSize)
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">The max size of message.</param>
/// <param name="type">Message type.</param>
public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type)
/// <param name="contextName">The optional Context Name.</param>
public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type, OctetString? contextName = null)
{
if (contextName == null)
contextName = OctetString.Empty;

switch (type)
{
case SnmpType.GetRequestPdu:
Expand All @@ -96,7 +104,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
Expand All @@ -114,7 +122,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new GetNextRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
Expand All @@ -132,7 +140,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new GetBulkRequestPdu(requestId, 0, 0, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
Expand All @@ -150,7 +158,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new SetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
Expand All @@ -168,7 +176,7 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
contextName,
new InformRequestPdu(requestId)),
DefaultPrivacyProvider.DefaultPair,
null);
Expand Down
5 changes: 3 additions & 2 deletions SharpSnmpLib/Messaging/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,11 @@ private static bool BulkHasNext(VersionCode version, IPEndPoint receiver, OctetS
/// Returns a new discovery request.
/// </summary>
/// <param name="type">Message type.</param>
/// <param name="contextName">The optional context name.</param>
/// <returns></returns>
public static Discovery GetNextDiscovery(SnmpType type)
public static Discovery GetNextDiscovery(SnmpType type, OctetString? contextName = null)
{
return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type);
return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type, contextName);
}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions SharpSnmpLib/Messaging/Net6Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public sealed partial class Discoverer
/// <param name="broadcastAddress">The broadcast address.</param>
/// <param name="community">The community.</param>
/// <param name="token">The cancellation token.</param>
/// <param name="contextName">The optional context name.</param>
/// <remarks><paramref name="broadcastAddress"/> must be an IPv4 address. IPv6 is not yet supported here.</remarks>
public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString? community, CancellationToken token)
public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString? community, CancellationToken token, OctetString? contextName = null)
{
if (broadcastAddress == null)
{
Expand All @@ -61,7 +62,7 @@ public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetStri
_requestId = Messenger.NextRequestId;
if (version == VersionCode.V3)
{
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize);
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize, contextName);
bytes = discovery.ToBytes();
}
else
Expand Down Expand Up @@ -141,7 +142,7 @@ private void AsyncBeginReceive(Socket socket, CancellationToken token)
/// <param name="community">The community.</param>
/// <param name="token">The cancellation token.</param>
/// <remarks><paramref name="broadcastAddress"/> must be an IPv4 address. IPv6 is not yet supported here.</remarks>
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, CancellationToken token)
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, CancellationToken token, OctetString? contextName = null)
{
if (broadcastAddress == null)
{
Expand All @@ -158,7 +159,7 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress
_requestId = Messenger.NextRequestId;
if (version == VersionCode.V3)
{
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize);
var discovery = new Discovery(Messenger.NextMessageId, _requestId, Messenger.MaxMessageSize, contextName);
bytes = discovery.ToBytes();
}
else
Expand Down

0 comments on commit e67d498

Please sign in to comment.