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) { GigEVisionAckn answer; UInt32 version; string macAddress; IPAddress ipv4; string model; string vendor; string serial; if (data.Length != typeof(GigEVisionAckn).StructLayoutAttribute.Size) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid size packet recieved from {0}", from.ToString())); return; } answer = data.GetStruct <GigEVisionAckn>(); if (NetworkUtils.NetworkToHostOrder16(answer.payloadLen) != data.Length - 8) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid size payload length value (got value {0} while value {1} was expected) recieved from {2}", NetworkUtils.NetworkToHostOrder16(answer.payloadLen), data.Length - 8, from.ToString())); return; } version = ((UInt32)NetworkUtils.NetworkToHostOrder16(answer.plMajorVersion)) << 16 | ((UInt32)NetworkUtils.NetworkToHostOrder16(answer.plMinorVersion)); if (version != 0x00010002) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid packet version (got value 0x{0:X8} while value 0x{1:X8} was expected) recieved from {2}", version, 0x00010002, from.ToString())); } if (answer.plIPCurrentAddr != 0) { ipv4 = new IPAddress((long)answer.plIPCurrentAddr); } else { ipv4 = from.Address; } macAddress = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", answer.plMacAddress.byte00, answer.plMacAddress.byte01, answer.plMacAddress.byte02, answer.plMacAddress.byte03, answer.plMacAddress.byte04, answer.plMacAddress.byte05); serial = Encoding.UTF8.GetString(answer.plSerialNumber); if (serial == "") { serial = macAddress; } vendor = Encoding.UTF8.GetString(answer.plManufacturer); if (vendor == "") { vendor = name; } model = Encoding.UTF8.GetString(answer.plModel); if (model == "") { model = Encoding.UTF8.GetString(answer.plUsername); } viewer.deviceFound(vendor, 0, ipv4, model, serial); }
public VivotekHeader(byte sessionId) { session = sessionId; magic = NetworkUtils.bigEndian32(Vivotek.magic); }
public override void reciever(IPEndPoint from, byte[] data) { mDNSHeader header; int headerSize; int expectedQueries, expectedAnwers; int position; headerSize = typeof(mDNSHeader).StructLayoutAttribute.Size; if (data.Length <= headerSize) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, "Warning: mDNS.reciever(): invalid packet size."); Logger.getInstance().WriteData(Logger.DebugLevel.Warn, data); return; } header = data.GetStruct <mDNSHeader>(); expectedQueries = NetworkUtils.NetworkToHostOrder16(header.questions); expectedAnwers = NetworkUtils.NetworkToHostOrder16(header.authorityRRs) + NetworkUtils.NetworkToHostOrder16(header.answerRRs) + NetworkUtils.NetworkToHostOrder16(header.additionalRRs); if (expectedAnwers > 0) { position = headerSize; try { readQueries(data, ref position, expectedQueries); readAnswers(data, ref position, expectedAnwers); } catch (OverflowException ex) { Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: mDNS.reciever(): packet parsing overflow at position 0x{0:X}!", position)); Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, ex.ToString()); Logger.getInstance().WriteData(Logger.DebugLevel.Warn, data); } } }
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)); } } } }
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 = String.Format("{0:X02}:{1:X02}:{2:X02}:{3:X02}:{4:X02}:{5:X02}", binary.mac.byte00, binary.mac.byte01, binary.mac.byte02, binary.mac.byte03, binary.mac.byte04, binary.mac.byte05); 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) { UbiquitiHeader header; int headerSize; int position; IPAddress IPv4; byte[] mac; string macAddress; string model; headerSize = typeof(UbiquitiHeader).StructLayoutAttribute.Size; if (data.Length < headerSize) { Logger.LogWarning(String.Format("Warning: Ubiquiti.reciever(): Invalid size packet recieved from {0}", from.ToString())); return; } header = data.GetStruct <UbiquitiHeader>(); if (NetworkUtils.bigEndian16(header.magic) != anwserMagic) { Logger.LogWarning(String.Format("Warning: Ubiquiti.reciever(): Invalid magic value from {0}", from.ToString())); /* not enougth tested Ubiquiti answer to say it is always this magic, so disabling return for now */ // return; } if (NetworkUtils.bigEndian16(header.packetSize) != data.Length - 4) { Logger.LogWarning(String.Format("Warning: Ubiquiti.reciever(): Invalid packet size value (got value {0} while value {1} was expected) from {2}", NetworkUtils.bigEndian16(header.packetSize), data.Length - 4, from.ToString())); return; } position = headerSize; IPv4 = null; mac = null; model = ""; while (position < data.Length) { UInt16 variable; byte[] value; variable = readNextValue(data, ref position, out value); switch (variable) { case (byte)UbiquitiValue.typeNull: Logger.LogWarning("Warning: Ubiquiti.reciever(): Invalid packet, variable type null"); return; case (byte)UbiquitiValue.macAddress1: // binary: 4 bytes case (byte)UbiquitiValue.macAddress2: // binary: 4 bytes if (value.Length != 6) { Logger.LogWarning(String.Format("Warning: Ubiquiti.reciever(): invalid size of variable 0x{0:X4} (expected={1}, found={2})", (UInt16)variable, 6, value.Length)); break; } if (mac == null) { mac = value; } break; case (byte)UbiquitiValue.macIPv4: // binary: 10 bytes if (value.Length != 10) { Logger.LogWarning(String.Format("Warning: Ubiquiti.reciever(): invalid size of variable 0x{0:X4} (expected={1}, found={2})", (UInt16)variable, 10, value.Length)); break; } if (mac == null) { mac = new byte[6]; Array.Copy(value, 0, mac, 0, 6); } if (IPv4 == null) { var IPv4Bytes = new byte[4]; Array.Copy(value, 6, IPv4Bytes, 0, 4); IPv4 = new IPAddress(IPv4Bytes); } break; case (byte)UbiquitiValue.model1: // string case (byte)UbiquitiValue.model2: // string if (model == "") { model = Encoding.UTF8.GetString(value); } break; } } if (IPv4 == null) { IPv4 = from.Address; } if (mac != null && IPv4 != null) { macAddress = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); if (model == "") { model = "unknown"; } viewer.deviceFound(name, 1, IPv4, model, macAddress); } }