diff --git a/server/opennicdnsclient.cpp b/server/opennicdnsclient.cpp index 3a67aa2..fce30bc 100644 --- a/server/opennicdnsclient.cpp +++ b/server/opennicdnsclient.cpp @@ -32,7 +32,7 @@ OpenNICDnsClient::~OpenNICDnsClient() close(); for(int n=0; n < mQueries.count(); n++) { - dns_query* q = mQueries.takeAt(0); + OpenNICDnsQuery* q = mQueries.takeAt(0); delete q; } mQueries.clear(); @@ -41,9 +41,9 @@ OpenNICDnsClient::~OpenNICDnsClient() /** * @brief assemble a reply packet and emit it through the reply() sinal */ -void OpenNICDnsClient::doReply(dns_query* q, dns_error error) +void OpenNICDnsClient::doReply(OpenNICDnsQuery* q, OpenNICDnsQuery::DNSError error) { - q->error = error; + q->setError(error); reply(*q); } @@ -104,11 +104,11 @@ void OpenNICDnsClient::purge() QDateTime now = QDateTime::currentDateTime(); for(int n=0; n < mQueries.count(); n++ ) { - dns_query* q = mQueries.at(n); - if ( q->expire < now ) + OpenNICDnsQuery* q = mQueries.at(n); + if ( q->expireTime() < now ) { - q->addr.clear(); - doReply(q,DNS_TIMEOUT); + q->addr().clear(); + doReply(q,OpenNICDnsQuery::DNS_TIMEOUT); disposeQuery(q); } } @@ -117,7 +117,7 @@ void OpenNICDnsClient::purge() /** * @brief Append an active query to the list of active queries. */ -void OpenNICDnsClient::appendActiveQuery(dns_query* q) +void OpenNICDnsClient::appendActiveQuery(OpenNICDnsQuery* q) { mQueries.append(q); } @@ -125,7 +125,7 @@ void OpenNICDnsClient::appendActiveQuery(dns_query* q) /** * @brief Dispose a query. */ -void OpenNICDnsClient::disposeQuery(dns_query *q) +void OpenNICDnsClient::disposeQuery(OpenNICDnsQuery *q) { int n = mQueries.indexOf(q); if ( n >= 0 ) @@ -138,12 +138,12 @@ void OpenNICDnsClient::disposeQuery(dns_query *q) * @brief Match a tid to a pending query. * @return the query or NULL. */ -dns_query* OpenNICDnsClient::findActiveQuery(quint16 tid) +OpenNICDnsQuery* OpenNICDnsClient::findActiveQuery(quint16 tid) { for(int n=0; n < mQueries.count(); n++) { - dns_query* q = mQueries.at(n); - if (tid == q->tid) + OpenNICDnsQuery* q = mQueries.at(n); + if (tid == q->tid()) { return q; } @@ -188,14 +188,14 @@ void OpenNICDnsClient::fetch(const quint8 *pkt, const quint8 *s, int pktsiz, cha */ void OpenNICDnsClient::processDatagram(QByteArray& datagram) { - quint8* pkt = (quint8*)datagram.data(); - int len = datagram.length(); - header* pkt_hdr; - const quint8 *p, *e, *s; - dns_query* q; - quint16 type; - char name[1025]; - int found, stop, dlen, nlen; + quint8* pkt = (quint8*)datagram.data(); + int len = datagram.length(); + header* pkt_hdr; + const quint8 *p, *e, *s; + OpenNICDnsQuery* q; + quint16 type; + char name[1025]; + int found, stop, dlen, nlen; /* We sent 1 query. We want to see more that 1 answer. */ pkt_hdr = (header *) pkt; @@ -208,8 +208,8 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) /* Received 0 answers */ if (pkt_hdr->nanswers == 0) { - q->addr.clear(); - doReply(q, DNS_DOES_NOT_EXIST); + q->addr().clear(); + doReply(q, OpenNICDnsQuery::DNS_DOES_NOT_EXIST); return; } @@ -220,7 +220,7 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) #define NTOHS(p) (((p)[0] << 8) | (p)[1]) /* We sent query class 1, query type 1 */ - if (&p[5] > e || NTOHS(p + 1) != q->query_type) + if (&p[5] > e || NTOHS(p + 1) != q->queryType()) return; /* Go to the first answer section */ @@ -242,7 +242,7 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) /* CNAME answer. shift to the next section */ dlen = htons(((uint16_t *) p)[5]); p += 12 + dlen; - } else if (type == q->query_type) { + } else if (type == q->queryType()) { found = stop = 1; } else { stop = 1; @@ -256,17 +256,17 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) if (p + dlen <= e) { /* Call user */ - if (q->query_type == DNS_MX_RECORD) + if (q->queryType() == OpenNICDnsQuery::DNS_MX_RECORD) { fetch((quint8*)pkt_hdr, p + 2, len, name, sizeof(name) - 1); p = (const unsigned char *)name; dlen = strlen(name); QByteArray mx((const char*)p,dlen); - q->mxName.append(mx); - doReply(q, DNS_OK); + q->mxName().append(mx); + doReply(q, OpenNICDnsQuery::DNS_OK); disposeQuery(q); } - else if (q->query_type == DNS_A_RECORD) + else if (q->queryType() == OpenNICDnsQuery::DNS_A_RECORD) { QByteArray addr((const char*)p,dlen); if (dlen >= 4) @@ -276,19 +276,19 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) QString::number((quint8)addr[1])+"."+ QString::number((quint8)addr[2])+"."+ QString::number((quint8)addr[3]); - q->addr.setAddress(sAddr); - doReply(q, DNS_OK); + q->addr().setAddress(sAddr); + doReply(q, OpenNICDnsQuery::DNS_OK); disposeQuery(q); } else { - doReply(q,DNS_ERROR); + doReply(q,OpenNICDnsQuery::DNS_ERROR); disposeQuery(q); } } else { - doReply(q,DNS_ERROR); + doReply(q,OpenNICDnsQuery::DNS_ERROR); disposeQuery(q); } } @@ -303,7 +303,7 @@ void OpenNICDnsClient::processDatagram(QByteArray& datagram) * @param port the port address at the resolver * @return Resultes are emitted by the reply() signal. */ -void OpenNICDnsClient::lookup(QHostAddress resolverAddress, OpenNICDomainName name, dns_query_type type, quint16 port) +void OpenNICDnsClient::lookup(QHostAddress resolverAddress, OpenNICDomainName name, OpenNICDnsQuery::DNSQueryType type, quint16 port) { setResolver(resolverAddress); lookup(name,type,port); @@ -316,15 +316,15 @@ void OpenNICDnsClient::lookup(QHostAddress resolverAddress, OpenNICDomainName na * @param port the port address at the resolver * @return Results are emitted by the reply() signal. */ -void OpenNICDnsClient::lookup(OpenNICDomainName name, dns_query_type qtype, quint16 port) +void OpenNICDnsClient::lookup(OpenNICDomainName name, OpenNICDnsQuery::DNSQueryType qtype, quint16 port) { - dns_query* q; + OpenNICDnsQuery* q; if ( !isOpen() ) { open(); } - if ( isOpen() && (q = new dns_query) != NULL ) + if ( isOpen() && (q = new OpenNICDnsQuery()) != NULL ) { QDateTime now = QDateTime::currentDateTime(); @@ -334,14 +334,14 @@ void OpenNICDnsClient::lookup(OpenNICDomainName name, dns_query_type qtype, quin const char* s; /* Init query structure */ - q->query_type = qtype; - q->tid = ++m_tid; - q->expire = now.addSecs(DNS_QUERY_TIMEOUT); - q->name = name; + q->setQueryType(qtype); + q->setTid(++m_tid); + q->setExpireTime(now.addSecs(DNS_QUERY_TIMEOUT)); + q->setName(name); /* Prepare DNS packet header */ pkt_hdr = (header*)pkt; - pkt_hdr->tid = q->tid; + pkt_hdr->tid = q->tid(); pkt_hdr->flags = htons(0x100); /* Haha. guess what it is */ pkt_hdr->nqueries = htons(1); /* Just one query */ pkt_hdr->nanswers = 0; @@ -382,7 +382,7 @@ void OpenNICDnsClient::lookup(OpenNICDomainName name, dns_query_type qtype, quin if ( !(p < pkt + sizeof(pkt)) ) { - doReply(q,DNS_ERROR); + doReply(q,OpenNICDnsQuery::DNS_ERROR); return; } n = p - pkt; /* Total packet length */ @@ -390,14 +390,14 @@ void OpenNICDnsClient::lookup(OpenNICDomainName name, dns_query_type qtype, quin QByteArray datagram(pkt,n); if ( mClientSocket->writeDatagram(datagram,resolverAddress(),port) < 0 ) { - doReply(q,DNS_ERROR); + doReply(q,OpenNICDnsQuery::DNS_ERROR); delete q; } appendActiveQuery(q); } else { - doReply(q,DNS_ERROR); + doReply(q,OpenNICDnsQuery::DNS_ERROR); delete q; return; } diff --git a/server/opennicdnsclient.h b/server/opennicdnsclient.h index 5e766cf..1ceafa3 100644 --- a/server/opennicdnsclient.h +++ b/server/opennicdnsclient.h @@ -21,6 +21,7 @@ #include #include "opennicdomainname.h" +#include "opennicdnsquery.h" #define DNS_QUERY_TIMEOUT 30 /* Query timeout, seconds */ #define DNS_MAX 1025 /* Maximum host name */ @@ -46,28 +47,16 @@ class OpenNICDnsClient : public QObject quint8 data[1]; /* Data, variable length */ } header; - typedef enum { - DNS_A_RECORD = 0x01, /* Lookup IP adress for host */ - DNS_MX_RECORD = 0x0f /* Lookup MX for domain */ - } dns_query_type; - /* * User defined function that will be called when DNS reply arrives for * requested hostname. "struct dns_cb_data" is passed to the user callback, * which has an error indicator, resolved address, etc. */ - typedef enum { - DNS_OK, /* No error */ - DNS_DOES_NOT_EXIST, /* Error: adress does not exist */ - DNS_TIMEOUT, /* Lookup time expired */ - DNS_ERROR /* No memory or other error */ - } dns_error; - OpenNICDnsClient(bool active=true, QObject *parent = 0); virtual ~OpenNICDnsClient(); bool isActive() {return mActive;} - dns_query* find(void* context); + OpenNICDnsQuery* find(void* context); public slots: virtual void setActive(bool active) {mActive=active;} @@ -78,63 +67,27 @@ class OpenNICDnsClient : public QObject bool isOpen() {return mClientSocket != NULL; } bool open(); void close(); - virtual void lookup(QHostAddress resolverAddress, OpenNICDomainName name, dns_query_type qtype, quint16 port=DEFAULT_DNS_PORT); - virtual void lookup(OpenNICDomainName name, dns_query_type qtype, quint16 port=DEFAULT_DNS_PORT); - virtual void reply(dns_query& data) {} + virtual void lookup(QHostAddress resolverAddress, OpenNICDomainName name, OpenNICDnsQuery::DNSQueryType qtype, quint16 port=DEFAULT_DNS_PORT); + virtual void lookup(OpenNICDomainName name, OpenNICDnsQuery::DNSQueryType qtype, quint16 port=DEFAULT_DNS_PORT); + virtual void reply(OpenNICDnsQuery& data) {} private slots: void readPendingDatagrams(); private: - void appendActiveQuery(dns_query* q); - dns_query* findActiveQuery(quint16 tid); - void disposeQuery(dns_query* q); + void appendActiveQuery(OpenNICDnsQuery* q); + OpenNICDnsQuery* findActiveQuery(quint16 tid); + void disposeQuery(OpenNICDnsQuery* q); void fetch(const quint8 *pkt, const quint8 *s, int pktsiz, char *dst, int dstlen); - void doReply(dns_query* q, dns_error error); + void doReply(OpenNICDnsQuery* q, OpenNICDnsQuery::DNSError error); void processDatagram(QByteArray& datagram); bool mActive; quint16 m_tid; /* Latest tid used */ QHostAddress mResolverAddress; /* The resolver address */ QUdpSocket* mClientSocket; /* UDP socket used for queries */ - QList mQueries; /* In-flight queries */ + QList mQueries; /* In-flight queries */ }; -/* - * User query. Holds mapping from application-level ID to DNS transaction id, - * and user defined callback function. - */ -class dns_query { -public: - dns_query() - : latency(NULL) - , error(OpenNICDnsClient::DNS_OK) - , query_type(OpenNICDnsClient::DNS_A_RECORD) - , tid(0) - {} - dns_query(const dns_query& other) - { - latency = other.latency; - error = other.error; - query_type = other.query_type; - tid = other.tid; - start = other.start; - expire = other.expire; - name = other.name; - addr = other.addr; - mxName = other.mxName; - } - ~dns_query() {} - QDateTime end() {return start.addMSecs(latency);} - quint64 latency; /* latency in milliseconds */ - OpenNICDnsClient::dns_error error; /* Result code */ - OpenNICDnsClient::dns_query_type query_type; /* Query type */ - quint16 tid; /* UDP DNS transaction ID */ - QDateTime start; /* The start of the query */ - QDateTime expire; /* Time when this query expire */ - OpenNICDomainName name; /* Host name */ - QHostAddress addr; /* Host address */ - QString mxName; /* MX record host name. */ -}; #endif // OPENNICDNSCLIENT_H diff --git a/server/opennicdnsquery.cpp b/server/opennicdnsquery.cpp new file mode 100644 index 0000000..05fd2c5 --- /dev/null +++ b/server/opennicdnsquery.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2012 Mike Sharkey + * + * "THE BEER-WARE LICENSE" (Revision 42): + * Mike Sharkey wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. + */ +#include "opennicdnsquery.h" + +#define inherited QObject + +OpenNICDnsQuery::OpenNICDnsQuery(QObject *parent) +: inherited(parent) +, mError(DNS_OK) +, mQueryType(DNS_A_RECORD) +, mTid(0) +{ +} + +OpenNICDnsQuery::OpenNICDnsQuery(const OpenNICDnsQuery& other) +{ + copy(other); +} + +OpenNICDnsQuery::~OpenNICDnsQuery() +{ +} + +/** + * @brief copy from the other + */ +OpenNICDnsQuery& OpenNICDnsQuery::operator=(const OpenNICDnsQuery& other) +{ + return copy(other); +} + +/** + * @brief copy from the other + */ +OpenNICDnsQuery& OpenNICDnsQuery::copy(const OpenNICDnsQuery& other) +{ + mError = other.mError; + mQueryType = other.mQueryType; + mTid = other.mTid; + mStartTime = other.mStartTime; + mEndTime = other.mEndTime; + mExpireTime = other.mExpireTime; + mName = other.mName; + mAddr = other.mAddr; + mMxName = other.mMxName; + return *this; +} + +/** + * @brief calculate the latency + */ +quint64 OpenNICDnsQuery::latency() +{ + return mStartTime.msecsTo(mEndTime); +} + +/** + * @brief set the end time and emit the finished signal + */ +void OpenNICDnsQuery::setEndTime(QDateTime endTime) +{ + mEndTime = endTime; + emit finished(this); +} + + + + + + diff --git a/server/opennicdnsquery.h b/server/opennicdnsquery.h new file mode 100644 index 0000000..457996c --- /dev/null +++ b/server/opennicdnsquery.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2012 Mike Sharkey + * + * "THE BEER-WARE LICENSE" (Revision 42): + * Mike Sharkey wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. + */ +#ifndef OPENNICDNSQUERY_H +#define OPENNICDNSQUERY_H + +#include +#include +#include +#include +#include + +#include "opennicdomainname.h" + +/* + * User query. Holds mapping from application-level ID to DNS transaction id, + * and user defined callback function. + */ +class OpenNICDnsQuery : public QObject +{ + Q_OBJECT + public: + typedef enum { + DNS_A_RECORD = 0x01, /* Lookup IP adress for host */ + DNS_MX_RECORD = 0x0f /* Lookup MX for domain */ + } DNSQueryType; + + typedef enum { + DNS_OK, /* No error */ + DNS_DOES_NOT_EXIST, /* Error: adress does not exist */ + DNS_TIMEOUT, /* Lookup time expired */ + DNS_ERROR /* No memory or other error */ + } DNSError; + + typedef enum + { + Red=0, /* service is down */ + Yellow, /* service may be down */ + Green /* service is up */ + } ColourCode; + + OpenNICDnsQuery(QObject *parent = 0); + OpenNICDnsQuery(const OpenNICDnsQuery& other); + virtual ~OpenNICDnsQuery(); + OpenNICDnsQuery& operator=(const OpenNICDnsQuery& other); + OpenNICDnsQuery& copy(const OpenNICDnsQuery& other); + + quint64 latency(); + DNSError error() {return mError;} + DNSQueryType queryType() {return mQueryType;} + quint16 tid() {return mTid;} + QDateTime& startTime() {return mStartTime;} + QDateTime& endTime() {return mEndTime;} + QDateTime& expireTime() {return mExpireTime;} + OpenNICDomainName& name() {return mName;} + QHostAddress& addr() {return mAddr;} + QString& mxName() {return mMxName;} + + signals: + void finished(OpenNICDnsQuery* query); + + public slots: + void setError(DNSError& error) {mError = error;} + void setQueryType(DNSQueryType& queryType) {mQueryType = queryType;} + void setTid(quint16 tid) {mTid = tid;} + void setStartTime(QDateTime startTime) {mStartTime = startTime;} + void setEndTime(QDateTime endTime); + void setExpireTime(QDateTime expireTime) {mExpireTime = expireTime;} + void setName(OpenNICDomainName& name) {mName = name;} + void setAddr(QHostAddress& addr) {mAddr = addr;} + void setMxName(QString& mxName) {mMxName = mxName;} + + private: + DNSError mError; /* Result code */ + DNSQueryType mQueryType; /* Query type */ + quint16 mTid; /* UDP DNS transaction ID */ + QDateTime mStartTime; /* The start of the query */ + QDateTime mEndTime; /* The end of the query */ + QDateTime mExpireTime; /* Time when this query expire */ + OpenNICDomainName mName; /* Host name */ + QHostAddress mAddr; /* Host address */ + QString mMxName; /* MX record host name. */ +}; + +#endif // OPENNICDNSQUERY_H + + + + + diff --git a/server/opennicresolverpool.cpp b/server/opennicresolverpool.cpp index a9d22d8..cbee598 100644 --- a/server/opennicresolverpool.cpp +++ b/server/opennicresolverpool.cpp @@ -128,6 +128,14 @@ void OpenNICResolverPool::swap(int a,int b) mItems.replace(b,t); } +/** + * @brief score the items in the pool + */ +void OpenNICResolverPool::score() +{ + +} + /** * @brief opposite of sort() */ diff --git a/server/opennicresolverpool.h b/server/opennicresolverpool.h index 28b86a0..5ae8ce9 100644 --- a/server/opennicresolverpool.h +++ b/server/opennicresolverpool.h @@ -39,8 +39,11 @@ class OpenNICResolverPool : public QObject public slots: void setMaxHistoryDepth(int maxHistoryDepth); void setActive(bool active); + + void score(); void randomize(); void sort(); + void clear(); private: void swap(int a,int b); diff --git a/server/opennicresolverpoolitem.cpp b/server/opennicresolverpoolitem.cpp index dcc7ea7..7ed4791 100644 --- a/server/opennicresolverpoolitem.cpp +++ b/server/opennicresolverpoolitem.cpp @@ -16,6 +16,7 @@ OpenNICResolverPoolItem::OpenNICResolverPoolItem(QObject* parent) : inherited(true, parent) +, mScore(0.0) , mMaxHistoryDepth(MAX_HISTORY_DEPTH) { clear(); @@ -23,6 +24,7 @@ OpenNICResolverPoolItem::OpenNICResolverPoolItem(QObject* parent) OpenNICResolverPoolItem::OpenNICResolverPoolItem(bool active, QObject* parent) : inherited(active, parent) +, mScore(0.0) , mMaxHistoryDepth(MAX_HISTORY_DEPTH) { clear(); @@ -30,6 +32,7 @@ OpenNICResolverPoolItem::OpenNICResolverPoolItem(bool active, QObject* parent) OpenNICResolverPoolItem::OpenNICResolverPoolItem(QHostAddress hostAddress, QString kind, QObject* parent) : inherited(true, parent) +, mScore(0.0) , mMaxHistoryDepth(MAX_HISTORY_DEPTH) { clear(); @@ -39,6 +42,7 @@ OpenNICResolverPoolItem::OpenNICResolverPoolItem(QHostAddress hostAddress, QStri OpenNICResolverPoolItem::OpenNICResolverPoolItem(const OpenNICResolverPoolItem& other) : inherited(true, NULL) +, mScore(0.0) , mMaxHistoryDepth(MAX_HISTORY_DEPTH) { copy(other); @@ -157,10 +161,10 @@ QDateTime OpenNICResolverPoolItem::lastReply() QDateTime rc; for(int n=0; n < mHistory.count(); n++) { - dns_query query = mHistory.at(n); - if ( query.error == OpenNICDnsClient::DNS_DOES_NOT_EXIST || query.error == OpenNICDnsClient::DNS_OK ) + OpenNICDnsQuery query = mHistory.at(n); + if ( query.error() == OpenNICDnsQuery::DNS_DOES_NOT_EXIST || query.error() == OpenNICDnsQuery::DNS_OK ) { - rc = query.end(); + rc = query.endTime(); break; } } @@ -175,10 +179,10 @@ QDateTime OpenNICResolverPoolItem::lastTimeout() QDateTime rc; for(int n=0; n < mHistory.count(); n++) { - dns_query query = mHistory.at(n); - if ( query.error == OpenNICDnsClient::DNS_TIMEOUT ) + OpenNICDnsQuery query = mHistory.at(n); + if ( query.error() == OpenNICDnsQuery::DNS_TIMEOUT ) { - rc = query.end(); + rc = query.endTime(); break; } } @@ -193,22 +197,22 @@ QString OpenNICResolverPoolItem::lastFault() QString rc; if (mHistory.count()) { - dns_query query = mHistory.at(0); - if (query.error == OpenNICDnsClient::DNS_OK) + OpenNICDnsQuery query = mHistory.at(0); + if (query.error() == OpenNICDnsQuery::DNS_OK) { - rc="DNS_OK " + query.addr.toString() + " " + query.name.domainName()+" ("+query.name.dnsService()+")"; + rc="DNS_OK " + query.addr().toString() + " " + query.name().domainName()+" ("+query.name().dnsService()+")"; } - else if (query.error == OpenNICDnsClient::DNS_DOES_NOT_EXIST) + else if (query.error() == OpenNICDnsQuery::DNS_DOES_NOT_EXIST) { - rc="DNS_DOES_NOT_EXIST " + query.name.domainName()+" ("+query.name.dnsService()+")"; + rc="DNS_DOES_NOT_EXIST " + query.name().domainName()+" ("+query.name().dnsService()+")"; } - else if (query.error == OpenNICDnsClient::DNS_TIMEOUT) + else if (query.error() == OpenNICDnsQuery::DNS_TIMEOUT) { - rc="DNS_TIMEOUT " + query.name.domainName()+" ("+query.name.dnsService()+")"; + rc="DNS_TIMEOUT " + query.name().domainName()+" ("+query.name().dnsService()+")"; } - else if (query.error == OpenNICDnsClient::DNS_ERROR) + else if (query.error() == OpenNICDnsQuery::DNS_ERROR) { - rc="DNS_ERROR " + query.name.domainName()+" ("+query.name.dnsService()+")"; + rc="DNS_ERROR " + query.name().domainName()+" ("+query.name().dnsService()+")"; } } @@ -223,8 +227,8 @@ bool OpenNICResolverPoolItem::alive() int deadCount=0; for(int n=0; n < mHistory.count(); n++) { - dns_query query = mHistory.at(n); - if ( query.error == OpenNICDnsClient::DNS_TIMEOUT || query.error == OpenNICDnsClient::DNS_ERROR ) + OpenNICDnsQuery query = mHistory.at(n); + if ( query.error() == OpenNICDnsQuery::DNS_TIMEOUT || query.error() == OpenNICDnsQuery::DNS_ERROR ) { ++deadCount; } @@ -261,11 +265,12 @@ QString& OpenNICResolverPoolItem::toString() */ OpenNICResolverPoolItem& OpenNICResolverPoolItem::copy(const OpenNICResolverPoolItem& other) { + mScore = other.mScore; mHistory = other.mHistory; mMaxHistoryDepth = other.mMaxHistoryDepth; mHostAddress = other.mHostAddress; mKind = other.mKind; - mTestsinFlight = other.mTestsinFlight; + mTestsInFlight = other.mTestsInFlight; return *this; } @@ -278,7 +283,7 @@ void OpenNICResolverPoolItem::clear() mMaxHistoryDepth=MAX_HISTORY_DEPTH; mHostAddress.clear(); mKind.clear(); - mTestsinFlight=0; + mTestsInFlight=0; } /** @@ -310,8 +315,8 @@ int OpenNICResolverPoolItem::replyCount() int nHistory = mHistory.count(); for(int n=0; n < nHistory; n++) { - dns_query query = mHistory[n]; - if ( query.error == OpenNICDnsClient::DNS_OK || query.error == OpenNICDnsClient::DNS_DOES_NOT_EXIST ) + OpenNICDnsQuery query = mHistory[n]; + if ( query.error() == OpenNICDnsQuery::DNS_OK || query.error() == OpenNICDnsQuery::DNS_DOES_NOT_EXIST ) { ++count; } @@ -328,8 +333,8 @@ int OpenNICResolverPoolItem::timeoutCount() int nHistory = mHistory.count(); for(int n=0; n < nHistory; n++) { - dns_query query = mHistory[n]; - if ( query.error == OpenNICDnsClient::DNS_TIMEOUT ) + OpenNICDnsQuery query = mHistory[n]; + if ( query.error() == OpenNICDnsQuery::DNS_TIMEOUT ) { ++count; } @@ -345,7 +350,7 @@ int OpenNICResolverPoolItem::lastLatency() int latency=BIG_LATENCY; /* something very big if there are no samples */ if ( mHistory.count() > 0 ) { - latency = mHistory[0].latency; + latency = mHistory[0].latency(); } return latency; } @@ -361,35 +366,65 @@ double OpenNICResolverPoolItem::averageLatency() { for(int n=0; n < nHistory; n++) { - total += mHistory[n].latency; + total += mHistory[n].latency(); } return total/nHistory; } return BIG_LATENCY; } +/** + * @brief calculate the score + */ +double OpenNICResolverPoolItem::score() +{ + double rc=0.0; + /** FIXME calculate the pool item scores */ + return rc; +} /** - * @brief add a DNS query result to the history for this resolver. + * @brief prune history record back to the limit. */ -void OpenNICResolverPoolItem::addToHistory(dns_query& query) +void OpenNICResolverPoolItem::pruneHistory() { - mHistory.prepend(query); - while(mHistory.count()>maxHistoryDepth()) /* prune... */ + /* prune... */ + while(mHistory.count()>0 && mHistory.count()>maxHistoryDepth()) { mHistory.takeFirst(); } } /** - * @brief get here on dns callback data, the dns_query object contains all we need to know. + * @brief add a DNS query result to the history for this resolver. + */ +void OpenNICResolverPoolItem::addToHistory(OpenNICDnsQuery& query) +{ + mHistory.prepend(query); + pruneHistory(); +} + +/** + * @brief get here on dns callback data, the dns_query object contains all we need to know about what happened during the query + * @param query the data associated with the query as it passed through the test layer and dns resolver layer */ -void OpenNICResolverPoolItem::reply(dns_query& query) +void OpenNICResolverPoolItem::reply(OpenNICDnsQuery& query) { inherited::reply(query); /* let our inherited sniff it and time-stamp it */ - if (mTestsinFlight > 0) + if (mTestsInFlight > 0) { - --mTestsinFlight; + switch(query.error()) + { + case OpenNICDnsQuery::DNS_OK: + break; + case OpenNICDnsQuery::DNS_DOES_NOT_EXIST: + break; + case OpenNICDnsQuery::DNS_TIMEOUT: + break; + case OpenNICDnsQuery::DNS_ERROR: + break; + } + --mTestsInFlight; addToHistory(query); } } @@ -401,7 +436,7 @@ void OpenNICResolverPoolItem::test() { if ( isActive() ) { - ++mTestsinFlight; + ++mTestsInFlight; resolve(hostAddress(), OpenNICSystem::randomDomain()); } } diff --git a/server/opennicresolverpoolitem.h b/server/opennicresolverpoolitem.h index e75b5ca..31bad6e 100644 --- a/server/opennicresolverpoolitem.h +++ b/server/opennicresolverpoolitem.h @@ -14,6 +14,7 @@ #include #include +#include "opennicdnsquery.h" #include "opennicresolvertest.h" #define RESOLVER_SAMPLE_STORAGE_LIMIT 10 /* number of samples to remember for averaging */ @@ -22,7 +23,6 @@ class OpenNICResolverPoolItem : public OpenNICResolverTest { Q_OBJECT public: - OpenNICResolverPoolItem(QObject *parent = 0); OpenNICResolverPoolItem(bool active, QObject *parent = 0); OpenNICResolverPoolItem(QHostAddress hostAddress, QString kind="", QObject* parent=NULL); @@ -35,15 +35,18 @@ class OpenNICResolverPoolItem : public OpenNICResolverTest int replyCount(); int timeoutCount(); int lastLatency(); - double averageLatency(); + double averageLatency(); QDateTime lastReply(); QDateTime lastTimeout(); QString lastFault(); - int inFlightTests() {return mTestsinFlight;} + int inFlightTests() {return mTestsInFlight;} bool alive(); + double score(); + void setScore(double score); + QString& kind() {return mKind;} void setKind(QString kind) {mKind=kind;} @@ -55,28 +58,29 @@ class OpenNICResolverPoolItem : public OpenNICResolverTest bool operator>=(OpenNICResolverPoolItem &other); bool operator<=(OpenNICResolverPoolItem &other); - QList& history() {return mHistory;} + QList& history() {return mHistory;} int historyDepth() {return mHistory.count();} int maxHistoryDepth() {return mMaxHistoryDepth;} QString& toString(); protected: - virtual void addToHistory(dns_query& query); + virtual void pruneHistory(); + virtual void addToHistory(OpenNICDnsQuery& query); virtual void test(); - virtual void reply(dns_query& query); + virtual void reply(OpenNICDnsQuery& query); public slots: void setMaxHistoryDepth(int maxHistoryDepth); void clear(); private: - QList mHistory; /* maintain a recent history */ + double mScore; /* the score realtive to other resolvers in the pool */ int mMaxHistoryDepth; /* the maximum depth of history */ + QList mHistory; /* maintain a recent history */ QHostAddress mHostAddress; /* host address wrapper */ QString mKind; /* the kind of resolver */ - int mTestsinFlight; /* number of tests in flight */ + int mTestsInFlight; /* number of tests in flight */ QString mString; /* for returning a string reference toString() */ - }; diff --git a/server/opennicresolvertest.cpp b/server/opennicresolvertest.cpp index b05c278..3a11191 100644 --- a/server/opennicresolvertest.cpp +++ b/server/opennicresolvertest.cpp @@ -9,6 +9,9 @@ #include "opennicresolvertest.h" #include "opennicsystem.h" +#define RANDOM_INTERVAL_MIN 10 /* seconds */ +#define RANDOM_INTERVAL_MAX (10*60) /* seconds */ + #define inherited OpenNICDnsClient #define TIMER_RESOLUTION 5 /* seconds */ @@ -42,9 +45,11 @@ void OpenNICResolverTest::setInterval(int seconds) /** * get here on reply data or timeout */ -void OpenNICResolverTest::reply(dns_query& rdata) +void OpenNICResolverTest::reply(OpenNICDnsQuery& query) { - rdata.latency = rdata.start.msecsTo(QDateTime::currentDateTime()); + /* time stamp the query reply */ + QDateTime endTime = QDateTime::currentDateTime(); + query.setEndTime(endTime); } /** @@ -52,11 +57,12 @@ void OpenNICResolverTest::reply(dns_query& rdata) */ void OpenNICResolverTest::resolve(QHostAddress addr,OpenNICDomainName name,quint16 port) { - dns_query* q = new dns_query; - q->addr = addr; - q->name = name; - q->start = QDateTime::currentDateTime(); - lookup(addr,name,OpenNICDnsClient::DNS_A_RECORD,port); + QDateTime startTime = QDateTime::currentDateTime(); + OpenNICDnsQuery* q = new OpenNICDnsQuery; + q->setAddr(addr); + q->setName(name); + q->setStartTime(startTime); + lookup(addr,name,OpenNICDnsQuery::DNS_A_RECORD,port); } void OpenNICResolverTest::timerEvent(QTimerEvent *e) @@ -72,8 +78,7 @@ void OpenNICResolverTest::timerEvent(QTimerEvent *e) { test(); } - setInterval(OpenNICSystem::random(10,60*10)); /* beween 10 seconds to 10 minutes */ - //setInterval(OpenNICSystem::random(10,60*5)); /* beween 10 seconds to 15 minutes */ + setInterval(OpenNICSystem::random(RANDOM_INTERVAL_MIN,RANDOM_INTERVAL_MAX)); /* set next random interval */ mTimerCount=0; } } diff --git a/server/opennicresolvertest.h b/server/opennicresolvertest.h index dc8387a..b1f08cc 100644 --- a/server/opennicresolvertest.h +++ b/server/opennicresolvertest.h @@ -16,6 +16,7 @@ #include #include +#include "opennicdnsquery.h" #include "opennicdnsclient.h" #include "opennicdomainname.h" @@ -28,7 +29,7 @@ class OpenNICResolverTest : public OpenNICDnsClient OpenNICResolverTest(bool active=true, QObject *parent=0); virtual ~OpenNICResolverTest(); protected slots: - virtual void reply(dns_query& rdata); + virtual void reply(OpenNICDnsQuery& query); virtual void setInterval(int seconds); protected: virtual void purge() {} diff --git a/server/server.pro b/server/server.pro index d82eb3b..e0f6a68 100644 --- a/server/server.pro +++ b/server/server.pro @@ -23,7 +23,8 @@ SOURCES += main.cpp\ opennicresolvertest.cpp \ opennicdnsclient.cpp \ opennicdomainname.cpp \ - opennicdomainnamepool.cpp + opennicdomainnamepool.cpp \ + opennicdnsquery.cpp win32::SOURCES += qtservice_win.cpp @@ -43,7 +44,8 @@ HEADERS += opennicserver.h \ opennicresolvertest.h \ opennicdnsclient.h \ opennicdomainname.h \ - opennicdomainnamepool.h + opennicdomainnamepool.h \ + opennicdnsquery.h RESOURCES += \ opennicserver.qrc