Beispiel #1
0
        internal RelayRequestState(SSUSession sess, Dictionary <IntroducerInfo, SSUSession> introducers) : base(sess)
        {
            if (!introducers.Any())
            {
                if (Session.RemoteEP != null)
                {
                    Logging.LogTransport($"SSU RelayRequestState {Session.DebugId} no established sessions to introducers. Trying direct connect.");
                    NextState = new SessionRequestState(Session, false);
                }
                else
                {
                    throw new FailedToConnectException($"SSU RelayRequestState {Session.DebugId} no established sessions to introducers");
                }
            }

            Introducers = introducers;

            foreach (var one in introducers)
            {
                Logging.LogTransport($"RelayRequestState {Session.DebugId} " +
                                     $"Trying {one.Key.EndPoint} to reach " +
                                     $"{Session.RemoteAddr.Options.TryGet( "host" )?.ToString() ?? Session.RemoteAddr.Options.ToString()}");
            }
            Session.Host.RelayResponseReceived += new SSUHost.RelayResponseInfo(Host_RelayResponseReceived);
        }
Beispiel #2
0
        private PeerTestState(SSUHost host, I2PRouterInfo router)
        {
            Host   = host;
            Router = router;

            Session = (SSUSession)Host.AddSession(router);
            //Session.StartPeerTest( this );
        }
Beispiel #3
0
        private PeerTestState(SSUHost host, I2PRouterAddress addr, I2PKeysAndCert dest)
        {
            Host = host;
            Addr = addr;
            Dest = dest;

            Session = (SSUSession)Host.AddSession(addr, dest);
            //Session.StartPeerTest( this );
        }
Beispiel #4
0
        public SessionRequestState(SSUSession sess, bool remoteisfirewalled) : base(sess)
        {
            RemoteIsFirewalled = remoteisfirewalled;

            var keys = I2PPrivateKey.GetNewKeyPair();

            PrivateKey = keys.PrivateKey;
            X          = keys.PublicKey;
        }
Beispiel #5
0
        public SessionCreatedState(SSUSession sess)
            : base(sess)
        {
            var keys = I2PPrivateKey.GetNewKeyPair();

            PrivateKey = keys.PrivateKey;
            Y          = keys.PublicKey;

            Session.MACKey    = Session.MyRouterContext.IntroKey;
            Session.SharedKey = Session.MyRouterContext.IntroKey;
        }
Beispiel #6
0
 internal void NoCpu(SSUSession sess)
 {
     lock ( NeedsCpu )
     {
         if (!NeedsCpu.Contains(sess))
         {
             return;
         }
         NeedsCpu.Remove(sess);
     }
 }
Beispiel #7
0
 internal void NeedCpu(SSUSession sess)
 {
     lock ( NeedsCpu )
     {
         if (NeedsCpu.Contains(sess))
         {
             return;
         }
         NeedsCpu.Add(sess);
     }
 }
Beispiel #8
0
        public ITransport AddSession(I2PRouterInfo router)
        {
            IPEndPoint remoteep = null;
            IPEndPoint key      = null;

            var addr = router.Adresses.First(a => (a.TransportStyle == "SSU") &&
                                             a.Options.Contains("key") &&
                                             (RouterContext.Inst.UseIpV6 ||
                                              a.Options.ValueContains("host", ".") ||
                                              a.Options.ValueContains("ihost0", ".")));
            var dest = router.Identity;

            if (addr.HaveHostAndPort)
            {
                remoteep = new IPEndPoint(addr.Host, addr.Port);

                if (!AllowConnectToSelf && IsOurIP(remoteep.Address))
                {
                    Logging.LogTransport($"SSU AddSession: [{dest.IdentHash.Id32}]:{key} - {addr}. Dropped. Not connecting to ourselves.");
                    return(null);
                }

                key = remoteep;

                Logging.LogDebugData($"SSU AddSession: [{dest.IdentHash.Id32}]:{key} - {addr}");

                lock ( Sessions )
                {
                    if (Sessions.ContainsKey(key))
                    {
                        var sess = Sessions[key];
                        return(sess);
                    }
                }
            }

            var newsess = new SSUSession(
                this,
                Send,
                remoteep,
                addr,
                dest,
                MyRouterContext);

            if (key != null)
            {
                lock ( Sessions )
                {
                    Sessions[key] = newsess;
                }
            }
            return(newsess);
        }
