public void Connect(IPeerNeighbor neighbor, ConnectInfo connectInfo)
 {
     if (this.state == State.Opened)
     {
         PeerCloseReason none = PeerCloseReason.None;
         if ((neighbor.IsInitiator || !connectInfo.HasBody()) || ((neighbor.State != PeerNeighborState.Connecting) && (neighbor.State != PeerNeighborState.Closed)))
         {
             none = PeerCloseReason.InvalidNeighbor;
         }
         else if (this.RemoveTimer(neighbor))
         {
             if (this.neighborManager.ConnectedNeighborCount >= this.config.MaxNeighbors)
             {
                 none = PeerCloseReason.NodeBusy;
             }
             else if (!PeerValidateHelper.ValidNodeAddress(connectInfo.Address))
             {
                 none = PeerCloseReason.InvalidNeighbor;
             }
             else
             {
                 PeerCloseReason reason2;
                 IPeerNeighbor   neighbor2;
                 string          action = "http://schemas.microsoft.com/net/2006/05/peer/Refuse";
                 this.ValidateNeighbor(neighbor, connectInfo.NodeId, out neighbor2, out reason2, out action);
                 if (neighbor != neighbor2)
                 {
                     this.SendWelcome(neighbor);
                     try
                     {
                         neighbor.ListenAddress = connectInfo.Address;
                     }
                     catch (ObjectDisposedException exception)
                     {
                         DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                     }
                     if (!neighbor.TrySetState(PeerNeighborState.Connected) && (neighbor.State < PeerNeighborState.Disconnecting))
                     {
                         throw Fx.AssertAndThrow("Neighbor state expected to be >= Disconnecting; it is " + neighbor.State.ToString());
                     }
                     if (neighbor2 != null)
                     {
                         this.SendTerminatingMessage(neighbor2, action, reason2);
                         this.neighborManager.CloseNeighbor(neighbor2, reason2, PeerCloseInitiator.LocalNode);
                     }
                 }
                 else
                 {
                     none = reason2;
                 }
             }
         }
         if (none != PeerCloseReason.None)
         {
             this.SendTerminatingMessage(neighbor, "http://schemas.microsoft.com/net/2006/05/peer/Refuse", none);
             this.neighborManager.CloseNeighbor(neighbor, none, PeerCloseInitiator.LocalNode);
         }
     }
 }
        // Maintainer is expected to validate and accept the contents of referrals
        // and to determine how many referrals it will accept from the array.
        // Neighbor reference is passed in case the Maintainer decided to reject a referral
        // based on invalid content and close the neighbor.
        public bool AddReferrals(IList <Referral> referrals, IPeerNeighbor neighbor)
        {
            Fx.Assert(null != config.Resolver, "");

            bool valid             = true;
            bool canShareReferrals = false;

            try
            {
                canShareReferrals = config.Resolver.CanShareReferrals;
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(SR.GetString(SR.ResolverException), e);
            }
            if (referrals != null && canShareReferrals)
            {
                foreach (Referral referral in referrals)
                {
                    // If any referral is invalid then the connection is bad so don't accept any referals from this neighbor.
                    if (referral == null ||
                        referral.NodeId == PeerTransportConstants.InvalidNodeId ||
                        !PeerValidateHelper.ValidNodeAddress(referral.Address) ||
                        !PeerValidateHelper.ValidReferralNodeAddress(referral.Address))
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    lock (ThisLock)
                    {
                        foreach (Referral referral in referrals)
                        {
                            EndpointAddress key = referral.Address.EndpointAddress;
                            if (referralCache.Count <= this.config.MaxReferralCacheSize && !referralCache.ContainsKey(key))
                            {
                                referralCache.Add(key, referral);
                            }
                        }
                    }

                    // Invoke any handler that is interested in Referrals being added.
                    ReferralsAddedHandler handler = ReferralsAdded;
                    if (handler != null)
                    {
                        ReferralsAdded(referrals, neighbor);
                    }
                }
            }
            return(valid);
        }
 private void UpdateEndpointsCollection(PeerNodeAddress address)
 {
     if (PeerValidateHelper.ValidNodeAddress(address))
     {
         Uri endpointUri = GetEndpointUri(address);
         if (!this.nodeAddresses.ContainsKey(endpointUri) && (endpointUri != GetEndpointUri(this.maintainer.GetListenAddress())))
         {
             this.nodeAddresses[endpointUri] = address;
         }
     }
 }
 void UpdateEndpointsCollection(PeerNodeAddress address)
 {
     // Don't accept invalid addresses
     if (PeerValidateHelper.ValidNodeAddress(address))
     {
         Uri key = GetEndpointUri(address);
         if (!nodeAddresses.ContainsKey(key) && key != GetEndpointUri(maintainer.GetListenAddress()))
         {
             nodeAddresses[key] = address;
         }
     }
 }
