diff --git a/library/src/networkdiscovery.cpp b/library/src/networkdiscovery.cpp index 36cf554d..0d04ec58 100644 --- a/library/src/networkdiscovery.cpp +++ b/library/src/networkdiscovery.cpp @@ -36,7 +36,8 @@ void NetworkDiscovery::searchDevices() discovered_devices.clear(); QByteArray data; auto dStream = std::make_unique(&data, QIODevice::ReadWrite); - (*dStream) << static_cast(0x2a) << static_cast(m_device_type); + (*dStream) << static_cast(0x2a); + (*dStream) << static_cast(m_device_type); if (socket != nullptr) { // auto datagram = QNetworkDatagram{data,m_broadcast_address, m_port}; @@ -54,29 +55,39 @@ void NetworkDiscovery::readPendingDatagrams() { while (socket->hasPendingDatagrams()) { auto datagram = socket->receiveDatagram(); - auto sender_address = QHostAddress(datagram.senderAddress().toIPv4Address()); auto data = datagram.data(); + if (datagram.isNull() || !datagram.isValid() || data.size() < 4) { + datagram.clear(); + return; + } + auto sender_address = QHostAddress(datagram.senderAddress().toIPv4Address()); QDataStream inStream { &data, QIODevice::ReadOnly }; quint16 the_answer_to_everything; quint16 device_type; inStream >> the_answer_to_everything; + if (the_answer_to_everything != 0x2a) { + datagram.clear(); + return; + } inStream >> device_type; qDebug() << "found device: " << sender_address << " type: " << device_type; if (device_type == static_cast(m_device_type)) { bool skip = false; for (auto address : m_own_ipv4) { - if (address == sender_address) + if (address == sender_address){ skip = true; + } } - if (skip) + if (skip){ continue; // do not answer or discover self + } } discovered_devices.append(QPair { static_cast(device_type), sender_address}); if (static_cast(device_type) == DeviceType::GUI) { data = QByteArray(); QDataStream outStream { &data, QIODevice::ReadWrite }; - outStream << static_cast(m_device_type); + outStream << static_cast(0x2a) << static_cast(m_device_type); socket->writeDatagram(data, sender_address, m_port); } }