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