Esempio n. 5
0
        public bool AddReferrals(IList <Referral> referrals, IPeerNeighbor neighbor)
        {
            bool flag = true;
            bool canShareReferrals = false;

            try
            {
                canShareReferrals = this.config.Resolver.CanShareReferrals;
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(System.ServiceModel.SR.GetString("ResolverException"), exception);
            }
            if ((referrals != null) && canShareReferrals)
            {
                foreach (Referral referral in referrals)
                {
                    if (((referral == null) || (referral.NodeId == 0L)) || (!PeerValidateHelper.ValidNodeAddress(referral.Address) || !PeerValidateHelper.ValidReferralNodeAddress(referral.Address)))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    lock (this.ThisLock)
                    {
                        foreach (Referral referral2 in referrals)
                        {
                            EndpointAddress endpointAddress = referral2.Address.EndpointAddress;
                            if ((this.referralCache.Count <= this.config.MaxReferralCacheSize) && !this.referralCache.ContainsKey(endpointAddress))
                            {
                                this.referralCache.Add(endpointAddress, referral2);
                            }
                        }
                    }
                    if (this.ReferralsAdded != null)
                    {
                        this.ReferralsAdded(referrals, neighbor);
                    }
                }
            }
            return(flag);
        }
Esempio n. 6
0
        //<Implementation of PeerConnector.IPeerConnectorContract>
        // Process Connect from the neighbor
        public void Connect(IPeerNeighbor neighbor, ConnectInfo connectInfo)
        {
            // Don't bother processing the message if Connector has closed
            if (this.state != State.Opened)
            {
                return;
            }

            PeerCloseReason closeReason = PeerCloseReason.None;

            // A connect message should only be received by a responder neighbor that is
            // in Connecting state. If not, we close the neighbor without bothering
            // to send a Refuse message
            // A malicious neighbor can format a message with a null connectInfo as an argument
            if (neighbor.IsInitiator || !connectInfo.HasBody() || (neighbor.State != PeerNeighborState.Connecting &&
                                                                   neighbor.State != PeerNeighborState.Closed))
            {
                closeReason = PeerCloseReason.InvalidNeighbor;
            }

            // Remove the timer from the timer table for this neighbor. If the timer is not
            // present, the neighbor is already being closed and the Connect message should
            // be ignored.
            else if (RemoveTimer(neighbor))
            {
                // Determine if Welcome or Refuse should be sent

                // Refuse if node has maximum allowed connected neighbors?
                if (this.neighborManager.ConnectedNeighborCount >= this.config.MaxNeighbors)
                {
                    closeReason = PeerCloseReason.NodeBusy;
                }
                else
                {
                    // Deserialization failed or connect info is invalid?
                    if (!PeerValidateHelper.ValidNodeAddress(connectInfo.Address))
                    {
                        closeReason = PeerCloseReason.InvalidNeighbor;
                    }
                    else
                    {
                        // Determine if neighbor should be accepted.
                        PeerCloseReason closeReason2;
                        IPeerNeighbor   neighborToClose;
                        string          action = PeerStrings.RefuseAction;
                        ValidateNeighbor(neighbor, connectInfo.NodeId, out neighborToClose, out closeReason2, out action);

                        if (neighbor != neighborToClose)    // new neighbor should be accepted
                        {
                            SendWelcome(neighbor);
                            try
                            {
                                neighbor.ListenAddress = connectInfo.Address;
                            }
                            catch (ObjectDisposedException e)
                            {
                                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                            }

                            if (!neighbor.TrySetState(PeerNeighborState.Connected))
                            {
                                if (!(neighbor.State >= PeerNeighborState.Disconnecting))
                                {
                                    throw Fx.AssertAndThrow("Neighbor state expected to be >= Disconnecting; it is " + neighbor.State.ToString());
                                }
                            }

                            if (neighborToClose != null)
                            {
                                // The other neighbor should be closed
                                SendTerminatingMessage(neighborToClose, action, closeReason2);
                                this.neighborManager.CloseNeighbor(neighborToClose, closeReason2, PeerCloseInitiator.LocalNode);
                            }
                        }
                        else
                        {
                            closeReason = closeReason2;
                        }
                    }
                }
            }

            if (closeReason != PeerCloseReason.None)
            {
                SendTerminatingMessage(neighbor, PeerStrings.RefuseAction, closeReason);
                this.neighborManager.CloseNeighbor(neighbor, closeReason, PeerCloseInitiator.LocalNode);
            }
        }