Beispiel #9
0
        void AddFailedSession(SSUSession sess)
        {
            if (sess == null)
            {
                return;
            }

            sess.Terminate();

            lock ( FailedSessions )
            {
                FailedSessions.Add(sess);
            }
        }
Beispiel #10
0
        public ITransport AddSession(I2PRouterAddress addr, I2PKeysAndCert dest)
        {
            IPEndPoint remoteep = null;
            IPEndPoint key      = null;

            if (addr.HaveHostAndPort)
            {
                remoteep = new IPEndPoint(addr.Host, addr.Port);

                if (!AllowConnectToSelf && IsOurIP(remoteep.Address))
                {
                    Logging.LogTransport($"SSU AddSession: [{dest.IdentHash.Id32}]:{key} - {addr}. Dropped. Not connecting to ourselves.");
                    return(null);
                }

                key = remoteep;

                Logging.LogDebugData($"SSU AddSession: [{dest.IdentHash.Id32}]:{key} - {addr}");

                lock ( Sessions )
                {
                    if (Sessions.ContainsKey(key))
                    {
                        var sess = Sessions[key];
                        return(sess);
                    }
                }
            }

            var newsess = new SSUSession(
                this,
                Send,
                remoteep,
                addr,
                dest,
                MTUProvider,
                MyRouterContext);

            if (key != null)
            {
                lock ( Sessions )
                {
                    Sessions[key] = newsess;
                }
            }
            return(newsess);
        }
Beispiel #11
0
        private void RemoveSession(SSUSession sess)
        {
            lock ( NeedsCpu )
            {
                if (NeedsCpu.Contains(sess))
                {
                    NeedsCpu.Remove(sess);
                }
            }

            lock ( Sessions )
            {
                var key = Sessions
                          .Where(s => s.Value == sess)
                          .Select(s => s.Key)
                          .FirstOrDefault();

                if (key != null)
                {
                    Sessions.Remove(key);
                }
            }
        }
Beispiel #12
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            SSUSession session = null;

            try
            {
                EndPoint ep   = RemoteEP;
                var      size = MySocket.EndReceiveFrom(ar, ref ep);

                if (ep.AddressFamily != AddressFamily.InterNetwork &&
                    (!SessionLayer.RouterContext.Inst.UseIpV6 ||
                     ep.AddressFamily != AddressFamily.InterNetworkV6))
                {
                    return;         // TODO: Add IPV6
                }
                if (size <= 37)
                {
                    Logging.LogDebugData($"SSU Recv: {size} bytes [0x{size:X}] from {ep} (hole punch, ignored)");
                    return;
                }

                var sessionendpoint = (IPEndPoint)ep;

                Logging.LogDebugData($"SSU Recv: {size} bytes [0x{size:X}] from {ep}");

                lock ( Sessions )
                {
                    if (!Sessions.ContainsKey(sessionendpoint))
                    {
                        if (IPFilter.IsFiltered(((IPEndPoint)ep).Address))
                        {
                            Logging.LogTransport($"SSUHost ReceiveCallback: IPAddress {sessionendpoint} is blocked. {size} bytes.");
                            return;
                        }

                        ++IncommingConnectionAttempts;

                        Logging.LogTransport($"SSUHost: incoming connection " +
                                             $"from {sessionendpoint} created.");

                        session = new SSUSession(
                            this,
                            Send,
                            (IPEndPoint)ep,
                            MyRouterContext);

                        Sessions[sessionendpoint] = session;

                        Logging.LogTransport($"SSUHost: incoming connection " +
                                             $"{session.DebugId} from {sessionendpoint} created.");

                        NeedCpu(session);
                        ConnectionCreated?.Invoke(session);
                    }
                    else
                    {
                        session = Sessions[sessionendpoint];
                    }
                }

                var localbuffer = BufRefLen.Clone(ReceiveBuf, 0, size);

#if DEBUG
                Stopwatch Stopwatch1 = new Stopwatch();
                Stopwatch1.Start();
#endif
                try
                {
                    session.Receive(localbuffer);
                }
                catch (ThreadAbortException taex)
                {
                    AddFailedSession(session);
                    Logging.Log(taex);
                }
                catch (ThreadInterruptedException tiex)
                {
                    AddFailedSession(session);
                    Logging.Log(tiex);
                }
                catch (ChecksumFailureException cfex)
                {
                    AddFailedSession(session);
                    Logging.Log(cfex);
                }
                catch (SignatureCheckFailureException scex)
                {
                    AddFailedSession(session);
                    Logging.Log(scex);
                }
                catch (FailedToConnectException fcex)
                {
                    AddFailedSession(session);
#if LOG_MUCH_TRANSPORT
                    Logging.LogTransport(fcex);
#endif
                    if (session != null)
                    {
                        NetDb.Inst.Statistics.FailedToConnect(session.RemoteRouterIdentity.IdentHash);
                        session.RaiseException(fcex);
                    }
                }
#if DEBUG
                Stopwatch1.Stop();
                if (Stopwatch1.ElapsedMilliseconds > SessionCallWarningLevelMilliseconds)
                {
                    Logging.LogTransport(
                        $"SSUHost ReceiveCallback: WARNING Session {session} used {Stopwatch1.ElapsedMilliseconds}ms cpu.");
                }
#endif
            }
            catch (Exception ex)
            {
                AddFailedSession(session);
                Logging.Log(ex);

                if (session != null && session.RemoteRouterIdentity != null && NetDb.Inst != null)
                {
                    NetDb.Inst.Statistics.DestinationInformationFaulty(session.RemoteRouterIdentity.IdentHash);
                    session.RaiseException(ex);
                }
            }
            finally
            {
                RemoteEP = LocalEP;
                MySocket.BeginReceiveFrom(ReceiveBuf, 0, ReceiveBuf.Length, SocketFlags.None, ref RemoteEP,
                                          new AsyncCallback(ReceiveCallback), MySocket);
            }
        }
