Пример #1
0
        public void Send(I2NPMessage msg)
        {
            if (Terminated)
            {
                throw new EndOfStreamEncounteredException();
            }

            lock ( SendQueue )
            {
                var len = SendQueue.Count;

                if (len < SendQueueLengthUpperLimit)
                {
                    SendQueue.AddLast(msg.Header5);
                }
#if DEBUG
                else
                {
                    DebugUtils.LogWarning(
                        string.Format("SSUSession {0}: SendQueue is {1} messages long! Dropping new message. Max queue: {2} ({3:###0}s)",
                                      DebugId, len, SessionMaxSendQueueLength, MinTimeBetweenSendQueueLogs.DeltaToNow.ToSeconds));
                }

                SessionMaxSendQueueLength = Math.Max(SessionMaxSendQueueLength, len);

                if ((len > SendQueueLengthWarningLimit) && (MinTimeBetweenSendQueueLogs.DeltaToNowMilliseconds > 4000))
                {
                    DebugUtils.LogWarning(
                        string.Format("SSUSession {0}: SendQueue is {1} messages long! Max queue: {2} ({3:###0}s)",
                                      DebugId, len, SessionMaxSendQueueLength, MinTimeBetweenSendQueueLogs.DeltaToNow.ToSeconds));
                    MinTimeBetweenSendQueueLogs.SetNow();
                }
#endif
            }
        }
Пример #2
0
 private void UpdateSSUReportedAddr(IPAddress ipaddr)
 {
     if (LastIPReport.DeltaToNow.ToMinutes < 30)
     {
         return;
     }
     LastIPReport.SetNow();
     MyRouterContext.SSUReportedAddr(ipaddr);
 }
Пример #3
0
 public void WriteTo(BufRef dest)
 {
     LastSent.SetNow();
     ++SendCount;
     if (BufBase.SameBuffer(Buf, Data))
     {
         dest.Write(Buf);
     }
     else
     {
         dest.Write(Buf);
         dest.Write(Data);
     }
 }
Пример #4
0
        public void Send(I2NPMessage msg)
        {
            if (Terminated)
            {
                throw new EndOfStreamEncounteredException();
            }

            var sendqlen = SendQueue.Count;

#if DEBUG
            SessionMaxSendQueueLength = Math.Max(SessionMaxSendQueueLength, sendqlen);

            if ((SendQueue.Count > SendQueueLengthWarningLimit) && (MinTimeBetweenSendQueueLogs.DeltaToNowMilliseconds > 4000))
            {
                DebugUtils.LogWarning(
                    string.Format("NTCPClient {0}: SendQueue is {1} messages long! Max queue: {2} ({3:###0}s)",
                                  DebugId, sendqlen, SessionMaxSendQueueLength, MinTimeBetweenSendQueueLogs.DeltaToNow.ToSeconds));
                MinTimeBetweenSendQueueLogs.SetNow();
            }
#endif

            lock ( SendQueue )
            {
                if (sendqlen < SendQueueLengthUpperLimit)
                {
                    SendQueue.AddLast(msg);
                }
#if DEBUG
                else
                {
                    DebugUtils.LogWarning(
                        string.Format("NTCPClient {0}: SendQueue is {1} messages long! Dropping new message. Max queue: {2} ({3:###0}s)",
                                      DebugId, sendqlen, SessionMaxSendQueueLength, MinTimeBetweenSendQueueLogs.DeltaToNow.ToSeconds));
                }
#endif
            }

            try
            {
                TryInitiateSend();
            }
            catch (SocketException ex)
            {
                if (NTCPContext.RemoteRouterIdentity != null)
                {
                    NetDb.Inst.Statistics.FailedToConnect(NTCPContext.RemoteRouterIdentity.IdentHash);
                }
                throw new EndOfStreamEncounteredException(ex.ToString());
            }
        }
Пример #5
0
        private void UpdateSSUReportedAddr(IPAddress ipaddr)
        {
            if (LastIPReport?.DeltaToNow.ToMinutes < 30)
            {
                return;
            }
            if (LastIPReport == null)
            {
                LastIPReport = new TickCounter();
            }
            LastIPReport.SetNow();

            MyRouterContext.SSUReportedAddr(ipaddr);
        }
Пример #6
0
        internal void LocalLeaseSetUpdated(I2PLeaseSet leaseset)
        {
            LatestLocalLeaseSet = leaseset;
            var dbsmessage = new DatabaseStoreMessage(leaseset);
            var info       = Send(true, new GarlicCloveDeliveryDestination(dbsmessage, Destination.IdentHash));

#if LOG_ALL_TUNNEL_TRANSFER
            if (info != null)
            {
                Logging.LogDebug(() => string.Format(
                                     "DestinationSession: LeaseSet update bundled in Destination trafic. ({0}) TrackingId: {1}, Ack MessageId: {2}.",
                                     info.KeyType, info.TrackingId, info.AckMessageId));
            }
#endif

            LatestLeaseSetSendTime.SetNow();
        }
Пример #7
0
        internal void ReportedAddress(IPAddress ipaddr)
        {
            if (LastExternalIPProcess.DeltaToNowSeconds < (LastIPReport == null ? 1 : 60))
            {
                return;
            }
            LastExternalIPProcess.SetNow();

            Logging.LogTransport($"SSU My IP: My external IP {ipaddr}");

            lock ( ReportedAddresses )
            {
                ReportedAddresses.AddLast(ipaddr);
                while (ReportedAddresses.Count > 200)
                {
                    ReportedAddresses.RemoveFirst();
                }

                var first      = ReportedAddresses.First.Value;
                var firstbytes = first.GetAddressBytes();
                if (ReportedAddresses.Count() > 10 && ReportedAddresses.All(a => BufUtils.Equal(a.GetAddressBytes(), firstbytes)))
                {
                    Logging.LogTransport($"SSU My IP: Start using unanimous remote reported external IP {ipaddr}");
                    UpdateSSUReportedAddr(ipaddr);
                }
                else
                {
                    var freq = ReportedAddresses.GroupBy(a => a.GetAddressBytes()).OrderBy(g => g.Count());
                    if (freq.First().Count() > 15)
                    {
                        Logging.LogTransport($"SSU My IP: Start using most frequently reported remote external IP {ipaddr}");
                        UpdateSSUReportedAddr(ipaddr);
                    }
                }
            }
        }
Пример #8
0
        internal void ReportedAddress(IPAddress ipaddr)
        {
            DebugUtils.LogDebug("SSU My external IP " + ipaddr.ToString());

            if (LastExternalIPProcess.DeltaToNowMilliseconds < 20000)
            {
                return;
            }
            LastExternalIPProcess.SetNow();

            lock ( ReportedAddresses )
            {
                ReportedAddresses.AddLast(ipaddr);
                while (ReportedAddresses.Count > 50)
                {
                    ReportedAddresses.RemoveFirst();
                }

                var first      = ReportedAddresses.First.Value;
                var firstbytes = first.GetAddressBytes();
                if (ReportedAddresses.Count() > 10 && ReportedAddresses.All(a => BufUtils.Equal(a.GetAddressBytes(), firstbytes)))
                {
                    DebugUtils.Log("SSU Start using unanimous remote reported external IP " + ipaddr.ToString());
                    UpdateSSUReportedAddr(ipaddr);
                }
                else
                {
                    var freq = ReportedAddresses.GroupBy(a => a.GetAddressBytes()).OrderBy(g => g.Count());
                    if (freq.First().Count() > 15)
                    {
                        DebugUtils.Log("SSU Start using most frequently reported remote external IP " + ipaddr.ToString());
                        UpdateSSUReportedAddr(ipaddr);
                    }
                }
            }
        }
Пример #9
0
 private void DataReceived()
 {
     LastReceive.SetNow();
 }
Пример #10
0
 private void DataSent()
 {
     LastSend.SetNow();
 }
Пример #11
0
        public override SSUState Run()
        {
            // Idle
            if (Timeout(InactivityTimeout))
            {
                Logging.LogTransport($"SSU EstablishedState {Session.DebugId}: Inactivity timeout. Sending SessionDestroyed.");
                SendSessionDestroyed();
                return(null);
            }

            UpdateSessionLengthStats.Do(() =>
                                        Session.Host.EPStatisitcs.UpdateSessionLength(
                                            Session.RemoteEP,
                                            Session.StartTime.DeltaToNow));

            var dosend = Session.Defragmenter.GotAcks ||
                         (!Session.SendQueue.IsEmpty) ||
                         Session.Fragmenter.GotUnsentFragments;

            TickSpan timeout = null;

            if (!dosend)
            {
                if (SessionLayer.RouterContext.Inst.IsFirewalled)
                {
                    if (Session.IsIntroducerConnection)
                    {
                        timeout = IntroducerKeepaliveTimeout;
                    }
                    else
                    {
                        timeout = NonIntroducerKeepaliveTimeout;
                    }
                }
                else
                {
                    timeout = NotFirewalledKeepaliveTimeout;
                }
            }

            if (!dosend && LastIntroducerKeepalive.DeltaToNow > timeout)
            {
                dosend = true;

#if LOG_MUCH_TRANSPORT
                Logging.LogTransport($"SSU EstablishedState {Session.DebugId}:" +
                                     $"{(Session.IsIntroducerConnection ? " Introducer" : "")} keepalive.");
#endif
                LastIntroducerKeepalive.SetNow();
            }

            if (dosend)
            {
                do
                {
                    SendAcksAndData();
                } while (!Session.SendQueue.IsEmpty);
            }

            ResendFragmentsAction.Do(delegate
            {
                if (Session.Fragmenter.AllFragmentsAcked)
                {
                    return;
                }

                ResendNotAcked(Session.Fragmenter.NotAckedFragments());
            });

            if (Session.QueuedFirstPeerTestToCharlie != null)
            {
                SendFirstPeerTestToCharlie(Session.QueuedFirstPeerTestToCharlie);
                Session.QueuedFirstPeerTestToCharlie = null;
            }

            return(this);
        }
Пример #12
0
 protected void DataSent()
 {
     LastAction.SetNow();
 }