12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2016 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef SHRPX_DNS_TRACKER_H
262c593315Sopenharmony_ci#define SHRPX_DNS_TRACKER_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "shrpx.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <map>
312c593315Sopenharmony_ci#include <chrono>
322c593315Sopenharmony_ci
332c593315Sopenharmony_ci#include "shrpx_dual_dns_resolver.h"
342c593315Sopenharmony_ci
352c593315Sopenharmony_ciusing namespace nghttp2;
362c593315Sopenharmony_ci
372c593315Sopenharmony_cinamespace shrpx {
382c593315Sopenharmony_ci
392c593315Sopenharmony_cistruct DNSQuery {
402c593315Sopenharmony_ci  DNSQuery(StringRef host, CompleteCb cb)
412c593315Sopenharmony_ci      : host(std::move(host)),
422c593315Sopenharmony_ci        cb(std::move(cb)),
432c593315Sopenharmony_ci        dlnext(nullptr),
442c593315Sopenharmony_ci        dlprev(nullptr),
452c593315Sopenharmony_ci        status(DNSResolverStatus::IDLE),
462c593315Sopenharmony_ci        in_qlist(false) {}
472c593315Sopenharmony_ci
482c593315Sopenharmony_ci  // Host name we lookup for.
492c593315Sopenharmony_ci  StringRef host;
502c593315Sopenharmony_ci  // Callback function called when name lookup finished.  This
512c593315Sopenharmony_ci  // callback is not called if name lookup finishes within
522c593315Sopenharmony_ci  // DNSTracker::resolve().
532c593315Sopenharmony_ci  CompleteCb cb;
542c593315Sopenharmony_ci  DNSQuery *dlnext, *dlprev;
552c593315Sopenharmony_ci  DNSResolverStatus status;
562c593315Sopenharmony_ci  // true if this object is in linked list ResolverEntry::qlist.
572c593315Sopenharmony_ci  bool in_qlist;
582c593315Sopenharmony_ci};
592c593315Sopenharmony_ci
602c593315Sopenharmony_cistruct ResolverEntry {
612c593315Sopenharmony_ci  // Host name this entry lookups for.
622c593315Sopenharmony_ci  ImmutableString host;
632c593315Sopenharmony_ci  // DNS resolver.  Only non-nullptr if status is
642c593315Sopenharmony_ci  // DNSResolverStatus::RUNNING.
652c593315Sopenharmony_ci  std::unique_ptr<DualDNSResolver> resolv;
662c593315Sopenharmony_ci  // DNSQuery interested in this name lookup result.  The result is
672c593315Sopenharmony_ci  // notified to them all.
682c593315Sopenharmony_ci  DList<DNSQuery> qlist;
692c593315Sopenharmony_ci  // Use the same enum with DNSResolverStatus
702c593315Sopenharmony_ci  DNSResolverStatus status;
712c593315Sopenharmony_ci  // result and its expiry time
722c593315Sopenharmony_ci  Address result;
732c593315Sopenharmony_ci  // time point when cached result expires.
742c593315Sopenharmony_ci  std::chrono::steady_clock::time_point expiry;
752c593315Sopenharmony_ci};
762c593315Sopenharmony_ci
772c593315Sopenharmony_ciclass DNSTracker {
782c593315Sopenharmony_cipublic:
792c593315Sopenharmony_ci  DNSTracker(struct ev_loop *loop, int family);
802c593315Sopenharmony_ci  ~DNSTracker();
812c593315Sopenharmony_ci
822c593315Sopenharmony_ci  // Lookups host name described in |dnsq|.  If name lookup finishes
832c593315Sopenharmony_ci  // within this function (either it came from /etc/hosts, host name
842c593315Sopenharmony_ci  // is numeric, lookup result is cached, etc), it returns
852c593315Sopenharmony_ci  // DNSResolverStatus::OK or DNSResolverStatus::ERROR.  If lookup is
862c593315Sopenharmony_ci  // successful, DNSResolverStatus::OK is returned, and |result| is
872c593315Sopenharmony_ci  // filled.  If lookup failed, DNSResolverStatus::ERROR is returned.
882c593315Sopenharmony_ci  // If name lookup is being done background, it returns
892c593315Sopenharmony_ci  // DNSResolverStatus::RUNNING.  Its completion is notified by
902c593315Sopenharmony_ci  // calling dnsq->cb.
912c593315Sopenharmony_ci  DNSResolverStatus resolve(Address *result, DNSQuery *dnsq);
922c593315Sopenharmony_ci  // Cancels name lookup requested by |dnsq|.
932c593315Sopenharmony_ci  void cancel(DNSQuery *dnsq);
942c593315Sopenharmony_ci  // Removes expired entries from ents_.
952c593315Sopenharmony_ci  void gc();
962c593315Sopenharmony_ci  // Starts GC timer.
972c593315Sopenharmony_ci  void start_gc_timer();
982c593315Sopenharmony_ci
992c593315Sopenharmony_ciprivate:
1002c593315Sopenharmony_ci  ResolverEntry make_entry(std::unique_ptr<DualDNSResolver> resolv,
1012c593315Sopenharmony_ci                           ImmutableString host, DNSResolverStatus status,
1022c593315Sopenharmony_ci                           const Address *result);
1032c593315Sopenharmony_ci
1042c593315Sopenharmony_ci  void update_entry(ResolverEntry &ent, std::unique_ptr<DualDNSResolver> resolv,
1052c593315Sopenharmony_ci                    DNSResolverStatus status, const Address *result);
1062c593315Sopenharmony_ci
1072c593315Sopenharmony_ci  void add_to_qlist(ResolverEntry &ent, DNSQuery *dnsq);
1082c593315Sopenharmony_ci
1092c593315Sopenharmony_ci  std::map<StringRef, ResolverEntry> ents_;
1102c593315Sopenharmony_ci  // Periodically iterates ents_, and removes expired entries to avoid
1112c593315Sopenharmony_ci  // excessive use of memory.  Since only backend API can potentially
1122c593315Sopenharmony_ci  // increase memory consumption, interval could be very long.
1132c593315Sopenharmony_ci  ev_timer gc_timer_;
1142c593315Sopenharmony_ci  struct ev_loop *loop_;
1152c593315Sopenharmony_ci  // IP version preference.
1162c593315Sopenharmony_ci  int family_;
1172c593315Sopenharmony_ci};
1182c593315Sopenharmony_ci
1192c593315Sopenharmony_ci} // namespace shrpx
1202c593315Sopenharmony_ci
1212c593315Sopenharmony_ci#endif // SHRPX_DNS_TRACKER_H
122