public async Task Handle(SetV3Command command) { Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu); ReportMessage report = discovery.GetResponse(10000, new IPEndPoint(command.IpAddress, command.Port)); var provider = GetPrivacyProvider(command.Password, command.PasswordType, command.Encryption, command.EncryptionType); SetRequestMessage request = new SetRequestMessage( Lextm.SharpSnmpLib.VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(command.UserName), new List <Variable> { new Variable(new ObjectIdentifier(command.OID.Id), new OctetString(command.OID.Value)) }, provider, report); ISnmpMessage reply = await request.GetResponseAsync(new IPEndPoint(command.IpAddress, command.Port)); if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError { throw ErrorException.Create("error in response", command.IpAddress, reply); } }
public async Task <ISnmpMessage?> SetV3UsmAsync <T>(IPAddress ip, string oid, string community, int retries, int port, TimeSpan timeout, string authPass, string privPass, T setValue) { if (ip == null) { throw new ArgumentNullException(nameof(ip)); } if (string.IsNullOrWhiteSpace(oid)) { throw new ArgumentNullException(nameof(oid)); } if (!Regex.IsMatch(oid, @"^(([0-9]+)\.)+[0-9]+$")) { throw new ArgumentException(oid, nameof(oid)); } if (string.IsNullOrWhiteSpace(community)) { throw new ArgumentNullException(nameof(community)); } if (port <= 0) { throw new ArgumentOutOfRangeException(nameof(port), port.ToString()); } if (retries <= 0) { throw new ArgumentOutOfRangeException(nameof(retries), retries.ToString()); } if (timeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(timeout), timeout.ToString()); } var startDate = DateTime.Now; var snmpType = "SET"; var snmpVersion = $"3 {SecurityModel.Usm}"; var attempt = 0; ISnmpMessage?response = null; while (attempt < retries) { var setValueByType = setValue switch { int x => new Variable(new ObjectIdentifier(oid), new Integer32(x)), string x => new Variable(new ObjectIdentifier(oid), new OctetString(x)), IPAddress x => new Variable(new ObjectIdentifier(oid), new IP(x.ToString())), uint x => new Variable(new ObjectIdentifier(oid), new Gauge32(x)), byte[] x => new Variable(new ObjectIdentifier(oid), new OctetString(x)), _ => throw new ArgumentOutOfRangeException(nameof(setValue)), }; try { var receiver = new IPEndPoint(ip, port); var clientEndPoint = ip.AddressFamily == AddressFamily.InterNetwork ? new IPEndPoint(IPAddress.Any, 0) : new IPEndPoint(IPAddress.IPv6Any, 0); var vList = new List <Variable>() { setValueByType }; var discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu); var report = await discovery.GetResponseAsync(receiver).ConfigureAwait(false); var auth = new SHA1AuthenticationProvider(new OctetString(authPass)); // AuthenticationPassword var priv = new DESPrivacyProvider(new OctetString(privPass), auth); //PrivacyPassword var request = new SetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(community), vList, priv, report); using var cts = new CancellationTokenSource(timeout); response = await request.GetResponseAsync(receiver, cts.Token).ConfigureAwait(false); if (response is ReportMessage) { if (response.Pdu().Variables.Count == 0) { throw new Exception("wrong report message received"); } var id = response.Pdu().Variables[0].Id; if (id != Messenger.NotInTimeWindow) { var error = id.GetErrorMessage(); throw new Exception($"ERROR: {error}"); } } break; } catch (Exception ex) when(ex is SnmpException || ex is SocketException || ex is OperationCanceledException || ex is System.TimeoutException) { if (ex is System.TimeoutException && ex.Message == "Could Not Connect To Server") { _Logger.LogInformation($"{ip} - DTLS failed {attempt + 1} time(s)"); } await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, null, snmpType, snmpVersion, ex.GetType().ToString(), ex.Message).ConfigureAwait(false); ++attempt; if (attempt >= retries) { throw; } } } if (response is null) { await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, community, snmpType, snmpVersion, SnmpType.Null.ToString(), null).ConfigureAwait(false); return(response); } var type = response.Pdu().TypeCode; var data = response.Pdu().ErrorStatus; await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, community, snmpType, snmpVersion, type.ToString(), data.ToString()).ConfigureAwait(false); return(response); }
public async Task <ISnmpMessage?> SetV2Async <T>(IPAddress ip, string oid, string community, int retries, int port, TimeSpan timeout, T setValue) { if (ip == null) { throw new ArgumentNullException(nameof(ip)); } if (string.IsNullOrWhiteSpace(oid)) { throw new ArgumentNullException(nameof(oid)); } if (!Regex.IsMatch(oid, @"^(([0-9]+)\.)+[0-9]+$")) { throw new ArgumentException(oid, nameof(oid)); } if (string.IsNullOrWhiteSpace(community)) { throw new ArgumentNullException(nameof(community)); } if (port <= 0) { throw new ArgumentOutOfRangeException(nameof(port), port.ToString()); } if (retries <= 0) { throw new ArgumentOutOfRangeException(nameof(retries), retries.ToString()); } if (timeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(timeout), timeout.ToString()); } var startDate = DateTime.Now; var snmpType = "SET"; var snmpVersion = "2c"; var attempt = 0; ISnmpMessage?response = null; while (attempt < retries) { var setValueByType = setValue switch { int x => new Variable(new ObjectIdentifier(oid), new Integer32(x)), string x => new Variable(new ObjectIdentifier(oid), new OctetString(x)), IPAddress x => new Variable(new ObjectIdentifier(oid), new IP(x.ToString())), uint x => new Variable(new ObjectIdentifier(oid), new Gauge32(x)), byte[] x => new Variable(new ObjectIdentifier(oid), new OctetString(x)), _ => throw new ArgumentOutOfRangeException(nameof(setValue)), }; try { var receiver = new IPEndPoint(ip, port); var request = new SetRequestMessage(Messenger.NextMessageId, VersionCode.V2, new OctetString(community), new List <Variable> { setValueByType }); using var cts = new CancellationTokenSource(timeout); response = await request.GetResponseAsync(receiver, cts.Token).ConfigureAwait(false); if (response is ReportMessage) { if (response.Pdu().Variables.Count == 0) { throw new Exception("wrong report message received"); } var id = response.Pdu().Variables[0].Id; if (id != Messenger.NotInTimeWindow) { var error = id.GetErrorMessage(); throw new Exception($"ERROR: {error}"); } } break; } catch (Exception ex) when(ex is SnmpException || ex is SocketException || ex is OperationCanceledException) { await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, community, snmpType, snmpVersion, ex.GetType().ToString(), ex.Message).ConfigureAwait(false); ++attempt; if (attempt >= retries) { throw; } } } if (response is null) { await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, community, snmpType, snmpVersion, SnmpType.Null.ToString(), null).ConfigureAwait(false); return(response); } var type = response.Pdu().TypeCode; var data = response.Pdu().ErrorStatus; await _SnmpLog.LogTransactionAsync(startDate, ip.ToString(), oid, community, snmpType, snmpVersion, type.ToString(), data.ToString()).ConfigureAwait(false); return(response); }