Exemplo n.º 1
0
        private void AsyncEndReceive(IAsyncResult iar)
        {
            // If no more active, then stop. This discards the received packet, if any (indeed, we may be there either
            // because we've received a packet, or because the socket has been closed).
            if (Interlocked.Exchange(ref _active, _active) == Inactive)
            {
                return;
            }

            //// We start another receive operation.
            //AsyncBeginReceive();

            byte[] buffer = (byte[])iar.AsyncState;

            try
            {
                EndPoint remote = _socket.AddressFamily == AddressFamily.InterNetwork ? new IPEndPoint(IPAddress.Any, 0) : new IPEndPoint(IPAddress.IPv6Any, 0);
                int      count  = _socket.EndReceiveFrom(iar, ref remote);
                HandleMessage(buffer, count, (IPEndPoint)remote);
            }
            catch (SocketException ex)
            {
                // ignore WSAECONNRESET, http://bytes.com/topic/c-sharp/answers/237558-strange-udp-socket-problem
                if (ex.SocketErrorCode != SocketError.ConnectionReset)
                {
                    // If the SnmpTrapListener was active, marks it as stopped and call HandleException.
                    // If it was inactive, the exception is likely to result from this, and we raise nothing.
                    long activeBefore = Interlocked.CompareExchange(ref _active, Inactive, Active);
                    if (activeBefore == Active)
                    {
                        HandleException(ex);
                    }
                }
            }
        }
 public static async Task SendTrapV1Async(
     EndPoint receiver, IPAddress agent, OctetString community, ObjectIdentifier enterprise, GenericCode generic, int specific, uint timestamp,
     IList <Variable> variables)
 {
     var message = new TrapV1Message(VersionCode.V1, agent, community, enterprise, generic, specific, timestamp, variables);
     await message.SendAsync(receiver).ConfigureAwait(false);
 }
