Exemplo n.º 1
0
 internal void ReportRelayResponse(SSUHeader header, RelayResponse response, IPEndPoint ep)
 {
     if (RelayResponseReceived != null)
     {
         lock ( RelayResponseReceivedLock )
         {
             RelayResponseReceived(header, response, ep);
         }
     }
 }
Exemplo n.º 2
0
        public override SSUState HandleMessage(SSUHeader header, BufRefLen reader)
        {
            if (header.MessageType == SSUHeader.MessageTypes.RelayResponse)
            {
                var response = new RelayResponse(reader);
                return(HandleRelayResponse(response));
            }

            return(this);
        }
Exemplo n.º 3
0
        public override SSUState HandleMessage(SSUHeader header, BufRefLen reader)
        {
            switch (header.MessageType)
            {
            case SSUHeader.MessageTypes.SessionRequest:
                var req = new SessionRequest(reader, I2PKeyType.DefaultAsymetricKeyCert);
                Logging.LogTransport($"SSU SessionCreatedState {Session.DebugId}: OK SessionRequest received.");

                BufUtils.DHI2PToSessionAndMAC(out var sessionkey, out var mackey,
                                              req.XKey.ModPow(PrivateKey.ToBigInteger(), I2PConstants.ElGamalP));

                Session.MACKey    = mackey;
                Session.SharedKey = sessionkey;

                Session.Host.ReportedAddress(new IPAddress(req.Address.ToByteArray()));

                // TODO: Remove comment when relaying is implemented

                /*
                 * if ( header.ExtendedOptions != null )
                 * {
                 *  if ( header.ExtendedOptions.Length == 2 && ( ( header.ExtendedOptions[0] & 0x01 ) != 0 ) )
                 *  {
                 *      RelayTag = BufUtils.RandomUint();
                 *  }
                 * }*/

                Request = req;
                SendSessionCreated();

                return(this);

            case SSUHeader.MessageTypes.RelayResponse:
                Logging.LogTransport(string.Format("SSU SessionCreatedState {0}: RelayResponse received from {1}.",
                                                   Session.DebugId, (Session.RemoteEP == null ? "<null>" : Session.RemoteEP.ToString())));
                var response = new RelayResponse(reader);
                Session.Host.ReportRelayResponse(header, response, Session.RemoteEP);
                break;

            case SSUHeader.MessageTypes.SessionConfirmed:
                return(ParseSessionConfirmed(header, reader));

            case SSUHeader.MessageTypes.PeerTest:
                HandleIncomingPeerTestPackage(reader);
                break;

            default:
                Logging.LogTransport($"SSU SessionCreatedState: Session {Session.DebugId} Unexpected Message: {header.MessageType}");
                break;
            }

            return(this);
        }
Exemplo n.º 4
0
        void Host_RelayResponseReceived(SSUHeader header, RelayResponse response, IPEndPoint ep)
        {
            Logging.LogTransport(
                $"SSU RelayRequestState: {Session.DebugId} RelayResponse from {ep} received.");

            if (header.MessageType == SSUHeader.MessageTypes.RelayResponse)
            {
                if (Introducers.Any(i => i.Value.RemoteEP == ep))
                {
                    HandleRelayResponse(response);
                }
                else
                {
                    Logging.LogTransport(
                        $"SSU RelayRequestState: {Session.DebugId} RelayResponse from {ep} received. " +
                        $"Ignored as not in wait set [{string.Join( ", ", Introducers.Select( i => i.Value.RemoteEP ) )}].");
                }
            }
        }
Exemplo n.º 5
0
        SSUState HandleRelayResponse(RelayResponse response)
        {
            var cep = response.CharlieEndpoint;

            Logging.LogTransport($"SSU RelayRequestState: {Session.DebugId} RelayResponse {response}");

            var noncematch = Nonce == response.Nonce.Peek32(0);

            Logging.LogTransport($"SSU RelayRequestState: {Session.DebugId} Nonce match: {noncematch}");
            if (!noncematch)
            {
                return(this);
            }

            Session.RemoteEP = response.CharlieEndpoint;
            Session.Host.RelayResponseReceived -= Host_RelayResponseReceived;

            NextState = new SessionRequestState(Session, true);
            return(NextState);
        }
Exemplo n.º 6
0
        public override SSUState HandleMessage(SSUHeader header, BufRefLen reader)
        {
            switch (header.MessageType)
            {
            case SSUHeader.MessageTypes.Data:
                try
                {
                    var datamsg = new SSUDataMessage(reader, Session.Defragmenter);
                    if (datamsg.ExplicitAcks != null)
                    {
                        Session.Fragmenter.GotAck(datamsg.ExplicitAcks);
                    }
                    if (datamsg.AckBitfields != null)
                    {
                        Session.Fragmenter.GotAck(datamsg.AckBitfields);
                    }
                    if (datamsg.NewMessages != null)
                    {
                        foreach (var msg in datamsg.NewMessages)
                        {
                            var i2npmsg = I2NPMessage.ReadHeader16((BufRefLen)msg.GetPayload());

#if LOG_MUCH_TRANSPORT
                            Logging.LogDebugData($"SSU {this} complete message " +
                                                 $"{msg.MessageId}: {i2npmsg.Expiration}");
#endif

                            if (i2npmsg.MessageType == I2NPMessage.MessageTypes.DeliveryStatus)
                            {
                                if (((DeliveryStatusMessage)i2npmsg.Message).IsNetworkId((ulong)I2PConstants.I2P_NETWORK_ID))
                                {
                                    continue;
                                }
                            }

                            Session.MessageReceived(i2npmsg);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Log("EstablishedState: SSUHost.SSUMessageTypes.Data", ex);
                }
                break;

            case SSUHeader.MessageTypes.PeerTest:
                HandleIncomingPeerTestPackage(reader);
                break;

            case SSUHeader.MessageTypes.RelayResponse:
                Logging.LogTransport($"SSU EstablishedState {Session.DebugId}: RelayResponse received from {Session.RemoteEP}.");
                var response = new RelayResponse(reader);
                Session.Host.ReportRelayResponse(header, response, Session.RemoteEP);
                break;

            case SSUHeader.MessageTypes.RelayIntro:
                var intro = new RelayIntro(reader);
                Logging.LogTransport($"SSU EstablishedState {Session.DebugId}: RelayIntro received from {Session.RemoteEP} for {intro.AliceEndpoint}.");

                var data = new BufLen(new byte[12]);
                data.Randomize();
                Send(intro.AliceEndpoint, data);

                ++Session.Host.EPStatisitcs[Session.RemoteEP].RelayIntrosReceived;
                ++Session.RelayIntroductionsReceived;
                break;

            case SSUHeader.MessageTypes.RelayRequest:
                // TODO: Implement
                // if ( !SSUHost.IntroductionSupported ) throw new Exception( "SSU relay introduction not supported" );
                Logging.LogTransport(string.Format("SSU EstablishedState {0}: Relay introduction not supported.", Session.DebugId));
                break;

            case SSUHeader.MessageTypes.SessionRequest:
                Logging.LogTransport(string.Format("SSU EstablishedState {0}: SessionRequest received. Ending session.", Session.DebugId));
                SendSessionDestroyed();
                return(null);

            default:
                Logging.LogTransport(string.Format("SSU EstablishedState {0}: Unexpected message received: {1}.",
                                                   Session.DebugId, header.MessageType));
                break;
            }

            return(this);
        }