nsswitch_reader.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 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_NSSWITCH_READER_H_
  5. #define NET_DNS_NSSWITCH_READER_H_
  6. #include <string>
  7. #include <tuple>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "net/base/net_export.h"
  11. namespace net {
  12. // Reader to read and parse Posix nsswitch.conf files, particularly the "hosts:"
  13. // database entry.
  14. class NET_EXPORT_PRIVATE NsswitchReader {
  15. public:
  16. // These values are emitted in metrics. Entries should not be renumbered and
  17. // numeric values should never be reused. (See NsswitchService in
  18. // tools/metrics/histograms/enums.xml.)
  19. enum class Service {
  20. kUnknown = 0,
  21. kFiles = 1,
  22. kDns = 2,
  23. kMdns = 3,
  24. kMdns4 = 4,
  25. kMdns6 = 5,
  26. kMdnsMinimal = 6,
  27. kMdns4Minimal = 7,
  28. kMdns6Minimal = 8,
  29. kMyHostname = 9,
  30. kResolve = 10,
  31. kNis = 11,
  32. kMaxValue = kNis
  33. };
  34. enum class Status {
  35. kUnknown,
  36. kSuccess,
  37. kNotFound,
  38. kUnavailable,
  39. kTryAgain,
  40. };
  41. enum class Action {
  42. kUnknown,
  43. kReturn,
  44. kContinue,
  45. kMerge,
  46. };
  47. struct ServiceAction {
  48. bool operator==(const ServiceAction& other) const {
  49. return std::tie(negated, status, action) ==
  50. std::tie(other.negated, other.status, other.action);
  51. }
  52. bool negated;
  53. Status status;
  54. Action action;
  55. };
  56. struct NET_EXPORT_PRIVATE ServiceSpecification {
  57. explicit ServiceSpecification(Service service,
  58. std::vector<ServiceAction> actions = {});
  59. ~ServiceSpecification();
  60. ServiceSpecification(const ServiceSpecification&);
  61. ServiceSpecification& operator=(const ServiceSpecification&);
  62. ServiceSpecification(ServiceSpecification&&);
  63. ServiceSpecification& operator=(ServiceSpecification&&);
  64. bool operator==(const ServiceSpecification& other) const {
  65. return std::tie(service, actions) ==
  66. std::tie(other.service, other.actions);
  67. }
  68. Service service;
  69. std::vector<ServiceAction> actions;
  70. };
  71. // Test-replacable call for the actual file read. Default implementation does
  72. // a fresh read of the nsswitch.conf file every time it is called. Returns
  73. // empty string on error reading the file.
  74. using FileReadCall = base::RepeatingCallback<std::string()>;
  75. NsswitchReader();
  76. virtual ~NsswitchReader();
  77. NsswitchReader(const NsswitchReader&) = delete;
  78. NsswitchReader& operator=(const NsswitchReader&) = delete;
  79. // Reads nsswitch.conf and parses the "hosts:" database. In case of multiple
  80. // matching databases, only parses the first. Assumes a basic default
  81. // configuration if the file cannot be read or a "hosts:" database cannot be
  82. // found.
  83. virtual std::vector<ServiceSpecification> ReadAndParseHosts();
  84. void set_file_read_call_for_testing(FileReadCall file_read_call) {
  85. file_read_call_ = std::move(file_read_call);
  86. }
  87. private:
  88. FileReadCall file_read_call_;
  89. };
  90. } // namespace net
  91. #endif // NET_DNS_NSSWITCH_READER_H_