Exemplo n.º 3
0
        private void AsyncReceive()
        {
            while (true)
            {
                // If no more active, then stop.
                if (Interlocked.Exchange(ref _active, _active) == Inactive)
                {
                    return;
                }

                try
                {
                    var      buffer = new byte[_bufferSize];
                    EndPoint remote = _socket.AddressFamily == AddressFamily.InterNetwork ? new IPEndPoint(IPAddress.Any, 0) : new IPEndPoint(IPAddress.IPv6Any, 0);

                    var count = _socket.ReceiveFrom(buffer, ref remote);
                    Task.Factory.StartNew(() => HandleMessage(buffer, count, (IPEndPoint)remote));
                }
                catch (SocketException ex)
                {
                    // ignore WSAECONNRESET, http://bytes.com/topic/c-sharp/answers/237558-strange-udp-socket-problem
                    if (ex.SocketErrorCode != SocketError.ConnectionReset)
                    {
                        // If the SnmpTrapListener was active, marks it as stopped and call HandleException.
                        // If it was inactive, the exception is likely to result from this, and we raise nothing.
                        var activeBefore = Interlocked.CompareExchange(ref _active, Inactive, Active);
                        if (activeBefore == Active)
                        {
                            HandleException(ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends an <see cref="ISnmpMessage"/>.
        /// </summary>
        /// <param name="message">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="manager">Manager</param>
        /// <param name="socket">The socket.</param>
        public static async Task SendAsync(this ISnmpMessage message, EndPoint manager, Socket socket)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            var code = message.TypeCode();

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

            var bytes = message.ToBytes();
            var info  = SocketExtension.EventArgsFactory.Create();

            info.RemoteEndPoint = manager;
            info.SetBuffer(bytes, 0, bytes.Length);
            using (var awaitable1 = new SocketAwaitable(info))
            {
                await socket.SendToAsync(awaitable1);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sends an <see cref="ISnmpMessage"/>.
        /// </summary>
        /// <param name="message">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="manager">Manager</param>
        /// <param name="socket">The socket.</param>
        public static void Send(this ISnmpMessage message, EndPoint manager, Socket socket)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            var code = message.TypeCode();

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

            var bytes = message.ToBytes();

            socket.SendTo(bytes, 0, bytes.Length, SocketFlags.None, manager);
        }
        public static void SendTrapV1(
            EndPoint receiver, IPAddress agent, OctetString community, ObjectIdentifier enterprise, GenericCode generic, int specific, uint timestamp,
            IList <Variable> variables)
        {
            var message = new TrapV1Message(VersionCode.V1, agent, community, enterprise, generic, specific, timestamp, variables);

            message.Send(receiver);
        }
        public static async Task SendTrapV2Async(
            int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList <Variable> variables)
        {
            if (version != VersionCode.V2)
            {
                throw new ArgumentException("Only SNMP v2c is supported", "version");
            }

            var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);
            await message.SendAsync(receiver).ConfigureAwait(false);
        }
        /// <summary>
        /// Gets the socket.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <returns></returns>
        public static Socket GetSocket(this EndPoint endpoint)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }

#if SSHARP
            return(new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp, 16384));
#else
            return(new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp));
#endif
        }
Exemplo n.º 9
0
        internal TcpListenerWebSocketContext(
            TcpClient tcpClient,
            string protocol,
            bool secure,
#if !NETCF || BCC || SSL
            ServerSslConfiguration sslConfig,
#endif
            Logger log)
        {
            _tcpClient = tcpClient;
            _secure    = secure;
            _log       = log;

            Stream netStream = tcpClient.GetStream();

#if BUFFERED
            netStream = new BufferedNetworkStream((NetworkStream)netStream);
#endif
#if !NETCF || BCC || SSL
            if (secure)
            {
                var sslStream = new SslStream(netStream, false, sslConfig.ClientCertificateValidationCallback);

                sslStream.AuthenticateAsServer(
                    sslConfig.ServerCertificate,
                    sslConfig.ClientCertificateRequired,
                    sslConfig.EnabledSslProtocols,
                    sslConfig.CheckCertificateRevocation);

                _stream = sslStream;
            }
            else
#endif
            {
                _stream = netStream;
            }

#if SSHARP
            var sock = tcpClient;
#else
            var sock = tcpClient.Client;
#endif
            _serverEndPoint = sock.LocalEndPoint;
            _userEndPoint   = sock.RemoteEndPoint;

            _request = HttpRequest.Read(_stream, 90000);

            _websocket = new WebSocket(this, protocol);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Sends a response message.
        /// </summary>
        /// <param name="response">
        /// A <see cref="ISnmpMessage"/>.
        /// </param>
        /// <param name="receiver">Receiver.</param>
        public async Task SendResponseAsync(ISnmpMessage response, EndPoint receiver)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

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

            if (_disposed)
            {
                throw new ObjectDisposedException("Listener");
            }

            if (_socket == null)
            {
                return;
            }

            var buffer = response.ToBytes();
            var info   = SocketExtension.EventArgsFactory.Create();

            try
            {
                info.RemoteEndPoint = receiver;
                info.SetBuffer(buffer, 0, buffer.Length);
                using (var awaitable1 = new SocketAwaitable(info))
                {
                    await _socket.SendToAsync(awaitable1);
                }
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode != SocketError.Interrupted)
                {
                    // IMPORTANT: interrupted means the socket is closed.
                    throw;
                }
            }
        }
