Exemple #1
0
        static void MyDestination_DataReceived(ClientDestination dest, BufLen data)
        {
            Logging.LogInformation($"Program {UnpublishedDestination}: data received {data:20}");

            var reader = new BufRefLen(data);
            var unzip  = LZUtils.BCGZipDecompressNew((BufLen)reader);
            var packet = new StreamingPacket((BufRefLen)unzip);

            Logging.LogInformation($"Program {UnpublishedDestination}: {packet}");
        }
        void UpdateCachedFields(BufRef reader)
        {
            CachedKey        = new I2PIdentHash(reader);
            CachedContent    = reader.Read8() == 0 ? MessageContent.RouterInfo : MessageContent.LeaseSet;
            CachedReplyToken = reader.Read32();
            if (CachedReplyToken != 0)
            {
                CachedReplyTunnelId = reader.Read32();
                CachedReplyGateway  = new I2PIdentHash(reader);
            }

            switch (CachedContent)
            {
            case MessageContent.RouterInfo:
                var length = reader.ReadFlip16();

#if USE_BC_GZIP
                CachedRouterInfo = new I2PRouterInfo(
                    new BufRefLen(LZUtils.BCGZipDecompressNew(new BufLen(reader, 0, length))), true);
#else
                using (var ms = new MemoryStream())
                {
                    ms.Write(reader.BaseArray, reader.BaseArrayOffset, length);
                    ms.Position = 0;

                    using (var gzs = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        var gzdata = StreamUtils.Read(gzs);
                        CachedRouterInfo = new I2PRouterInfo(new BufRefLen(gzdata), true);
                    }
                }
#endif

                reader.Seek(length);
                break;

            case MessageContent.LeaseSet:
                CachedLeaseSet = new I2PLeaseSet(reader);
                break;

            /*
             * case MessageContent.LeaseSet2:
             * break;
             *
             * case MessageContent.EncryptedLeaseSet:
             * break;
             *
             * case MessageContent.MetaLeaseSet:
             * break;
             */
            default:
                throw new InvalidDataException($"DatabaseStoreMessage: {CachedContent} not supported");
            }
        }
Exemple #3
0
        static void MyDestination_DataReceived(ClientDestination dest, BufLen data)
        {
            Logging.LogInformation($"Program {PublishedDestination}: data received {data:15}");

            var reader = new BufRefLen(data);
            var unzip  = LZUtils.BCGZipDecompressNew((BufLen)reader);
            var packet = new StreamingPacket((BufRefLen)unzip);

            Logging.LogInformation($"Program {PublishedDestination}: {packet} {packet.Payload}");

            PublishedDestination.LookupDestination(packet?.From.IdentHash, (hash, ls, tag) =>
            {
                if (ls is null)
                {
                    Logging.LogInformation($"Program {PublishedDestination}: Failed to lookup {hash?.Id32Short}.");
                    return;
                }

                var s  = new BufRefStream();
                var sh = new StreamingPacket(
                    PacketFlags.SYNCHRONIZE
                    | PacketFlags.FROM_INCLUDED
                    | PacketFlags.SIGNATURE_INCLUDED
                    | PacketFlags.MAX_PACKET_SIZE_INCLUDED
                    | PacketFlags.NO_ACK)
                {
                    From            = PublishedDestination.Destination,
                    SigningKey      = MyDestinationInfo.PrivateSigningKey,
                    ReceiveStreamId = packet.ReceiveStreamId,
                    SendStreamId    = SendId,
                    NACKs           = new List <uint>(),
                    Payload         = new BufLen(BufUtils.RandomBytes(30)),
                };

                sh.Write(s);
                var buf    = s.ToByteArray();
                var zipped = LZUtils.BCGZipCompressNew(new BufLen(buf));
                zipped.PokeFlip16(4353, 4);                // source port
                zipped.PokeFlip16(25, 6);                  // dest port
                zipped[9] = (byte)PayloadFormat.Streaming; // streaming

                Logging.LogInformation($"Program {PublishedDestination}: Sending {zipped:20}.");
                PublishedDestination.Send(ls.Destination, zipped);
            });
        }
Exemple #4
0
        public void TestGZip()
        {
            var smalldata = BufUtils.RandomBytes(200);
            var bigdata   = BufUtils.RandomBytes(2 * 1024 * 1024);

            var smalldata_zero = new byte[200];
            var bigdata_zero   = new byte[2 * 1024 * 1024];

            var b1 = LZUtils.BCGZipCompressNew(new BufLen(smalldata));
            var b2 = LZUtils.BCGZipDecompressNew(b1);

            Assert.IsTrue(b2 == new BufLen(smalldata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(smalldata_zero));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(smalldata_zero));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata_zero));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata_zero));

            var ba1 = LZUtils.BCGZipCompress(bigdata);

            b2 = LZUtils.BCGZipDecompressNew(new BufLen(ba1));
            Assert.IsTrue(b2 == new BufLen(bigdata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata_zero));
            var ba2 = LZUtils.BCGZipDecompress(b1);

            Assert.IsTrue(new BufLen(ba2) == new BufLen(bigdata_zero));

            for (int i = bigdata.Length / 10; i < bigdata.Length - bigdata.Length / 10; ++i)
            {
                bigdata[i] = 42;
            }
            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata));
        }
Exemple #5
0
        void UpdateCachedFields(BufRef reader)
        {
            CachedKey        = new I2PIdentHash(reader);
            CachedContent    = reader.Read8() == 0 ? MessageContent.RouterInfo : MessageContent.LeaseSet;
            CachedReplyToken = reader.Read32();
            if (CachedReplyToken != 0)
            {
                CachedReplyTunnelId = reader.Read32();
                CachedReplyGateway  = new I2PIdentHash(reader);
            }

            if (CachedContent == MessageContent.RouterInfo)
            {
                var length = reader.ReadFlip16();

#if USE_BC_GZIP
                CachedRouterInfo = new I2PRouterInfo(
                    new BufRefLen(LZUtils.BCGZipDecompressNew(new BufLen(reader, 0, length))), true);
#else
                using (var ms = new MemoryStream())
                {
                    ms.Write(reader.BaseArray, reader.BaseArrayOffset, length);
                    ms.Position = 0;

                    using (var gzs = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        var gzdata = StreamUtils.Read(gzs);
                        CachedRouterInfo = new I2PRouterInfo(new BufRefLen(gzdata), true);
                    }
                }
#endif

                reader.Seek(length);
            }
            else
            {
                CachedLeaseSet = new I2PLeaseSet(reader);
            }
        }