PrivateDnsConfiguration.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef NETD_RESOLV_PRIVATEDNSCONFIGURATION_H
  17. #define NETD_RESOLV_PRIVATEDNSCONFIGURATION_H
  18. #include <list>
  19. #include <map>
  20. #include <mutex>
  21. #include <vector>
  22. #include <android-base/thread_annotations.h>
  23. #include "DnsTlsServer.h"
  24. namespace android {
  25. namespace net {
  26. // The DNS over TLS mode on a specific netId.
  27. enum class PrivateDnsMode : uint8_t { OFF, OPPORTUNISTIC, STRICT };
  28. // Validation status of a DNS over TLS server (on a specific netId).
  29. enum class Validation : uint8_t { in_process, success, fail, unknown_server, unknown_netid };
  30. struct PrivateDnsStatus {
  31. PrivateDnsMode mode;
  32. std::list<DnsTlsServer> validatedServers;
  33. };
  34. // TODO: remove this C-style struct and use PrivateDnsStatus everywhere.
  35. struct ExternalPrivateDnsStatus {
  36. PrivateDnsMode mode;
  37. int numServers;
  38. struct PrivateDnsInfo {
  39. sockaddr_storage ss;
  40. const char* hostname;
  41. Validation validation;
  42. } serverStatus[MAXNS];
  43. };
  44. class PrivateDnsConfiguration {
  45. public:
  46. int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
  47. const std::string& name, const std::set<std::vector<uint8_t>>& fingerprints);
  48. PrivateDnsStatus getStatus(unsigned netId);
  49. // DEPRECATED, use getStatus() above.
  50. void getStatus(unsigned netId, ExternalPrivateDnsStatus* status);
  51. void clear(unsigned netId);
  52. private:
  53. typedef std::map<DnsTlsServer, Validation, AddressComparator> PrivateDnsTracker;
  54. void validatePrivateDnsProvider(const DnsTlsServer& server, PrivateDnsTracker& tracker,
  55. unsigned netId, uint32_t mark) REQUIRES(mPrivateDnsLock);
  56. bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success);
  57. // Start validation for newly added servers as well as any servers that have
  58. // landed in Validation::fail state. Note that servers that have failed
  59. // multiple validation attempts but for which there is still a validating
  60. // thread running are marked as being Validation::in_process.
  61. bool needsValidation(const PrivateDnsTracker& tracker, const DnsTlsServer& server);
  62. std::mutex mPrivateDnsLock;
  63. std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
  64. // Structure for tracking the validation status of servers on a specific netId.
  65. // Using the AddressComparator ensures at most one entry per IP address.
  66. std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
  67. };
  68. extern PrivateDnsConfiguration gPrivateDnsConfiguration;
  69. } // namespace net
  70. } // namespace android
  71. #endif /* NETD_RESOLV_PRIVATEDNSCONFIGURATION_H */