Exemplo n.º 11
0
        internal UpnpNatDevice(IPAddress localAddress, string deviceDetails, string serviceType)
        {
            this.LastSeen     = DateTime.Now;
            this.localAddress = localAddress;

            // Split the string at the "location" section so i can extract the ipaddress and service description url
            string locationDetails = deviceDetails.Substring(deviceDetails.IndexOf("Location", StringComparison.InvariantCultureIgnoreCase) + 9).Split('\r')[0];

            this.serviceType = serviceType;

            // Make sure we have no excess whitespace
            locationDetails = locationDetails.Trim();

            // FIXME: Is this reliable enough. What if we get a hostname as opposed to a proper http address
            // Are we going to get addresses with the "http://" attached?
            if (locationDetails.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
            {
                NatUtility.Log("Found device at: {0}", locationDetails);
                // This bit strings out the "http://" from the string
                locationDetails = locationDetails.Substring(7);

                // We then split off the end of the string to get something like: 192.168.0.3:241 in our string
                string hostAddressAndPort = locationDetails.Remove(locationDetails.IndexOf('/'));

                // From this we parse out the IP address and Port
                if (hostAddressAndPort.IndexOf(':') > 0)
                {
                    this.hostEndPoint = new IPEndPoint(IPAddress.Parse(hostAddressAndPort.Remove(hostAddressAndPort.IndexOf(':'))),
                                                       Convert.ToUInt16(hostAddressAndPort.Substring(hostAddressAndPort.IndexOf(':') + 1), System.Globalization.CultureInfo.InvariantCulture));
                }
                else
                {
                    // there is no port specified, use default port (80)
                    this.hostEndPoint = new IPEndPoint(IPAddress.Parse(hostAddressAndPort), 80);
                }

                NatUtility.Log("Parsed device as: {0}", this.hostEndPoint.ToString());

                // The service description URL is the remainder of the "locationDetails" string. The bit that was originally after the ip
                // and port information
                this.serviceDescriptionUrl = locationDetails.Substring(locationDetails.IndexOf('/'));
            }
            else
            {
                Trace.WriteLine("Couldn't decode address. Please send following string to the developer: ");
                Trace.WriteLine(deviceDetails);
            }
        }
Exemplo n.º 12
0
        private async Task ReceiveAsync()
        {
            EndPoint remote = _socket.AddressFamily == AddressFamily.InterNetwork ? new IPEndPoint(IPAddress.Any, 0) : new IPEndPoint(IPAddress.IPv6Any, 0);

            while (true)
            {
                // If no more active, then stop.
                if (Interlocked.Exchange(ref _active, _active) == Inactive)
                {
                    return;
                }

                int count;
                var reply = new byte[_bufferSize];
                var args  = SocketExtension.EventArgsFactory.Create();
                try
                {
                    args.RemoteEndPoint = remote;
                    args.SetBuffer(reply, 0, _bufferSize);
                    using (var awaitable = new SocketAwaitable(args))
                    {
                        count = await _socket.ReceiveAsync(awaitable);
                    }

                    await Task.Factory.StartNew(() => HandleMessage(reply, count, (IPEndPoint)remote));
                }
                catch (SocketException ex)
                {
                    // ignore WSAECONNRESET, http://bytes.com/topic/c-sharp/answers/237558-strange-udp-socket-problem
                    if (ex.SocketErrorCode != SocketError.ConnectionReset)
                    {
                        // If the SnmpTrapListener was active, marks it as stopped and call HandleException.
                        // If it was inactive, the exception is likely to result from this, and we raise nothing.
                        var activeBefore = Interlocked.CompareExchange(ref _active, Inactive, Active);
                        if (activeBefore == Active)
                        {
                            HandleException(ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Sends an <see cref="ISnmpMessage"/>.
        /// </summary>
        /// <param name="message">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="manager">Manager</param>
        public static async Task SendAsync(this ISnmpMessage message, EndPoint manager)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            var code = message.TypeCode();

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

            using (var socket = manager.GetSocket())
                await message.SendAsync(manager, socket).ConfigureAwait(false);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Sends a response message.
        /// </summary>
        /// <param name="response">
        /// A <see cref="ISnmpMessage"/>.
        /// </param>
        /// <param name="receiver">Receiver.</param>
        public void SendResponse(ISnmpMessage response, EndPoint receiver)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

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

            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (_socket == null)
            {
                return;
            }

            var buffer = response.ToBytes();

            try
            {
                _socket.SendTo(buffer, 0, buffer.Length, 0, receiver);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode != SocketError.Interrupted)
                {
                    // IMPORTANT: interrupted means the socket is closed.
                    throw;
                }
            }
        }
Exemplo n.º 15
0
        private void CreatePortMapListen(object obj)
        {
            CreatePortMapListenState state = obj as CreatePortMapListenState;

            UdpClient udpClient = state.UdpClient;

            state.UdpClientReady.WaitOne();              // Evidently UdpClient has some lazy-init Send/Receive race?
            IPEndPoint endPoint = new IPEndPoint(localAddress, PmpConstants.ServerPort);

            while (!state.Success)
            {
                byte[] data;
                try
                {
                    data = udpClient.Receive(ref endPoint);
                }
                catch (SocketException)
                {
                    state.Success = false;
                    return;
                }

                if (data.Length < 16)
                {
                    continue;
                }

                if (data[0] != PmpConstants.Version)
                {
                    continue;
                }

                byte opCode = (byte)(data[1] & (byte)127);

                Protocol protocol = Protocol.Tcp;
                if (opCode == PmpConstants.OperationCodeUdp)
                {
                    protocol = Protocol.Udp;
                }

                short resultCode = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 2));
                uint  epoch      = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 4));

                int privatePort = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 8));
                int publicPort  = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 10));

                uint lifetime = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 12));

                if (resultCode != PmpConstants.ResultCodeSuccess)
                {
                    state.Success = false;
                    return;
                }

                if (lifetime == 0)
                {
                    //mapping was deleted
                    state.Success = true;
                    state.Mapping = null;
                    return;
                }
                else
                {
                    //mapping was created
                    //TODO: verify that the private port+protocol are a match
                    Mapping mapping = state.Mapping;
                    mapping.PublicPort = publicPort;
                    mapping.Protocol   = protocol;
                    mapping.Expiration = DateTime.Now.AddSeconds(lifetime);

                    state.Success = true;
                }
            }
        }
