Example #1
0
        public void TestGarlicCreateMessage()
        {
            var dsm1 = new DeliveryStatusMessage(0x425c)
            {
                MessageId = 2354
            };

            var dsm2 = new DeliveryStatusMessage(0x425c)
            {
                MessageId = 2354
            };

            dsm2.Timestamp  = dsm1.Timestamp;
            dsm2.Expiration = dsm1.Expiration;

            var dsm1h = dsm1.CreateHeader16;
            var dsm2h = dsm2.CreateHeader16;

            var dsm1hap = dsm1h.HeaderAndPayload;
            var dsm2hap = dsm2h.HeaderAndPayload;

            var st11 = FreenetBase64.Encode(dsm1hap);
            var st12 = FreenetBase64.Encode(dsm2hap);

            Assert.IsTrue(dsm1hap == dsm2hap);

            var gcd1 = new GarlicCloveDeliveryDestination(
                dsm1,
                Destination.IdentHash);

            var gcd2 = new GarlicCloveDeliveryDestination(
                dsm2,
                Destination.IdentHash);

            var gcd1ar = gcd1.ToByteArray();
            var gcd2ar = gcd2.ToByteArray();

            var st1 = FreenetBase64.Encode(new BufLen(gcd1ar));
            var st2 = FreenetBase64.Encode(new BufLen(gcd2ar));

            Assert.IsTrue(BufUtils.Equal(gcd1ar, gcd2ar));

            var msg1 = new GarlicClove(gcd1);
            var msg2 = new GarlicClove(gcd2);

            var g1 = new Garlic(msg1, msg2);
            var g2 = new Garlic(new BufRefLen(g1.ToByteArray()).Clone());

            Assert.IsTrue(BufUtils.Equal(g1.ToByteArray(), g2.ToByteArray()));
        }
Example #2
0
        internal void SetIntroducers(IEnumerable <IntroducerInfo> introducers)
        {
            var result = new List <KeyValuePair <string, string> >();
            var ix     = 0;

            foreach (var one in introducers)
            {
                result.Add(new KeyValuePair <string, string>($"ihost{ix}", one.Host.ToString()));
                result.Add(new KeyValuePair <string, string>($"iport{ix}", one.Port.ToString()));
                result.Add(new KeyValuePair <string, string>($"ikey{ix}", FreenetBase64.Encode(one.IntroKey)));
                result.Add(new KeyValuePair <string, string>($"itag{ix}", one.IntroTag.ToString()));
                ++ix;
            }

            SSUIntroducersInfo = result;
            MyRouterInfoCache  = null;
        }
Example #3
0
        public override string ToString()
        {
            string addr;

            if (IPAddressOk)
            {
                addr = (new IPAddress(AliceIPAddr.ToByteArray())).ToString();
            }
            else if (AliceIPAddr.Length == 0)
            {
                addr = "<empty>";
            }
            else
            {
                addr = AliceIPAddr.ToString();
            }
            return(string.Format("PeerTest: Test nonce: {0}, Alice IP#: {1}, Port: {2}, Intro key: {3}",
                                 TestNonce.PeekFlip32(0), addr, AlicePort.PeekFlip16(0),
                                 FreenetBase64.Encode(IntroKey)));
        }
Example #4
0
        private void UpdateRouterContext()
        {
            var addr = new I2PRouterAddress(RouterContext.Inst.ExtAddress, RouterContext.Inst.UDPPort, 5, "SSU");

            var ssucaps = "";

            if (PeerTestSupported)
            {
                ssucaps += "B";
            }
            if (IntroductionSupported)
            {
                ssucaps += "C";
            }

            addr.Options["caps"] = ssucaps;
            addr.Options["key"]  = FreenetBase64.Encode(RouterContext.Inst.IntroKey);
            foreach (var intro in IntroducersInfo)
            {
                addr.Options[intro.Key] = intro.Value;
            }

            RouterContext.Inst.UpdateAddress(this, addr);
        }
Example #5
0
        internal void SetIntroducers(IEnumerable <IntroducerInfo> introducers)
        {
            if (!introducers.Any())
            {
                NoIntroducers();
                return;
            }

            var result = new List <KeyValuePair <string, string> >();
            var ix     = 0;

            foreach (var one in introducers)
            {
                result.Add(new KeyValuePair <string, string>($"ihost{ix}", one.Host.ToString()));
                result.Add(new KeyValuePair <string, string>($"iport{ix}", one.Port.ToString()));
                result.Add(new KeyValuePair <string, string>($"ikey{ix}", FreenetBase64.Encode(one.IntroKey)));
                result.Add(new KeyValuePair <string, string>($"itag{ix}", one.IntroTag.ToString()));
                ++ix;
            }

            IntroducersInfo = result;

            UpdateRouterContext();
        }
Example #6
0
        public override string ToString()
        {
            var hc = HashCache != null?FreenetBase64.Encode(new BufLen( HashCache )) : "<null>";

            return(string.Format("I2PRoutingKey: HashCacheDay {0}, HashCache: {1}.", HashCacheDay, hc));
        }
Example #7
0
 public override string ToString()
 {
     return("I2PSignature: " + FreenetBase64.Encode(Sig));
 }
