| 1 | |
| 2 | /* |
| 3 | * SPDX-FileName: QtDNSClient.cxx |
| 4 | * SPDX-FileComment: Qt wrapper around DNS client |
| 5 | * SPDX-FileCopyrightText: Copyright (C) 2025 James Turner |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include <QtDNSClient.hxx> |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <cassert> |
| 13 | #include <random> |
| 14 | |
| 15 | #include <simgear/debug/logstream.hxx> |
| 16 | #include <simgear/math/sg_random.hxx> |
| 17 | |
| 18 | QtDNSClient::QtDNSClient(QObject* pr, QString service) : QObject(pr), |
| 19 | m_serviceName(service) |
| 20 | { |
| 21 | // always use Google for this one: it's too early to configure |
| 22 | // and this should have a higher chance of success |
| 23 | m_dnsClient.reset(new simgear::DNS::Client{"8.8.8.8"}); |
| 24 | } |
| 25 | |
| 26 | void QtDNSClient::makeDNSRequest() |
| 27 | { |
| 28 | m_naptrRequest = new simgear::DNS::NAPTRRequest("terrasync.flightgear.org"); |
| 29 | m_naptrRequest->qservice = m_serviceName.toStdString(); |
| 30 | m_naptrRequest->qflags = "U"; |
| 31 | |
| 32 | m_dnsClient->makeRequest(m_naptrRequest); |
| 33 | |
| 34 | connect(&m_updateTimer, &QTimer::timeout, this, &QtDNSClient::onUpdateDNSRequest); |
| 35 | m_updateTimer.start(); |
| 36 | } |
| 37 | |
| 38 | void QtDNSClient::onUpdateDNSRequest() |
| 39 | { |
| 40 | using namespace std::string_literals; |
| 41 | |
| 42 | m_dnsClient->update(0); |
| 43 | if (m_naptrRequest->isTimeout()) { |
| 44 | m_updateTimer.stop(); |
| 45 | emit failed(tr("Unable to query update servers from DNS.")); |
| 46 | } |
| 47 | |
| 48 | if (m_naptrRequest->isComplete()) { |
| 49 | disconnect(&m_updateTimer, &QTimer::timeout, this, &QtDNSClient::onUpdateDNSRequest); |
| 50 | |
| 51 | if (m_naptrRequest->entries.empty()) { |
| 52 | m_updateTimer.stop(); |
| 53 | emit failed(tr("No update servers are currently available.")); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | selectServer(); |
| 58 | emit finished(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | QString QtDNSClient::result() const |
| 63 | { |
| 64 | if (m_results.empty()) { |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | return m_results.front(); |
| 69 | } |
| 70 | |
| 71 | QStringList QtDNSClient::results() const |
| 72 | { |
| 73 | return m_results; |
| 74 | } |
| 75 | |
| 76 | void QtDNSClient::selectServer() |
| 77 | { |
| 78 | assert(!m_naptrRequest->entries.empty()); |
| 79 | |
| 80 | const auto order = m_naptrRequest->entries[0]->order; |
| 81 | simgear::DNS::NAPTRRequest::NAPTR_list availableServers; |
| 82 | |
| 83 | for (const auto& entry : m_naptrRequest->entries) { |
| 84 | if (entry->order != order) |
| 85 | continue; |
| 86 | |
| 87 | const auto regex = entry->regexp; |
| 88 | if (!simgear::strutils::starts_with(regex, "!^.*$!")) { |
| 89 | SG_LOG(SG_TERRASYNC, SG_DEV_WARN, "ignoring unsupported regexp: " << regex); |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (!simgear::strutils::ends_with(regex, "!")) { |
| 94 | SG_LOG(SG_TERRASYNC, SG_DEV_WARN, "ignoring unsupported regexp: " << regex); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | // always use first entry |
| 99 | if (availableServers.empty() || (entry->preference == availableServers[0]->preference)) { |
| 100 | SG_LOG(SG_TERRASYNC, SG_DEBUG, "available server regexp: " << regex); |
| 101 | availableServers.push_back(entry); |
| 102 | } |
| 103 | } // of initial entries iteration |
| 104 | |
| 105 | // permute |
| 106 | auto rd = std::random_device{}; |
| 107 | auto rng = std::default_random_engine{rd()}; |
| 108 | std::shuffle(std::begin(availableServers), std::end(availableServers), rng); |
| 109 | |
| 110 | for (auto s : availableServers) { |
| 111 | const auto server = s->regexp; |
| 112 | std::string ret = server.substr(6, server.length() - 7); // trim off the regexp |
| 113 | m_results.append(QString::fromStdString(ret)); |
| 114 | } |
| 115 | } |