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 2 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
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
36 changes: 27 additions & 9 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 Expand Up @@ -197,7 +205,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
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