• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_HOST_RESOLVER_PROC_H_
6 #define NET_DNS_HOST_RESOLVER_PROC_H_
7 
8 #include <string>
9 
10 #include "base/memory/ref_counted.h"
11 #include "net/base/address_family.h"
12 #include "net/base/net_export.h"
13 #include "net/base/network_handle.h"
14 
15 namespace net {
16 
17 class AddressList;
18 
19 // Interface for a getaddrinfo()-like procedure. This is used by tests
20 // to control the underlying resolutions in HostResolverManager.
21 // HostResolverProcs can be chained together; they fallback to the next
22 // procedure in the chain by calling ResolveUsingPrevious(). Unless
23 // `allow_fallback_to_system_or_default` is set to false, `default_proc_`
24 // (set via SetDefault()) is added to the end of the chain and the actual system
25 // resolver acts as the final fallback after the default proc.
26 //
27 // Note that implementations of HostResolverProc *MUST BE THREADSAFE*, since
28 // the HostResolver implementation using them can be multi-threaded.
29 class NET_EXPORT HostResolverProc
30     : public base::RefCountedThreadSafe<HostResolverProc> {
31  public:
32   explicit HostResolverProc(scoped_refptr<HostResolverProc> previous,
33                             bool allow_fallback_to_system_or_default = true);
34 
35   HostResolverProc(const HostResolverProc&) = delete;
36   HostResolverProc& operator=(const HostResolverProc&) = delete;
37 
38   // Resolves |host| to an address list, restricting the results to addresses
39   // in |address_family|. If successful returns OK and fills |addrlist| with
40   // a list of socket addresses. Otherwise returns a network error code, and
41   // fills |os_error| with a more specific error if it was non-NULL.
42   virtual int Resolve(const std::string& host,
43                       AddressFamily address_family,
44                       HostResolverFlags host_resolver_flags,
45                       AddressList* addrlist,
46                       int* os_error) = 0;
47 
48   // Same as above but requires an additional `network` parameter. Differently
49   // from above the lookup will be performed specifically for `network`.
50   virtual int Resolve(const std::string& host,
51                       AddressFamily address_family,
52                       HostResolverFlags host_resolver_flags,
53                       AddressList* addrlist,
54                       int* os_error,
55                       handles::NetworkHandle network);
56 
57  protected:
58   friend class base::RefCountedThreadSafe<HostResolverProc>;
59 
60   virtual ~HostResolverProc();
61 
62   // Asks the fallback procedure (if set) to do the resolve.
63   int ResolveUsingPrevious(const std::string& host,
64                            AddressFamily address_family,
65                            HostResolverFlags host_resolver_flags,
66                            AddressList* addrlist,
67                            int* os_error);
68 
69  private:
70   friend class HostResolverManager;
71   friend class HostResolverSystemTask;
72   friend class MockHostResolverBase;
73   friend class ScopedDefaultHostResolverProc;
74 
75   // Sets the previous procedure in the chain.  Aborts if this would result in a
76   // cycle.
77   void SetPreviousProc(scoped_refptr<HostResolverProc> proc);
78 
79   // Sets the last procedure in the chain, i.e. appends |proc| to the end of the
80   // current chain.  Aborts if this would result in a cycle.
81   void SetLastProc(scoped_refptr<HostResolverProc> proc);
82 
83   // Returns the last procedure in the chain starting at |proc|.  Will return
84   // NULL iff |proc| is NULL.
85   static HostResolverProc* GetLastProc(HostResolverProc* proc);
86 
87   // Sets the default host resolver procedure that is used by
88   // HostResolverManager. This can be used through ScopedDefaultHostResolverProc
89   // to set a catch-all DNS block in unit-tests (individual tests should use
90   // MockHostResolver to prevent hitting the network).
91   static HostResolverProc* SetDefault(HostResolverProc* proc);
92   static HostResolverProc* GetDefault();
93 
94   bool allow_fallback_to_system_;
95   scoped_refptr<HostResolverProc> previous_proc_;
96   static HostResolverProc* default_proc_;
97 };
98 
99 }  // namespace net
100 
101 #endif  // NET_DNS_HOST_RESOLVER_PROC_H_
102