Exemplo n.º 1
0
 internal void SendFirstPeerTestToCharlie(PeerTest msg)
 {
     lock ( Sessions )
     {
         if (Sessions.Count > 0)
         {
             Sessions.Random().SendFirstPeerTestToCharlie(msg);
         }
     }
 }
Exemplo n.º 2
0
        void SendFirstPeerTestToCharlie(PeerTest msg)
        {
            SendMessage(
                SSUHeader.MessageTypes.PeerTest,
                Session.MACKey,
                Session.SharedKey,
                (start, writer) =>
            {
#if NO_LOG_ALL_TRANSPORT
                DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Bob, relaying first message to Charlie. " + msg.ToString());
#endif
                msg.WriteTo(writer);

                return(true);
            });
        }
Exemplo n.º 3
0
        private void HandleIncomingPeerTestPackage(BufRefLen reader)
        {
            // We are Alice or Charlie and are receiving probe packet with intro key.

            var msg = new PeerTest(reader);

            if (Session.Host.PeerTestInstance.OurTestNonce(msg.TestNonce))
            {
                // We are alice running a test
#if NO_LOG_ALL_TRANSPORT
                DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Alice, and are getting a direct probe from Charlie. " + msg.ToString());
#endif
                Session.Host.PeerTestInstance.CharlieDirectResponseReceived(msg);
                return;
            }

            var nonceinfo = Session.Host.GetNonceInfo(msg.TestNonce.Peek32(0));
            if (nonceinfo == null)
            {
                DebugUtils.LogDebug("SSU PeerTest " + Session.DebugId + " Created state: HandleIncomingPeerTestPackage received an unknown nonce. Dropped.");
                return;
            }

            if (nonceinfo.Role == PeerTestRole.Charlie)
            {
                // We are Charlie and are getting a direct probe from Alice

                SendMessage(
                    SSUHeader.MessageTypes.PeerTest,
                    msg.IntroKey,
                    msg.IntroKey,
                    (start, writer) =>
                {
                    var toalice = new PeerTest(msg.TestNonce, msg.AliceIPAddr, msg.AlicePort, Session.MyRouterContext.IntroKey);
#if NO_LOG_ALL_TRANSPORT
                    DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Charlie, and are getting a direct probe from Alice. " + toalice.ToString());
#endif
                    toalice.WriteTo(writer);

                    return(true);
                });
            }
            else
            {
                DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": Unexpected PeerTest received: " + msg.ToString());
            }
        }
Exemplo n.º 4
0
        private void RespondToPeerTestInitiationFromAlice(PeerTest msg, PeerTestNonceInfo nonceinfo)
        {
            // Nonce already in use for another test? Just ignore.
            if (nonceinfo != null)
            {
#if NO_LOG_ALL_TRANSPORT
                DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Bob getting a iniation from Alice, but will drop it due to nonce clash. " +
                               msg.ToString());
#endif
                return;
            }

            Session.Host.SetNonceInfo(msg.TestNonce.Peek32(0), PeerTestRole.Bob);

            var pt = new PeerTest(msg.TestNonce,
                                  Session.RemoteEP.Address, Session.RemoteEP.Port,
                                  msg.IntroKey);
#if NO_LOG_ALL_TRANSPORT
            DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Bob and sending first relay to Charlie: " + pt.ToString());
#endif
            Session.Host.SendFirstPeerTestToCharlie(pt);
            return;
        }
Exemplo n.º 5
0
        private void HandleIncomingPeerTestPackage(BufRefLen reader)
        {
            var msg       = new PeerTest(reader);
            var nonceinfo = Session.Host.GetNonceInfo(msg.TestNonce.Peek32(0));

            if (msg.AliceIPAddr.Length == 0 && msg.AlicePort.Peek16(0) == 0)
            {
                // We are Bob, and we got the first message from Alice
                RespondToPeerTestInitiationFromAlice(msg, nonceinfo);
                return;
            }

            if (!msg.IPAddressOk)
            {
#if NO_LOG_ALL_TRANSPORT
                DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": HandleIncomingPeerTestPackage: IP Address not accepted. Ignorning. " + msg.ToString());
#endif
                return;
            }

            if (nonceinfo != null)
            {
                switch (nonceinfo.Role)
                {
                case PeerTestRole.Bob:
                {
                    // Got initial response from Charlie
                    // Try to contact Alice
                    SendMessage(
                        new IPEndPoint(new IPAddress(msg.AliceIPAddr.ToByteArray()), msg.AlicePort.PeekFlip16(0)),
                        SSUHeader.MessageTypes.PeerTest,
                        msg.IntroKey,
                        msg.IntroKey,
                        (start, writer) =>
                        {
#if NO_LOG_ALL_TRANSPORT
                            DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Bob, relaying response from Charlie to Alice " + msg.ToString());
#endif
                            msg.WriteTo(writer);

                            return(true);
                        });
                }
                    return;

                case PeerTestRole.Charlie:
                {
                    // We got the final test from Alice
                    SendMessage(
                        SSUHeader.MessageTypes.PeerTest,
                        Session.MACKey,
                        Session.SharedKey,
                        (start, writer) =>
                        {
#if NO_LOG_ALL_TRANSPORT
                            DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Charlie, responding to the final message from Alice " + msg.ToString());
#endif
                            msg.WriteTo(writer);

                            return(true);
                        });
                }
                    return;

                default:
                    DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": Unexpedted PeerTest received. " + msg.ToString());
                    break;
                }
            }
            else
            {
                // We are Charlie receiving a relay from Bob.
                Session.Host.SetNonceInfo(msg.TestNonce.Peek32(0), PeerTestRole.Charlie);

                // Reply to Bob
                SendMessage(
                    SSUHeader.MessageTypes.PeerTest,
                    Session.MACKey,
                    Session.SharedKey,
                    (start, writer) =>
                {
#if NO_LOG_ALL_TRANSPORT
                    DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Charlie, responding to Bob. " + msg.ToString());
#endif
                    msg.WriteTo(writer);

                    return(true);
                });

                // Try to contact Alice
                var toalice = new PeerTest(msg.TestNonce, msg.AliceIPAddr, msg.AlicePort, Session.MyRouterContext.IntroKey);
                SendMessage(
                    new IPEndPoint(new IPAddress(msg.AliceIPAddr.ToByteArray()), msg.AlicePort.PeekFlip16(0)),
                    SSUHeader.MessageTypes.PeerTest,
                    msg.IntroKey,
                    msg.IntroKey,
                    (start, writer) =>
                {
#if NO_LOG_ALL_TRANSPORT
                    DebugUtils.Log("SSU PeerTest " + Session.DebugId + ": We are Charlie, sending first probe to Alice. " + toalice.ToString());
#endif
                    toalice.WriteTo(writer);

                    return(true);
                });
            }
        }
Exemplo n.º 6
0
 internal void SendFirstPeerTestToCharlie(PeerTest msg)
 {
     QueuedFirstPeerTestToCharlie = msg;
 }
Exemplo n.º 7
0
 internal void CharlieDirectResponseReceived(PeerTest msg)
 {
 }