示例#1
0
        public override void reciever(IPEndPoint from, byte[] data)
        {
            Dahua1Section1 section1;
            int            section1Len;
            byte           section2Len;
            UInt16         section3Len;

            int deviceMacSize;

            UInt32 deviceIPv4;

            string deviceModel, deviceSerial, deviceIPv6;

            byte[] deviceTypeArray, deviceMacArray;
            byte[] section3Array;

            int index;

            section1Len = Marshal.SizeOf(typeof(Dahua1Section1));

            deviceMacSize = 17; // "00:11:22:33:44:55".Length()

            // section 1:
            if (data.Length < Marshal.SizeOf(typeof(Dahua1Section1)))
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua1.reciever(): Invalid packet size (less than section1) from {0}", from.ToString()));
                return;
            }

            section1 = data.GetStruct <Dahua1Section1>();

            if (section1.headerMagic != answerMagic)
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua1.reciever(): Wrong header magic recieved from {0}", from.ToString()));
                return;
            }

            // IP Address
            deviceIPv4 = NetworkUtils.littleEndian32(section1.ip);

            // device type
            deviceModel = Encoding.UTF8.GetString(section1.deviceType);

            section2Len = section1.section2Len;
            section3Len = NetworkUtils.littleEndian16(section1.section3Len);

            if (section1Len + section2Len + section3Len != data.Length)
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua1.reciever(): Packet has wrong size = {0} (expected was {1})", data.Length, section1Len + section2Len + section3Len));
                return;
            }

            // section 2:
            // mac Address
            deviceMacSize  = Math.Min(deviceMacSize, section2Len);
            deviceMacArray = new byte[deviceMacSize];
            index          = typeof(Dahua1Section1).StructLayoutAttribute.Size;
            Array.Copy(data, index, deviceMacArray, 0, deviceMacSize);
            index       += deviceMacArray.Length;
            deviceSerial = Encoding.UTF8.GetString(deviceMacArray);

            // we still have more data in section 2
            if (section2Len > deviceMacSize)
            {
                // if deviceType from section2, replace deviceType with this one
                deviceTypeArray = new byte[section2Len - deviceMacSize];
                Array.Copy(data, index, deviceTypeArray, 0, deviceTypeArray.Length);
                index      += deviceTypeArray.Length;
                deviceModel = Encoding.UTF8.GetString(deviceTypeArray);
            }

            // section 3:
            deviceIPv6 = null;
            if (section3Len > 0)
            {
                Dictionary <string, string> values;

                section3Array = new byte[section3Len];
                Array.Copy(data, index, section3Array, 0, section3Array.Length);
                index += section3Array.Length;

                values = parseSection3(section3Array);
                if (values.ContainsKey("SerialNo"))
                {
                    deviceSerial = values["SerialNo"];
                }
                if (values.ContainsKey("IPv6Addr"))
                {
                    deviceIPv6 = values["IPv6Addr"];
                    if (deviceIPv6.Contains(';'))
                    {
                        var IPv6Split = deviceIPv6.Split(';');
                        deviceIPv6 = IPv6Split[0];
                    }
                    if (deviceIPv6.Contains('/'))
                    {
                        var IPv6Split = deviceIPv6.Split('/');
                        deviceIPv6 = IPv6Split[0];
                    }
                }
            }

            if (deviceIPv4 != 0)
            {
                viewer.deviceFound(name, 1, new IPAddress(deviceIPv4), deviceModel, deviceSerial);
            }
            else
            {
                viewer.deviceFound(name, 1, from.Address, deviceModel, deviceSerial);
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua1.reciever(): Recieved ipv4 is null (from: {0})", from.Address.ToString()));
            }

            if (deviceIPv6 != null)
            {
                IPAddress ip;
                if (IPAddress.TryParse(deviceIPv6, out ip))
                {
                    viewer.deviceFound(name, 2, ip, deviceModel, deviceSerial);
                }
                else
                {
                    Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua1.reciever(): Invalid ipv6 format: {0}", deviceIPv6));
                }
            }
        }