diff --git a/SharpSnmpLib/Messaging/Discoverer.cs b/SharpSnmpLib/Messaging/Discoverer.cs index a8905291..e00fbe1a 100644 --- a/SharpSnmpLib/Messaging/Discoverer.cs +++ b/SharpSnmpLib/Messaging/Discoverer.cs @@ -59,6 +59,20 @@ public sealed partial class Discoverer /// The discovering time interval, in milliseconds. /// must be configured to a valid multicast address when IPv6 is used. For example, "[ff02::1]:161" public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval) + { + Discover(version, broadcastAddress, community, interval, OctetString.Empty); + } + + /// + /// Discovers agents of the specified version in a specific time interval. + /// + /// The version. + /// The broadcast address. + /// The community. + /// The discovering time interval, in milliseconds. + /// The optional Context Name. + /// must be configured to a valid multicast address when IPv6 is used. For example, "[ff02::1]:161" + public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval, OctetString contextName) { if (broadcastAddress == null) { @@ -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 @@ -284,6 +298,20 @@ private void HandleMessage(byte[] buffer, int count, IPEndPoint remote) /// The discovering time interval, in milliseconds. /// must be an IPv4 address. IPv6 is not yet supported here. public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval) + { + await DiscoverAsync(version, broadcastAddress, community, interval, OctetString.Empty); + } + + /// + /// Discovers agents of the specified version in a specific time interval. + /// + /// The version. + /// The broadcast address. + /// The community. + /// The discovering time interval, in milliseconds. + /// The optional context name. + /// must be an IPv4 address. IPv6 is not yet supported here. + public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval, OctetString contextName) { if (broadcastAddress == null) { @@ -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 diff --git a/SharpSnmpLib/Messaging/Discovery.cs b/SharpSnmpLib/Messaging/Discovery.cs index ab4abbdf..c6c8fc42 100644 --- a/SharpSnmpLib/Messaging/Discovery.cs +++ b/SharpSnmpLib/Messaging/Discovery.cs @@ -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())), - 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())), + DefaultPrivacyProvider.DefaultPair, + null); + } + + /// + /// Initializes a new instance of the class. + /// + /// The request id. + /// The message id. + /// The max size of message. + /// The optional Context Name. + 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())), + DefaultPrivacyProvider.DefaultPair, + null); } /// @@ -180,6 +204,113 @@ public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type } } + /// + /// Initializes a new instance of the class. + /// + /// The request id. + /// The message id. + /// The max size of message. + /// Message type. + /// The optional Context Name. + 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())), + 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())), + 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())), + 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())), + 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)); + } + } + /// /// Gets the response. /// @@ -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()); + } } /// diff --git a/SharpSnmpLib/Messaging/Messenger.cs b/SharpSnmpLib/Messaging/Messenger.cs index 9ba4ea93..ec53de72 100644 --- a/SharpSnmpLib/Messaging/Messenger.cs +++ b/SharpSnmpLib/Messaging/Messenger.cs @@ -1205,10 +1205,22 @@ private static bool BulkHasNext(VersionCode version, IPEndPoint receiver, OctetS /// Returns a new discovery request. /// /// Message type. + /// The optional context name. /// public static Discovery GetNextDiscovery(SnmpType type) { - return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type); + return GetNextDiscovery(type, OctetString.Empty); + } + + /// + /// Returns a new discovery request. + /// + /// Message type. + /// The optional context name. + /// + public static Discovery GetNextDiscovery(SnmpType type, OctetString contextName) + { + return new Discovery(NextMessageId, NextRequestId, MaxMessageSize, type, contextName); } /// diff --git a/SharpSnmpLib/Messaging/Net6Discoverer.cs b/SharpSnmpLib/Messaging/Net6Discoverer.cs index 6c16f3f6..c02a0cb3 100644 --- a/SharpSnmpLib/Messaging/Net6Discoverer.cs +++ b/SharpSnmpLib/Messaging/Net6Discoverer.cs @@ -40,6 +40,20 @@ public sealed partial class Discoverer /// The cancellation token. /// must be an IPv4 address. IPv6 is not yet supported here. public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString? community, CancellationToken token) + { + Discover(version, broadcastAddress, community, token, OctetString.Empty); + } + + /// + /// Discovers agents of the specified version in a specific time interval. + /// + /// The version. + /// The broadcast address. + /// The community. + /// The cancellation token. + /// The optional context name. + /// must be an IPv4 address. IPv6 is not yet supported here. + public void Discover(VersionCode version, IPEndPoint broadcastAddress, OctetString? community, CancellationToken token, OctetString contextName) { if (broadcastAddress == null) { @@ -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 @@ -142,6 +156,20 @@ private void AsyncBeginReceive(Socket socket, CancellationToken token) /// The cancellation token. /// must be an IPv4 address. IPv6 is not yet supported here. public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, CancellationToken token) + { + await DiscoverAsync(version, broadcastAddress, community, token, OctetString.Empty); + } + + /// + /// Discovers agents of the specified version in a specific time interval. + /// + /// The version. + /// The broadcast address. + /// The community. + /// The cancellation token. + /// The context name. + /// must be an IPv4 address. IPv6 is not yet supported here. + public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, CancellationToken token, OctetString contextName) { if (broadcastAddress == null) { @@ -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