Example #8
0
        public override string ToString()
        {
            var hc = HashCache != null?FreenetBase64.Encode(new BufLen( HashCache )) : "<null>";

            return($"I2PRoutingKey: HashCacheDay {HashCacheDay}, HashCache: {hc}.");
        }
Example #9
0
        private void SendLSDatabaseLookup(I2PIdentHash ident, IdentUpdateRequestInfo info)
        {
            var outtunnel   = TunnelProvider.Inst.GetEstablishedOutboundTunnel(true);
            var replytunnel = TunnelProvider.Inst.GetInboundTunnel(true);

            if (outtunnel is null || replytunnel is null)
            {
                Logging.LogDebug($"SendLSDatabaseLookup: " +
                                 $"outtunnel: {outtunnel}, replytunnel: {replytunnel}");
                return;
            }

            var getnext = DateTime.UtcNow.Hour >= 23;
            var ff      = NetDb.Inst.GetClosestFloodfill(
                ident,
                DatabaseLookupSelectFloodfillCount + 2 * info.Retries,
                info.AlreadyQueried,
                getnext)
                          .Select(r => new { Id = r, NetDb.Inst.Statistics[r].Score });

#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.Any())
            {
                Logging.Log($"IdentResolver failed to find a floodfill router to lookup ({ident}): ");
                return;
            }

            var minscore = ff.Min(r => r.Score);

            for (int i = 0; i < DatabaseLookupSelectFloodfillCount; ++i)
            {
                var oneff = ff
                            .RandomWeighted(r => r.Score - minscore + 0.1)
                            .Id;
                try
                {
                    var msg = new DatabaseLookupMessage(
                        ident,
                        replytunnel.Destination, replytunnel.GatewayTunnelId,
                        DatabaseLookupMessage.LookupTypes.LeaseSet, null);

                    outtunnel.Send(new TunnelMessageRouter(msg, oneff));

                    info.AlreadyQueried.Add(oneff);

#if !LOG_ALL_IDENT_LOOKUPS
                    Logging.Log($"IdentResolver: LeaseSet query {msg.Key.Id32Short} " +
                                $"sent to {oneff.Id32Short}. Dist: {oneff ^ msg.Key.RoutingKey}");
#endif
                }
                catch (Exception ex)
                {
                    Logging.Log("SendLSDatabaseLookup", ex);
                }
            }
        }
Example #10
0
        // This test does not work
        //[Test]
        public void TestSSU()
        {
            //Logging.LogToFile( "TestSSU.log" );

            RouterContext testcontext = new RouterContext(new I2PCertificate(I2PSigningKey.SigningKeyTypes.DSA_SHA1));

            testcontext.DefaultTCPPort = 2048 + BufUtils.RandomInt(5000);
            testcontext.DefaultUDPPort = 2048 + BufUtils.RandomInt(5000);

            var host = new SSUHost(testcontext, new FixedMTU());

            host.AllowConnectToSelf = true;

            host.ConnectionCreated += host_ConnectionCreated;

            // Remote
            var dnsa = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(a => a.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();
            var addr = new I2PRouterAddress(dnsa, testcontext.UDPPort, 6, "SSU");

            addr.Options["key"] = FreenetBase64.Encode(testcontext.IntroKey);

            RouterContext remotetestcontext = new RouterContext(new I2PCertificate(I2PSigningKey.SigningKeyTypes.DSA_SHA1));

            remotetestcontext.DefaultTCPPort = testcontext.DefaultTCPPort + 5;
            remotetestcontext.DefaultUDPPort = testcontext.DefaultUDPPort + 5;

            var remotehost = new SSUHost(remotetestcontext, new FixedMTU());

            remotehost.AllowConnectToSelf = true;
            var client = remotehost.AddSession(addr, testcontext.MyRouterIdentity);

            client.Connect();

            var data = new BufLen(BufUtils.RandomBytes(30000));

            var messagecount = 900; // If the out queue is larger than 1000 msgs we start discarding them

            for (int i = 0; i < messagecount; ++i)
            {
                client.Send(new DataMessage(data));
            }

            System.Threading.Thread.Sleep(10000);

            for (int i = 0; i < messagecount; ++i)
            {
                if (i % 10 == 0)
                {
                    System.Threading.Thread.Sleep(100);
                }
                client.Send(new DataMessage(data));
            }

            var start = new TickCounter();

            while (DataReceived.Count < 2 * messagecount)
            {
                if (start.DeltaToNow.ToMinutes >= 1)
                {
                    Assert.Fail("Failed to receive sent data due to a timeout");
                    break;
                }

                System.Threading.Thread.Sleep(500);
            }

            for (int i = 0; i < 100; ++i)
            {
                Assert.IsTrue(((DataMessage)DataReceived.Random().Message).DataMessagePayload ==
                              new BufLen(data));
            }

            System.Threading.Thread.Sleep(500);

            client.Terminate();

            System.Threading.Thread.Sleep(500);

            host.Terminate();

            System.Threading.Thread.Sleep(500);
        }
Example #11
0
 public string ToBase64()
 {
     return(FreenetBase64.Encode(new BufLen(ToByteArray())));
 }
Example #12
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);
            }
        }