1// Copyright (c) 2011 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_BASE_HOST_RESOLVER_PROC_H_
6#define NET_BASE_HOST_RESOLVER_PROC_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/ref_counted.h"
12#include "net/base/address_family.h"
13
14namespace net {
15
16class 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.
25class HostResolverProc : public base::RefCountedThreadSafe<HostResolverProc> {
26 public:
27  explicit HostResolverProc(HostResolverProc* previous);
28
29  // Resolves |host| to an address list, restricting the results to addresses
30  // in |address_family|. If successful returns OK and fills |addrlist| with
31  // a list of socket addresses. Otherwise returns a network error code, and
32  // fills |os_error| with a more specific error if it was non-NULL.
33  virtual int Resolve(const std::string& host,
34                      AddressFamily address_family,
35                      HostResolverFlags host_resolver_flags,
36                      AddressList* addrlist,
37                      int* os_error) = 0;
38
39 protected:
40  friend class base::RefCountedThreadSafe<HostResolverProc>;
41
42  virtual ~HostResolverProc();
43
44  // Asks the fallback procedure (if set) to do the resolve.
45  int ResolveUsingPrevious(const std::string& host,
46                           AddressFamily address_family,
47                           HostResolverFlags host_resolver_flags,
48                           AddressList* addrlist,
49                           int* os_error);
50
51 private:
52  friend class HostResolverImpl;
53  friend class MockHostResolverBase;
54  friend class ScopedDefaultHostResolverProc;
55
56  // Sets the previous procedure in the chain.  Aborts if this would result in a
57  // cycle.
58  void SetPreviousProc(HostResolverProc* proc);
59
60  // Sets the last procedure in the chain, i.e. appends |proc| to the end of the
61  // current chain.  Aborts if this would result in a cycle.
62  void SetLastProc(HostResolverProc* proc);
63
64  // Returns the last procedure in the chain starting at |proc|.  Will return
65  // NULL iff |proc| is NULL.
66  static HostResolverProc* GetLastProc(HostResolverProc* proc);
67
68  // Sets the default host resolver procedure that is used by HostResolverImpl.
69  // This can be used through ScopedDefaultHostResolverProc to set a catch-all
70  // DNS block in unit-tests (individual tests should use MockHostResolver to
71  // prevent hitting the network).
72  static HostResolverProc* SetDefault(HostResolverProc* proc);
73  static HostResolverProc* GetDefault();
74
75  scoped_refptr<HostResolverProc> previous_proc_;
76  static HostResolverProc* default_proc_;
77
78  DISALLOW_COPY_AND_ASSIGN(HostResolverProc);
79};
80
81// Resolves |host| to an address list, using the system's default host resolver.
82// (i.e. this calls out to getaddrinfo()). If successful returns OK and fills
83// |addrlist| with a list of socket addresses. Otherwise returns a
84// network error code, and fills |os_error| with a more specific errir if it
85// was non-NULL.
86int SystemHostResolverProc(const std::string& host,
87                           AddressFamily address_family,
88                           HostResolverFlags host_resolver_flags,
89                           AddressList* addrlist,
90                           int* os_error);
91
92}  // namespace net
93
94#endif  // NET_BASE_HOST_RESOLVER_PROC_H_
95