示例#1
0
 /// <summary>Match the end point with another by IP and port.</summary>
 public static bool Match(this IPEndPoint endPoint, IPEndPoint matchWith)
 {
     return(endPoint.MatchIpOnly(matchWith) && endPoint.Port == matchWith.Port);
 }
        /// <summary>
        /// Determines if the peer should be disconnected.
        /// Peer should be disconnected in case it's IP is from the same group in which any other peer
        /// is and the peer wasn't added using -connect or -addNode command line arguments.
        /// </summary>
        private bool CheckIfPeerFromSameNetworkGroup(IReadOnlyNetworkPeerCollection networkPeers, IPEndPoint ipEndpoint, List <IPEndPoint> iprangeFilteringExclusions)
        {
            // Don't disconnect if range filtering is not turned on.
            if (!this.connectionManagerSettings.IpRangeFiltering)
            {
                this.logger.LogTrace("(-)[IP_RANGE_FILTERING_OFF]:false");
                return(false);
            }

            // Don't disconnect if this peer has a local host address.
            if (ipEndpoint.Address.IsLocal())
            {
                this.logger.LogTrace("(-)[IP_IS_LOCAL]:false");
                return(false);
            }

            // Don't disconnect if this peer is in -addnode or -connect.
            if (this.connectionManagerSettings.RetrieveAddNodes().Union(this.connectionManagerSettings.Connect).Any(ep => ipEndpoint.MatchIpOnly(ep)))
            {
                this.logger.LogTrace("(-)[ADD_NODE_OR_CONNECT]:false");
                return(false);
            }

            // Don't disconnect if this peer is in the exclude from IP range filtering group.
            if (iprangeFilteringExclusions.Any(ip => ip.MatchIpOnly(ipEndpoint)))
            {
                this.logger.LogTrace("(-)[PEER_IN_IPRANGEFILTER_EXCLUSIONS]:false");
                return(false);
            }

            byte[] peerGroup = ipEndpoint.MapToIpv6().Address.GetGroup();

            foreach (INetworkPeer connectedPeer in networkPeers)
            {
                if (ipEndpoint == connectedPeer.PeerEndPoint)
                {
                    continue;
                }

                byte[] group = connectedPeer.PeerEndPoint.MapToIpv6().Address.GetGroup();

                if (peerGroup.SequenceEqual(group))
                {
                    this.logger.LogTrace("(-)[SAME_GROUP]:true");
                    return(true);
                }
            }

            return(false);
        }