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