Beispiel #1
0
        static readonly byte[] Services = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };  // TODO: Fill in services

        public static VersionMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var protocolVersion = reader.ReadUInt32();

            reader.Seek(8, SeekOrigin.Current);
            var timestamp = reader.ReadDateTimeOffset(true);

            reader.Seek(8, SeekOrigin.Current);
            var to = reader.ReadIPEndPoint();

            reader.Seek(8, SeekOrigin.Current);
            var from        = reader.ReadIPEndPoint();
            var nonce       = reader.ReadUInt64();
            var userAgent   = reader.ReadString();
            var startHeight = reader.ReadInt32();

            bool relay = true;

            if (reader.Position < payload.Length)
            {
                relay = reader.ReadBool();
            }

            if (reader.Position != payload.Length)
            {
                throw new Exception($"Unparsed data remaining in {nameof(VersionMessage)}");
            }

            return(new VersionMessage(
                       protocolVersion,
                       timestamp,
                       to,
                       from,
                       nonce,
                       userAgent,
                       startHeight,
                       relay
                       ));
        }
        public static AddrMessage Parse(byte[] payload)
        {
            var reader = new PayloadReader(payload);

            var count     = reader.ReadVarInt(); // TODO: Warn when count exceeds 1000, this ought to be a maximum
            var nodeInfos = new NodeInfo[count]; // TODO: Don't allow peer to force us to allocate a lot of memory here

            for (UInt64 i = 0; i < count; i++)
            {
                var timestamp = reader.ReadDateTimeOffset(longer: false); // Clients with protocol version >= 31402 will provide this timestamp
                reader.Seek(8, SeekOrigin.Current);
                var endpoint = reader.ReadIPEndPoint();

                nodeInfos[i] = new NodeInfo(timestamp, endpoint);
            }

            reader.ThrowIfNotEndReached();

            return(new AddrMessage
            {
                NodeInfos = nodeInfos
            });
        }