dns_response_result_extractor.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2020 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_RESPONSE_RESULT_EXTRACTOR_H_
  5. #define NET_DNS_DNS_RESPONSE_RESULT_EXTRACTOR_H_
  6. #include <stdint.h>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/strings/string_piece.h"
  9. #include "net/base/net_export.h"
  10. #include "net/dns/host_cache.h"
  11. #include "net/dns/public/dns_query_type.h"
  12. namespace net {
  13. class DnsResponse;
  14. // Higher-level parser to take a DnsResponse and extract results.
  15. class NET_EXPORT_PRIVATE DnsResponseResultExtractor {
  16. public:
  17. enum class ExtractionError {
  18. kOk = 0,
  19. // Record failed to parse.
  20. kMalformedRecord,
  21. // Malformed CNAME
  22. kMalformedCname,
  23. // Found CNAME or result record with an unexpected name.
  24. kNameMismatch,
  25. // Malformed result record
  26. kMalformedResult,
  27. // CNAME record after a result record
  28. kCnameAfterResult,
  29. // Multiple CNAME records for the same owner name.
  30. kMultipleCnames,
  31. // Invalid alias chain, e.g. contains loops or disjoint aliases.
  32. kBadAliasChain,
  33. // Not expected. Used for DCHECKs.
  34. kUnexpected,
  35. };
  36. explicit DnsResponseResultExtractor(const DnsResponse* response);
  37. ~DnsResponseResultExtractor();
  38. DnsResponseResultExtractor(const DnsResponseResultExtractor&) = delete;
  39. DnsResponseResultExtractor& operator=(const DnsResponseResultExtractor&) =
  40. delete;
  41. // Extract results from the response. `query_type` must match the qtype from
  42. // the DNS query, and it must have already been validated (expected to be done
  43. // by DnsTransaction) that the response matches the query.
  44. //
  45. // `original_domain_name` is the query name (in dotted form) before any
  46. // aliasing or prepending port/scheme. It is expected to be the name under
  47. // which any basic query types, e.g. A or AAAA, are queried.
  48. //
  49. // May have the side effect of recording metrics about DnsResponses as they
  50. // are parsed, so while not an absolute requirement, any given DnsResponse
  51. // should only be used and extracted from at most once.
  52. ExtractionError ExtractDnsResults(DnsQueryType query_type,
  53. base::StringPiece original_domain_name,
  54. uint16_t request_port,
  55. HostCache::Entry* out_results) const;
  56. // Creates the results of a NODATA response (successfully parsed but without
  57. // any results) appropriate for `query_type`.
  58. static HostCache::Entry CreateEmptyResult(DnsQueryType query_type);
  59. private:
  60. const raw_ptr<const DnsResponse, DanglingUntriaged> response_;
  61. };
  62. } // namespace net
  63. #endif // NET_DNS_DNS_RESPONSE_RESULT_EXTRACTOR_H_