xref: /aosp_15_r20/external/grpc-grpc/src/core/resolver/endpoint_addresses.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/resolver/endpoint_addresses.h"
22 
23 #include <string.h>
24 
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/status/status.h"
30 #include "absl/status/statusor.h"
31 #include "absl/strings/str_cat.h"
32 #include "absl/strings/str_join.h"
33 
34 #include <grpc/support/log.h>
35 
36 #include "src/core/lib/address_utils/sockaddr_utils.h"
37 #include "src/core/lib/channel/channel_args.h"
38 #include "src/core/lib/gpr/useful.h"
39 
40 // IWYU pragma: no_include <sys/socket.h>
41 
42 namespace grpc_core {
43 
EndpointAddresses(const grpc_resolved_address & address,const ChannelArgs & args)44 EndpointAddresses::EndpointAddresses(const grpc_resolved_address& address,
45                                      const ChannelArgs& args)
46     : addresses_(1, address), args_(args) {}
47 
EndpointAddresses(std::vector<grpc_resolved_address> addresses,const ChannelArgs & args)48 EndpointAddresses::EndpointAddresses(
49     std::vector<grpc_resolved_address> addresses, const ChannelArgs& args)
50     : addresses_(std::move(addresses)), args_(args) {
51   GPR_ASSERT(!addresses_.empty());
52 }
53 
EndpointAddresses(const EndpointAddresses & other)54 EndpointAddresses::EndpointAddresses(const EndpointAddresses& other)
55     : addresses_(other.addresses_), args_(other.args_) {}
56 
operator =(const EndpointAddresses & other)57 EndpointAddresses& EndpointAddresses::operator=(
58     const EndpointAddresses& other) {
59   if (&other == this) return *this;
60   addresses_ = other.addresses_;
61   args_ = other.args_;
62   return *this;
63 }
64 
EndpointAddresses(EndpointAddresses && other)65 EndpointAddresses::EndpointAddresses(EndpointAddresses&& other) noexcept
66     : addresses_(std::move(other.addresses_)), args_(std::move(other.args_)) {}
67 
operator =(EndpointAddresses && other)68 EndpointAddresses& EndpointAddresses::operator=(
69     EndpointAddresses&& other) noexcept {
70   addresses_ = std::move(other.addresses_);
71   args_ = std::move(other.args_);
72   return *this;
73 }
74 
Cmp(const EndpointAddresses & other) const75 int EndpointAddresses::Cmp(const EndpointAddresses& other) const {
76   for (size_t i = 0; i < addresses_.size(); ++i) {
77     if (other.addresses_.size() == i) return 1;
78     if (addresses_[i].len > other.addresses_[i].len) return 1;
79     if (addresses_[i].len < other.addresses_[i].len) return -1;
80     int retval =
81         memcmp(addresses_[i].addr, other.addresses_[i].addr, addresses_[i].len);
82     if (retval != 0) return retval;
83   }
84   if (other.addresses_.size() > addresses_.size()) return -1;
85   return QsortCompare(args_, other.args_);
86 }
87 
ToString() const88 std::string EndpointAddresses::ToString() const {
89   std::vector<std::string> addr_strings;
90   for (const auto& address : addresses_) {
91     auto addr_str = grpc_sockaddr_to_string(&address, false);
92     addr_strings.push_back(addr_str.ok() ? std::move(*addr_str)
93                                          : addr_str.status().ToString());
94   }
95   std::vector<std::string> parts = {
96       absl::StrCat("addrs=[", absl::StrJoin(addr_strings, ", "), "]")};
97   if (args_ != ChannelArgs()) {
98     parts.emplace_back(absl::StrCat("args=", args_.ToString()));
99   }
100   return absl::StrJoin(parts, " ");
101 }
102 
operator ()(const grpc_resolved_address & addr1,const grpc_resolved_address & addr2) const103 bool ResolvedAddressLessThan::operator()(
104     const grpc_resolved_address& addr1,
105     const grpc_resolved_address& addr2) const {
106   if (addr1.len < addr2.len) return true;
107   return memcmp(addr1.addr, addr2.addr, addr1.len) < 0;
108 }
109 
operator ==(const EndpointAddressSet & other) const110 bool EndpointAddressSet::operator==(const EndpointAddressSet& other) const {
111   if (addresses_.size() != other.addresses_.size()) return false;
112   auto other_it = other.addresses_.begin();
113   for (auto it = addresses_.begin(); it != addresses_.end(); ++it) {
114     GPR_ASSERT(other_it != other.addresses_.end());
115     if (it->len != other_it->len ||
116         memcmp(it->addr, other_it->addr, it->len) != 0) {
117       return false;
118     }
119     ++other_it;
120   }
121   return true;
122 }
123 
operator <(const EndpointAddressSet & other) const124 bool EndpointAddressSet::operator<(const EndpointAddressSet& other) const {
125   auto other_it = other.addresses_.begin();
126   for (auto it = addresses_.begin(); it != addresses_.end(); ++it) {
127     if (other_it == other.addresses_.end()) return false;
128     if (it->len < other_it->len) return true;
129     if (it->len > other_it->len) return false;
130     int r = memcmp(it->addr, other_it->addr, it->len);
131     if (r != 0) return r < 0;
132     ++other_it;
133   }
134   return other_it != other.addresses_.end();
135 }
136 
ToString() const137 std::string EndpointAddressSet::ToString() const {
138   std::vector<std::string> parts;
139   parts.reserve(addresses_.size());
140   for (const auto& address : addresses_) {
141     parts.push_back(
142         grpc_sockaddr_to_string(&address, false).value_or("<unknown>"));
143   }
144   return absl::StrCat("{", absl::StrJoin(parts, ", "), "}");
145 }
146 
147 }  // namespace grpc_core
148