2 * \file DNSClient.cxx - simple DNS resolver client engine for SimGear
5 // Written by James Turner
7 // Copyright (C) 2016 Torsten Dreyer - torsten (at) t3r (dot) de
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Library General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "DNSClient.hxx"
27 #include <simgear/debug/logstream.hxx>
33 class Client::ClientPrivate {
36 if (dns_init(NULL, 0) < 0)
37 SG_LOG(SG_IO, SG_ALERT, "Can't init udns library" );
39 if( dns_open(NULL) < 0 )
40 SG_LOG(SG_IO, SG_ALERT, "Can't open udns context" );
48 Request::Request( const std::string & dn ) :
61 bool Request::isTimeout() const
63 return (time(NULL) - _start) > _timeout_secs;
66 NAPTRRequest::NAPTRRequest( const std::string & dn ) :
72 static bool sortNAPTR( const NAPTRRequest::NAPTR_ptr a, const NAPTRRequest::NAPTR_ptr b )
74 if( a->order > b->order ) return false;
75 if( a->order < b->order ) return true;
76 return a->preference < b->preference;
79 static void dnscbNAPTR(struct dns_ctx *ctx, struct dns_rr_naptr *result, void *data)
81 NAPTRRequest * r = static_cast<NAPTRRequest*>(data);
83 r->cname = result->dnsnaptr_cname;
84 r->qname = result->dnsnaptr_qname;
85 r->ttl = result->dnsnaptr_ttl;
86 for (int i = 0; i < result->dnsnaptr_nrr; i++) {
87 if( !r->qservice.empty() && r->qservice != result->dnsnaptr_naptr[i].service )
90 //TODO: case ignore and result flags may have more than one flag
91 if( !r->qflags.empty() && r->qflags != result->dnsnaptr_naptr[i].flags )
94 NAPTRRequest::NAPTR_ptr naptr(new NAPTRRequest::NAPTR);
95 r->entries.push_back(naptr);
96 naptr->order = result->dnsnaptr_naptr[i].order;
97 naptr->preference = result->dnsnaptr_naptr[i].preference;
98 naptr->flags = result->dnsnaptr_naptr[i].flags;
99 naptr->service = result->dnsnaptr_naptr[i].service;
100 naptr->regexp = result->dnsnaptr_naptr[i].regexp;
101 naptr->replacement = result->dnsnaptr_naptr[i].replacement;
103 std::sort( r->entries.begin(), r->entries.end(), sortNAPTR );
109 void NAPTRRequest::submit()
111 if (!dns_submit_naptr(NULL, getDn().c_str(), 0, dnscbNAPTR, this )) {
112 SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
128 void Client::makeRequest(const Request_ptr& r)
134 void Client::update(int waitTimeout)
136 time_t now = time(NULL);
137 if( dns_timeouts( NULL, waitTimeout, now ) < 0 )
139 dns_ioevent(NULL, now);
142 } // of namespace DNS
144 } // of namespace simgear