1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2016 Tatsuhiro Tsujikawa 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 #ifndef SHRPX_DNS_TRACKER_H 26 #define SHRPX_DNS_TRACKER_H 27 28 #include "shrpx.h" 29 30 #include <map> 31 #include <chrono> 32 33 #include "shrpx_dual_dns_resolver.h" 34 35 using namespace nghttp2; 36 37 namespace shrpx { 38 39 struct DNSQuery { DNSQueryDNSQuery40 DNSQuery(StringRef host, CompleteCb cb) 41 : host(std::move(host)), 42 cb(std::move(cb)), 43 dlnext(nullptr), 44 dlprev(nullptr), 45 status(DNSResolverStatus::IDLE), 46 in_qlist(false) {} 47 48 // Host name we lookup for. 49 StringRef host; 50 // Callback function called when name lookup finished. This 51 // callback is not called if name lookup finishes within 52 // DNSTracker::resolve(). 53 CompleteCb cb; 54 DNSQuery *dlnext, *dlprev; 55 DNSResolverStatus status; 56 // true if this object is in linked list ResolverEntry::qlist. 57 bool in_qlist; 58 }; 59 60 struct ResolverEntry { 61 // Host name this entry lookups for. 62 ImmutableString host; 63 // DNS resolver. Only non-nullptr if status is 64 // DNSResolverStatus::RUNNING. 65 std::unique_ptr<DualDNSResolver> resolv; 66 // DNSQuery interested in this name lookup result. The result is 67 // notified to them all. 68 DList<DNSQuery> qlist; 69 // Use the same enum with DNSResolverStatus 70 DNSResolverStatus status; 71 // result and its expiry time 72 Address result; 73 // time point when cached result expires. 74 std::chrono::steady_clock::time_point expiry; 75 }; 76 77 class DNSTracker { 78 public: 79 DNSTracker(struct ev_loop *loop, int family); 80 ~DNSTracker(); 81 82 // Lookups host name described in |dnsq|. If name lookup finishes 83 // within this function (either it came from /etc/hosts, host name 84 // is numeric, lookup result is cached, etc), it returns 85 // DNSResolverStatus::OK or DNSResolverStatus::ERROR. If lookup is 86 // successful, DNSResolverStatus::OK is returned, and |result| is 87 // filled. If lookup failed, DNSResolverStatus::ERROR is returned. 88 // If name lookup is being done background, it returns 89 // DNSResolverStatus::RUNNING. Its completion is notified by 90 // calling dnsq->cb. 91 DNSResolverStatus resolve(Address *result, DNSQuery *dnsq); 92 // Cancels name lookup requested by |dnsq|. 93 void cancel(DNSQuery *dnsq); 94 // Removes expired entries from ents_. 95 void gc(); 96 // Starts GC timer. 97 void start_gc_timer(); 98 99 private: 100 ResolverEntry make_entry(std::unique_ptr<DualDNSResolver> resolv, 101 ImmutableString host, DNSResolverStatus status, 102 const Address *result); 103 104 void update_entry(ResolverEntry &ent, std::unique_ptr<DualDNSResolver> resolv, 105 DNSResolverStatus status, const Address *result); 106 107 void add_to_qlist(ResolverEntry &ent, DNSQuery *dnsq); 108 109 std::map<StringRef, ResolverEntry> ents_; 110 // Periodically iterates ents_, and removes expired entries to avoid 111 // excessive use of memory. Since only backend API can potentially 112 // increase memory consumption, interval could be very long. 113 ev_timer gc_timer_; 114 struct ev_loop *loop_; 115 // IP version preference. 116 int family_; 117 }; 118 119 } // namespace shrpx 120 121 #endif // SHRPX_DNS_TRACKER_H 122