Exemplo n.º 16
0
        private void ServicesReceived(IAsyncResult result)
        {
            HttpWebResponse response = null;

            try
            {
                int            abortCount  = 0;
                int            bytesRead   = 0;
                byte[]         buffer      = new byte[10240];
                StringBuilder  servicesXml = new StringBuilder();
                XmlDocument    xmldoc      = new XmlDocument();
                HttpWebRequest request     = result.AsyncState as HttpWebRequest;
                response = request.EndGetResponse(result) as HttpWebResponse;
                Stream s = response.GetResponseStream();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    NatUtility.Log("{0}: Couldn't get services list: {1}", HostEndPoint, response.StatusCode);
                    return;                     // FIXME: This the best thing to do??
                }

                while (true)
                {
                    bytesRead = s.Read(buffer, 0, buffer.Length);
                    servicesXml.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
                    try
                    {
                        xmldoc.LoadXml(servicesXml.ToString());
                        response.Close();
                        break;
                    }
                    catch (XmlException)
                    {
                        // If we can't receive the entire XML within 500ms, then drop the connection
                        // Unfortunately not all routers supply a valid ContentLength (mine doesn't)
                        // so this hack is needed to keep testing our recieved data until it gets successfully
                        // parsed by the xmldoc. Without this, the code will never pick up my router.
                        if (abortCount++ > 50)
                        {
                            response.Close();
                            return;
                        }
                        NatUtility.Log("{0}: Couldn't parse services list", HostEndPoint);
#if SSHARP
                        CrestronEnvironment.Sleep(10);
#else
                        System.Threading.Thread.Sleep(10);
#endif
                    }
                }

                NatUtility.Log("{0}: Parsed services list", HostEndPoint);
                XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
                ns.AddNamespace("ns", "urn:schemas-upnp-org:device-1-0");
                XmlNodeList nodes = xmldoc.SelectNodes("//*/ns:serviceList", ns);

                foreach (XmlNode node in nodes)
                {
                    //Go through each service there
                    foreach (XmlNode service in node.ChildNodes)
                    {
                        //If the service is a WANIPConnection, then we have what we want
                        string type = service["serviceType"].InnerText;
                        NatUtility.Log("{0}: Found service: {1}", HostEndPoint, type);
                        StringComparison c = StringComparison.OrdinalIgnoreCase;
                        if (type.Equals(this.serviceType, c))
                        {
                            this.controlUrl = service["controlURL"].InnerText;
                            NatUtility.Log("{0}: Found upnp service at: {1}", HostEndPoint, controlUrl);
                            try
                            {
                                Uri u = new Uri(controlUrl);
                                if (u.IsAbsoluteUri)
                                {
                                    IPEndPoint old = hostEndPoint;
                                    this.hostEndPoint = new IPEndPoint(IPAddress.Parse(u.Host), u.Port);
                                    NatUtility.Log("{0}: Absolute URI detected. Host address is now: {1}", old, HostEndPoint);
                                    this.controlUrl = controlUrl.Substring(u.GetLeftPart(UriPartial.Authority).Length);
                                    NatUtility.Log("{0}: New control url: {1}", HostEndPoint, controlUrl);
                                }
                            }
                            catch
                            {
                                NatUtility.Log("{0}: Assuming control Uri is relative: {1}", HostEndPoint, controlUrl);
                            }
                            NatUtility.Log("{0}: Handshake Complete", HostEndPoint);
                            this.callback(this);
                            return;
                        }
                    }
                }

                //If we get here, it means that we didn't get WANIPConnection service, which means no uPnP forwarding
                //So we don't invoke the callback, so this device is never added to our lists
            }
            catch (WebException ex)
            {
                // Just drop the connection, FIXME: Should i retry?
                NatUtility.Log("{0}: Device denied the connection attempt: {1}", HostEndPoint, ex);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }