Esempio n. 1
0
        internal bool Contains(I2PIdentHash dest)
        {
            if (CurrentlyUnresolvableRouters.Contains(dest))
            {
                throw new RouterUnresolvableException("Unable to resolve " + dest.ToString());
            }

            lock ( QueuedMessages )
            {
                return(QueuedMessages.ContainsKey(dest));
            }
        }
Esempio n. 2
0
        internal void Add(I2PIdentHash dest, I2NPMessage msg)
        {
            if (CurrentlyUnresolvableRouters.Contains(dest))
            {
                throw new RouterUnresolvableException("Destination is tagged as unresolvable " + dest.ToString());
            }

            var sendlookup = false;

            lock ( QueuedMessages )
            {
                if (!QueuedMessages.ContainsKey(dest))
                {
                    var newld = new LookupDestination(dest);
                    newld.Add(msg);
                    QueuedMessages[dest] = newld;
                    sendlookup           = true;
                }
                else
                {
                    var queue = QueuedMessages[dest];
                    if (queue.Messages.Count < MaxMessagesInQueue)
                    {
                        queue.Add(msg);
                    }
#if DEBUG
                    else
                    {
                        Logging.LogWarning("UnknownRouterQueue: Add: Too many messages in queue. Dropping new message.");
                    }
#endif
                }
            }

            if (sendlookup)
            {
                NetDb.Inst.IdentHashLookup.LookupRouterInfo(dest);
            }
        }
Esempio n. 3
0
        private void SendLSDatabaseLookup(I2PIdentHash ident, IdentUpdateRequestInfo info)
        {
            /*
             * var replytunnel = TunnelProvider.Inst.GetInboundTunnel();
             * if ( replytunnel == null ) return;
             */

            var ff = NetDb.Inst.GetClosestFloodfill(ident, DatabaseLookupSelectFloodfillCount * 2, info.AlreadyQueried, false).ToArray();

#if LOG_ALL_IDENT_LOOKUPS
            StringBuilder foundrouters     = new StringBuilder();
            StringBuilder foundrouterskeys = new StringBuilder();

            foreach (var router in ff)
            {
                foundrouters.AppendFormat("{0}{1}{2}", (foundrouters.Length != 0 ? ", " : ""),
                                          router.Id32Short,
                                          FreenetBase64.Encode(router.Hash));
                foundrouterskeys.AppendFormat("{0}{1}{2}", (foundrouterskeys.Length != 0 ? ", " : ""),
                                              router.Id32Short,
                                              FreenetBase64.Encode(router.RoutingKey.Hash));
            }
            var st  = foundrouters.ToString();
            var st2 = foundrouterskeys.ToString();
#endif

            if (ff == null || ff.Length == 0)
            {
                DebugUtils.Log("IdentResolver failed to find a floodfill router to lookup (" + ident.ToString() + "): ");
                return;
            }

            ff.Shuffle();
            ff = ff.Take(DatabaseLookupSelectFloodfillCount).ToArray();

            foreach (var oneff in ff)
            {
                try
                {
                    var msg = new DatabaseLookupMessage(
                        ident,
                        RouterContext.Inst.MyRouterIdentity.IdentHash,
                        DatabaseLookupMessage.LookupTypes.LeaseSet);

                    /*
                     * var msg = new DatabaseLookupMessage(
                     *          ident,
                     *          replytunnel.Destination, replytunnel.GatewayTunnelId,
                     *          DatabaseLookupMessage.LookupTypes.LeaseSet, null );
                     */

                    //TunnelProvider.Inst.SendEncrypted( oneff.Identity, false, msg );
                    TransportProvider.Send(oneff, msg);
#if LOG_ALL_IDENT_LOOKUPS
                    DebugUtils.Log(string.Format("IdentResolver: LeaseSet query {0} sent to {1}. Dist: {2}",
                                                 msg.Key.Id32Short,
                                                 oneff.Id32Short,
                                                 oneff ^ msg.Key.RoutingKey));
#endif
                }
                catch (Exception ex)
                {
                    DebugUtils.Log("SendLSDatabaseLookup", ex);
                }
            }

            lock (info.AlreadyQueried)
            {
                info.AlreadyQueried.AddRange(ff);
            }
        }
