xref: /aosp_15_r20/external/cronet/net/dns/dns_config_service_posix.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_DNS_CONFIG_SERVICE_POSIX_H_
6 #define NET_DNS_DNS_CONFIG_SERVICE_POSIX_H_
7 
8 #include <netinet/in.h>
9 #include <resolv.h>
10 #include <sys/types.h>
11 
12 #include <memory>
13 #include <optional>
14 
15 #include "base/compiler_specific.h"
16 #include "base/gtest_prod_util.h"
17 #include "net/base/net_export.h"
18 #include "net/dns/dns_config_service.h"
19 
20 namespace net {
21 struct DnsConfig;
22 
23 // Use DnsConfigService::CreateSystemService to use it outside of tests.
24 namespace internal {
25 
26 // Service for reading and watching POSIX system (except Android and Linux) DNS
27 // settings.
28 // This object is not thread-safe and methods may perform blocking I/O so
29 // methods must be called on a sequence that allows blocking (i.e.
30 // base::MayBlock). It may be constructed on a different sequence than which
31 // it's later called on. WatchConfig() must be called prior to ReadConfig().
32 class NET_EXPORT_PRIVATE DnsConfigServicePosix : public DnsConfigService {
33  public:
34   DnsConfigServicePosix();
35 
36   DnsConfigServicePosix(const DnsConfigServicePosix&) = delete;
37   DnsConfigServicePosix& operator=(const DnsConfigServicePosix&) = delete;
38 
39   ~DnsConfigServicePosix() override;
40 
41   void RefreshConfig() override;
42 
43  protected:
44   // DnsConfigService:
45   void ReadConfigNow() override;
46   bool StartWatching() override;
47 
48   // Create |config_reader_|.
49   void CreateReader();
50 
51  private:
52   FRIEND_TEST_ALL_PREFIXES(DnsConfigServicePosixTest,
53                            ChangeConfigMultipleTimes);
54   class Watcher;
55   class ConfigReader;
56 
57   std::unique_ptr<Watcher> watcher_;
58   std::unique_ptr<ConfigReader> config_reader_;
59 };
60 
61 // Returns nullopt iff a valid config could not be determined.
62 std::optional<DnsConfig> NET_EXPORT_PRIVATE
63 ConvertResStateToDnsConfig(const struct __res_state& res);
64 
65 }  // namespace internal
66 
67 }  // namespace net
68 
69 #endif  // NET_DNS_DNS_CONFIG_SERVICE_POSIX_H_
70