dns_config_service_posix.h 2.0 KB

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