From fb46694ab8bb87d55aa13abf840e4029eab70586 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Thu, 2 Jan 2020 21:05:04 +0100 Subject: [PATCH] Re-send mdns queries for unresolved PTRs --- src/ippdiscovery.cpp | 92 ++++++++++++++++++++++++++------------------ src/ippdiscovery.h | 13 ++++++- 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/src/ippdiscovery.cpp b/src/ippdiscovery.cpp index 30b81c0..9bffa97 100644 --- a/src/ippdiscovery.cpp +++ b/src/ippdiscovery.cpp @@ -5,6 +5,8 @@ #define AAAA 28 #define SRV 33 +#define ALL 255 //for querying + void put_addr(Bytestream& bts, QStringList addr) { for(int i = 0; i < addr.length(); i++) @@ -57,6 +59,11 @@ IppDiscovery::~IppDiscovery() { void IppDiscovery::discover() { + sendQuery(PTR, {"_ipp","_tcp","local"}); +} + +void IppDiscovery::sendQuery(quint16 qtype, QStringList addr) { + qDebug() << "discovering" << qtype << addr; Bytestream query; quint16 transactionid = 0; @@ -64,18 +71,38 @@ void IppDiscovery::discover() { quint16 questions = 1; query << transactionid << flags << questions << (quint16)0 << (quint16)0 << (quint16)0; - put_addr(query, {"_ipp","_tcp","local"}); - query << (quint16)0x000C << (quint16)0x0001; + put_addr(query, addr); + query << qtype << (quint16)0x0001; QByteArray bytes((char*)(query.raw()), query.size()); socket->writeDatagram(bytes, QHostAddress("224.0.0.251"), 5353); - qDebug() << "discovering"; - } + void IppDiscovery::update() { + for(QStringList::Iterator it = _ipp.begin(); it != _ipp.end(); it++) + { + quint16 port = _ports[*it]; + QString target = _targets[*it]; + QString rp = _rps[*it]; + + for(QMultiMap::Iterator ait = _AAs.begin(); ait != _AAs.end(); ait++) + { + if(ait.key() == target) + { + QString ip = ait.value(); + QString addr = ip+":"+QString::number(port)+"/"+rp; + if(!_found.contains(addr)) + { + _found.append(addr); + _found.sort(Qt::CaseInsensitive); + } + } + } + } + qDebug() << _favourites << _found; this->setStringList(_favourites+_found); } @@ -89,12 +116,7 @@ void IppDiscovery::readPendingDatagrams() QHostAddress sender; quint16 senderPort; - QMap ptrs; - QMap rps; - QMap ports; - QMap targets; - QMultiMap AAs; - QMultiMap AAAAs; + QStringList ipp_ptrs; socket->readDatagram((char*)(resp.raw()), size, &sender, &senderPort); sender = QHostAddress(sender.toIPv4Address()); @@ -121,7 +143,10 @@ void IppDiscovery::readPendingDatagrams() if (atype == PTR) { QString tmpname = get_addr(resp).join("."); - ptrs[aaddr] = tmpname; + if(aaddr.endsWith("_ipp._tcp.local")) + { + ipp_ptrs.append(tmpname); + } } else if(atype == TXT) { @@ -133,7 +158,7 @@ void IppDiscovery::readPendingDatagrams() { std::string tmprp; tmp/tmp.remaining() >> tmprp; - rps[aaddr] = tmprp.c_str(); + _rps[aaddr] = tmprp.c_str(); } } } @@ -142,15 +167,15 @@ void IppDiscovery::readPendingDatagrams() quint16 prio, w, port; resp >> prio >> w >> port; QString target = get_addr(resp).join("."); - ports[aaddr] = port; - targets[aaddr] = target; + _ports[aaddr] = port; + _targets[aaddr] = target; } else if(atype == A) { quint32 addr; resp >> addr; QHostAddress haddr(addr); - AAs.insert(aaddr, haddr.toString()); + _AAs.insert(aaddr, haddr.toString()); } else { @@ -160,33 +185,26 @@ void IppDiscovery::readPendingDatagrams() } - qDebug() << "ptrs" << ptrs; - qDebug() << "rps" << rps; - qDebug() << "ports" << ports; - qDebug() << "targets" << targets; - qDebug() << "AAs" << AAs; - qDebug() << "AAAAs" << AAAAs; + qDebug() << "new ipp ptrs" << ipp_ptrs; + qDebug() << "ipp ptrs" << _ipp; + qDebug() << "rps" << _rps; + qDebug() << "ports" << _ports; + qDebug() << "targets" << _targets; + qDebug() << "AAs" << _AAs; + qDebug() << "AAAAs" << _AAAAs; - for(QMap::Iterator it = ptrs.begin(); it != ptrs.end(); it++) + for(QStringList::Iterator it = ipp_ptrs.begin(); it != ipp_ptrs.end(); it++) { - quint16 port = ports[it.value()]; - QString target = targets[it.value()]; - QString rp = rps[it.value()]; - - for(QMultiMap::Iterator ait = AAs.begin(); ait != AAs.end(); ait++) + if(!_ipp.contains(*it)) { - if(ait.key() == target) - { - QString ip = ait.value(); - QString addr = ip+":"+QString::number(port)+"/"+rp; - if(!_found.contains(addr)) - { - _found.append(addr); - _found.sort(Qt::CaseInsensitive); - } - } + _ipp.append(*it); + } + if(!_ports.contains(*it) || !_targets.contains(*it) || !_rps.contains(*it)) + { // if the PTR doesn't already resolve, ask for everything about it + sendQuery(ALL, it->split('.')); } } + } this->update(); diff --git a/src/ippdiscovery.h b/src/ippdiscovery.h index f988eaa..3dd71dd 100644 --- a/src/ippdiscovery.h +++ b/src/ippdiscovery.h @@ -18,9 +18,20 @@ signals: public slots: void readPendingDatagrams(); - void update(); protected: private: + void sendQuery(quint16 qtype, QStringList addr); + + void update(); + + QStringList _ipp; + QMap _rps; + QMap _ports; + QMap _targets; + + QMultiMap _AAs; + QMultiMap _AAAAs; + QStringList _favourites; QStringList _found; QUdpSocket* socket;