DnsTlsServer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2017 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. #include "DnsTlsServer.h"
  17. #include <algorithm>
  18. namespace {
  19. // Returns a tuple of references to the elements of a.
  20. auto make_tie(const sockaddr_in& a) {
  21. return std::tie(a.sin_port, a.sin_addr.s_addr);
  22. }
  23. // Returns a tuple of references to the elements of a.
  24. auto make_tie(const sockaddr_in6& a) {
  25. // Skip flowinfo, which is not relevant.
  26. return std::tie(
  27. a.sin6_port,
  28. a.sin6_addr,
  29. a.sin6_scope_id
  30. );
  31. }
  32. } // namespace
  33. // These binary operators make sockaddr_storage comparable. They need to be
  34. // in the global namespace so that the std::tuple < and == operators can see them.
  35. static bool operator <(const in6_addr& x, const in6_addr& y) {
  36. return std::lexicographical_compare(
  37. std::begin(x.s6_addr), std::end(x.s6_addr),
  38. std::begin(y.s6_addr), std::end(y.s6_addr));
  39. }
  40. static bool operator ==(const in6_addr& x, const in6_addr& y) {
  41. return std::equal(
  42. std::begin(x.s6_addr), std::end(x.s6_addr),
  43. std::begin(y.s6_addr), std::end(y.s6_addr));
  44. }
  45. static bool operator <(const sockaddr_storage& x, const sockaddr_storage& y) {
  46. if (x.ss_family != y.ss_family) {
  47. return x.ss_family < y.ss_family;
  48. }
  49. // Same address family.
  50. if (x.ss_family == AF_INET) {
  51. const sockaddr_in& x_sin = reinterpret_cast<const sockaddr_in&>(x);
  52. const sockaddr_in& y_sin = reinterpret_cast<const sockaddr_in&>(y);
  53. return make_tie(x_sin) < make_tie(y_sin);
  54. } else if (x.ss_family == AF_INET6) {
  55. const sockaddr_in6& x_sin6 = reinterpret_cast<const sockaddr_in6&>(x);
  56. const sockaddr_in6& y_sin6 = reinterpret_cast<const sockaddr_in6&>(y);
  57. return make_tie(x_sin6) < make_tie(y_sin6);
  58. }
  59. return false; // Unknown address type. This is an error.
  60. }
  61. static bool operator ==(const sockaddr_storage& x, const sockaddr_storage& y) {
  62. if (x.ss_family != y.ss_family) {
  63. return false;
  64. }
  65. // Same address family.
  66. if (x.ss_family == AF_INET) {
  67. const sockaddr_in& x_sin = reinterpret_cast<const sockaddr_in&>(x);
  68. const sockaddr_in& y_sin = reinterpret_cast<const sockaddr_in&>(y);
  69. return make_tie(x_sin) == make_tie(y_sin);
  70. } else if (x.ss_family == AF_INET6) {
  71. const sockaddr_in6& x_sin6 = reinterpret_cast<const sockaddr_in6&>(x);
  72. const sockaddr_in6& y_sin6 = reinterpret_cast<const sockaddr_in6&>(y);
  73. return make_tie(x_sin6) == make_tie(y_sin6);
  74. }
  75. return false; // Unknown address type. This is an error.
  76. }
  77. namespace android {
  78. namespace net {
  79. // This comparison ignores ports and fingerprints.
  80. bool AddressComparator::operator() (const DnsTlsServer& x, const DnsTlsServer& y) const {
  81. if (x.ss.ss_family != y.ss.ss_family) {
  82. return x.ss.ss_family < y.ss.ss_family;
  83. }
  84. // Same address family.
  85. if (x.ss.ss_family == AF_INET) {
  86. const sockaddr_in& x_sin = reinterpret_cast<const sockaddr_in&>(x.ss);
  87. const sockaddr_in& y_sin = reinterpret_cast<const sockaddr_in&>(y.ss);
  88. return x_sin.sin_addr.s_addr < y_sin.sin_addr.s_addr;
  89. } else if (x.ss.ss_family == AF_INET6) {
  90. const sockaddr_in6& x_sin6 = reinterpret_cast<const sockaddr_in6&>(x.ss);
  91. const sockaddr_in6& y_sin6 = reinterpret_cast<const sockaddr_in6&>(y.ss);
  92. return std::tie(x_sin6.sin6_addr, x_sin6.sin6_scope_id) <
  93. std::tie(y_sin6.sin6_addr, y_sin6.sin6_scope_id);
  94. }
  95. return false; // Unknown address type. This is an error.
  96. }
  97. // Returns a tuple of references to the elements of s.
  98. auto make_tie(const DnsTlsServer& s) {
  99. return std::tie(
  100. s.ss,
  101. s.name,
  102. s.fingerprints,
  103. s.protocol
  104. );
  105. }
  106. bool DnsTlsServer::operator <(const DnsTlsServer& other) const {
  107. return make_tie(*this) < make_tie(other);
  108. }
  109. bool DnsTlsServer::operator ==(const DnsTlsServer& other) const {
  110. return make_tie(*this) == make_tie(other);
  111. }
  112. bool DnsTlsServer::wasExplicitlyConfigured() const {
  113. return !name.empty() || !fingerprints.empty();
  114. }
  115. } // namespace net
  116. } // namespace android