예제 #1
0
        private static void ProcessTzspPacket(byte[] packet, ushort listenPort)
        {
            // TZSP header
            // version (1) == 1
            // type (1) == 0
            // protocol (2) == 1
            // tags (N)
            //   type (1) == anything, but 1 means no more tags (not even the rest of the fields of this tag)
            //   length (1)
            //   data (N)
            // packetdata (...)
            //      Ethernet MAC header (14)
            //      IP header
            //        +0 version (0.5)
            //        +0.5 header length / 4 (0.5)
            //        +2 total length (2)
            //        +9 protocol (1)
            //        +12 source address (4)
            //        +16 destination address (4)
            //        ...options...
            //      inner packet

            using var stream = new MemoryStream(packet);
            using var reader = new MultiEndianBinaryReader(stream, ByteOrder.BigEndian);

            var tzspVersion = reader.ReadByte();

            if (tzspVersion != 1)
            {
                throw new NotSupportedException("TZSP version != 1");
            }

            var tzspType = reader.ReadByte();

            if (tzspType != 0)
            {
                throw new NotSupportedException("TZSP type != 0");
            }

            var tzspProtocol = reader.ReadUInt16();

            if (tzspProtocol != 1)
            {
                throw new NotSupportedException("TZSP protocol != 1");
            }

            while (true)
            {
                var tagType = reader.ReadByte();

                if (tagType == 1)
                {
                    break;
                }

                var tagLength = reader.ReadByte();
                stream.Position += tagLength; // Skip the tag.
            }

            stream.Position += 14; // Skip MAC header

            var firstByte = reader.ReadByte();

            var ipVersion = (firstByte & 0xF0) >> 4;

            if (ipVersion != 4)
            {
                return; // We only support IPv4
            }
            var headerLength          = (firstByte & 0x0F) * 4;
            var positionAfterIpHeader = stream.Position - 1 + headerLength;

            stream.Position += 1; // Skip to total length.

            // Captured packet, ignoring any TZSP layers.
            var totalPacketLengthIncludingIpHeader = reader.ReadUInt16();

            stream.Position += 5; // Skip to protocol.

            var protocol = reader.ReadByte();

            stream.Position += 2; // Skip to source address.

            var sourceAddress      = new IPAddress(reader.ReadBytesAndVerify(4));
            var destinationAddress = new IPAddress(reader.ReadBytesAndVerify(4));

            var sourceAddressString      = sourceAddress.ToString();
            var destinationAddressString = destinationAddress.ToString();

            string sourceAddressType      = DetermineIPv4AddressType(sourceAddress);
            string destinationAddressType = DetermineIPv4AddressType(destinationAddress);

            stream.Position = positionAfterIpHeader;

            int?sourcePort      = null;
            int?destinationPort = null;

            if (protocol == 17 || protocol == 6)
            {
                // If UDP or TCP, source and destination port are next fields.
                sourcePort      = reader.ReadUInt16();
                destinationPort = reader.ReadUInt16();
            }
            else
            {
                // unknown protocol
            }

            string protocolName = "unknown";

            if (Enum.IsDefined(typeof(ProtocolType), (int)protocol))
            {
                protocolName = ((ProtocolType)protocol).ToString().ToLowerInvariant();
            }

            BytesBase.WithLabels(sourceAddressString, sourceAddressType, destinationAddressString, destinationAddressType, protocolName, listenPort.ToString()).Inc(totalPacketLengthIncludingIpHeader);
            PacketsBase.WithLabels(sourceAddressString, sourceAddressType, destinationAddressString, destinationAddressType, protocolName, listenPort.ToString()).Inc();
        }
예제 #2
0
 public void sendPacket(PacketsBase packet)
 {
 }