Пример #1
0
        private bool TryGetReference(Span <byte> buffer, out object value, out int consumedLength)
        {
            var index = 0;

            value          = default;
            consumedLength = default;
            if (!TryDescribeData(buffer, out var type, out var length))
            {
                return(false);
            }

            if (type != Amf0Type.Reference)
            {
                return(false);
            }

            index          = NetworkBitConverter.ToUInt16(buffer.Slice(Amf0CommonValues.MARKER_LENGTH, sizeof(ushort)));
            consumedLength = Amf0CommonValues.MARKER_LENGTH + sizeof(ushort);
            if (_referenceTable.Count <= index)
            {
                return(false);
            }
            value = _referenceTable[index];
            return(true);
        }
Пример #2
0
        public override void Deserialize(SerializationContext context)
        {
            var span      = context.ReadBuffer.Span;
            var eventType = (UserControlEventType)NetworkBitConverter.ToUInt16(span);

            span = span.Slice(sizeof(ushort));
            Contract.Assert(eventType == UserControlEventType.StreamIsRecorded);
            StreamID = NetworkBitConverter.ToUInt32(span);
        }
Пример #3
0
 public bool ReadNet16(out ushort x)
 {
     if (position + 2 > count)
     {
         x = 0;
         return(false);
     }
     x         = NetworkBitConverter.ToUInt16(data, position);
     position += 2;
     return(true);
 }
Пример #4
0
        public Message Provide(MessageHeader header, SerializationContext context, out int consumed)
        {
            var type = (UserControlEventType)NetworkBitConverter.ToUInt16(context.ReadBuffer.Span);

            if (!_messageFactories.TryGetValue(type, out var t))
            {
                throw new NotSupportedException();
            }
            consumed = 0;
            return((Message)Activator.CreateInstance(t));
        }
Пример #5
0
 public static bool GetProtocol(Bytes packet, out ushort protocol)
 {
     try {
         int offset = EthernetAddress.Length * 2;
         protocol = NetworkBitConverter.ToUInt16(packet, offset);
     }
     catch {
         protocol = 0;
         return(false);
     }
     return(true);
 }        //Get the protocol for this packet
Пример #6
0
        public static ResourceRecord Parse(byte [] buffer, ref int offset)
        {
            string name;

            if ((buffer[offset] & 0xc0) == 0xc0)
            {
                // Detected compression.  Name is at offset in
                // lower 14 bits of next 2 bytes.
                int start = NetworkBitConverter.ToInt16(buffer, offset);
                if (LabelEncoding.GetString(buffer, start & 0x3fff,
                                            out name) == 0)
                {
                    throw new InvalidDnsFormatException("name");
                }
                offset += 2;
            }
            else
            {
                int used = LabelEncoding.GetString(buffer, offset, out name);
                if (used == 0)
                {
                    throw new InvalidDnsFormatException("name");
                }
                offset += used;
            }

            ushort rType = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            ushort rClass = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            uint ttlSeconds = NetworkBitConverter.ToUInt32(buffer, offset);

            offset += 4;

            ushort rdataLength = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            byte [] rdata = new byte[rdataLength];
            Array.Copy(buffer, offset, rdata, 0, rdataLength);
            offset += rdataLength;

            return(new ResourceRecord(name, (Type)rType, (Class)rClass,
                                      ttlSeconds, rdata));
        }
Пример #7
0
        public ArpHeader(Bytes packet, ushort index)
        {
            //since this is already known to be an arp request
            //we skip the sanity checks...

            VTable.Assert(packet.Length - index >= 96);

            // check hardware type == 0x0001 (Ethernet)
            htype = NetworkBitConverter.ToUInt16(packet, index);
            DebugStub.Assert(htype == 0x001);
            index += 2;

            // check protocol type == 0x0800 (IP)
            ptype = NetworkBitConverter.ToUInt16(packet, index);
            DebugStub.Assert(ptype == 0x0800);
            index += 2;

            // check hardware address len is 6 bytes
            hlen = packet[index++];
            DebugStub.Assert(hlen == 6);


            // check IP address len is 4 bytes
            plen = packet[index++];
            DebugStub.Assert(plen == 4);

            op     = NetworkBitConverter.ToUInt16(packet, index);
            index += 2;

            senderEthernetAddr = EthernetAddress.ParseBytes(packet.Array, packet.Start + index);
            index += EthernetAddress.Length;

            uint addr = NetworkBitConverter.ToUInt32(packet, index);

            index       += 4;
            senderIPAddr = new IPv4(addr);


            destEthernetAddr = EthernetAddress.ParseBytes(packet.Array, packet.Start + index);
            index           += EthernetAddress.Length;

            addr       = NetworkBitConverter.ToUInt32(packet, index);
            index     += 4;
            destIPAddr = new IPv4(addr);
            //sgc complains
            pad0 = 0;
            pad1 = 0;
        }
