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 "src/core/resolver/endpoint_addresses.h"
20
21 #include <grpc/support/port_platform.h>
22 #include <string.h>
23
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "absl/log/check.h"
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 #include "src/core/lib/address_utils/sockaddr_utils.h"
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/util/useful.h"
36
37 // IWYU pragma: no_include <sys/socket.h>
38
39 namespace grpc_core {
40
EndpointAddresses(const grpc_resolved_address & address,const ChannelArgs & args)41 EndpointAddresses::EndpointAddresses(const grpc_resolved_address& address,
42 const ChannelArgs& args)
43 : addresses_(1, address), args_(args) {}
44
EndpointAddresses(std::vector<grpc_resolved_address> addresses,const ChannelArgs & args)45 EndpointAddresses::EndpointAddresses(
46 std::vector<grpc_resolved_address> addresses, const ChannelArgs& args)
47 : addresses_(std::move(addresses)), args_(args) {
48 CHECK(!addresses_.empty());
49 }
50
EndpointAddresses(const EndpointAddresses & other)51 EndpointAddresses::EndpointAddresses(const EndpointAddresses& other)
52 : addresses_(other.addresses_), args_(other.args_) {}
53
operator =(const EndpointAddresses & other)54 EndpointAddresses& EndpointAddresses::operator=(
55 const EndpointAddresses& other) {
56 if (&other == this) return *this;
57 addresses_ = other.addresses_;
58 args_ = other.args_;
59 return *this;
60 }
61
EndpointAddresses(EndpointAddresses && other)62 EndpointAddresses::EndpointAddresses(EndpointAddresses&& other) noexcept
63 : addresses_(std::move(other.addresses_)), args_(std::move(other.args_)) {}
64
operator =(EndpointAddresses && other)65 EndpointAddresses& EndpointAddresses::operator=(
66 EndpointAddresses&& other) noexcept {
67 addresses_ = std::move(other.addresses_);
68 args_ = std::move(other.args_);
69 return *this;
70 }
71
Cmp(const EndpointAddresses & other) const72 int EndpointAddresses::Cmp(const EndpointAddresses& other) const {
73 for (size_t i = 0; i < addresses_.size(); ++i) {
74 if (other.addresses_.size() == i) return 1;
75 if (addresses_[i].len > other.addresses_[i].len) return 1;
76 if (addresses_[i].len < other.addresses_[i].len) return -1;
77 int retval =
78 memcmp(addresses_[i].addr, other.addresses_[i].addr, addresses_[i].len);
79 if (retval != 0) return retval;
80 }
81 if (other.addresses_.size() > addresses_.size()) return -1;
82 return QsortCompare(args_, other.args_);
83 }
84
ToString() const85 std::string EndpointAddresses::ToString() const {
86 std::vector<std::string> addr_strings;
87 for (const auto& address : addresses_) {
88 auto addr_str = grpc_sockaddr_to_string(&address, false);
89 addr_strings.push_back(addr_str.ok() ? std::move(*addr_str)
90 : addr_str.status().ToString());
91 }
92 std::vector<std::string> parts = {
93 absl::StrCat("addrs=[", absl::StrJoin(addr_strings, ", "), "]")};
94 if (args_ != ChannelArgs()) {
95 parts.emplace_back(absl::StrCat("args=", args_.ToString()));
96 }
97 return absl::StrJoin(parts, " ");
98 }
99
operator ()(const grpc_resolved_address & addr1,const grpc_resolved_address & addr2) const100 bool ResolvedAddressLessThan::operator()(
101 const grpc_resolved_address& addr1,
102 const grpc_resolved_address& addr2) const {
103 if (addr1.len < addr2.len) return true;
104 return memcmp(addr1.addr, addr2.addr, addr1.len) < 0;
105 }
106
operator ==(const EndpointAddressSet & other) const107 bool EndpointAddressSet::operator==(const EndpointAddressSet& other) const {
108 if (addresses_.size() != other.addresses_.size()) return false;
109 auto other_it = other.addresses_.begin();
110 for (auto it = addresses_.begin(); it != addresses_.end(); ++it) {
111 CHECK(other_it != other.addresses_.end());
112 if (it->len != other_it->len ||
113 memcmp(it->addr, other_it->addr, it->len) != 0) {
114 return false;
115 }
116 ++other_it;
117 }
118 return true;
119 }
120
operator <(const EndpointAddressSet & other) const121 bool EndpointAddressSet::operator<(const EndpointAddressSet& other) const {
122 auto other_it = other.addresses_.begin();
123 for (auto it = addresses_.begin(); it != addresses_.end(); ++it) {
124 if (other_it == other.addresses_.end()) return false;
125 if (it->len < other_it->len) return true;
126 if (it->len > other_it->len) return false;
127 int r = memcmp(it->addr, other_it->addr, it->len);
128 if (r != 0) return r < 0;
129 ++other_it;
130 }
131 return other_it != other.addresses_.end();
132 }
133
ToString() const134 std::string EndpointAddressSet::ToString() const {
135 std::vector<std::string> parts;
136 parts.reserve(addresses_.size());
137 for (const auto& address : addresses_) {
138 parts.push_back(
139 grpc_sockaddr_to_string(&address, false).value_or("<unknown>"));
140 }
141 return absl::StrCat("{", absl::StrJoin(parts, ", "), "}");
142 }
143
144 } // namespace grpc_core
145