Esempio n. 1
0
        /// <inheritdoc />
        public override void Read(NatReader reader)
        {
            base.Read(reader);

            reader.ReadUInt16(); // reserved
            RequestedLifetime = reader.ReadTimeSpan();
            ClientAddress     = reader.ReadIPv6Address();
        }
Esempio n. 2
0
        /// <inheritdoc />
        public override void Read(NatReader reader)
        {
            Version = reader.ReadByte();
            var opcode = reader.ReadByte();

            Opcode     = (Opcode)(opcode & 0x7f);
            IsResponse = (opcode & 0x80) == 0x80;
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override void Read(NatReader reader)
        {
            base.Read(reader);

            reader.ReadByte(); // reserved
            ResultCode = reader.ReadByte();
            Lifetime   = reader.ReadTimeSpan();
            EpochTime  = reader.ReadTimeSpan();
            var _ = reader.ReadBytes(reserved2.Length);
        }
Esempio n. 4
0
        /// <inheritdoc />
        public override void Read(NatReader reader)
        {
            base.Read(reader);

            Nonce    = reader.ReadBytes(NonceLength);
            Protocol = (ProtocolType)reader.ReadByte();
            reader.ReadByte(); // reserved 24 bits (3 bytes)
            reader.ReadByte();
            reader.ReadByte();
            InternalPort             = reader.ReadUInt16();
            AssignedExternalPort     = reader.ReadUInt16();
            AssignedExternalAdddress = reader.ReadIPv6Address();
        }
Esempio n. 5
0
 /// <inheritdoc />
 public override void Read(NatReader reader)
 {
 }
Esempio n. 6
0
        /// <summary>
        ///   Create a message from the specified datagram.
        /// </summary>
        /// <param name="datagram">
        ///   The byte array containing the message.
        /// </param>
        /// <returns>
        ///   A <see cref="Message"/> that represents the <paramref name="datagram"/>.
        /// </returns>
        public static Message Create(byte[] datagram)
        {
            if (datagram == null || datagram.Length < 4)
            {
                throw new InvalidDataException("NAT-PCP datagram must be at least 4 bytes.");
            }

            using (var ms = new MemoryStream(datagram, false))
            {
                // Get the type of message.
                var reader = new NatReader(ms);
                var header = new Message();
                header.Read(reader);
                Message msg = null;
                if (header.IsResponse)
                {
                    switch (header.Opcode)
                    {
                    case Opcode.Announce:
                        msg = new Response();     // no specific response data
                        break;

                    case Opcode.Map:
                        msg = new MapResponse();
                        break;

                    case Opcode.Peer:
                        msg = new Response();     // TODO
                        break;

                    default:
                        throw new NotSupportedException($"NAT-PCP response with opcode {header.Opcode}.");
                    }
                }
                else
                {
                    switch (header.Opcode)
                    {
                    case Opcode.Announce:
                        msg = new AnnounceRequest();
                        break;

                    case Opcode.Map:
                        msg = new MapRequest();
                        break;

                    case Opcode.Peer:
                        msg = new PeerRequest();
                        break;

                    default:
                        throw new NotSupportedException($"NAT-PCP request with opcode {header.Opcode}.");
                    }
                }

                // Read the message data.
                reader.Position = 0;
                msg.Read(reader);

                return(msg);
            }
        }