Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix v3 context name #690

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions SharpSnmpLib/Messaging/Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public sealed partial class Discoverer
/// <param name="interval">The discovering time interval, in milliseconds.</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)
{
Discover(version, broadcastAddress, community, interval, OctetString.Empty);
}

/// <summary>
/// Discovers agents of the specified version in a specific time interval.
/// </summary>
/// <param name="version">The version.</param>
/// <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, OctetString contextName)
{
if (broadcastAddress == null)
{
Expand All @@ -75,7 +89,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 @@ -284,6 +298,20 @@ private void HandleMessage(byte[] buffer, int count, IPEndPoint remote)
/// <param name="interval">The discovering time interval, in milliseconds.</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)
{
await DiscoverAsync(version, broadcastAddress, community, interval, OctetString.Empty);
}

/// <summary>
/// Discovers agents of the specified version in a specific time interval.
/// </summary>
/// <param name="version">The version.</param>
/// <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, OctetString contextName)
{
if (broadcastAddress == null)
{
Expand All @@ -300,7 +328,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
167 changes: 154 additions & 13 deletions SharpSnmpLib/Messaging/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,42 @@ public sealed partial class Discovery
public Discovery(int messageId, int requestId, int maxMessageSize)
{
_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
}

/// <summary>
/// Initializes a new instance of the <see cref="Discovery"/> class.
/// </summary>
/// <param name="requestId">The request id.</param>
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">The max size of message.</param>
/// <param name="contextName">The optional Context Name.</param>
public Discovery(int messageId, int requestId, int maxMessageSize, OctetString contextName)
{
_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
}

/// <summary>
Expand Down Expand Up @@ -180,6 +204,113 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Discovery"/> class.
/// </summary>
/// <param name="requestId">The request id.</param>
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">The max size of message.</param>
/// <param name="type">Message type.</param>
/// <param name="contextName">The optional Context Name.</param>
public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type, OctetString contextName)
{
switch (type)
{
case SnmpType.GetRequestPdu:
{
_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new GetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}

case SnmpType.GetNextRequestPdu:
{
_discovery = new GetNextRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new GetNextRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}

case SnmpType.GetBulkRequestPdu:
{
_discovery = new GetBulkRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new GetBulkRequestPdu(requestId, 0, 0, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}

case SnmpType.SetRequestPdu:
{
_discovery = new SetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new SetRequestPdu(requestId, new List<Variable>())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}

case SnmpType.InformRequestPdu:
{
_discovery = new InformRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
contextName,
new InformRequestPdu(requestId)),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}

default:
throw new ArgumentException("Discovery message must be a request.", nameof(type));
}
}

/// <summary>
/// Gets the response.
/// </summary>
Expand All @@ -197,7 +328,17 @@ public ReportMessage GetResponse(int timeout, IPEndPoint receiver)
}

using var socket = receiver.GetSocket();
return (ReportMessage)_discovery.GetResponse(timeout, receiver, Empty, socket);
var response = _discovery.GetResponse(timeout, receiver, Empty, socket);

try
{
return (ReportMessage)response;
}
catch
{
//The cast was not possible with a system that needed contextname=public
return new ReportMessage(response.Version, response.Header, response.Parameters, response.Scope, response.Privacy, response?.ToBytes());
}
}

/// <summary>
Expand Down
14 changes: 13 additions & 1 deletion SharpSnmpLib/Messaging/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,22 @@ 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)
{
return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type);
return GetNextDiscovery(type, OctetString.Empty);
}

/// <summary>
/// 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, OctetString contextName)
{
return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type, contextName);
}

/// <summary>
Expand Down
32 changes: 30 additions & 2 deletions SharpSnmpLib/Messaging/Net6Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public sealed partial class Discoverer
/// <param name="token">The cancellation token.</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)
{
Discover(version, broadcastAddress, community, token, OctetString.Empty);
}

/// <summary>
/// Discovers agents of the specified version in a specific time interval.
/// </summary>
/// <param name="version">The version.</param>
/// <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, OctetString contextName)
{
if (broadcastAddress == null)
{
Expand All @@ -61,7 +75,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 @@ -142,6 +156,20 @@ private void AsyncBeginReceive(Socket socket, CancellationToken token)
/// <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)
{
await DiscoverAsync(version, broadcastAddress, community, token, OctetString.Empty);
}

/// <summary>
/// Discovers agents of the specified version in a specific time interval.
/// </summary>
/// <param name="version">The version.</param>
/// <param name="broadcastAddress">The broadcast address.</param>
/// <param name="community">The community.</param>
/// <param name="token">The cancellation token.</param>
/// <param name="contextName">The 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, CancellationToken token, OctetString contextName)
{
if (broadcastAddress == null)
{
Expand All @@ -158,7 +186,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