Пример #8
0
        public const int Size = 8;  //size of UDP header

        public UDPHeader(Bytes packet, int index)
        {
            srcPort = NetworkBitConverter.ToUInt16(packet, index);
            index  += 2;

            dstPort = NetworkBitConverter.ToUInt16(packet, index);
            index  += 2;

            length = NetworkBitConverter.ToUInt16(packet, index);
            index += 2;

            checksum = NetworkBitConverter.ToUInt16(packet, index);
            //sgc complains
            pad0 = 0;
            pad1 = 0;
            pad2 = 0;
        }
Пример #9
0
        public bool TryGetPacket(Span <byte> buffer, out List <KeyValuePair <string, object> > headers, out List <Message> messages, out int consumed)
        {
            headers  = default;
            messages = default;
            consumed = 0;

            if (buffer.Length < 1)
            {
                return(false);
            }
            var version = NetworkBitConverter.ToUInt16(buffer);

            buffer    = buffer.Slice(sizeof(ushort));
            consumed += sizeof(ushort);
            var headerCount = NetworkBitConverter.ToUInt16(buffer);

            buffer    = buffer.Slice(sizeof(ushort));
            consumed += sizeof(ushort);
            headers   = new List <KeyValuePair <string, object> >();
            messages  = new List <Message>();
            for (int i = 0; i < headerCount; i++)
            {
                if (!TryReadHeader(buffer, out var header, out var headerConsumed))
                {
                    return(false);
                }
                headers.Add(header);
                buffer    = buffer.Slice(headerConsumed);
                consumed += headerConsumed;
            }

            var messageCount = NetworkBitConverter.ToUInt16(buffer);

            buffer    = buffer.Slice(sizeof(ushort));
            consumed += sizeof(ushort);
            for (int i = 0; i < messageCount; i++)
            {
                if (!TryGetMessage(buffer, out var message, out var messageConsumed))
                {
                    return(false);
                }
                messages.Add(message);
                consumed += messageConsumed;
            }
            return(true);
        }
Пример #10
0
        public static Query Parse(byte [] buffer, ref int offset)
        {
            string name;

            int used = LabelEncoding.GetString(buffer, offset, out name);

            if (used == 0)
            {
                throw new InvalidDnsFormatException("name");
            }
            offset += used;

            ushort qType = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            ushort qClass = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            return(new Query(name, (Type)qType, (Class)qClass));
        }
Пример #11
0
        //create a byte aligned ipheader
        //index marks the starting location of the ip header within the buffer
        public IpHeader(Bytes packet, int index)
        {
            //assert that the packet is large enough for a ipheader
            VTable.Assert(packet.Length - index > 20);

            verLen      = packet[index++];
            tos         = packet[index++];
            totalLength = NetworkBitConverter.ToUInt16(packet, index);
            index      += 2;
            id          = NetworkBitConverter.ToUInt16(packet, index);
            index      += 2;
            offset      = NetworkBitConverter.ToUInt16(packet, index);
            index      += 2;
            ttl         = packet[index++];
            protocol    = packet[index++];
            checksum    = NetworkBitConverter.ToUInt16(packet, index);
            index      += 2;
            uint addr;

            addr        = NetworkBitConverter.ToUInt32(packet, index);
            srcAddress  = new IPv4(addr);
            index      += 4;
            addr        = NetworkBitConverter.ToUInt32(packet, index);
            destAddress = new IPv4(addr);
#if false
            DebugStub.Print("IpHeader verlen 0x{0,8:x}\n tos {1}\n" +
                            " totalLength {2}\n ttl {3}\n protocol 0x{4,4:x}\n" +
                            " checksum 0x{5,4:x}\n src {6}\n, dest {7}\n",
                            DebugStub.ArgList(verLen, tos, totalLength, ttl, protocol, checksum, srcAddress,
                                              destAddress));
#endif
            //sgc complains...
            pad0 = 0;
            pad1 = 0;
            pad2 = 0;
            pad3 = 0;
        }
Пример #12
0
        internal bool TryGetStringImpl(Span <byte> buffer, int lengthOfLengthField, out string value, out int consumedLength)
        {
            value          = default;
            consumedLength = default;
            var stringLength = 0;

            if (lengthOfLengthField == Amf0CommonValues.STRING_HEADER_LENGTH)
            {
                stringLength = (int)NetworkBitConverter.ToUInt16(buffer);
            }
            else
            {
                stringLength = (int)NetworkBitConverter.ToUInt32(buffer);
            }

            if (buffer.Length - lengthOfLengthField < stringLength)
            {
                return(false);
            }

            value          = Encoding.UTF8.GetString(buffer.Slice(lengthOfLengthField, stringLength));
            consumedLength = lengthOfLengthField + stringLength;
            return(true);
        }
