Esempio n. 1
0
        private void SendResponse(DiscoveryPacket receivedQueryPacket, BinaryNumber networkID, IPAddress remotePeerIP)
        {
            NetworkInfo network = NetUtilities.GetNetworkInfo(remotePeerIP);

            DiscoveryPacket responsePacket = DiscoveryPacket.CreateResponsePacket(_announcePort, receivedQueryPacket.Challenge);

            byte[] packet = responsePacket.ToArray(networkID, network.LocalIP);

            _listener.SendTo(packet, 0, packet.Length, remotePeerIP);
        }
Esempio n. 2
0
            private void NetworkWatcher(object state)
            {
                try
                {
                    List <NetworkInfo> currentNetworks = NetUtilities.GetNetworkInfo();
                    List <NetworkInfo> newNetworks     = new List <NetworkInfo>();

                    lock (_networks)
                    {
                        foreach (NetworkInfo currentNetwork in currentNetworks)
                        {
                            if (!_networks.Contains(currentNetwork))
                            {
                                newNetworks.Add(currentNetwork);
                            }
                        }

                        _networks.Clear();
                        _networks.AddRange(currentNetworks);
                    }

                    if (newNetworks.Count > 0)
                    {
                        BroadcastNetworkDiscovered(newNetworks.ToArray());
                    }
                }
                catch
                { }
                finally
                {
                    if (_networkWatcher != null)
                    {
                        _networkWatcher.Change(NETWORK_WATCHER_INTERVAL, Timeout.Infinite);
                    }
                }
            }
Esempio n. 3
0
            public Listener(int listenerPort)
            {
                _listenerPort = listenerPort;

                switch (Environment.OSVersion.Platform)
                {
                case PlatformID.Win32NT:
                    if (Environment.OSVersion.Version.Major < 6)
                    {
                        //below vista
                        _udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    }
                    else
                    {
                        //vista & above
                        _udpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                        _udpListener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                    }
                    break;

                case PlatformID.Unix:     //mono framework
                    if (Socket.OSSupportsIPv6)
                    {
                        _udpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                    }
                    else
                    {
                        _udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    }

                    break;

                default:     //unknown
                    _udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    break;
                }

                _udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
                _udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                if (_udpListener.AddressFamily == AddressFamily.InterNetwork)
                {
                    _udpListener.Bind(new IPEndPoint(IPAddress.Any, listenerPort));
                }
                else
                {
                    _udpListener.Bind(new IPEndPoint(IPAddress.IPv6Any, listenerPort));

                    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                    {
                        if ((nic.OperationalStatus == OperationalStatus.Up) && (nic.Supports(NetworkInterfaceComponent.IPv6)) && nic.SupportsMulticast)
                        {
                            try
                            {
                                _udpListener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(IPAddress.Parse(IPV6_MULTICAST_IP), nic.GetIPProperties().GetIPv6Properties().Index));
                            }
                            catch
                            { }
                        }
                    }
                }

                _networks = NetUtilities.GetNetworkInfo();

                _udpListenerThread = new Thread(RecvDataAsync);
                _udpListenerThread.IsBackground = true;
                _udpListenerThread.Start(_udpListener);

                _networkWatcher = new Timer(NetworkWatcher, null, NETWORK_WATCHER_INTERVAL, Timeout.Infinite);
            }
Esempio n. 4
0
        private void NetworkWatcherAsync(object state)
        {
            try
            {
                bool networkChanged            = false;
                List <NetworkInfo> newNetworks = new List <NetworkInfo>();

                {
                    List <NetworkInfo> currentNetworks = NetUtilities.GetNetworkInfo();

                    networkChanged = (currentNetworks.Count != _networks.Count);

                    foreach (NetworkInfo currentNetwork in currentNetworks)
                    {
                        if (!_networks.Contains(currentNetwork))
                        {
                            networkChanged = true;
                            newNetworks.Add(currentNetwork);
                        }
                    }

                    _networks.Clear();
                    _networks.AddRange(currentNetworks);
                }

                if (networkChanged)
                {
                    lock (_localNetworkDhtManagers)
                    {
                        //remove local network dht manager with offline networks
                        {
                            List <LocalNetworkDhtManager> localNetworkDhtManagersToRemove = new List <LocalNetworkDhtManager>();

                            foreach (LocalNetworkDhtManager localNetworkDhtManager in _localNetworkDhtManagers)
                            {
                                if (!_networks.Contains(localNetworkDhtManager.Network))
                                {
                                    localNetworkDhtManagersToRemove.Add(localNetworkDhtManager);
                                }
                            }

                            foreach (LocalNetworkDhtManager localNetworkDhtManager in localNetworkDhtManagersToRemove)
                            {
                                localNetworkDhtManager.Dispose();
                                _localNetworkDhtManagers.Remove(localNetworkDhtManager);
                            }
                        }

                        //add local network dht managers for new online networks
                        if (newNetworks.Count > 0)
                        {
                            foreach (NetworkInfo network in newNetworks)
                            {
                                if (IPAddress.IsLoopback(network.LocalIP))
                                {
                                    continue; //skip loopback networks
                                }
                                if (network.LocalIP.AddressFamily == AddressFamily.InterNetworkV6)
                                {
                                    continue; //skip ipv6 private networks for saving resources
                                }
                                if (!NetUtilities.IsPrivateIP(network.LocalIP))
                                {
                                    continue; //skip public networks
                                }
                                _localNetworkDhtManagers.Add(new LocalNetworkDhtManager(network));

                                Debug.Write(this.GetType().Name, "local network dht manager created: " + network.LocalIP.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write(this.GetType().Name, ex);
            }
        }