mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-28 16:20:01 +02:00
Integrate qjdns/jdnsshared, make a shared library, and do some implementation
This commit is contained in:
45
thirdparty/jdns/CMakeLists.txt
vendored
45
thirdparty/jdns/CMakeLists.txt
vendored
@@ -6,39 +6,46 @@ SET(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
SET( QT_USE_QTNETWORK TRUE )
|
||||
INCLUDE( ${QT_USE_FILE} )
|
||||
|
||||
add_definitions( ${QT_DEFINITIONS} )
|
||||
add_definitions( -DQT_SHARED )
|
||||
|
||||
if(WIN32)
|
||||
SET(PLATFORM_SPECIFIC_LIBS "ws2_32.dll" "advapi32.dll" )
|
||||
set(PLATFORM_SPECIFIC_LIBS "ws2_32.dll" "advapi32.dll" )
|
||||
endif(WIN32)
|
||||
|
||||
set(JDNS_SOURCES
|
||||
jdns_util.c
|
||||
jdns_packet.c
|
||||
jdns_mdnsd.c
|
||||
jdns_sys.c
|
||||
jdns.c
|
||||
qjdns_sock.cpp
|
||||
qjdns.cpp
|
||||
set(TOMAHAWK_JDNS_SOURCES
|
||||
jdns/jdns_util.c
|
||||
jdns/jdns_packet.c
|
||||
jdns/jdns_mdnsd.c
|
||||
jdns/jdns_sys.c
|
||||
jdns/jdns.c
|
||||
jdns/qjdns_sock.cpp
|
||||
jdns/qjdns.cpp
|
||||
jdnsshared/jdnsshared.cpp
|
||||
)
|
||||
|
||||
set(JDNS_HEADERS
|
||||
qjdns.h
|
||||
set(TOMAHAWK_JDNS_HEADERS
|
||||
jdns/qjdns.h
|
||||
jdns/qjdns_helpers.h
|
||||
jdnsshared/jdnsshared.h
|
||||
jdnsshared/jdnsshared_helpers.h
|
||||
)
|
||||
|
||||
include_directories(
|
||||
.
|
||||
${QT_INCLUDE_DIR}
|
||||
${QT_INCLUDES}
|
||||
jdns
|
||||
)
|
||||
|
||||
qt4_wrap_cpp( JDNS_MOC ${JDNS_HEADERS} )
|
||||
qt4_wrap_cpp( TOMAHAWK_JDNS_MOC ${TOMAHAWK_JDNS_HEADERS} )
|
||||
|
||||
if(WIN32)
|
||||
ADD_LIBRARY(jdns SHARED ${JDNS_SOURCES} ${JDNS_MOC})
|
||||
else()
|
||||
ADD_LIBRARY(jdns STATIC ${JDNS_SOURCES} ${JDNS_MOC})
|
||||
endif()
|
||||
ADD_LIBRARY(tomahawk_jdns SHARED ${TOMAHAWK_JDNS_SOURCES} ${TOMAHAWK_JDNS_MOC})
|
||||
|
||||
target_link_libraries(jdns
|
||||
target_link_libraries(tomahawk_jdns
|
||||
${QT_LIBRARIES}
|
||||
)
|
||||
|
||||
INSTALL(TARGETS jdns ARCHIVE DESTINATION lib)
|
||||
SET_TARGET_PROPERTIES( tomahawk_jdns PROPERTIES DEFINE_SYMBOL MAKE_JDNS_LIB )
|
||||
|
||||
INSTALL(TARGETS tomahawk_jdns DESTINATION lib)
|
||||
|
638
thirdparty/jdns/jdns/qjdns.cpp
vendored
Normal file
638
thirdparty/jdns/jdns/qjdns.cpp
vendored
Normal file
@@ -0,0 +1,638 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Justin Karneges
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "qjdns.h"
|
||||
|
||||
#include <time.h>
|
||||
#include "qjdns_sock.h"
|
||||
#include "qjdns_helpers.h"
|
||||
#include "jdns.h"
|
||||
|
||||
// for fprintf
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// QJDns::NameServer
|
||||
//----------------------------------------------------------------------------
|
||||
QJDns::NameServer::NameServer()
|
||||
{
|
||||
port = JDNS_UNICAST_PORT;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// QJDns::Record
|
||||
//----------------------------------------------------------------------------
|
||||
QJDns::Record::Record()
|
||||
{
|
||||
ttl = 0;
|
||||
type = -1;
|
||||
haveKnown = false;
|
||||
}
|
||||
|
||||
bool QJDns::Record::verify() const
|
||||
{
|
||||
jdns_rr_t *rr = export_record(*this);
|
||||
int ok = jdns_rr_verify(rr);
|
||||
jdns_rr_delete(rr);
|
||||
return (ok ? true : false);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// QJDns
|
||||
//----------------------------------------------------------------------------
|
||||
QJDns::Private::Private(QJDns *_q) :
|
||||
QObject(_q),
|
||||
q(_q),
|
||||
stepTrigger(this),
|
||||
debugTrigger(this),
|
||||
stepTimeout(this),
|
||||
pErrors(0),
|
||||
pPublished(0),
|
||||
pResponses(0)
|
||||
{
|
||||
sess = 0;
|
||||
shutting_down = false;
|
||||
new_debug_strings = false;
|
||||
pending = 0;
|
||||
|
||||
connect(&stepTrigger, SIGNAL(timeout()), SLOT(doNextStepSlot()));
|
||||
stepTrigger.setSingleShot(true);
|
||||
|
||||
connect(&debugTrigger, SIGNAL(timeout()), SLOT(doDebug()));
|
||||
debugTrigger.setSingleShot(true);
|
||||
|
||||
connect(&stepTimeout, SIGNAL(timeout()), SLOT(st_timeout()));
|
||||
stepTimeout.setSingleShot(true);
|
||||
|
||||
my_srand();
|
||||
|
||||
clock.start();
|
||||
}
|
||||
|
||||
QJDns::Private::~Private()
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
void QJDns::Private::cleanup()
|
||||
{
|
||||
if(sess)
|
||||
{
|
||||
jdns_session_delete(sess);
|
||||
sess = 0;
|
||||
}
|
||||
|
||||
shutting_down = false;
|
||||
pending = 0;
|
||||
|
||||
// it is safe to delete the QUdpSocket objects here without
|
||||
// deleteLater, since this code path never occurs when
|
||||
// a signal from those objects is on the stack
|
||||
qDeleteAll(socketForHandle);
|
||||
socketForHandle.clear();
|
||||
handleForSocket.clear();
|
||||
|
||||
stepTrigger.stop();
|
||||
stepTimeout.stop();
|
||||
need_handle = 0;
|
||||
}
|
||||
|
||||
bool QJDns::Private::init(QJDns::Mode _mode, const QHostAddress &address)
|
||||
{
|
||||
mode = _mode;
|
||||
|
||||
jdns_callbacks_t callbacks;
|
||||
callbacks.app = this;
|
||||
callbacks.time_now = cb_time_now;
|
||||
callbacks.rand_int = cb_rand_int;
|
||||
callbacks.debug_line = cb_debug_line;
|
||||
callbacks.udp_bind = cb_udp_bind;
|
||||
callbacks.udp_unbind = cb_udp_unbind;
|
||||
callbacks.udp_read = cb_udp_read;
|
||||
callbacks.udp_write = cb_udp_write;
|
||||
sess = jdns_session_new(&callbacks);
|
||||
jdns_set_hold_ids_enabled(sess, 1);
|
||||
next_handle = 1;
|
||||
need_handle = false;
|
||||
|
||||
int ret;
|
||||
|
||||
jdns_address_t *baddr = qt2addr(address);
|
||||
if(mode == Unicast)
|
||||
{
|
||||
ret = jdns_init_unicast(sess, baddr, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
jdns_address_t *maddr;
|
||||
if(address.protocol() == QAbstractSocket::IPv6Protocol)
|
||||
maddr = jdns_address_multicast6_new();
|
||||
else
|
||||
maddr = jdns_address_multicast4_new();
|
||||
ret = jdns_init_multicast(sess, baddr, JDNS_MULTICAST_PORT, maddr);
|
||||
jdns_address_delete(maddr);
|
||||
}
|
||||
jdns_address_delete(baddr);
|
||||
|
||||
if(!ret)
|
||||
{
|
||||
jdns_session_delete(sess);
|
||||
sess = 0;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void QJDns::Private::setNameServers(const QList<NameServer> &nslist)
|
||||
{
|
||||
jdns_nameserverlist_t *addrs = jdns_nameserverlist_new();
|
||||
for(int n = 0; n < nslist.count(); ++n)
|
||||
{
|
||||
jdns_address_t *addr = qt2addr(nslist[n].address);
|
||||
jdns_nameserverlist_append(addrs, addr, nslist[n].port);
|
||||
jdns_address_delete(addr);
|
||||
}
|
||||
jdns_set_nameservers(sess, addrs);
|
||||
jdns_nameserverlist_delete(addrs);
|
||||
}
|
||||
|
||||
void QJDns::Private::process()
|
||||
{
|
||||
if(!stepTrigger.isActive())
|
||||
{
|
||||
stepTimeout.stop();
|
||||
stepTrigger.start();
|
||||
}
|
||||
}
|
||||
|
||||
void QJDns::Private::processDebug()
|
||||
{
|
||||
new_debug_strings = true;
|
||||
if(!debugTrigger.isActive())
|
||||
debugTrigger.start();
|
||||
}
|
||||
|
||||
void QJDns::Private::doNextStep()
|
||||
{
|
||||
if(shutting_down && complete_shutdown)
|
||||
{
|
||||
cleanup();
|
||||
emit q->shutdownFinished();
|
||||
return;
|
||||
}
|
||||
|
||||
QPointer<QObject> self = this;
|
||||
|
||||
int ret = jdns_step(sess);
|
||||
|
||||
QList<LateError> errors;
|
||||
QList<int> published;
|
||||
QList<LateResponse> responses;
|
||||
bool finish_shutdown = false;
|
||||
|
||||
pErrors = &errors;
|
||||
pPublished = &published;
|
||||
pResponses = &responses;
|
||||
|
||||
while(1)
|
||||
{
|
||||
jdns_event_t *e = jdns_next_event(sess);
|
||||
if(!e)
|
||||
break;
|
||||
|
||||
if(e->type == JDNS_EVENT_SHUTDOWN)
|
||||
{
|
||||
finish_shutdown = true;
|
||||
}
|
||||
else if(e->type == JDNS_EVENT_PUBLISH)
|
||||
{
|
||||
if(e->status != JDNS_STATUS_SUCCESS)
|
||||
{
|
||||
QJDns::Error error;
|
||||
if(e->status == JDNS_STATUS_CONFLICT)
|
||||
error = QJDns::ErrorConflict;
|
||||
else
|
||||
error = QJDns::ErrorGeneric;
|
||||
LateError le;
|
||||
le.source_type = 1;
|
||||
le.id = e->id;
|
||||
le.error = error;
|
||||
errors += le;
|
||||
}
|
||||
else
|
||||
{
|
||||
published += e->id;
|
||||
}
|
||||
}
|
||||
else if(e->type == JDNS_EVENT_RESPONSE)
|
||||
{
|
||||
if(e->status != JDNS_STATUS_SUCCESS)
|
||||
{
|
||||
QJDns::Error error;
|
||||
if(e->status == JDNS_STATUS_NXDOMAIN)
|
||||
error = QJDns::ErrorNXDomain;
|
||||
else if(e->status == JDNS_STATUS_TIMEOUT)
|
||||
error = QJDns::ErrorTimeout;
|
||||
else
|
||||
error = QJDns::ErrorGeneric;
|
||||
LateError le;
|
||||
le.source_type = 0;
|
||||
le.id = e->id;
|
||||
le.error = error;
|
||||
errors += le;
|
||||
}
|
||||
else
|
||||
{
|
||||
QJDns::Response out_response;
|
||||
for(int n = 0; n < e->response->answerCount; ++n)
|
||||
out_response.answerRecords += import_record(e->response->answerRecords[n]);
|
||||
LateResponse lr;
|
||||
lr.id = e->id;
|
||||
lr.response = out_response;
|
||||
if(mode == Unicast)
|
||||
lr.do_cancel = true;
|
||||
else
|
||||
lr.do_cancel = false;
|
||||
responses += lr;
|
||||
}
|
||||
}
|
||||
|
||||
jdns_event_delete(e);
|
||||
}
|
||||
|
||||
if(ret & JDNS_STEP_TIMER)
|
||||
stepTimeout.start(jdns_next_timer(sess));
|
||||
else
|
||||
stepTimeout.stop();
|
||||
|
||||
need_handle = (ret & JDNS_STEP_HANDLE);
|
||||
|
||||
// read the lists safely enough so that items can be deleted
|
||||
// behind our back
|
||||
|
||||
while(!errors.isEmpty())
|
||||
{
|
||||
LateError i = errors.takeFirst();
|
||||
if(i.source_type == 0)
|
||||
jdns_cancel_query(sess, i.id);
|
||||
else
|
||||
jdns_cancel_publish(sess, i.id);
|
||||
emit q->error(i.id, i.error);
|
||||
if(!self)
|
||||
return;
|
||||
}
|
||||
|
||||
while(!published.isEmpty())
|
||||
{
|
||||
int i = published.takeFirst();
|
||||
emit q->published(i);
|
||||
if(!self)
|
||||
return;
|
||||
}
|
||||
|
||||
while(!responses.isEmpty())
|
||||
{
|
||||
LateResponse i = responses.takeFirst();
|
||||
if(i.do_cancel)
|
||||
jdns_cancel_query(sess, i.id);
|
||||
emit q->resultsReady(i.id, i.response);
|
||||
if(!self)
|
||||
return;
|
||||
}
|
||||
|
||||
if(finish_shutdown)
|
||||
{
|
||||
// if we have pending udp packets to write, stick around
|
||||
if(pending > 0)
|
||||
{
|
||||
pending_wait = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
complete_shutdown = true;
|
||||
process();
|
||||
}
|
||||
}
|
||||
|
||||
pErrors = 0;
|
||||
pPublished = 0;
|
||||
pResponses = 0;
|
||||
}
|
||||
|
||||
void QJDns::Private::removeCancelled(int id)
|
||||
{
|
||||
if(pErrors)
|
||||
{
|
||||
for(int n = 0; n < pErrors->count(); ++n)
|
||||
{
|
||||
if(pErrors->at(n).id == id)
|
||||
{
|
||||
pErrors->removeAt(n);
|
||||
--n; // adjust position
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(pPublished)
|
||||
{
|
||||
for(int n = 0; n < pPublished->count(); ++n)
|
||||
{
|
||||
if(pPublished->at(n) == id)
|
||||
{
|
||||
pPublished->removeAt(n);
|
||||
--n; // adjust position
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(pResponses)
|
||||
{
|
||||
for(int n = 0; n < pResponses->count(); ++n)
|
||||
{
|
||||
if(pResponses->at(n).id == id)
|
||||
{
|
||||
pResponses->removeAt(n);
|
||||
--n; // adjust position
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QJDns::Private::udp_readyRead()
|
||||
{
|
||||
QUdpSocket *sock = (QUdpSocket *)sender();
|
||||
int handle = handleForSocket.value(sock);
|
||||
|
||||
if(need_handle)
|
||||
{
|
||||
jdns_set_handle_readable(sess, handle);
|
||||
process();
|
||||
}
|
||||
else
|
||||
{
|
||||
// eat packet
|
||||
QByteArray buf(4096, 0);
|
||||
QHostAddress from_addr;
|
||||
quint16 from_port;
|
||||
sock->readDatagram(buf.data(), buf.size(), &from_addr, &from_port);
|
||||
}
|
||||
}
|
||||
|
||||
void QJDns::Private::udp_bytesWritten(qint64)
|
||||
{
|
||||
if(pending > 0)
|
||||
{
|
||||
--pending;
|
||||
if(shutting_down && pending_wait && pending == 0)
|
||||
{
|
||||
pending_wait = false;
|
||||
complete_shutdown = true;
|
||||
process();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QJDns::Private::st_timeout()
|
||||
{
|
||||
doNextStep();
|
||||
}
|
||||
|
||||
void QJDns::Private::doNextStepSlot()
|
||||
{
|
||||
doNextStep();
|
||||
}
|
||||
|
||||
void QJDns::Private::doDebug()
|
||||
{
|
||||
if(new_debug_strings)
|
||||
{
|
||||
new_debug_strings = false;
|
||||
if(!debug_strings.isEmpty())
|
||||
emit q->debugLinesReady();
|
||||
}
|
||||
}
|
||||
|
||||
QJDns::QJDns(QObject *parent)
|
||||
:QObject(parent)
|
||||
{
|
||||
d = new Private(this);
|
||||
}
|
||||
|
||||
QJDns::~QJDns()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool QJDns::init(Mode mode, const QHostAddress &address)
|
||||
{
|
||||
return d->init(mode, address);
|
||||
}
|
||||
|
||||
void QJDns::shutdown()
|
||||
{
|
||||
d->shutting_down = true;
|
||||
d->pending_wait = false;
|
||||
d->complete_shutdown = false;
|
||||
jdns_shutdown(d->sess);
|
||||
d->process();
|
||||
}
|
||||
|
||||
QStringList QJDns::debugLines()
|
||||
{
|
||||
QStringList tmp = d->debug_strings;
|
||||
d->debug_strings.clear();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
QJDns::SystemInfo QJDns::systemInfo()
|
||||
{
|
||||
SystemInfo out;
|
||||
jdns_dnsparams_t *params = jdns_system_dnsparams();
|
||||
for(int n = 0; n < params->nameservers->count; ++n)
|
||||
{
|
||||
NameServer ns;
|
||||
ns.address = addr2qt(params->nameservers->item[n]->address);
|
||||
out.nameServers += ns;
|
||||
}
|
||||
for(int n = 0; n < params->domains->count; ++n)
|
||||
out.domains += str2qt(params->domains->item[n]);
|
||||
for(int n = 0; n < params->hosts->count; ++n)
|
||||
{
|
||||
DnsHost h;
|
||||
h.name = str2qt(params->hosts->item[n]->name);
|
||||
h.address = addr2qt(params->hosts->item[n]->address);
|
||||
out.hosts += h;
|
||||
}
|
||||
jdns_dnsparams_delete(params);
|
||||
return out;
|
||||
}
|
||||
|
||||
#define PROBE_BASE 20000
|
||||
#define PROBE_RANGE 100
|
||||
|
||||
QHostAddress QJDns::detectPrimaryMulticast(const QHostAddress &address)
|
||||
{
|
||||
my_srand();
|
||||
|
||||
QUdpSocket *sock = new QUdpSocket;
|
||||
QUdpSocket::BindMode mode;
|
||||
mode |= QUdpSocket::ShareAddress;
|
||||
mode |= QUdpSocket::ReuseAddressHint;
|
||||
int port = -1;
|
||||
for(int n = 0; n < PROBE_RANGE; ++n)
|
||||
{
|
||||
if(sock->bind(address, PROBE_BASE + n, mode))
|
||||
{
|
||||
port = PROBE_BASE + n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(port == -1)
|
||||
{
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
|
||||
jdns_address_t *a;
|
||||
if(address.protocol() == QAbstractSocket::IPv6Protocol)
|
||||
a = jdns_address_multicast6_new();
|
||||
else
|
||||
a = jdns_address_multicast4_new();
|
||||
QHostAddress maddr = addr2qt(a);
|
||||
jdns_address_delete(a);
|
||||
|
||||
if(address.protocol() == QAbstractSocket::IPv6Protocol)
|
||||
{
|
||||
int x;
|
||||
if(!qjdns_sock_setMulticast6(sock->socketDescriptor(), maddr.toIPv6Address().c, &x))
|
||||
{
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
qjdns_sock_setTTL6(sock->socketDescriptor(), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int x;
|
||||
if(!qjdns_sock_setMulticast4(sock->socketDescriptor(), maddr.toIPv4Address(), &x))
|
||||
{
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
qjdns_sock_setTTL4(sock->socketDescriptor(), 0);
|
||||
}
|
||||
|
||||
QHostAddress result;
|
||||
QByteArray out(128, 0);
|
||||
for(int n = 0; n < out.size(); ++n)
|
||||
out[n] = rand();
|
||||
if(sock->writeDatagram(out.data(), out.size(), maddr, port) == -1)
|
||||
{
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
if(!sock->waitForReadyRead(1000))
|
||||
{
|
||||
fprintf(stderr, "QJDns::detectPrimaryMulticast: timeout while checking %s\n", qPrintable(address.toString()));
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
QByteArray in(128, 0);
|
||||
QHostAddress from_addr;
|
||||
quint16 from_port;
|
||||
int ret = sock->readDatagram(in.data(), in.size(), &from_addr, &from_port);
|
||||
if(ret == -1)
|
||||
{
|
||||
delete sock;
|
||||
return QHostAddress();
|
||||
}
|
||||
|
||||
if(from_port != port)
|
||||
continue;
|
||||
in.resize(ret);
|
||||
if(in != out)
|
||||
continue;
|
||||
|
||||
result = from_addr;
|
||||
break;
|
||||
}
|
||||
delete sock;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void QJDns::setNameServers(const QList<NameServer> &list)
|
||||
{
|
||||
d->setNameServers(list);
|
||||
}
|
||||
|
||||
int QJDns::queryStart(const QByteArray &name, int type)
|
||||
{
|
||||
int id = jdns_query(d->sess, (const unsigned char *)name.data(), type);
|
||||
d->process();
|
||||
return id;
|
||||
}
|
||||
|
||||
void QJDns::queryCancel(int id)
|
||||
{
|
||||
jdns_cancel_query(d->sess, id);
|
||||
d->removeCancelled(id);
|
||||
d->process();
|
||||
}
|
||||
|
||||
int QJDns::publishStart(PublishMode m, const Record &record)
|
||||
{
|
||||
jdns_rr_t *rr = export_record(record);
|
||||
|
||||
int pubmode;
|
||||
if(m == QJDns::Unique)
|
||||
pubmode = JDNS_PUBLISH_UNIQUE;
|
||||
else
|
||||
pubmode = JDNS_PUBLISH_SHARED;
|
||||
|
||||
int id = jdns_publish(d->sess, pubmode, rr);
|
||||
jdns_rr_delete(rr);
|
||||
d->process();
|
||||
return id;
|
||||
}
|
||||
|
||||
void QJDns::publishUpdate(int id, const Record &record)
|
||||
{
|
||||
jdns_rr_t *rr = export_record(record);
|
||||
|
||||
jdns_update_publish(d->sess, id, rr);
|
||||
jdns_rr_delete(rr);
|
||||
d->process();
|
||||
}
|
||||
|
||||
void QJDns::publishCancel(int id)
|
||||
{
|
||||
jdns_cancel_publish(d->sess, id);
|
||||
d->removeCancelled(id);
|
||||
d->process();
|
||||
}
|
||||
|
||||
//#include "qjdns.moc"
|
17
thirdparty/jdns/jdns_export.h
vendored
Normal file
17
thirdparty/jdns/jdns_export.h
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef JDNS_EXPORT_H
|
||||
#define JDNS_EXPORT_H
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#ifdef Q_WS_WIN
|
||||
# if defined(MAKE_JDNS_LIB)
|
||||
# define JDNS_EXPORT Q_DECL_EXPORT
|
||||
# else
|
||||
# define JDNS_EXPORT Q_DECL_IMPORT
|
||||
# endif
|
||||
#else
|
||||
# define JDNS_EXPORT Q_DECL_EXPORT
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
1199
thirdparty/jdns/jdnsshared/jdnsshared.cpp
vendored
Normal file
1199
thirdparty/jdns/jdnsshared/jdnsshared.cpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
511
thirdparty/jdns/jdnsshared/jdnsshared.h
vendored
Normal file
511
thirdparty/jdns/jdnsshared/jdnsshared.h
vendored
Normal file
@@ -0,0 +1,511 @@
|
||||
/*
|
||||
* Copyright (C) 2006,2007 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef JDNSSHARED_H
|
||||
#define JDNSSHARED_H
|
||||
|
||||
#include "qjdns.h"
|
||||
|
||||
class JDnsShared;
|
||||
class JDnsSharedPrivate;
|
||||
class JDnsSharedRequestPrivate;
|
||||
class JDnsSharedDebugPrivate;
|
||||
|
||||
/**
|
||||
\brief Collects debugging information from JDnsShared
|
||||
|
||||
\note Iris users should utilize NetNames for DNS capabilities, <i>not</i> JDnsSharedDebug. See the JDnsShared documentation for more information.
|
||||
|
||||
JDnsSharedDebug is used to collect debugging information from one or many JDnsShared objects. To use it, simply create it and pass it to JDnsShared::setDebug().
|
||||
|
||||
Example use:
|
||||
|
||||
\code
|
||||
JDnsSharedDebug *db = new JDnsSharedDebug;
|
||||
connect(db, SIGNAL(debugLinesReady(const QStringList &)),
|
||||
SLOT(db_debugLinesReady(const QStringList &)));
|
||||
|
||||
JDnsShared *jdnsShared1 = new JDnsShared(JDnsShared::UnicastInternet);
|
||||
jdnsShared1->setDebug(db, "U");
|
||||
|
||||
JDnsShared *jdnsShared2 = new JDnsShared(JDnsShared::UnicastLocal);
|
||||
jdnsShared2->setDebug(db, "L");
|
||||
...
|
||||
void db_debugLinesReady(const QStringList &lines)
|
||||
{
|
||||
foreach(QString line, lines)
|
||||
printf("%s\n", qPrintable(line));
|
||||
}
|
||||
\endcode
|
||||
|
||||
JDnsShared reports debug lines with the name and interface number prepended to each line. For example, if there is debug information to report about the second interface added to \a jdnsShared2 in the above example, the lines would be prepended with "L1: ".
|
||||
|
||||
Do not destroy JDnsSharedDebug until all of the JDnsShared objects associated with it have been destroyed.
|
||||
|
||||
\sa JDnsShared JDnsSharedRequest
|
||||
*/
|
||||
class JDnsSharedDebug : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
\brief Constructs a new object with the given \a parent
|
||||
*/
|
||||
JDnsSharedDebug(QObject *parent = 0);
|
||||
|
||||
/**
|
||||
\brief Destroys the object
|
||||
*/
|
||||
virtual ~JDnsSharedDebug();
|
||||
|
||||
/**
|
||||
\brief Read the available debug information
|
||||
|
||||
Debug information is reported as a series of lines. The lines are of reasonable length, and so if you're storing a backlog of the most recent debug information, it should be safe to make the cut-off point based on lines.
|
||||
|
||||
\sa readyRead
|
||||
*/
|
||||
QStringList readDebugLines();
|
||||
|
||||
signals:
|
||||
/**
|
||||
\brief Emitted when there is debug information to report
|
||||
|
||||
\sa readDebugLines
|
||||
*/
|
||||
void readyRead();
|
||||
|
||||
private:
|
||||
friend class JDnsShared;
|
||||
friend class JDnsSharedPrivate;
|
||||
friend class JDnsSharedDebugPrivate;
|
||||
JDnsSharedDebugPrivate *d;
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Performs a DNS operation using JDnsShared
|
||||
|
||||
\note Iris users should utilize NetNames for DNS capabilities, <i>not</i> JDnsSharedRequest. See the JDnsShared documentation for more information.
|
||||
|
||||
JDnsSharedRequest is used to perform DNS operations on a JDnsShared object. Many requests may be performed simultaneously, such that a single JDnsShared object can be "shared" across the application. Please see the JDnsShared documentation for more complete information about how the overall system works.
|
||||
|
||||
Call query() to perform a query. Call publish() (or publishUpdate()) to make DNS records available on the local network (JDnsShared::Multicast mode only). When the operation has something to report, the resultsReady() signal is emitted. Call success() to determine the status of the operation. If success() returns false, then the operation has failed and the reason for the failure can be determined with error(). If success() returns true, then the meaning differs depending on the type of operation being performed:
|
||||
<ul>
|
||||
<li>For JDnsShared::UnicastInternet and JDnsShared::UnicastLocal modes, call results() to obtain the records obtained by the query. In these modes, resultsReady() is only emitted once, at which point the operation is no longer active.</li>
|
||||
<li>For JDnsShared::Multicast, operations are long-lived. Query operations never timeout, and resultsReady() may be emitted multiple times. In order to stop the query, either call cancel() or destroy the JDnsSharedRequest object. Similarly, publishing is long-lived. The record stays published as long as the JDnsSharedRequest has not been cancelled or destroyed.</li>
|
||||
</ul>
|
||||
|
||||
Here is how you might look up an A record:
|
||||
|
||||
\code
|
||||
JDnsSharedRequest *req = new JDnsSharedRequest(jdnsShared);
|
||||
connect(req, SIGNAL(resultsReady()), SLOT(req_resultsReady()));
|
||||
req->query("psi-im.org", QJDns::A);
|
||||
...
|
||||
void req_resultsReady()
|
||||
{
|
||||
if(req->success())
|
||||
{
|
||||
// print all of the IP addresses obtained
|
||||
QList<QJDns::Record> results = req->results();
|
||||
foreach(QJDns::Record r, results)
|
||||
{
|
||||
if(r.type == QJDns::A)
|
||||
printf("%s\n", qPrintable(r.address.toString());
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("Error resolving!\n");
|
||||
}
|
||||
\endcode
|
||||
|
||||
Here is an example of publishing a record:
|
||||
|
||||
\code
|
||||
JDnsSharedRequest *pub = new JDnsSharedRequest(jdnsShared);
|
||||
connect(pub, SIGNAL(resultsReady()), SLOT(pub_resultsReady()));
|
||||
|
||||
// let's publish an A record
|
||||
QJDns::Record rec;
|
||||
rec.owner = "SomeComputer.local.";
|
||||
rec.type = QJDns::A;
|
||||
rec.ttl = 120;
|
||||
rec.haveKnown = true;
|
||||
rec.address = QHostAddress("192.168.0.32");
|
||||
|
||||
pub->publish(QJDns::Unique, rec);
|
||||
...
|
||||
void pub_resultsReady()
|
||||
{
|
||||
if(pub->success())
|
||||
printf("Record published\n");
|
||||
else
|
||||
printf("Error publishing!\n");
|
||||
}
|
||||
\endcode
|
||||
|
||||
To update an existing record, use publishUpdate():
|
||||
|
||||
\code
|
||||
// the IP address of the host changed, so make a new record
|
||||
QJDns::Record rec;
|
||||
rec.owner = "SomeComputer.local.";
|
||||
rec.type = QJDns::A;
|
||||
rec.ttl = 120;
|
||||
rec.haveKnown = true;
|
||||
rec.address = QHostAddress("192.168.0.64");
|
||||
|
||||
// update it
|
||||
pub->publishUpdate(rec);
|
||||
\endcode
|
||||
|
||||
As a special exception, the address value can be left unspecified for A and Aaaa record types, which tells JDnsShared to substitute the address value with the address of whatever interfaces the record gets published on. This is the preferred way to publish the IP address of your own machine, and in fact it is the only way to do so if you have multiple interfaces, because there will likely be a different IP address value for each interface (the record resolves to a different answer depending on which interface a query comes from).
|
||||
|
||||
\code
|
||||
// let's publish our own A record
|
||||
QJDns::Record rec;
|
||||
rec.owner = "MyComputer.local.";
|
||||
rec.type = QJDns::A;
|
||||
rec.ttl = 120;
|
||||
rec.haveKnown = true;
|
||||
rec.address = QHostAddress();
|
||||
|
||||
pub->publish(QJDns::Unique, rec);
|
||||
\endcode
|
||||
|
||||
When you want to unpublish, call cancel() or destroy the JDnsSharedRequest.
|
||||
|
||||
\sa JDnsShared
|
||||
*/
|
||||
class JDnsSharedRequest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
\brief Operation type
|
||||
*/
|
||||
enum Type
|
||||
{
|
||||
Query, ///< Query operation, initiated by query()
|
||||
Publish ///< Publish operation, initiated by publish() or publishUpdate()
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Request error
|
||||
*/
|
||||
enum Error
|
||||
{
|
||||
ErrorNoNet, ///< There are no available network interfaces to operate on. This happens if JDnsShared::addInterface() was not called.
|
||||
ErrorGeneric, ///< Generic error during the operation.
|
||||
ErrorNXDomain, ///< The name looked up does not exist.
|
||||
ErrorTimeout, ///< The operation timed out.
|
||||
ErrorConflict ///< Attempt to publish an already published unique record.
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Constructs a new object with the given \a jdnsShared and \a parent
|
||||
*/
|
||||
JDnsSharedRequest(JDnsShared *jdnsShared, QObject *parent = 0);
|
||||
|
||||
/**
|
||||
\brief Destroys the object
|
||||
|
||||
If there is an active operation, it is cancelled.
|
||||
*/
|
||||
~JDnsSharedRequest();
|
||||
|
||||
/**
|
||||
\brief The type of operation being performed
|
||||
*/
|
||||
Type type();
|
||||
|
||||
/**
|
||||
\brief Perform a query operation
|
||||
*/
|
||||
void query(const QByteArray &name, int type);
|
||||
|
||||
/**
|
||||
\brief Perform a publish operation
|
||||
*/
|
||||
void publish(QJDns::PublishMode m, const QJDns::Record &record);
|
||||
|
||||
/**
|
||||
\brief Update a record that is currently published
|
||||
*/
|
||||
void publishUpdate(const QJDns::Record &record);
|
||||
|
||||
/**
|
||||
\brief Cancels the current operation
|
||||
*/
|
||||
void cancel();
|
||||
|
||||
/**
|
||||
\brief Indicates whether or not the operation was successful
|
||||
*/
|
||||
bool success() const;
|
||||
|
||||
/**
|
||||
\brief Returns the reason for error
|
||||
*/
|
||||
Error error() const;
|
||||
|
||||
/**
|
||||
\brief Returns the results of the operation
|
||||
*/
|
||||
QList<QJDns::Record> results() const;
|
||||
|
||||
signals:
|
||||
/**
|
||||
\brief Indicates that the operation has something to report
|
||||
|
||||
After receiving this signal, call success() to check on the status of the operation, followed by results() or error() as appropriate.
|
||||
*/
|
||||
void resultsReady();
|
||||
|
||||
private:
|
||||
friend class JDnsShared;
|
||||
friend class JDnsSharedPrivate;
|
||||
friend class JDnsSharedRequestPrivate;
|
||||
JDnsSharedRequestPrivate *d;
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Abstraction layer on top of QJDns
|
||||
|
||||
\note Iris users should utilize NetNames for DNS capabilities, <i>not</i> JDnsShared. JDnsShared is provided for non-Iris users (and it is also used internally by NetNames). To use JDnsShared by itself, simply drop the jdnsshared.h and jdnsshared.cpp files, along with JDNS, into your project. It is not a full replacement for Qt's Q3Dns, as some tasks are left to you, but it covers most of it.
|
||||
|
||||
QJDns supports everything a typical application should ever need in DNS. However, it is expected that modern applications will need to maintain multiple QJDns instances at the same time, and this is where things can get complicated. For example, most applications will want at least two QJDns instances: one for IPv4 unicast and one for IPv6 unicast.
|
||||
|
||||
A single JDnsShared object encapsulates multiple instances of QJDns that are related. For example, an IPv4 unicast instance and an IPv6 unicast instance could be coupled within JDnsShared. Then, when a unicast operation is performed on the JDnsShared object, both underlying instances will be queried as appropriate. The application would not need to perform two resolutions itself, nor deal with any related complexity.
|
||||
|
||||
Further, individual operations are performed using a separate class called JDnsSharedRequest, eliminating the need for the application to directly interface with a central QJDns object or track integer handles. This makes it easier for individual parts of the application to "share" the same instance (or set of instances) of QJDns, hence the name.
|
||||
|
||||
JDnsShared is a thin abstraction. QJDns subtypes (e.g. QJDns::Type, QJDns::Record, etc) are still used with JDnsShared. Because of the duplication of documentation effort between NetNames and QJDns, there is no formal documentation for QJDns. Users of JDnsShared will need to read qjdns.h, although a basic explanation of the elements can be found below.
|
||||
|
||||
Types:
|
||||
<table>
|
||||
<tr><td>QJDns::Type</td><td>This is a convenience enumeration for common DNS record types. For example: A, Aaaa, Srv, etc. The values directly map to the integer values of the DNS protocol (e.g. Srv = 33). See qjdns.h for all of the types and values.</td></tr>
|
||||
<tr><td>QJDns::Record</td><td>This class holds a DNS record. The main fields are <i>type</i> (integer type, probably something listed in QJDns::Type), <i>rdata</i> (QByteArray of the record value), and <i>haveKnown</i> (boolean to indicate if a decoded form of the record value is also available). See qjdns.h for the possible known fields. You will most-likely always work with known types. Received records that have a type listed in QJDns::Type are guaranteed to be known and will provide a decoded value. If you are creating a record for publishing, you will need to set <i>owner</i>, <i>ttl</i>, and <i>type</i>. If the type to be published is listed in QJDns::Type, then you will need to set <i>haveKnown</i> to true and set the known fields as appropriate, otherwise you need to set <i>rdata</i>. You do not need to supply an encoded form in <i>rdata</i> for known types, it can be left empty in that case.</td></tr>
|
||||
<tr><td>QJDns::PublishMode</td><td>This is for Multicast DNS, and can either be Unique or Shared. A shared record can be published by multiple owners (for example, a "_ssh._tcp.local." PTR record might resolve to many different SSH services owned by different machines). A unique record can only have one owner (for example, a "mycomputer.local." A record would resolve to the IP address of the machine that published it). Attempting to publish a record on a network where a unique record is already present will result in a conflict error.</td></tr>
|
||||
</table>
|
||||
|
||||
Functions:
|
||||
<table>
|
||||
<tr><td>QJDns::detectPrimaryMulticast()</td><td>Detects a multicast interface. Pass QHostAddress::Any or QHostAddress::AnyIPv6, depending on which type of interface is desired.</td></tr>
|
||||
</table>
|
||||
|
||||
To use JDnsShared, first create an instance of it, set it up by calling addInterface() as necessary, and then use JDnsSharedRequest to perform operations on it.
|
||||
|
||||
Here is an example of how to create and set up a JDnsShared object for typical DNS resolution:
|
||||
|
||||
\code
|
||||
// construct
|
||||
JDnsShared *dns = new JDnsShared(JDnsShared::UnicastInternet);
|
||||
|
||||
// add IPv4 and IPv6 interfaces
|
||||
dns->addInterface(QHostAddress::Any);
|
||||
dns->addInterface(QHostAddress::AnyIPv6);
|
||||
|
||||
// at this point, the object is ready for operation
|
||||
\endcode
|
||||
|
||||
Perform a resolution like this:
|
||||
|
||||
\code
|
||||
JDnsSharedRequest *req = new JDnsSharedRequest(dns);
|
||||
connect(req, SIGNAL(resultsReady()), SLOT(req_resultsReady()));
|
||||
req->query("psi-im.org", QJDns::A);
|
||||
...
|
||||
void req_resultsReady()
|
||||
{
|
||||
if(req->success())
|
||||
{
|
||||
// print all of the IP addresses obtained
|
||||
QList<QJDns::Record> results = req->results();
|
||||
foreach(QJDns::Record r, results)
|
||||
{
|
||||
if(r.type == QJDns::A)
|
||||
printf("%s\n", qPrintable(r.address.toString());
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("Error resolving!\n");
|
||||
}
|
||||
\endcode
|
||||
|
||||
It is important to filter the results as shown in the above example. QJDns guarantees at least one record in the results will be of the type queried for, but there may also be CNAME records present (of course, if the query was for a CNAME type, then the results will only be CNAME records). The recommended approach is to simply filter for the record types desired, as shown, rather than single out CNAME specifically.
|
||||
|
||||
When you are finished with a JDnsShared object, it should be shut down before deleting:
|
||||
|
||||
\code
|
||||
connect(dns, SIGNAL(shutdownFinished()), SLOT(dns_shutdownFinished()));
|
||||
dns->shutdown();
|
||||
...
|
||||
void dns_shutdownFinished()
|
||||
{
|
||||
delete dns;
|
||||
}
|
||||
\endcode
|
||||
|
||||
Setting up JDnsShared for UnicastLocal and Multicast mode is done the same way as with UnicastInternet.
|
||||
|
||||
For example, here is how Multicast mode could be set up:
|
||||
|
||||
\code
|
||||
// construct
|
||||
JDnsShared *dns = new JDnsShared(JDnsShared::Multicast);
|
||||
|
||||
// add IPv4 interface
|
||||
QHostAddress addr = QJDns::detectPrimaryMulticast(QHostAddress::Any);
|
||||
dns->addInterface(addr);
|
||||
|
||||
// at this point, the object is ready for operation
|
||||
\endcode
|
||||
|
||||
JDnsShared provides a lot of functionality, but certain aspects of DNS are deemed out of its scope. Below are the responsibilities of the user of JDnsShared, if a more complete DNS behavior is desired:
|
||||
<ul>
|
||||
<li>Querying for several "qualified" names. You should first query for the name as provided, and if that fails then query for name + ".domain" (for every domain the computer is in). See domains().</li>
|
||||
<li>Detecting for ".local" in the name to be queried, and using that to decide whether to query via Multicast/UnicastLocal or UnicastInternet.</li>
|
||||
<li>For zeroconf/Bonjour, keep in mind that JDnsShared only provides low-level record queries. DNS-SD and any higher layers would be your job.</li>
|
||||
</ul>
|
||||
|
||||
Using a custom DNS implementation, such as JDnsShared, has the drawback that it is difficult to take advantage of platform-specific features (for example, an OS-wide DNS cache or LDAP integration). An application strategy for normal DNS should probably be:
|
||||
<ul>
|
||||
<li>If an A or AAAA record is desired, use a native lookup.</li>
|
||||
<li>Else, if the platform has advanced DNS features already (ie, res_query), use those.</li>
|
||||
<li>Else, use JDnsShared.</li>
|
||||
</ul>
|
||||
|
||||
For Multicast DNS, awareness of the platform is doubly important. There should only be one Multicast DNS "Responder" per computer, and using JDnsShared in Multicast mode at the same time could result in a conflict. An application strategy for Multicast DNS should be:
|
||||
<ul>
|
||||
<li>If the platform has a Multicast DNS daemon installed already, use it somehow.</li>
|
||||
<li>Else, use JDnsShared.</li>
|
||||
</ul>
|
||||
|
||||
\sa JDnsSharedRequest
|
||||
*/
|
||||
class JDnsShared : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
\brief The mode to operate in
|
||||
*/
|
||||
enum Mode
|
||||
{
|
||||
/**
|
||||
For regular DNS resolution. In this mode, lookups are performed on all interfaces, and the first returned result is used.
|
||||
*/
|
||||
UnicastInternet,
|
||||
|
||||
/**
|
||||
Perform regular DNS resolution using the Multicast DNS address. This is used to resolve large and/or known Multicast DNS names without actually multicasting anything.
|
||||
*/
|
||||
UnicastLocal,
|
||||
|
||||
/**
|
||||
Multicast DNS querying and publishing.
|
||||
|
||||
\note For Multicast mode, JDnsShared supports up to one interface for each IP version (e.g. one IPv4 interface and one IPv6 interface), and expects the default/primary multicast interface for that IP version to be used.
|
||||
*/
|
||||
Multicast
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Constructs a new object with the given \a mode and \a parent
|
||||
*/
|
||||
JDnsShared(Mode mode, QObject *parent = 0);
|
||||
|
||||
/**
|
||||
\brief Destroys the object
|
||||
*/
|
||||
~JDnsShared();
|
||||
|
||||
/**
|
||||
\brief Sets the debug object to report to
|
||||
|
||||
If a debug object is set using this function, then JDnsShared will send output text to it, prefixing each line with \a name.
|
||||
*/
|
||||
void setDebug(JDnsSharedDebug *db, const QString &name);
|
||||
|
||||
/**
|
||||
\brief Adds an interface to operate on
|
||||
|
||||
For UnicastInternet and UnicastLocal, these will almost always be QHostAddress::Any or QHostAddress::AnyIPv6 (operate on the default interface for IPv4 or IPv6, respectively).
|
||||
|
||||
For Multicast, it is expected that the default/primary multicast interface will be used here. Do not pass QHostAddress::Any (or AnyIPv6) with Multicast mode.
|
||||
|
||||
Returns true if the interface was successfully added, otherwise returns false.
|
||||
*/
|
||||
bool addInterface(const QHostAddress &addr);
|
||||
|
||||
/**
|
||||
\brief Removes a previously-added interface
|
||||
*/
|
||||
void removeInterface(const QHostAddress &addr);
|
||||
|
||||
/**
|
||||
\brief Shuts down the object
|
||||
|
||||
This operation primarily exists for Multicast mode, so that any published records have a chance to be unpublished. If the JDnsShared object is simply deleted without performing a shutdown, then published records will linger on the network until their TTLs expire.
|
||||
|
||||
When shutdown is complete, the shutdownFinished() signal will be emitted.
|
||||
*/
|
||||
void shutdown();
|
||||
|
||||
/**
|
||||
\brief The domains to search in
|
||||
|
||||
You should perform a separate resolution for every domain configured on this machine.
|
||||
*/
|
||||
static QList<QByteArray> domains();
|
||||
|
||||
/**
|
||||
\brief Performs a blocking shutdown of many JDnsShared instances
|
||||
|
||||
This function is a convenient way to shutdown multiple JDnsShared instances synchronously. The internal shutdown procedure uses no more than a few cycles of the eventloop, so it should be safe to call without worry of the application being overly stalled. This function takes ownership of the instances passed to it, and will delete them upon completion.
|
||||
|
||||
It is worth noting that this function is implemented without the use of a nested eventloop. All of the JDnsShared instances are moved into a temporary thread to perform the shutdown procedure, which should not cause any unexpected behavior in the current thread.
|
||||
|
||||
\code
|
||||
QList<JDnsShared*> list;
|
||||
list += jdnsShared_unicast;
|
||||
list += jdnsShared_multicast;
|
||||
JDnsShared::waitForShutdown(list);
|
||||
|
||||
// collect remaining debug information
|
||||
QStringList finalDebugLines = jdnsSharedDebug.readDebugLines();
|
||||
\endcode
|
||||
*/
|
||||
static void waitForShutdown(const QList<JDnsShared*> &instances);
|
||||
|
||||
signals:
|
||||
/**
|
||||
\brief Indicates the object has been shut down
|
||||
*/
|
||||
void shutdownFinished();
|
||||
|
||||
private:
|
||||
friend class JDnsSharedRequest;
|
||||
friend class JDnsSharedPrivate;
|
||||
JDnsSharedPrivate *d;
|
||||
};
|
||||
|
||||
#endif
|
322
thirdparty/jdns/jdnsshared/jdnsshared_helpers.h
vendored
Normal file
322
thirdparty/jdns/jdnsshared/jdnsshared_helpers.h
vendored
Normal file
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
* Copyright (C) 2006-2008 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
// Note: JDnsShared supports multiple interfaces for multicast, but only one
|
||||
// for IPv4 and one for IPv6. Sharing multiple interfaces of the same IP
|
||||
// version for multicast is unfortunately not possible without reworking
|
||||
// the jdns subsystem.
|
||||
//
|
||||
// The reason for this limitation is that in order to do multi-interface
|
||||
// multicast, you have to do a single bind to Any, and then use special
|
||||
// functions to determine which interface a packet came from and to
|
||||
// specify which interface a packet should go out on. Again this is just
|
||||
// not possible with the current system and the assumptions made by jdns.
|
||||
|
||||
// Note: When quering against multiple interfaces with multicast, it is
|
||||
// possible that different answers for a unique record may be reported
|
||||
// on each interface. We don't do anything about this.
|
||||
|
||||
#include "jdnsshared.h"
|
||||
#include "jdns_export.h"
|
||||
|
||||
// safeobj stuff, from qca
|
||||
|
||||
class JDNS_EXPORT JDnsSharedSafeTimer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
void JDnsSharedReleaseAndDeleteLater(QObject *owner, QObject *obj)
|
||||
{
|
||||
obj->disconnect(owner);
|
||||
obj->setParent(0);
|
||||
obj->deleteLater();
|
||||
}
|
||||
|
||||
JDnsSharedSafeTimer(QObject *parent = 0) :
|
||||
QObject(parent)
|
||||
{
|
||||
t = new QTimer(this);
|
||||
connect(t, SIGNAL(timeout()), SIGNAL(timeout()));
|
||||
}
|
||||
|
||||
~JDnsSharedSafeTimer()
|
||||
{
|
||||
JDnsSharedReleaseAndDeleteLater(this, t);
|
||||
}
|
||||
|
||||
int interval() const { return t->interval(); }
|
||||
bool isActive() const { return t->isActive(); }
|
||||
bool isSingleShot() const { return t->isSingleShot(); }
|
||||
void setInterval(int msec) { t->setInterval(msec); }
|
||||
void setSingleShot(bool singleShot) { t->setSingleShot(singleShot); }
|
||||
int timerId() const { return t->timerId(); }
|
||||
|
||||
public slots:
|
||||
void start(int msec) { t->start(msec); }
|
||||
void start() { t->start(); }
|
||||
void stop() { t->stop(); }
|
||||
|
||||
signals:
|
||||
void timeout();
|
||||
|
||||
private:
|
||||
QTimer *t;
|
||||
};
|
||||
|
||||
// for caching system info
|
||||
|
||||
class SystemInfoCache
|
||||
{
|
||||
public:
|
||||
QJDns::SystemInfo info;
|
||||
QTime time;
|
||||
};
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Handle
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
// QJDns uses integer handle ids, but they are only unique within
|
||||
// the relevant QJDns instance. Since we want our handles to be
|
||||
// unique across all instances, we'll make an instance/id pair.
|
||||
class Handle
|
||||
{
|
||||
public:
|
||||
QJDns *jdns;
|
||||
int id;
|
||||
|
||||
Handle() : jdns(0), id(-1)
|
||||
{
|
||||
}
|
||||
|
||||
Handle(QJDns *_jdns, int _id) : jdns(_jdns), id(_id)
|
||||
{
|
||||
}
|
||||
|
||||
~Handle() {}
|
||||
|
||||
bool operator==(const Handle &a) const
|
||||
{
|
||||
if(a.jdns == jdns && a.id == id)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator!=(const Handle &a) const
|
||||
{
|
||||
return !(operator==(a));
|
||||
}
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// JDnsShutdown
|
||||
//----------------------------------------------------------------------------
|
||||
class JDnsShutdownAgent : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
JDnsShutdownAgent();
|
||||
~JDnsShutdownAgent() {}
|
||||
void start();
|
||||
|
||||
signals:
|
||||
void started();
|
||||
};
|
||||
|
||||
|
||||
class JDnsShutdownWorker : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QList<JDnsShared*> list;
|
||||
|
||||
JDnsShutdownWorker(const QList<JDnsShared*> &_list);
|
||||
virtual ~JDnsShutdownWorker() {}
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
|
||||
private slots:
|
||||
void jdns_shutdownFinished();
|
||||
};
|
||||
|
||||
class JDnsShutdown : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
QMutex m;
|
||||
QWaitCondition w;
|
||||
QList<JDnsShared*> list;
|
||||
JDnsShutdownAgent *agent;
|
||||
JDnsShutdownWorker *worker;
|
||||
int phase;
|
||||
|
||||
JDnsShutdown();
|
||||
~JDnsShutdown() {}
|
||||
void waitForShutdown(const QList<JDnsShared*> &_list);
|
||||
|
||||
protected:
|
||||
virtual void run();
|
||||
|
||||
private slots:
|
||||
void agent_started();
|
||||
|
||||
void worker_finished();
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// JDnsSharedDebug
|
||||
//----------------------------------------------------------------------------
|
||||
class JDnsSharedDebugPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
JDnsSharedDebug *q;
|
||||
QMutex m;
|
||||
QStringList lines;
|
||||
bool dirty;
|
||||
|
||||
JDnsSharedDebugPrivate(JDnsSharedDebug *_q);
|
||||
~JDnsSharedDebugPrivate() {}
|
||||
|
||||
void addDebug(const QString &name, const QStringList &_lines);
|
||||
|
||||
private slots:
|
||||
void doUpdate();
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// JDnsSharedRequest
|
||||
//----------------------------------------------------------------------------
|
||||
class JDnsSharedPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
class Instance
|
||||
{
|
||||
public:
|
||||
QJDns *jdns;
|
||||
QHostAddress addr;
|
||||
int index;
|
||||
|
||||
Instance() : jdns(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
enum PreprocessMode
|
||||
{
|
||||
None, // don't muck with anything
|
||||
FillInAddress, // for A/AAAA
|
||||
FillInPtrOwner6, // for PTR, IPv6
|
||||
FillInPtrOwner4, // for PTR, IPv4
|
||||
};
|
||||
|
||||
JDnsShared *q;
|
||||
JDnsShared::Mode mode;
|
||||
bool shutting_down;
|
||||
JDnsSharedDebug *db;
|
||||
QString dbname;
|
||||
|
||||
QList<Instance*> instances;
|
||||
QHash<QJDns*,Instance*> instanceForQJDns;
|
||||
|
||||
QSet<JDnsSharedRequest*> requests;
|
||||
QHash<Handle,JDnsSharedRequest*> requestForHandle;
|
||||
|
||||
JDnsSharedPrivate(JDnsShared *_q);
|
||||
|
||||
~JDnsSharedPrivate() {}
|
||||
|
||||
JDnsSharedRequest *findRequest(QJDns *jdns, int id) const;
|
||||
|
||||
void jdns_link(QJDns *jdns);
|
||||
|
||||
int getNewIndex() const;
|
||||
|
||||
void addDebug(int index, const QString &line);
|
||||
|
||||
void doDebug(QJDns *jdns, int index);
|
||||
|
||||
PreprocessMode determinePpMode(const QJDns::Record &in);
|
||||
|
||||
QJDns::Record manipulateRecord(const QJDns::Record &in, PreprocessMode ppmode, bool *modified = 0);
|
||||
|
||||
bool addInterface(const QHostAddress &addr);
|
||||
void removeInterface(const QHostAddress &addr);
|
||||
|
||||
void queryStart(JDnsSharedRequest *obj, const QByteArray &name, int qType);
|
||||
void queryCancel(JDnsSharedRequest *obj);
|
||||
void publishStart(JDnsSharedRequest *obj, QJDns::PublishMode m, const QJDns::Record &record);
|
||||
void publishUpdate(JDnsSharedRequest *obj, const QJDns::Record &record);
|
||||
void publishCancel(JDnsSharedRequest *obj);
|
||||
|
||||
public slots:
|
||||
void late_shutdown();
|
||||
|
||||
private slots:
|
||||
void jdns_resultsReady(int id, const QJDns::Response &results);
|
||||
void jdns_published(int id);
|
||||
void jdns_error(int id, QJDns::Error e);
|
||||
void jdns_shutdownFinished();
|
||||
void jdns_debugLinesReady();
|
||||
};
|
||||
|
||||
class JDnsSharedRequestPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
JDnsSharedRequest *q;
|
||||
JDnsSharedPrivate *jsp;
|
||||
|
||||
// current action
|
||||
JDnsSharedRequest::Type type;
|
||||
QByteArray name;
|
||||
int qType;
|
||||
QJDns::PublishMode pubmode;
|
||||
JDnsSharedPrivate::PreprocessMode ppmode;
|
||||
QJDns::Record pubrecord;
|
||||
|
||||
// a single request might have to perform multiple QJDns operations
|
||||
QList<Handle> handles;
|
||||
|
||||
// keep a list of handles that successfully publish
|
||||
QList<Handle> published;
|
||||
|
||||
// use to weed out dups for multicast
|
||||
QList<QJDns::Record> queryCache;
|
||||
|
||||
bool success;
|
||||
JDnsSharedRequest::Error error;
|
||||
QList<QJDns::Record> results;
|
||||
JDnsSharedSafeTimer lateTimer;
|
||||
|
||||
JDnsSharedRequestPrivate(JDnsSharedRequest *_q);
|
||||
~JDnsSharedRequestPrivate() {};
|
||||
|
||||
void resetSession();
|
||||
|
||||
private slots:
|
||||
void lateTimer_timeout();
|
||||
};
|
||||
|
1047
thirdparty/jdns/qjdns.cpp
vendored
1047
thirdparty/jdns/qjdns.cpp
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user