From 4b571f2a242b6e5468633b5879d5ec9858c7fa0c Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Tue, 8 Nov 2016 17:08:38 +0100 Subject: [PATCH] Implement SRV and TXT DNS records --- simgear/io/DNSClient.cxx | 70 +++++++++++++++++++++++++++++++++++++++- simgear/io/DNSClient.hxx | 30 +++++++++++++++-- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/simgear/io/DNSClient.cxx b/simgear/io/DNSClient.cxx index 8681e900..9c129eae 100644 --- a/simgear/io/DNSClient.cxx +++ b/simgear/io/DNSClient.cxx @@ -69,6 +69,75 @@ NAPTRRequest::NAPTRRequest( const std::string & dn ) : _type = DNS_T_NAPTR; } +SRVRequest::SRVRequest( const std::string & dn ) : + Request(dn) +{ + _type = DNS_T_SRV; +} + +static void dnscbSRV(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data) +{ + SRVRequest * r = static_cast(data); + if (result) { + r->cname = result->dnssrv_cname; + r->qname = result->dnssrv_qname; + r->ttl = result->dnssrv_ttl; + for (int i = 0; i < result->dnssrv_nrr; i++) { + SRVRequest::SRV_ptr srv(new SRVRequest::SRV); + r->entries.push_back(srv); + srv->priority = result->dnssrv_srv[i].priority; + srv->weight = result->dnssrv_srv[i].weight; + srv->port = result->dnssrv_srv[i].port; + srv->target = result->dnssrv_srv[i].name; + } +// std::sort( r->entries.begin(), r->entries.end(), sortNAPTR ); + free(result); + } + r->setComplete(); +} +void SRVRequest::submit() +{ + // protocol and service an already encoded in DN so pass in NULL for both + if (!dns_submit_srv(NULL, getDn().c_str(), NULL, NULL, 0, dnscbSRV, this )) { + SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn()); + return; + } + _start = time(NULL); +} + +TXTRequest::TXTRequest( const std::string & dn ) : + Request(dn) +{ + _type = DNS_T_TXT; +} + +static void dnscbTXT(struct dns_ctx *ctx, struct dns_rr_txt *result, void *data) +{ + TXTRequest * r = static_cast(data); + if (result) { + r->cname = result->dnstxt_cname; + r->qname = result->dnstxt_qname; + r->ttl = result->dnstxt_ttl; + for (int i = 0; i < result->dnstxt_nrr; i++) { + //TODO: interprete the .len field of dnstxt_txt? + r->entries.push_back(string((char*)result->dnstxt_txt[i].txt)); + } + free(result); + } + r->setComplete(); +} + +void TXTRequest::submit() +{ + // protocol and service an already encoded in DN so pass in NULL for both + if (!dns_submit_txt(NULL, getDn().c_str(), 0, 0, dnscbTXT, this )) { + SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn()); + return; + } + _start = time(NULL); +} + + static bool sortNAPTR( const NAPTRRequest::NAPTR_ptr a, const NAPTRRequest::NAPTR_ptr b ) { if( a->order > b->order ) return false; @@ -130,7 +199,6 @@ void Client::makeRequest(const Request_ptr& r) r->submit(); } - void Client::update(int waitTimeout) { time_t now = time(NULL); diff --git a/simgear/io/DNSClient.hxx b/simgear/io/DNSClient.hxx index 7cabd41c..41fa6a17 100644 --- a/simgear/io/DNSClient.hxx +++ b/simgear/io/DNSClient.hxx @@ -31,6 +31,7 @@ #include #include +#include namespace simgear { @@ -61,6 +62,7 @@ protected: time_t _timeout_secs; time_t _start; }; +typedef SGSharedPtr Request_ptr; class NAPTRRequest : public Request { @@ -84,7 +86,32 @@ public: std::string qservice; }; -typedef SGSharedPtr Request_ptr; +class SRVRequest : public Request +{ +public: + SRVRequest( const std::string & dn ); + virtual void submit(); + + struct SRV : SGReferenced { + int priority; + int weight; + int port; + std::string target; + }; + typedef SGSharedPtr SRV_ptr; + typedef std::vector SRV_list; + SRV_list entries; +}; + +class TXTRequest : public Request +{ +public: + TXTRequest( const std::string & dn ); + virtual void submit(); + + typedef std::vector TXT_list; + TXT_list entries; +}; class Client { @@ -99,7 +126,6 @@ public: // void cancelRequest(const Request_ptr& r, std::string reason = std::string()); private: - class ClientPrivate; std::auto_ptr d; };