From 27fff3b72a4e00f515c63168e03b3454f0d91d19 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Wed, 9 Nov 2016 12:03:37 +0100 Subject: [PATCH] Sort DNS TXT entries and provide an attribute map --- simgear/io/DNSClient.cxx | 16 ++++++++++++++-- simgear/io/DNSClient.hxx | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/simgear/io/DNSClient.cxx b/simgear/io/DNSClient.cxx index 13f44b41..c1c7b25f 100644 --- a/simgear/io/DNSClient.cxx +++ b/simgear/io/DNSClient.cxx @@ -75,6 +75,13 @@ SRVRequest::SRVRequest( const std::string & dn ) : _type = DNS_T_SRV; } +static bool sortSRV( const SRVRequest::SRV_ptr a, const SRVRequest::SRV_ptr b ) +{ + if( a->priority > b->priority ) return false; + if( a->priority < b->priority ) return true; + return a->weight > b->weight; +} + static void dnscbSRV(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data) { SRVRequest * r = static_cast(data); @@ -90,7 +97,7 @@ static void dnscbSRV(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data) srv->port = result->dnssrv_srv[i].port; srv->target = result->dnssrv_srv[i].name; } -// std::sort( r->entries.begin(), r->entries.end(), sortNAPTR ); + std::sort( r->entries.begin(), r->entries.end(), sortSRV ); free(result); } r->setComplete(); @@ -120,7 +127,12 @@ static void dnscbTXT(struct dns_ctx *ctx, struct dns_rr_txt *result, void *data) 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)); + string txt = string((char*)result->dnstxt_txt[i].txt); + r->entries.push_back( txt ); + string_list tokens = simgear::strutils::split( txt, "=", 1 ); + if( tokens.size() == 2 ) { + r->attributes[tokens[0]] = tokens[1]; + } } free(result); } diff --git a/simgear/io/DNSClient.hxx b/simgear/io/DNSClient.hxx index 41fa6a17..2d2a4882 100644 --- a/simgear/io/DNSClient.hxx +++ b/simgear/io/DNSClient.hxx @@ -110,7 +110,9 @@ public: virtual void submit(); typedef std::vector TXT_list; + typedef std::map TXT_Attribute_map; TXT_list entries; + TXT_Attribute_map attributes; }; class Client