1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 14 namespace net { 15 16 class AddressList; 17 18 // Interface for a getaddrinfo()-like procedure. This is used by unit-tests 19 // to control the underlying resolutions in HostResolverImpl. HostResolverProcs 20 // can be chained together; they fallback to the next procedure in the chain 21 // by calling ResolveUsingPrevious(). 22 // 23 // Note that implementations of HostResolverProc *MUST BE THREADSAFE*, since 24 // the HostResolver implementation using them can be multi-threaded. 25 class NET_EXPORT HostResolverProc 26 : public base::RefCountedThreadSafe<HostResolverProc> { 27 public: 28 explicit HostResolverProc(HostResolverProc* previous); 29 30 // Resolves |host| to an address list, restricting the results to addresses 31 // in |address_family|. If successful returns OK and fills |addrlist| with 32 // a list of socket addresses. Otherwise returns a network error code, and 33 // fills |os_error| with a more specific error if it was non-NULL. 34 virtual int Resolve(const std::string& host, 35 AddressFamily address_family, 36 HostResolverFlags host_resolver_flags, 37 AddressList* addrlist, 38 int* os_error) = 0; 39 40 protected: 41 friend class base::RefCountedThreadSafe<HostResolverProc>; 42 43 virtual ~HostResolverProc(); 44 45 // Asks the fallback procedure (if set) to do the resolve. 46 int ResolveUsingPrevious(const std::string& host, 47 AddressFamily address_family, 48 HostResolverFlags host_resolver_flags, 49 AddressList* addrlist, 50 int* os_error); 51 52 private: 53 friend class HostResolverImpl; 54 friend class MockHostResolverBase; 55 friend class ScopedDefaultHostResolverProc; 56 57 // Sets the previous procedure in the chain. Aborts if this would result in a 58 // cycle. 59 void SetPreviousProc(HostResolverProc* proc); 60 61 // Sets the last procedure in the chain, i.e. appends |proc| to the end of the 62 // current chain. Aborts if this would result in a cycle. 63 void SetLastProc(HostResolverProc* proc); 64 65 // Returns the last procedure in the chain starting at |proc|. Will return 66 // NULL iff |proc| is NULL. 67 static HostResolverProc* GetLastProc(HostResolverProc* proc); 68 69 // Sets the default host resolver procedure that is used by HostResolverImpl. 70 // This can be used through ScopedDefaultHostResolverProc to set a catch-all 71 // DNS block in unit-tests (individual tests should use MockHostResolver to 72 // prevent hitting the network). 73 static HostResolverProc* SetDefault(HostResolverProc* proc); 74 static HostResolverProc* GetDefault(); 75 76 scoped_refptr<HostResolverProc> previous_proc_; 77 static HostResolverProc* default_proc_; 78 79 DISALLOW_COPY_AND_ASSIGN(HostResolverProc); 80 }; 81 82 // Resolves |host| to an address list, using the system's default host resolver. 83 // (i.e. this calls out to getaddrinfo()). If successful returns OK and fills 84 // |addrlist| with a list of socket addresses. Otherwise returns a 85 // network error code, and fills |os_error| with a more specific error if it 86 // was non-NULL. 87 NET_EXPORT_PRIVATE int SystemHostResolverCall( 88 const std::string& host, 89 AddressFamily address_family, 90 HostResolverFlags host_resolver_flags, 91 AddressList* addrlist, 92 int* os_error); 93 94 // Wraps call to SystemHostResolverCall as an instance of HostResolverProc. 95 class NET_EXPORT_PRIVATE SystemHostResolverProc : public HostResolverProc { 96 public: 97 SystemHostResolverProc(); 98 virtual int Resolve(const std::string& hostname, 99 AddressFamily address_family, 100 HostResolverFlags host_resolver_flags, 101 AddressList* addr_list, 102 int* os_error) OVERRIDE; 103 protected: 104 virtual ~SystemHostResolverProc(); 105 106 DISALLOW_COPY_AND_ASSIGN(SystemHostResolverProc); 107 }; 108 109 } // namespace net 110 111 #endif // NET_DNS_HOST_RESOLVER_PROC_H_ 112