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/ext/filters/client_channel/server_address.h"
22
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/str_join.h"
29
30 #include "src/core/lib/iomgr/sockaddr_utils.h"
31
32 namespace grpc_core {
33
34 //
35 // ServerAddressWeightAttribute
36 //
37 const char* ServerAddressWeightAttribute::kServerAddressWeightAttributeKey =
38 "server_address_weight";
39
40 //
41 // ServerAddress
42 //
43
ServerAddress(const grpc_resolved_address & address,grpc_channel_args * args,std::map<const char *,std::unique_ptr<AttributeInterface>> attributes)44 ServerAddress::ServerAddress(
45 const grpc_resolved_address& address, grpc_channel_args* args,
46 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
47 : address_(address), args_(args), attributes_(std::move(attributes)) {}
48
ServerAddress(const void * address,size_t address_len,grpc_channel_args * args,std::map<const char *,std::unique_ptr<AttributeInterface>> attributes)49 ServerAddress::ServerAddress(
50 const void* address, size_t address_len, grpc_channel_args* args,
51 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
52 : args_(args), attributes_(std::move(attributes)) {
53 memcpy(address_.addr, address, address_len);
54 address_.len = static_cast<socklen_t>(address_len);
55 }
56
ServerAddress(const ServerAddress & other)57 ServerAddress::ServerAddress(const ServerAddress& other)
58 : address_(other.address_), args_(grpc_channel_args_copy(other.args_)) {
59 for (const auto& p : other.attributes_) {
60 attributes_[p.first] = p.second->Copy();
61 }
62 }
operator =(const ServerAddress & other)63 ServerAddress& ServerAddress::operator=(const ServerAddress& other) {
64 address_ = other.address_;
65 grpc_channel_args_destroy(args_);
66 args_ = grpc_channel_args_copy(other.args_);
67 attributes_.clear();
68 for (const auto& p : other.attributes_) {
69 attributes_[p.first] = p.second->Copy();
70 }
71 return *this;
72 }
73
ServerAddress(ServerAddress && other)74 ServerAddress::ServerAddress(ServerAddress&& other) noexcept
75 : address_(other.address_),
76 args_(other.args_),
77 attributes_(std::move(other.attributes_)) {
78 other.args_ = nullptr;
79 }
operator =(ServerAddress && other)80 ServerAddress& ServerAddress::operator=(ServerAddress&& other) noexcept {
81 address_ = other.address_;
82 grpc_channel_args_destroy(args_);
83 args_ = other.args_;
84 other.args_ = nullptr;
85 attributes_ = std::move(other.attributes_);
86 return *this;
87 }
88
89 namespace {
90
CompareAttributes(const std::map<const char *,std::unique_ptr<ServerAddress::AttributeInterface>> & attributes1,const std::map<const char *,std::unique_ptr<ServerAddress::AttributeInterface>> & attributes2)91 int CompareAttributes(
92 const std::map<const char*,
93 std::unique_ptr<ServerAddress::AttributeInterface>>&
94 attributes1,
95 const std::map<const char*,
96 std::unique_ptr<ServerAddress::AttributeInterface>>&
97 attributes2) {
98 auto it2 = attributes2.begin();
99 for (auto it1 = attributes1.begin(); it1 != attributes1.end(); ++it1) {
100 // attributes2 has fewer elements than attributes1
101 if (it2 == attributes2.end()) return -1;
102 // compare keys
103 int retval = strcmp(it1->first, it2->first);
104 if (retval != 0) return retval;
105 // compare values
106 retval = it1->second->Cmp(it2->second.get());
107 if (retval != 0) return retval;
108 ++it2;
109 }
110 // attributes1 has fewer elements than attributes2
111 if (it2 != attributes2.end()) return 1;
112 // equal
113 return 0;
114 }
115
116 } // namespace
117
Cmp(const ServerAddress & other) const118 int ServerAddress::Cmp(const ServerAddress& other) const {
119 if (address_.len > other.address_.len) return 1;
120 if (address_.len < other.address_.len) return -1;
121 int retval = memcmp(address_.addr, other.address_.addr, address_.len);
122 if (retval != 0) return retval;
123 retval = grpc_channel_args_compare(args_, other.args_);
124 if (retval != 0) return retval;
125 return CompareAttributes(attributes_, other.attributes_);
126 }
127
GetAttribute(const char * key) const128 const ServerAddress::AttributeInterface* ServerAddress::GetAttribute(
129 const char* key) const {
130 auto it = attributes_.find(key);
131 if (it == attributes_.end()) return nullptr;
132 return it->second.get();
133 }
134
135 // Returns a copy of the address with a modified attribute.
136 // If the new value is null, the attribute is removed.
WithAttribute(const char * key,std::unique_ptr<AttributeInterface> value) const137 ServerAddress ServerAddress::WithAttribute(
138 const char* key, std::unique_ptr<AttributeInterface> value) const {
139 ServerAddress address = *this;
140 if (value == nullptr) {
141 address.attributes_.erase(key);
142 } else {
143 address.attributes_[key] = std::move(value);
144 }
145 return address;
146 }
147
ToString() const148 std::string ServerAddress::ToString() const {
149 std::vector<std::string> parts = {
150 grpc_sockaddr_to_string(&address_, false),
151 };
152 if (args_ != nullptr) {
153 parts.emplace_back(
154 absl::StrCat("args={", grpc_channel_args_string(args_), "}"));
155 }
156 if (!attributes_.empty()) {
157 std::vector<std::string> attrs;
158 for (const auto& p : attributes_) {
159 attrs.emplace_back(absl::StrCat(p.first, "=", p.second->ToString()));
160 }
161 parts.emplace_back(
162 absl::StrCat("attributes={", absl::StrJoin(attrs, ", "), "}"));
163 }
164 return absl::StrJoin(parts, " ");
165 }
166
167 } // namespace grpc_core
168