Esempio n. 4
0
        private static bool Send(I2PIdentHash dest, I2NPMessage data, int reclvl)
        {
            ITransport transp = null;

            try
            {
                if (dest == RouterContext.Inst.MyRouterIdentity.IdentHash)
                {
                    Logging.LogTransport("TransportProvider: Loopback " + data.ToString());
                    TransportProvider.Inst.DistributeIncomingMessage(null, data.Header16);
                    return(true);
                }

                if (TransportProvider.Inst.CurrentlyUnknownRouters.Contains(dest))
                {
                    TransportProvider.Inst.CurrentlyUnknownRouters.Add(dest, data);
                    return(true);
                }

                transp = TransportProvider.Inst.GetEstablishedTransport(dest, false);
                if (transp != null)
                {
                    transp.Send(data);
                    return(true);
                }

                if (NetDb.Inst.Contains(dest))
                {
                    transp = TransportProvider.Inst.GetTransport(dest);
                    if (transp == null)
                    {
                        throw new ArgumentException("Unable to contact " + dest.ToString());
                    }
                    transp.Send(data);
                }
                else
                {
                    if (TransportProvider.Inst.CurrentlyUnresolvableRouters.Contains(dest))
                    {
                        throw new ArgumentException("Unable to resolve " + dest.ToString());
                    }

                    TransportProvider.Inst.CurrentlyUnknownRouters.Add(dest, data);
                }
            }
            catch (FailedToConnectException ex)
            {
                if (transp != null)
                {
                    TransportProvider.Inst.Remove(transp);
                }

                if (dest != null && NetDb.Inst != null)
                {
                    NetDb.Inst.Statistics.FailedToConnect(dest);
                }
                Logging.LogTransport("TransportProvider.Send: " + (transp == null ? "<>" : transp.DebugId) +
                                     " Exception " + ex.GetType().ToString() + ", " + ex.Message);

                throw;
            }
            catch (EndOfStreamEncounteredException ex)
            {
                TransportProvider.Inst.Remove(transp);

                Logging.LogTransport("TransportProvider.Send: Connection " + (transp == null ? "<>" : transp.DebugId) +
                                     " closed exception: " + ex.GetType().ToString() + ", " + ex.Message);

                if (reclvl > 1 || !Send(dest, data, reclvl + 1))
                {
                    Logging.LogTransport("TransportProvider.Send: Recconnection failed to " + dest.Id32Short + ", reclvl: " + reclvl.ToString() + ".");
                    throw;
                }
            }
            catch (RouterUnresolvableException ex)
            {
                if (dest != null)
                {
                    NetDb.Inst.Statistics.DestinationInformationFaulty(dest);
                }
                Logging.LogTransport("TransportProvider.Send: Unresolvable router: " + ex.Message);

                throw;
            }
            catch (Exception ex)
            {
                if (transp != null)
                {
                    TransportProvider.Inst.Remove(transp);
                }

                if (dest != null)
                {
                    NetDb.Inst.Statistics.DestinationInformationFaulty(dest);
                }
                Logging.LogTransport("TransportProvider.Send: Exception " + ex.GetType().ToString() + ", " + ex.Message);

                throw;
            }
            return(true);
        }
Esempio n. 5
0
        private void SendRIDatabaseLookup(I2PIdentHash ident, IdentUpdateRequestInfo info)
        {
            var ff = NetDb.Inst.GetClosestFloodfill(ident, 10, info.AlreadyQueried, false).ToArray();

            if (ff == null || ff.Length == 0)
            {
                DebugUtils.Log("IdentResolver: failed to find a floodfill router to lookup (" + ident.ToString() + "): ");
                return;
            }

            ff.Shuffle();
            ff = ff.Take(DatabaseLookupSelectFloodfillCount).ToArray();

            foreach (var oneff in ff)
            {
                try
                {
                    var msg = new DatabaseLookupMessage(
                        ident,
                        RouterContext.Inst.MyRouterIdentity.IdentHash,
                        DatabaseLookupMessage.LookupTypes.RouterInfo);

                    TransportProvider.Send(oneff, msg);
#if LOG_ALL_IDENT_LOOKUPS
                    DebugUtils.Log("IdentResolver: RouterInfo query " + msg.Key.Id32Short + " sent to " + oneff.Id32Short);
#endif
                }
                catch (Exception ex)
                {
                    DebugUtils.Log("SendRIDatabaseLookup", ex);
                }
            }

            lock (info.AlreadyQueried)
            {
                info.AlreadyQueried.AddRange(ff);
            }
        }