/// <summary>
        /// Searches for Internet Gateways in the network.
        /// </summary>
        public void SearchGateways()
        {
            try
            {
                this.searchTimer?.Dispose();
                this.searchTimer = null;

                if (this.upnpClient is null)
                {
                    this.upnpClient = new UPnPClient(this.sniffers);
                    this.upnpClient.OnDeviceFound += this.UpnpClient_OnDeviceFound;
                }

                lock (this.ipAddressesFound)
                {
                    this.ipAddressesFound.Clear();
                }

                this.State = PeerToPeerNetworkState.SearchingForGateway;

                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:1", 1);
                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:2", 1);

                this.searchTimer = new Timer(this.SearchTimeout, null, 10000, Timeout.Infinite);
            }
            catch (Exception ex)
            {
                this.exception = ex;
                this.State     = PeerToPeerNetworkState.Error;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Searches for Internet Gateways in the network.
        /// </summary>
        public void SearchGateways()
        {
            try
            {
                if (this.upnpClient == null)
                {
                    this.upnpClient = new UPnPClient(this.sniffers);
                    this.upnpClient.OnDeviceFound += new UPnPDeviceLocationEventHandler(UpnpClient_OnDeviceFound);
                }

                lock (this.ipAddressesFound)
                {
                    this.ipAddressesFound.Clear();
                }

                this.State = PeerToPeerNetworkState.SearchingForGateway;

                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:1", 1);
                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:2", 1);
            }
            catch (Exception ex)
            {
                this.exception = ex;
                this.State     = PeerToPeerNetworkState.Error;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            this.disposed = true;
            this.State    = PeerToPeerNetworkState.Closed;

            NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged;

            this.searchTimer?.Dispose();
            this.searchTimer = null;

            this.tcpListener?.Stop();
            this.tcpListener = null;

            if (this.tcpMappingAdded)
            {
                this.tcpMappingAdded = false;
                try
                {
                    this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, (ushort)this.localEndpoint.Port, "TCP");
                }
                catch (Exception)
                {
                    // Ignore
                }
            }

            if (this.udpMappingAdded)
            {
                this.udpMappingAdded = false;
                try
                {
                    this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, (ushort)this.localEndpoint.Port, "UDP");
                }
                catch (Exception)
                {
                    // Ignore
                }
            }

            this.serviceWANIPConnectionV1 = null;

            this.upnpClient?.Dispose();
            this.upnpClient = null;

            this.ipAddressesFound?.Clear();
            this.ipAddressesFound = null;

            this.ready?.Dispose();
            this.ready = null;

            this.error?.Dispose();
            this.error = null;
        }
Esempio n. 4
0
        private void StartSearch()
        {
            try
            {
                this.upnpClient = new UPnPClient();
                this.upnpClient.OnDeviceFound += new UPnPDeviceLocationEventHandler(UpnpClient_OnDeviceFound);

                this.State = PeerToPeerNetworkState.SearchingForGateway;

                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:1", 1);
                this.upnpClient.StartSearch("urn:schemas-upnp-org:service:WANIPConnection:2", 1);
            }
            catch (Exception ex)
            {
                this.exception = ex;
                this.State     = PeerToPeerNetworkState.Error;
            }
        }
Esempio n. 5
0
        private void UpnpClient_OnDeviceFound(UPnPClient Sender, DeviceLocationEventArgs e)
        {
            try
            {
                lock (this.ipAddressesFound)
                {
                    if (this.ipAddressesFound.ContainsKey(e.RemoteEndPoint.Address))
                    {
                        return;
                    }

                    this.ipAddressesFound[e.RemoteEndPoint.Address] = true;
                }

                e.Location.StartGetDevice(this.DeviceRetrieved, e);
            }
            catch (Exception ex)
            {
                this.exception = ex;
                this.State     = PeerToPeerNetworkState.Error;
            }
        }
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public virtual void Dispose()
        {
            this.disposed = true;
            this.State    = PeerToPeerNetworkState.Closed;

            NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged;

            this.searchTimer?.Dispose();
            this.searchTimer = null;

            foreach (InternetGatewayRegistration Registration in this.ports)
            {
                if (Registration.TcpRegistered)
                {
                    Registration.TcpRegistered = false;
                    try
                    {
                        Log.Notice("Deleting Internet Gateway port mapping.",
                                   new KeyValuePair <string, object>("Host", string.Empty),
                                   new KeyValuePair <string, object>("External Port", Registration.ExternalPort),
                                   new KeyValuePair <string, object>("Protocol", "TCP"),
                                   new KeyValuePair <string, object>("Local Port", Registration.LocalPort),
                                   new KeyValuePair <string, object>("Application", Registration.ApplicationName));

                        this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, Registration.LocalPort, "TCP");
                    }
                    catch (Exception)
                    {
                        // Ignore
                    }
                }

                if (Registration.UdpRegistered)
                {
                    Registration.UdpRegistered = false;
                    try
                    {
                        Log.Notice("Deleting Internet Gateway port mapping.",
                                   new KeyValuePair <string, object>("Host", string.Empty),
                                   new KeyValuePair <string, object>("External Port", Registration.ExternalPort),
                                   new KeyValuePair <string, object>("Protocol", "UDP"),
                                   new KeyValuePair <string, object>("Local Port", Registration.LocalPort),
                                   new KeyValuePair <string, object>("Application", Registration.ApplicationName));

                        this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, Registration.LocalPort, "UDP");
                    }
                    catch (Exception)
                    {
                        // Ignore
                    }
                }
            }

            this.serviceWANIPConnectionV1 = null;

            this.upnpClient?.Dispose();
            this.upnpClient = null;

            this.ipAddressesFound?.Clear();
            this.ipAddressesFound = null;

            this.ready?.Dispose();
            this.ready = null;

            this.error?.Dispose();
            this.error = null;
        }
Esempio n. 7
0
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            this.State = PeerToPeerNetworkState.Closed;

            if (!(this.tcpListener is null))
            {
                this.tcpListener.Stop();
                this.tcpListener = null;
            }

            if (this.tcpMappingAdded)
            {
                this.tcpMappingAdded = false;
                try
                {
                    this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, (ushort)this.localEndpoint.Port, "TCP");
                }
                catch (Exception)
                {
                    // Ignore
                }
            }

            if (this.udpMappingAdded)
            {
                this.udpMappingAdded = false;
                try
                {
                    this.serviceWANIPConnectionV1.DeletePortMapping(string.Empty, (ushort)this.localEndpoint.Port, "UDP");
                }
                catch (Exception)
                {
                    // Ignore
                }
            }

            this.serviceWANIPConnectionV1 = null;

            if (!(this.upnpClient is null))
            {
                this.upnpClient.Dispose();
                this.upnpClient = null;
            }

            if (!(this.ipAddressesFound is null))
            {
                this.ipAddressesFound.Clear();
                this.ipAddressesFound = null;
            }

            if (!(this.ready is null))
            {
                this.ready.Close();
                this.ready = null;
            }

            if (!(this.error is null))
            {
                this.error.Close();
                this.error = null;
            }
        }