Пример #1
0
        public SessionRequestState(SSUSession sess) : base(sess)
        {
            var keys = I2PPrivateKey.GetNewKeyPair();

            PrivateKey = keys.PrivateKey;
            X          = keys.PublicKey;
        }
Пример #2
0
        public SessionCreatedState(SSUSession sess)
            : base(sess)
        {
            var keys = I2PPrivateKey.GetNewKeyPair();

            PrivateKey = keys.PrivateKey;
            Y          = keys.PublicKey;
        }
Пример #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 );
        }
Пример #4
0
 internal void NoCpu(SSUSession sess)
 {
     lock ( NeedsCpu )
     {
         if (!NeedsCpu.Contains(sess))
         {
             return;
         }
         NeedsCpu.Remove(sess);
     }
 }
Пример #5
0
 internal void NeedCpu(SSUSession sess)
 {
     lock ( NeedsCpu )
     {
         if (NeedsCpu.Contains(sess))
         {
             return;
         }
         NeedsCpu.Add(sess);
     }
 }
Пример #6
0
        void AddFailedSession(SSUSession sess)
        {
            if (sess == null)
            {
                return;
            }

            sess.Terminate();

            lock ( FailedSessions )
            {
                FailedSessions.Add(sess);
            }
        }
Пример #7
0
        internal RelayRequestState(SSUSession sess, IList <IntroducerInfo> introducers) : base(sess)
        {
            Introducers = introducers;

            if (Introducers.Count == 0)
            {
                throw new FailedToConnectException("SSU +{Session.TransportInstance}+ Failed to find a non established introducer");
            }
            else
            {
                CurrentIntroducer = Introducers[0];
                Introducers.RemoveAt(0);

                Session.Host.RelayResponseReceived += new SSUHost.RelayResponseInfo(Host_RelayResponseReceived);
            }
        }
Пример #8
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).SingleOrDefault();
                if (key != null)
                {
                    Sessions.Remove(key);
                }
            }
        }
Пример #9
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(string.Format("SSU AddSession: [{0}]:{1} - {2}. Dropped. Not connecting to ourselves.", dest.IdentHash.Id32, key, addr));
                    return(null);
                }

                key = remoteep;

#if LOG_ALL_TRANSPORT
                Logging.LogTransport(string.Format("SSU AddSession: [{0}]:{1} - {2}", dest.IdentHash.Id32, key, addr));
#endif

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

            var newsess = new SSUSession(this, remoteep, addr, dest, MTUProvider, MyRouterContext);
            if (key != null)
            {
                lock ( Sessions )
                {
                    Sessions[key] = newsess;
                }
            }
            return(newsess);
        }
Пример #10
0
 public SessionConfirmedState(SSUSession sess, SessionRequestState req)
     : base(sess)
 {
     Request = req;
 }
Пример #11
0
 protected SSUState(SSUSession sess)
 {
     Session = sess;
 }
Пример #12
0
 public EstablishedState(SSUSession sess)
     : base(sess)
 {
 }
Пример #13
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)
                {
                    return;                                                   // TODO: Add IPV6
                }
                if (size <= 37)
                {
#if LOG_ALL_TRANSPORT
                    Logging.LogTransport(string.Format("SSU Recv: {0} bytes [0x{0:X}] from {1} (hole punch, ignored)", size, ep));
#endif
                    return;
                }

                var key = (IPEndPoint)ep;

#if LOG_ALL_TRANSPORT
                Logging.LogTransport(string.Format("SSU Recv: {0} bytes [0x{0:X}] from {1}", size, ep));
#endif

                lock ( Sessions )
                {
                    if (!Sessions.ContainsKey(key))
                    {
                        if (IPFilter.IsFiltered(((IPEndPoint)ep).Address))
                        {
                            Logging.LogTransport(string.Format("SSUHost ReceiveCallback: IPAddress {0} is blocked. {1} bytes.",
                                                               key, size));
                            return;
                        }

                        ++IncommingConnectionAttempts;

                        session       = new SSUSession(this, (IPEndPoint)ep, MTUProvider, MyRouterContext);
                        Sessions[key] = session;
                        Logging.LogTransport("SSUHost: incoming connection " + session.DebugId + " from " + key.ToString() + " created.");
                        NeedCpu(session);
                        ConnectionCreated?.Invoke(session);
                    }
                    else
                    {
                        session = Sessions[key];
                    }
                }

                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_ALL_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(
                        string.Format("SSUHost ReceiveCallback: WARNING Session {0} used {1}ms cpu.", session, Stopwatch1.ElapsedMilliseconds));
                }
#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);
            }
        }
Пример #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(
                            string.Format("SSUHost Run: WARNING Session {0} used {1}ms cpu.", sess, Stopwatch1.ElapsedMilliseconds));
                    }
#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();
            }
        }