Пример #13
0
        public static Format Parse(byte [] buffer, ref int offset)
        {
            //
            // First extract enough information to instantiate a Dns.Format
            //
            ushort id = NetworkBitConverter.ToUInt16(buffer, offset);

            offset += 2;

            ushort flagsAndCodes = NetworkBitConverter.ToUInt16(buffer,
                                                                offset);

            offset += 2;

            Flags flags = (Flags)((int)flagsAndCodes & (int)Flags.ALL);

            OpCode opCode =
                (OpCode)(((int)flagsAndCodes >> OpCodeRoll) & OpCodeMask);

            RCode rCode = (RCode)((int)flagsAndCodes & RCodeMask);

            //
            // Instantiate Dns.Format
            //
            Format format = new Format(id, flags, opCode, rCode);

            //
            // Extract query and record counts and then instantiate
            // Query and ResourceRecord objects
            //

            ushort totalQueries = NetworkBitConverter.ToUInt16(buffer,
                                                               offset);

            offset += 2;

            ushort totalAnswerRRs = NetworkBitConverter.ToUInt16(buffer,
                                                                 offset);

            offset += 2;

            ushort totalAuthorityRRs = NetworkBitConverter.ToUInt16(buffer,
                                                                    offset);

            offset += 2;

            ushort totalAdditionalRRs = NetworkBitConverter.ToUInt16(buffer,
                                                                     offset);

            offset += 2;

            for (ushort i = 0; i < totalQueries; i++)
            {
                Query query = Query.Parse(buffer, ref offset);
                format.queries.Add(query);
            }

            // XXX Parsing stops here when a query fails.
            //
            // When a query fails, servers sometimes
            // indicate resource records are present, but the
            // data that we'd expect to be a resource record
            // does not match the format defined in
            // RFC1035. Details may exist in another RFC (???).
            if (rCode != RCode.NoError)
            {
                return(format);
            }

            for (ushort i = 0; i < totalAnswerRRs; i++)
            {
                ResourceRecord rr = ResourceRecord.Parse(buffer, ref offset);
                format.answerRRs.Add(rr);
            }

            for (ushort i = 0; i < totalAuthorityRRs; i++)
            {
                ResourceRecord rr = ResourceRecord.Parse(buffer, ref offset);
                format.authorityRRs.Add(rr);
            }

            for (ushort i = 0; i < totalAdditionalRRs; i++)
            {
                ResourceRecord rr = ResourceRecord.Parse(buffer, ref offset);
                format.additionalRRs.Add(rr);
            }

            return(format);
        }
Пример #14
0
        public static DhcpFormat Parse(Bytes buffer)
        {
            DhcpFormat p = new DhcpFormat(BootType.NotSpecified);

            p.optionsUsedLength = 0;

            if (buffer.Length < DhcpFormat.MinLength)
            {
                throw new InvalidDhcpFormatException("Format less than minimum size");
            }
            int offset = 0;

            p.op = buffer[offset++];
            if (p.op != (byte)BootType.Request &&
                p.op != (byte)BootType.Reply)
            {
                throw new InvalidDhcpFormatException("Bad Type (op = {0})", p.op);
            }
            p.htype = buffer[offset++];
            // No check

            p.hlen = buffer[offset++];
            if (p.hlen > HardwareAddressLength)
            {
                throw new InvalidDhcpFormatException("Bad address length (hlen {0})",
                                                     p.hlen);
            }
            p.hops = buffer[offset++];
            // No check

            p.xid   = NetworkBitConverter.ToUInt32(buffer, offset);
            offset += 4;

            p.secs  = NetworkBitConverter.ToUInt16(buffer, offset);
            offset += 2;

            p.flags = NetworkBitConverter.ToUInt16(buffer, offset);
            offset += 2;

            p.ciaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.yiaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.siaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.giaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            Bitter.ToByteArray(buffer, offset, HardwareAddressLength, p.chaddr, 0);
            offset += HardwareAddressLength;
            Bitter.ToByteArray(buffer, offset, ServerNameLength, p.sname, 0);
            offset += ServerNameLength;
            Bitter.ToByteArray(buffer, offset, BootFileLength, p.file, 0);
            offset += BootFileLength;

            p.cookie = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;
            if (p.cookie != DhcpCookie)
            {
                throw new InvalidDhcpFormatException("Bad cookie (0x{0x:x8})",
                                                     p.cookie);
            }
            int available = buffer.Length - offset;

            if (available > p.options.Length)
            {
                p.options = new byte [available];
            }
            p.optionsUsedLength = available;
            Bitter.ToByteArray(buffer, offset, available, p.options, 0);

            return(p);
        }