protected IPAddress[] subNetListIPv4Addresses(IPAddress address, IPAddress subNetMask, UInt32 maxLen) { UInt32 addr, mask, first, last, len, i, current; IPAddress[] result; addr = NetworkUtils.ntohl(BitConverter.ToUInt32(address.GetAddressBytes(), 0)); mask = NetworkUtils.ntohl(BitConverter.ToUInt32(subNetMask.GetAddressBytes(), 0)); first = (addr & mask) + 1; last = (addr | ~mask) - 1; len = last - first + 1; if (len > maxLen) { len = maxLen; } result = new IPAddress[len]; for (i = 0; i < len; i++) { current = first + i; result[i] = new IPAddress( BitConverter.ToUInt32(new byte[] { (byte)(current >> 24), (byte)(current >> 16), (byte)(current >> 8), (byte)current }, 0)); } return(result); }
public override void reciever(IPEndPoint from, byte[] data) { VivotekHeader header; int headerSize; int position; string model, mac; IPAddress IPv4; header = data.GetStruct <VivotekHeader>(); headerSize = typeof(VivotekHeader).StructLayoutAttribute.Size; if (NetworkUtils.ntohl(header.magic) != magic) { Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: Vivotek.reciever(): Wrong packet magic value"); return; } mac = ""; model = ""; IPv4 = null; position = headerSize; while (position < data.Length) { byte variable; byte[] value; variable = readNextValue(data, ref position, out value); switch (variable) { case (byte)VivotekValue.invalid: Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: Vivotek.reciever(): Invalid packet"); return; case (byte)VivotekValue.IPAddress: IPv4 = new IPAddress(value); break; case (byte)VivotekValue.longName: Logger.WriteLine(Logger.DebugLevel.Debug, "longName"); Logger.WriteData(Logger.DebugLevel.Debug, value); break; case (byte)VivotekValue.macAddress: mac = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", value[0], value[1], value[2], value[3], value[4], value[5]); break; case (byte)VivotekValue.shortName: model = Encoding.UTF8.GetString(value); break; } } if (IPv4 != null) { viewer.deviceFound(name, 1, IPv4, model, mac); } }
private IPAddress readAnswer_A(byte[] data, ref int position, int dataLen) { UInt32 ipVal; if (dataLen != 4) { Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: readAnswer_A(): Invalid address size!"); return(IPAddress.Any); } ipVal = NetworkUtils.ntohl(readUInt32(data, ref position)); return(new IPAddress(ipVal)); }
public override byte[] sender(IPEndPoint dest) { Dahua2Header header; byte[] headerArray; string bodyStr; byte[] bodyArray; byte[] result; int headerSize; header = new Dahua2Header { headerSize = dtohl((UInt32)typeof(Dahua2Header).StructLayoutAttribute.Size), headerMagic = NetworkUtils.ntohl(magic), reserved1 = 0, reserved2 = 0, packetSize1 = 0, reserved3 = 0, packetSize2 = 0, reserved4 = 0 }; bodyStr = "{ \"method\" : \"DHDiscover.search\", \"params\" : { \"mac\" : \"\", \"uni\" : 1 } }\r\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) { Dahua1Section1 section1; UInt16 section2Len; UInt16 section3Len; int deviceMacSize; UInt32 deviceIP; string deviceIPStr, deviceModel, deviceSerial; byte[] deviceTypeArray, deviceMacArray; byte[] section3Array; int index; deviceMacSize = 17; // "00:11:22:33:44:55".Length() // section 1: section1 = data.GetStruct <Dahua1Section1>(); if (dtohl(section1.headerMagic) != magicResponse) { return; } // IP Address deviceIP = NetworkUtils.ntohl(section1.ip); deviceIPStr = String.Format("{0}.{1}.{2}.{3}", (byte)((deviceIP >> 24) & 0xFF), (byte)((deviceIP >> 16) & 0xFF), (byte)((deviceIP >> 8) & 0xFF), (byte)((deviceIP) & 0xFF) ); // device type deviceModel = Encoding.UTF8.GetString(section1.deviceType); section2Len = dtohs(section1.section2Len); section3Len = dtohs(section1.section3Len); // 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: if (section3Len > 0) { Dictionary <string, string> values; section3Array = new byte[section3Len]; Array.Copy(data, index, section3Array, 0, section3Array.Length); index += section3Array.Length; values = parseSection3(data); if (values.ContainsKey("SerialNo")) { deviceSerial = values["SerialNo"]; } } viewer.deviceFound(name, 1, deviceIPStr, deviceModel, deviceSerial); }
public override void reciever(IPEndPoint from, byte[] data) { string deviceIPStr, deviceModel, deviceSerial; // xml is much bigger if (data.Length == typeof(BoschBinaryResponse).StructLayoutAttribute.Size) { BoschBinaryResponse binary; UInt32 ip; binary = data.GetStruct <BoschBinaryResponse>(); if (NetworkUtils.ntohl(binary.magic) != magic) { Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: Bosch.reciever(): Packet with wrong header."); return; } ip = NetworkUtils.ntohl(binary.ip); deviceIPStr = String.Format("{0}.{1}.{2}.{3}", (byte)((ip >> 24) & 0xFF), (byte)((ip >> 16) & 0xFF), (byte)((ip >> 8) & 0xFF), (byte)((ip) & 0xFF) ); 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, deviceIPStr, deviceModel, deviceSerial); } else { string xml; Regex type, ip, mac, serial; Match m; xml = Encoding.UTF8.GetString(data); type = new Regex("<friendlyName>([^<]*)</friendlyName>"); ip = new Regex("<unitIPAddress>([^<]*)</unitIPAddress>"); mac = new Regex("<physAddress>([^<]*)</physAddress>"); serial = new Regex("<serialNumber>([^<]*)</serialNumber>"); deviceIPStr = ""; m = ip.Match(xml); if (m.Success) { deviceIPStr = 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; } } viewer.deviceFound(name, 2, deviceIPStr, deviceModel, deviceSerial); } }
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.ntohl(binary.magic) != answerMagic) { Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: Bosch.reciever(): Packet with wrong header."); return; } ip = btohl(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.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.WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: Bosch.reciever(): Invalid ipv6 format: {0}", deviceIPv6Str)); } } }