qdnslookup.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
  4. ** Contact: http://www.qt-project.org/
  5. **
  6. ** This file is part of the QtNetwork module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** GNU Lesser General Public License Usage
  10. ** This file may be used under the terms of the GNU Lesser General Public
  11. ** License version 2.1 as published by the Free Software Foundation and
  12. ** appearing in the file LICENSE.LGPL included in the packaging of this
  13. ** file. Please review the following information to ensure the GNU Lesser
  14. ** General Public License version 2.1 requirements will be met:
  15. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  16. **
  17. ** In addition, as a special exception, Nokia gives you certain additional
  18. ** rights. These rights are described in the Nokia Qt LGPL Exception
  19. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  20. **
  21. ** GNU General Public License Usage
  22. ** Alternatively, this file may be used under the terms of the GNU General
  23. ** Public License version 3.0 as published by the Free Software Foundation
  24. ** and appearing in the file LICENSE.GPL included in the packaging of this
  25. ** file. Please review the following information to ensure the GNU General
  26. ** Public License version 3.0 requirements will be met:
  27. ** http://www.gnu.org/copyleft/gpl.html.
  28. **
  29. ** Other Usage
  30. ** Alternatively, this file may be used in accordance with the terms and
  31. ** conditions contained in a signed written agreement between you and Nokia.
  32. **
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qdnslookup.h"
  42. #include "qdnslookup_p.h"
  43. #include <QCoreApplication>
  44. #include <QDateTime>
  45. #include <QMetaType>
  46. #include <QThreadStorage>
  47. #include <QUrl>
  48. QT_BEGIN_NAMESPACE
  49. Q_GLOBAL_STATIC(QDnsLookupThreadPool, theDnsLookupThreadPool);
  50. Q_GLOBAL_STATIC(QThreadStorage<bool *>, theDnsLookupSeedStorage);
  51. static bool qt_qdnsmailexchangerecord_less_than(const QDnsMailExchangeRecord &r1, const QDnsMailExchangeRecord &r2)
  52. {
  53. // Lower numbers are more preferred than higher ones.
  54. return r1.preference() < r2.preference();
  55. }
  56. /*!
  57. Sorts a list of QDnsMailExchangeRecord objects according to RFC 5321.
  58. */
  59. static void qt_qdnsmailexchangerecord_sort(QList<QDnsMailExchangeRecord> &records)
  60. {
  61. // If we have no more than one result, we are done.
  62. if (records.size() <= 1)
  63. return;
  64. // Order the records by preference.
  65. qSort(records.begin(), records.end(), qt_qdnsmailexchangerecord_less_than);
  66. int i = 0;
  67. while (i < records.size()) {
  68. // Determine the slice of records with the current preference.
  69. QList<QDnsMailExchangeRecord> slice;
  70. const quint16 slicePreference = records[i].preference();
  71. for (int j = i; j < records.size(); ++j) {
  72. if (records[j].preference() != slicePreference)
  73. break;
  74. slice << records[j];
  75. }
  76. // Randomize the slice of records.
  77. while (!slice.isEmpty()) {
  78. const unsigned int pos = qrand() % slice.size();
  79. records[i++] = slice.takeAt(pos);
  80. }
  81. }
  82. }
  83. static bool qt_qdnsservicerecord_less_than(const QDnsServiceRecord &r1, const QDnsServiceRecord &r2)
  84. {
  85. // Order by priority, or if the priorities are equal,
  86. // put zero weight records first.
  87. return r1.priority() < r2.priority()
  88. || (r1.priority() == r2.priority()
  89. && r1.weight() == 0 && r2.weight() > 0);
  90. }
  91. /*!
  92. Sorts a list of QDnsServiceRecord objects according to RFC 2782.
  93. */
  94. static void qt_qdnsservicerecord_sort(QList<QDnsServiceRecord> &records)
  95. {
  96. // If we have no more than one result, we are done.
  97. if (records.size() <= 1)
  98. return;
  99. // Order the records by priority, and for records with an equal
  100. // priority, put records with a zero weight first.
  101. qSort(records.begin(), records.end(), qt_qdnsservicerecord_less_than);
  102. int i = 0;
  103. while (i < records.size()) {
  104. // Determine the slice of records with the current priority.
  105. QList<QDnsServiceRecord> slice;
  106. const quint16 slicePriority = records[i].priority();
  107. unsigned int sliceWeight = 0;
  108. for (int j = i; j < records.size(); ++j) {
  109. if (records[j].priority() != slicePriority)
  110. break;
  111. sliceWeight += records[j].weight();
  112. slice << records[j];
  113. }
  114. #ifdef QDNSLOOKUP_DEBUG
  115. qDebug("qt_qdnsservicerecord_sort() : priority %i (size: %i, total weight: %i)",
  116. slicePriority, slice.size(), sliceWeight);
  117. #endif
  118. // Order the slice of records.
  119. while (!slice.isEmpty()) {
  120. const unsigned int weightThreshold = qrand() % (sliceWeight + 1);
  121. unsigned int summedWeight = 0;
  122. for (int j = 0; j < slice.size(); ++j) {
  123. summedWeight += slice[j].weight();
  124. if (summedWeight >= weightThreshold) {
  125. #ifdef QDNSLOOKUP_DEBUG
  126. qDebug("qt_qdnsservicerecord_sort() : adding %s %i (weight: %i)",
  127. qPrintable(slice[j].target()), slice[j].port(),
  128. slice[j].weight());
  129. #endif
  130. // Adjust the slice weight and take the current record.
  131. sliceWeight -= slice[j].weight();
  132. records[i++] = slice.takeAt(j);
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /*!
  140. \class QDnsLookup
  141. \brief The QDnsLookup class represents a DNS lookup.
  142. \inmodule QtNetwork
  143. \ingroup network
  144. QDnsLookup uses the mechanisms provided by the operating system to perform
  145. DNS lookups. To perform a lookup you need to specify a \l name and \l type
  146. then invoke the \l{QDnsLookup::lookup()}{lookup()} slot. The
  147. \l{QDnsLookup::finished()}{finished()} signal will be emitted upon
  148. completion.
  149. For example, you can determine which servers an XMPP chat client should
  150. connect to for a given domain with:
  151. \snippet doc/src/snippets/code/src_network_kernel_qdnslookup.cpp 0
  152. Once the request finishes you can handle the results with:
  153. \snippet doc/src/snippets/code/src_network_kernel_qdnslookup.cpp 1
  154. \note If you simply want to find the IP address(es) associated with a host
  155. name, or the host name associated with an IP address you should use
  156. QHostInfo instead.
  157. */
  158. /*!
  159. \enum QDnsLookup::Error
  160. Indicates all possible error conditions found during the
  161. processing of the DNS lookup.
  162. \value NoError no error condition.
  163. \value ResolverError there was an error initializing the system's
  164. DNS resolver.
  165. \value OperationCancelledError the lookup was aborted using the abort()
  166. method.
  167. \value InvalidRequestError the requested DNS lookup was invalid.
  168. \value InvalidReplyError the reply returned by the server was invalid.
  169. \value ServerFailureError the server encountered an internal failure
  170. while processing the request (SERVFAIL).
  171. \value ServerRefusedError the server refused to process the request for
  172. security or policy reasons (REFUSED).
  173. \value NotFoundError the requested domain name does not exist
  174. (NXDOMAIN).
  175. */
  176. /*!
  177. \enum QDnsLookup::Type
  178. Indicates the type of DNS lookup that was performed.
  179. \value A IPv4 address records.
  180. \value AAAA IPv6 address records.
  181. \value ANY any records.
  182. \value CNAME canonical name records.
  183. \value MX mail exchange records.
  184. \value NS name server records.
  185. \value PTR pointer records.
  186. \value SRV service records.
  187. \value TXT text records.
  188. */
  189. /*!
  190. \fn void QDnsLookup::finished()
  191. This signal is emitted when the reply has finished processing.
  192. */
  193. /*!
  194. \fn void QDnsLookup::nameChanged(const QString &name)
  195. This signal is emitted when the lookup \l name changes.
  196. \a name is the new lookup name.
  197. */
  198. /*!
  199. \fn void QDnsLookup::typeChanged(Type type)
  200. This signal is emitted when the lookup \l type changes.
  201. \a type is the new lookup type.
  202. */
  203. /*!
  204. Constructs a QDnsLookup object and sets \a parent as the parent object.
  205. The \l type property will default to QDnsLookup::A.
  206. */
  207. QDnsLookup::QDnsLookup(QObject *parent)
  208. : QObject(parent)
  209. , d_ptr(new QDnsLookupPrivate(this))
  210. {
  211. qRegisterMetaType<QDnsLookupReply>();
  212. }
  213. /*!
  214. Constructs a QDnsLookup object for the given \a type and \a name and sets
  215. \a parent as the parent object.
  216. */
  217. QDnsLookup::QDnsLookup(Type type, const QString &name, QObject *parent)
  218. : QObject(parent)
  219. , d_ptr(new QDnsLookupPrivate(this))
  220. {
  221. Q_D(QDnsLookup);
  222. qRegisterMetaType<QDnsLookupReply>();
  223. d->name = name;
  224. d->type = type;
  225. }
  226. /*!
  227. Destroys the QDnsLookup object.
  228. It is safe to delete a QDnsLookup object even if it is not finished, you
  229. will simply never receive its results.
  230. */
  231. QDnsLookup::~QDnsLookup()
  232. {
  233. }
  234. /*!
  235. \property QDnsLookup::error
  236. \brief the type of error that occurred if the DNS lookup failed, or NoError.
  237. */
  238. QDnsLookup::Error QDnsLookup::error() const
  239. {
  240. return d_func()->reply.error;
  241. }
  242. /*!
  243. \property QDnsLookup::errorString
  244. \brief a human-readable description of the error if the DNS lookup failed.
  245. */
  246. QString QDnsLookup::errorString() const
  247. {
  248. return d_func()->reply.errorString;
  249. }
  250. /*!
  251. \property QDnsLookup::finished
  252. \brief whether the reply has finished or was aborted.
  253. */
  254. bool QDnsLookup::isFinished() const
  255. {
  256. return d_func()->isFinished;
  257. }
  258. /*!
  259. \property QDnsLookup::name
  260. \brief the name to lookup.
  261. \note The name will be encoded using IDNA, which means it's unsuitable for
  262. querying SRV records compatible with the DNS-SD specification.
  263. */
  264. QString QDnsLookup::name() const
  265. {
  266. return d_func()->name;
  267. }
  268. void QDnsLookup::setName(const QString &name)
  269. {
  270. Q_D(QDnsLookup);
  271. if (name != d->name) {
  272. d->name = name;
  273. emit nameChanged(name);
  274. }
  275. }
  276. /*!
  277. \property QDnsLookup::type
  278. \brief the type of DNS lookup.
  279. */
  280. QDnsLookup::Type QDnsLookup::type() const
  281. {
  282. return d_func()->type;
  283. }
  284. void QDnsLookup::setType(Type type)
  285. {
  286. Q_D(QDnsLookup);
  287. if (type != d->type) {
  288. d->type = type;
  289. emit typeChanged(type);
  290. }
  291. }
  292. /*!
  293. Returns the list of canonical name records associated with this lookup.
  294. */
  295. QList<QDnsDomainNameRecord> QDnsLookup::canonicalNameRecords() const
  296. {
  297. return d_func()->reply.canonicalNameRecords;
  298. }
  299. /*!
  300. Returns the list of host address records associated with this lookup.
  301. */
  302. QList<QDnsHostAddressRecord> QDnsLookup::hostAddressRecords() const
  303. {
  304. return d_func()->reply.hostAddressRecords;
  305. }
  306. /*!
  307. Returns the list of mail exchange records associated with this lookup.
  308. The records are sorted according to
  309. \l{http://www.rfc-editor.org/rfc/rfc5321.txt}{RFC 5321}, so if you use them
  310. to connect to servers, you should try them in the order they are listed.
  311. */
  312. QList<QDnsMailExchangeRecord> QDnsLookup::mailExchangeRecords() const
  313. {
  314. return d_func()->reply.mailExchangeRecords;
  315. }
  316. /*!
  317. Returns the list of name server records associated with this lookup.
  318. */
  319. QList<QDnsDomainNameRecord> QDnsLookup::nameServerRecords() const
  320. {
  321. return d_func()->reply.nameServerRecords;
  322. }
  323. /*!
  324. Returns the list of pointer records associated with this lookup.
  325. */
  326. QList<QDnsDomainNameRecord> QDnsLookup::pointerRecords() const
  327. {
  328. return d_func()->reply.pointerRecords;
  329. }
  330. /*!
  331. Returns the list of service records associated with this lookup.
  332. The records are sorted according to
  333. \l{http://www.rfc-editor.org/rfc/rfc2782.txt}{RFC 2782}, so if you use them
  334. to connect to servers, you should try them in the order they are listed.
  335. */
  336. QList<QDnsServiceRecord> QDnsLookup::serviceRecords() const
  337. {
  338. return d_func()->reply.serviceRecords;
  339. }
  340. /*!
  341. Returns the list of text records associated with this lookup.
  342. */
  343. QList<QDnsTextRecord> QDnsLookup::textRecords() const
  344. {
  345. return d_func()->reply.textRecords;
  346. }
  347. /*!
  348. Aborts the DNS lookup operation.
  349. If the lookup is already finished, does nothing.
  350. */
  351. void QDnsLookup::abort()
  352. {
  353. Q_D(QDnsLookup);
  354. if (d->runnable) {
  355. d->runnable = 0;
  356. d->reply = QDnsLookupReply();
  357. d->reply.error = QDnsLookup::OperationCancelledError;
  358. d->reply.errorString = tr("Operation cancelled");
  359. d->isFinished = true;
  360. emit finished();
  361. }
  362. }
  363. /*!
  364. Performs the DNS lookup.
  365. The \l{QDnsLookup::finished()}{finished()} signal is emitted upon completion.
  366. */
  367. void QDnsLookup::lookup()
  368. {
  369. Q_D(QDnsLookup);
  370. d->isFinished = false;
  371. d->reply = QDnsLookupReply();
  372. d->runnable = new QDnsLookupRunnable(d->type, QUrl::toAce(d->name));
  373. connect(d->runnable, SIGNAL(finished(QDnsLookupReply)),
  374. this, SLOT(_q_lookupFinished(QDnsLookupReply)),
  375. Qt::BlockingQueuedConnection);
  376. theDnsLookupThreadPool()->start(d->runnable);
  377. }
  378. /*!
  379. \class QDnsDomainNameRecord
  380. \brief The QDnsDomainNameRecord class stores information about a domain
  381. name record.
  382. \inmodule QtNetwork
  383. \ingroup network
  384. When performing a name server lookup, zero or more records will be returned.
  385. Each record is represented by a QDnsDomainNameRecord instance.
  386. \sa QDnsLookup
  387. */
  388. /*!
  389. Constructs an empty domain name record object.
  390. */
  391. QDnsDomainNameRecord::QDnsDomainNameRecord()
  392. : d(new QDnsDomainNameRecordPrivate)
  393. {
  394. }
  395. /*!
  396. Constructs a copy of \a other.
  397. */
  398. QDnsDomainNameRecord::QDnsDomainNameRecord(const QDnsDomainNameRecord &other)
  399. : d(other.d)
  400. {
  401. }
  402. /*!
  403. Destroys a domain name record.
  404. */
  405. QDnsDomainNameRecord::~QDnsDomainNameRecord()
  406. {
  407. }
  408. /*!
  409. Returns the name for this record.
  410. */
  411. QString QDnsDomainNameRecord::name() const
  412. {
  413. return d->name;
  414. }
  415. /*!
  416. Returns the duration in seconds for which this record is valid.
  417. */
  418. quint32 QDnsDomainNameRecord::timeToLive() const
  419. {
  420. return d->timeToLive;
  421. }
  422. /*!
  423. Returns the value for this domain name record.
  424. */
  425. QString QDnsDomainNameRecord::value() const
  426. {
  427. return d->value;
  428. }
  429. /*!
  430. Assigns the data of the \a other object to this record object,
  431. and returns a reference to it.
  432. */
  433. QDnsDomainNameRecord &QDnsDomainNameRecord::operator=(const QDnsDomainNameRecord &other)
  434. {
  435. d = other.d;
  436. return *this;
  437. }
  438. /*!
  439. \class QDnsHostAddressRecord
  440. \brief The QDnsHostAddressRecord class stores information about a host
  441. address record.
  442. \inmodule QtNetwork
  443. \ingroup network
  444. When performing an address lookup, zero or more records will be
  445. returned. Each record is represented by a QDnsHostAddressRecord instance.
  446. \sa QDnsLookup
  447. */
  448. /*!
  449. Constructs an empty host address record object.
  450. */
  451. QDnsHostAddressRecord::QDnsHostAddressRecord()
  452. : d(new QDnsHostAddressRecordPrivate)
  453. {
  454. }
  455. /*!
  456. Constructs a copy of \a other.
  457. */
  458. QDnsHostAddressRecord::QDnsHostAddressRecord(const QDnsHostAddressRecord &other)
  459. : d(other.d)
  460. {
  461. }
  462. /*!
  463. Destroys a host address record.
  464. */
  465. QDnsHostAddressRecord::~QDnsHostAddressRecord()
  466. {
  467. }
  468. /*!
  469. Returns the name for this record.
  470. */
  471. QString QDnsHostAddressRecord::name() const
  472. {
  473. return d->name;
  474. }
  475. /*!
  476. Returns the duration in seconds for which this record is valid.
  477. */
  478. quint32 QDnsHostAddressRecord::timeToLive() const
  479. {
  480. return d->timeToLive;
  481. }
  482. /*!
  483. Returns the value for this host address record.
  484. */
  485. QHostAddress QDnsHostAddressRecord::value() const
  486. {
  487. return d->value;
  488. }
  489. /*!
  490. Assigns the data of the \a other object to this record object,
  491. and returns a reference to it.
  492. */
  493. QDnsHostAddressRecord &QDnsHostAddressRecord::operator=(const QDnsHostAddressRecord &other)
  494. {
  495. d = other.d;
  496. return *this;
  497. }
  498. /*!
  499. \class QDnsMailExchangeRecord
  500. \brief The QDnsMailExchangeRecord class stores information about a DNS MX record.
  501. \inmodule QtNetwork
  502. \ingroup network
  503. When performing a lookup on a service, zero or more records will be
  504. returned. Each record is represented by a QDnsMailExchangeRecord instance.
  505. The meaning of the fields is defined in
  506. \l{http://www.rfc-editor.org/rfc/rfc1035.txt}{RFC 1035}.
  507. \sa QDnsLookup
  508. */
  509. /*!
  510. Constructs an empty mail exchange record object.
  511. */
  512. QDnsMailExchangeRecord::QDnsMailExchangeRecord()
  513. : d(new QDnsMailExchangeRecordPrivate)
  514. {
  515. }
  516. /*!
  517. Constructs a copy of \a other.
  518. */
  519. QDnsMailExchangeRecord::QDnsMailExchangeRecord(const QDnsMailExchangeRecord &other)
  520. : d(other.d)
  521. {
  522. }
  523. /*!
  524. Destroys a mail exchange record.
  525. */
  526. QDnsMailExchangeRecord::~QDnsMailExchangeRecord()
  527. {
  528. }
  529. /*!
  530. Returns the domain name of the mail exchange for this record.
  531. */
  532. QString QDnsMailExchangeRecord::exchange() const
  533. {
  534. return d->exchange;
  535. }
  536. /*!
  537. Returns the name for this record.
  538. */
  539. QString QDnsMailExchangeRecord::name() const
  540. {
  541. return d->name;
  542. }
  543. /*!
  544. Returns the preference for this record.
  545. */
  546. quint16 QDnsMailExchangeRecord::preference() const
  547. {
  548. return d->preference;
  549. }
  550. /*!
  551. Returns the duration in seconds for which this record is valid.
  552. */
  553. quint32 QDnsMailExchangeRecord::timeToLive() const
  554. {
  555. return d->timeToLive;
  556. }
  557. /*!
  558. Assigns the data of the \a other object to this record object,
  559. and returns a reference to it.
  560. */
  561. QDnsMailExchangeRecord &QDnsMailExchangeRecord::operator=(const QDnsMailExchangeRecord &other)
  562. {
  563. d = other.d;
  564. return *this;
  565. }
  566. /*!
  567. \class QDnsServiceRecord
  568. \brief The QDnsServiceRecord class stores information about a DNS SRV record.
  569. \inmodule QtNetwork
  570. \ingroup network
  571. When performing a lookup on a service, zero or more records will be
  572. returned. Each record is represented by a QDnsServiceRecord instance.
  573. The meaning of the fields is defined in
  574. \l{http://www.rfc-editor.org/rfc/rfc2782.txt}{RFC 2782}.
  575. \sa QDnsLookup
  576. */
  577. /*!
  578. Constructs an empty service record object.
  579. */
  580. QDnsServiceRecord::QDnsServiceRecord()
  581. : d(new QDnsServiceRecordPrivate)
  582. {
  583. }
  584. /*!
  585. Constructs a copy of \a other.
  586. */
  587. QDnsServiceRecord::QDnsServiceRecord(const QDnsServiceRecord &other)
  588. : d(other.d)
  589. {
  590. }
  591. /*!
  592. Destroys a service record.
  593. */
  594. QDnsServiceRecord::~QDnsServiceRecord()
  595. {
  596. }
  597. /*!
  598. Returns the name for this record.
  599. */
  600. QString QDnsServiceRecord::name() const
  601. {
  602. return d->name;
  603. }
  604. /*!
  605. Returns the port on the target host for this service record.
  606. */
  607. quint16 QDnsServiceRecord::port() const
  608. {
  609. return d->port;
  610. }
  611. /*!
  612. Returns the priority for this service record.
  613. A client must attempt to contact the target host with the lowest-numbered
  614. priority.
  615. */
  616. quint16 QDnsServiceRecord::priority() const
  617. {
  618. return d->priority;
  619. }
  620. /*!
  621. Returns the domain name of the target host for this service record.
  622. */
  623. QString QDnsServiceRecord::target() const
  624. {
  625. return d->target;
  626. }
  627. /*!
  628. Returns the duration in seconds for which this record is valid.
  629. */
  630. quint32 QDnsServiceRecord::timeToLive() const
  631. {
  632. return d->timeToLive;
  633. }
  634. /*!
  635. Returns the weight for this service record.
  636. The weight field specifies a relative weight for entries with the same
  637. priority. Entries with higher weights should be selected with a higher
  638. probability.
  639. */
  640. quint16 QDnsServiceRecord::weight() const
  641. {
  642. return d->weight;
  643. }
  644. /*!
  645. Assigns the data of the \a other object to this record object,
  646. and returns a reference to it.
  647. */
  648. QDnsServiceRecord &QDnsServiceRecord::operator=(const QDnsServiceRecord &other)
  649. {
  650. d = other.d;
  651. return *this;
  652. }
  653. /*!
  654. \class QDnsTextRecord
  655. \brief The QDnsTextRecord class stores information about a DNS TXT record.
  656. \inmodule QtNetwork
  657. \ingroup network
  658. When performing a text lookup, zero or more records will be
  659. returned. Each record is represented by a QDnsTextRecord instance.
  660. The meaning of the fields is defined in
  661. \l{http://www.rfc-editor.org/rfc/rfc1035.txt}{RFC 1035}.
  662. \sa QDnsLookup
  663. */
  664. /*!
  665. Constructs an empty text record object.
  666. */
  667. QDnsTextRecord::QDnsTextRecord()
  668. : d(new QDnsTextRecordPrivate)
  669. {
  670. }
  671. /*!
  672. Constructs a copy of \a other.
  673. */
  674. QDnsTextRecord::QDnsTextRecord(const QDnsTextRecord &other)
  675. : d(other.d)
  676. {
  677. }
  678. /*!
  679. Destroys a text record.
  680. */
  681. QDnsTextRecord::~QDnsTextRecord()
  682. {
  683. }
  684. /*!
  685. Returns the name for this text record.
  686. */
  687. QString QDnsTextRecord::name() const
  688. {
  689. return d->name;
  690. }
  691. /*!
  692. Returns the duration in seconds for which this record is valid.
  693. */
  694. quint32 QDnsTextRecord::timeToLive() const
  695. {
  696. return d->timeToLive;
  697. }
  698. /*!
  699. Returns the values for this text record.
  700. */
  701. QList<QByteArray> QDnsTextRecord::values() const
  702. {
  703. return d->values;
  704. }
  705. /*!
  706. Assigns the data of the \a other object to this record object,
  707. and returns a reference to it.
  708. */
  709. QDnsTextRecord &QDnsTextRecord::operator=(const QDnsTextRecord &other)
  710. {
  711. d = other.d;
  712. return *this;
  713. }
  714. void QDnsLookupPrivate::_q_lookupFinished(const QDnsLookupReply &_reply)
  715. {
  716. Q_Q(QDnsLookup);
  717. if (runnable == q->sender()) {
  718. #ifdef QDNSLOOKUP_DEBUG
  719. qDebug("DNS reply for %s: %i (%s)", qPrintable(name), _reply.error, qPrintable(_reply.errorString));
  720. #endif
  721. reply = _reply;
  722. runnable = 0;
  723. isFinished = true;
  724. emit q->finished();
  725. }
  726. }
  727. void QDnsLookupRunnable::run()
  728. {
  729. QDnsLookupReply reply;
  730. // Validate input.
  731. if (requestName.isEmpty()) {
  732. reply.error = QDnsLookup::InvalidRequestError;
  733. reply.errorString = tr("Invalid domain name");
  734. emit finished(reply);
  735. return;
  736. }
  737. // Perform request.
  738. query(requestType, requestName, &reply);
  739. // Sort results.
  740. if (!theDnsLookupSeedStorage()->hasLocalData()) {
  741. qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(this));
  742. theDnsLookupSeedStorage()->setLocalData(new bool(true));
  743. }
  744. qt_qdnsmailexchangerecord_sort(reply.mailExchangeRecords);
  745. qt_qdnsservicerecord_sort(reply.serviceRecords);
  746. emit finished(reply);
  747. }
  748. QDnsLookupThreadPool::QDnsLookupThreadPool()
  749. : signalsConnected(false)
  750. {
  751. // Run up to 5 lookups in parallel.
  752. setMaxThreadCount(5);
  753. }
  754. void QDnsLookupThreadPool::start(QRunnable *runnable)
  755. {
  756. // Ensure threads complete at application destruction.
  757. if (!signalsConnected) {
  758. QMutexLocker signalsLocker(&signalsMutex);
  759. if (!signalsConnected) {
  760. QCoreApplication *app = QCoreApplication::instance();
  761. if (!app) {
  762. qWarning("QDnsLookup requires a QCoreApplication");
  763. delete runnable;
  764. return;
  765. }
  766. moveToThread(app->thread());
  767. connect(app, SIGNAL(destroyed()),
  768. SLOT(_q_applicationDestroyed()), Qt::DirectConnection);
  769. signalsConnected = true;
  770. }
  771. }
  772. QThreadPool::start(runnable);
  773. }
  774. void QDnsLookupThreadPool::_q_applicationDestroyed()
  775. {
  776. waitForDone();
  777. signalsConnected = false;
  778. }
  779. QT_END_NAMESPACE