Exemplo n.º 1
0
        /// <summary>
        /// Sends this <see cref="ISnmpMessage"/> and handles the response from agent.
        /// </summary>
        /// <param name="request">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="receiver">Agent.</param>
        /// <param name="udpSocket">The UDP <see cref="Socket"/> to use to send/receive.</param>
        /// <returns></returns>
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, IPEndPoint receiver, Socket udpSocket)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            if (udpSocket == null)
            {
                throw new ArgumentNullException(nameof(udpSocket));
            }

            var registry = new UserRegistry();

            if (request.Version == VersionCode.V3)
            {
                registry.Add(request.Parameters.UserName, request.Privacy);
            }

            return(await request.GetResponseAsync(receiver, registry, udpSocket).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, IPEndPoint receiver, Socket udpSocket, CancellationToken token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            if (udpSocket == null)
            {
                throw new ArgumentNullException(nameof(udpSocket));
            }

            var registry = new UserRegistry();

            if (request.Version == VersionCode.V3 && request.Header.SecurityModel == SecurityModel.Usm)
            {
                if (request.Parameters.UserName is null)
                {
                    throw new Exception($"{nameof(request)}.{nameof(request.Parameters)}.{nameof(request.Parameters.UserName)} cannot be null when using {SecurityModel.Usm}");
                }

                registry.Add(request.Parameters.UserName, request.Privacy);
            }

            return(await request.GetResponseAsync(receiver, registry, udpSocket, token).ConfigureAwait(false));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends this <see cref="ISnmpMessage"/> and handles the response from agent.
        /// </summary>
        /// <param name="request">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="receiver">Port number.</param>
        /// <param name="registry">User registry.</param>
        /// <returns></returns>
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, IPEndPoint receiver, UserRegistry registry)
        {
            // TODO: make more usage of UserRegistry.
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            var code = request.TypeCode();

            if (code == SnmpType.TrapV1Pdu || code == SnmpType.TrapV2Pdu || code == SnmpType.ReportPdu)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "not a request message: {0}", code));
            }

            using (var socket = receiver.GetSocket())
            {
                return(await request.GetResponseAsync(receiver, registry, socket).ConfigureAwait(false));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        /// <returns></returns>
        public async Task <ReportMessage> GetResponseAsync(IPEndPoint receiver)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            using (Socket socket = receiver.GetSocket())
            {
                return((ReportMessage)await _discovery.GetResponseAsync(receiver, Empty, socket).ConfigureAwait(false));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        /// <returns></returns>
        public async Task <ReportMessage> GetResponseAsync(int timeout, IPEndPoint receiver, int timeoutRetries)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            using (var socket = new UdpClient())
            {
                return((ReportMessage)await _discovery.GetResponseAsync(timeout, receiver, Empty, socket).ConfigureAwait(false));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sends this <see cref="ISnmpMessage"/> and handles the response from agent.
        /// </summary>
        /// <param name="request">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="receiver">Port number.</param>
        /// <param name="registry">User registry.</param>
        /// <returns></returns>
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, int timeout, IPEndPoint receiver, UserRegistry registry, int timeoutRetries = 1, CancellationToken cancellationToken = default)
        {
            // TODO: make more usage of UserRegistry.
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            var code = request.TypeCode();

            if (code == SnmpType.TrapV1Pdu || code == SnmpType.TrapV2Pdu || code == SnmpType.ReportPdu)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "not a request message: {0}", code));
            }

            while (true)
            {
                try
                {
                    using (var socket = new UdpClient())
                    {
                        return(await request.GetResponseAsync(timeout, receiver, registry, socket, cancellationToken).ConfigureAwait(false));
                    }
                }
                catch (TimeoutException)
                {
                    if (timeoutRetries > 1)
                    {
                        timeoutRetries--;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 7
0
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, IPEndPoint receiver, CancellationToken token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            var code = request.TypeCode();

            if (code == SnmpType.TrapV1Pdu || code == SnmpType.TrapV2Pdu || code == SnmpType.ReportPdu)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "not a request message: {0}", code));
            }

            using var socket = receiver.GetSocket();
            return(await request.GetResponseAsync(receiver, socket, token).ConfigureAwait(false));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Sends this <see cref="ISnmpMessage"/> and handles the response from agent.
        /// </summary>
        /// <param name="request">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="receiver">Port number.</param>
        /// <returns></returns>
        public static async Task <ISnmpMessage> GetResponseAsync(this ISnmpMessage request, IPEndPoint receiver, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            var code = request.TypeCode();

            if (code == SnmpType.TrapV1Pdu || code == SnmpType.TrapV2Pdu || code == SnmpType.ReportPdu)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "not a request message: {0}", code));
            }

            var tcs = new TaskCompletionSource <bool>();

            using (var cancel = cancellationToken.Register(() => tcs.SetCanceled()))
                using (var socket = receiver.GetSocket())
                {
                    var response = request.GetResponseAsync(receiver, socket);
                    var result   = await Task.WhenAny(tcs.Task, response).ConfigureAwait(false);

                    if (result == tcs.Task)
                    {
                        // This should always be true....
                        // Otherwise throw your own.
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                    return(await response.ConfigureAwait(false));
                }
        }