Beispiel #13
0
        internal void IntroducerSessionTerminated(SSUSession s)
        {
            ConsiderUpdateIntroducers.TimeToAction = new TickSpan(0);

            Logging.LogTransport($"Introducer session terminated {s}");
        }
Beispiel #14
0
        private void RunSession(SSUSession sess, RunBatchWait sync)
        {
            if (sess.Terminated)
            {
                return;
            }

            try
            {
                lock ( sess )
                {
#if DEBUG
                    Stopwatch Stopwatch1 = new Stopwatch();
                    Stopwatch1.Start();
#endif
                    var running = sess.Run();
                    if (!running)
                    {
                        Logging.LogTransport($"SSUHost: Terminated Session {sess.DebugId} removed.");
                        RemoveSession(sess);
                    }
#if DEBUG
                    Stopwatch1.Stop();
                    if (Stopwatch1.ElapsedMilliseconds > SessionCallWarningLevelMilliseconds)
                    {
                        Logging.LogTransport(
                            $"SSUHost Run: WARNING Session {sess} used {Stopwatch1.ElapsedMilliseconds}ms cpu.");
                    }
#endif
                }
            }
            catch (ThreadAbortException taex)
            {
                AddFailedSession(sess);
                Logging.Log(taex);
            }
            catch (ThreadInterruptedException tiex)
            {
                AddFailedSession(sess);
                Logging.Log(tiex);
            }
            catch (ChecksumFailureException cfex)
            {
                AddFailedSession(sess);
                Logging.Log(cfex);
            }
            catch (SignatureCheckFailureException scex)
            {
                AddFailedSession(sess);
                Logging.Log(scex);
            }
            catch (EndOfStreamEncounteredException eosex)
            {
                AddFailedSession(sess);
                Logging.Log(eosex);
            }
            catch (FailedToConnectException fcex)
            {
                AddFailedSession(sess);
                Logging.LogTransport(
                    string.Format("SSUHost Run: Session failed to connect: {0}", fcex.Message));

                if (sess != null && sess.RemoteRouterIdentity != null)
                {
                    NetDb.Inst.Statistics.FailedToConnect(sess.RemoteRouterIdentity.IdentHash);
                }

                // Reserve the execption list for serious errors
                // sess.RaiseException( fcex );
            }
            catch (Exception ex)
            {
                AddFailedSession(sess);
                Logging.Log(ex);

                sess.RaiseException(ex);
            }
            finally
            {
                sync.Set();
            }
        }
Beispiel #15
0
 public EstablishedState(SSUSession sess)
     : base(sess)
 {
 }
Beispiel #16
0
 protected SSUState(SSUSession sess)
 {
     Session = sess;
 }
Beispiel #17
0
 public SessionConfirmedState(SSUSession sess, SessionRequestState req)
     : base(sess)
 {
     Request = req;
 }