public override byte[] sender(IPEndPoint dest) { UInt32 payload; payload = NetworkUtils.littleEndian32(requestVersion); return(payload.GetBytes()); }
public override byte[] sender(IPEndPoint dest) { Dahua2Header header; byte[] headerArray; string bodyStr; byte[] bodyArray; byte[] result; int headerSize; header = new Dahua2Header { headerSize = NetworkUtils.littleEndian32((UInt32)typeof(Dahua2Header).StructLayoutAttribute.Size), headerMagic = NetworkUtils.bigEndian32(magic), _reserved_08 = 0, _reserved_0C = 0, packetSize1 = 0, _reserved_14 = 0, packetSize2 = 0, _reserved_1C = 0 }; bodyStr = "{ \"method\" : \"DHDiscover.search\", \"params\" : { \"mac\" : \"\", \"uni\" : 1 } }\n"; bodyArray = Encoding.UTF8.GetBytes(bodyStr); headerSize = typeof(Dahua2Header).StructLayoutAttribute.Size; result = new byte[headerSize + bodyArray.Length]; header.packetSize1 = (UInt32)bodyArray.Length; header.packetSize2 = (UInt32)bodyArray.Length; headerArray = header.GetBytes(); headerArray.CopyTo(result, 0); bodyArray.CopyTo(result, headerSize); return(result); }
public override void reciever(IPEndPoint from, byte[] data) { string deviceModel, deviceSerial; // xml is much bigger if (data.Length == typeof(BoschBinaryAnswer).StructLayoutAttribute.Size) { BoschBinaryAnswer binary; UInt32 ip; binary = data.GetStruct <BoschBinaryAnswer>(); if (NetworkUtils.bigEndian32(binary.magic) != answerMagic) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, "Warning: Bosch.reciever(): Packet with wrong header."); return; } ip = NetworkUtils.littleEndian32(binary.ipv4); deviceSerial = binary.mac.ToString(); deviceModel = name; viewer.deviceFound(name, 1, new IPAddress(ip), deviceModel, deviceSerial); } else { string xml; string deviceIPv4Str, deviceIPv6Str; Regex type, ipv4, ipv6, mac, serial; Match m; IPAddress ip; xml = Encoding.UTF8.GetString(data); type = new Regex("<friendlyName>([^<]*)</friendlyName>"); ipv4 = new Regex("<unitIPAddress>([^<]*)</unitIPAddress>"); ipv6 = new Regex("<unitIPv6Address>([^<]*)</unitIPv6Address>"); mac = new Regex("<physAddress>([^<]*)</physAddress>"); serial = new Regex("<serialNumber>([^<]*)</serialNumber>"); deviceIPv4Str = ""; m = ipv4.Match(xml); if (m.Success) { deviceIPv4Str = m.Groups[1].Value; } deviceIPv6Str = ""; m = ipv6.Match(xml); if (m.Success) { deviceIPv6Str = m.Groups[1].Value; } deviceModel = ""; m = type.Match(xml); if (m.Success) { deviceModel = m.Groups[1].Value; } deviceSerial = ""; m = serial.Match(xml); if (m.Success) { deviceSerial = m.Groups[1].Value; } else { m = mac.Match(xml); if (m.Success) { deviceSerial = m.Groups[1].Value; } } if (!IPAddress.TryParse(deviceIPv4Str, out ip)) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Bosch.reciever(): Invalid ipv4 format: {0}", deviceIPv4Str)); ip = from.Address; } viewer.deviceFound(name, 2, ip, deviceModel, deviceSerial); if (IPAddress.TryParse(deviceIPv6Str, out ip)) { viewer.deviceFound(name, 2, ip, deviceModel, deviceSerial); } else { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Bosch.reciever(): Invalid ipv6 format: {0}", deviceIPv6Str)); } } }
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)); } } }
public override void reciever(IPEndPoint from, byte[] data) { Dahua2Header header; string bodyStr; byte[] body; int headerSize, packetSize; string method; IPAddress ip; headerSize = typeof(Dahua2Header).StructLayoutAttribute.Size; header = data.GetStruct <Dahua2Header>(); if (NetworkUtils.littleEndian32(header.headerSize) != headerSize) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua2.reciever(): recieved invalid frame (headerSize={0}, expected {1})!", header.headerSize, headerSize)); return; } packetSize = data.Length - headerSize; if (NetworkUtils.littleEndian32(header.packetSize1) != packetSize) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua2.reciever(): recieved invalid frame (packetSize={0}, expected {1})!", NetworkUtils.littleEndian32(header.packetSize1), packetSize)); return; } body = new byte[packetSize]; for (int i = 0; i < packetSize; i++) { body[i] = data[headerSize + i]; } bodyStr = Encoding.UTF8.GetString(body); method = extractJsonString("method", bodyStr); if (method == "client.notifyDevInfo") { string deviceModel, deviceIPv4, deviceIPv6, deviceSerial; string IPv4Section, IPv6Section; deviceModel = extractJsonString("DeviceType", bodyStr); if (deviceModel == null) { deviceModel = "Dahua"; } IPv4Section = extractJsonSection("IPv4Address", bodyStr); deviceIPv4 = extractJsonString("IPAddress", IPv4Section); if (deviceIPv4 == null) { deviceIPv4 = from.Address.ToString(); } deviceIPv6 = null; IPv6Section = extractJsonSection("IPv6Address", bodyStr); if (IPv6Section != null) { deviceIPv6 = extractJsonString("IPAddress", IPv6Section); if (deviceIPv6 != null) { int sub = deviceIPv6.IndexOfAny(new char[] { '/', '\\' }); if (sub > 0) { deviceIPv6 = deviceIPv6.Substring(0, sub - 1); } } } deviceSerial = extractJsonString("SerialNo", bodyStr); if (deviceSerial == null) { deviceSerial = extractJsonString("mac", bodyStr); } if (deviceSerial == null) { deviceSerial = "Dahua device"; } if (!IPAddress.TryParse(deviceIPv4, out ip)) { ip = from.Address; Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua2.reciever(): Invalid ipv4 format: {0}", deviceIPv4)); } viewer.deviceFound(name, 2, ip, deviceModel, deviceSerial); if (deviceIPv6 != null) { if (IPAddress.TryParse(deviceIPv6, out ip)) { viewer.deviceFound(name, 2, ip, deviceModel, deviceSerial); } else { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Dahua2.reciever(): Invalid ipv6 format: {0}", deviceIPv6)); } } } }