diff options
Diffstat (limited to 'grpc/src/core/lib/channel/channelz.cc')
-rw-r--r-- | grpc/src/core/lib/channel/channelz.cc | 120 |
1 files changed, 108 insertions, 12 deletions
diff --git a/grpc/src/core/lib/channel/channelz.cc b/grpc/src/core/lib/channel/channelz.cc index 188203cb..452d6641 100644 --- a/grpc/src/core/lib/channel/channelz.cc +++ b/grpc/src/core/lib/channel/channelz.cc @@ -20,16 +20,19 @@ #include "src/core/lib/channel/channelz.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "absl/strings/escaping.h" #include "absl/strings/strip.h" #include <grpc/grpc.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/string_util.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include "src/core/lib/address_utils/sockaddr_utils.h" #include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/status_util.h" #include "src/core/lib/gpr/string.h" @@ -39,6 +42,7 @@ #include "src/core/lib/gprpp/memory.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/exec_ctx.h" +#include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/slice/b64.h" #include "src/core/lib/slice/slice_internal.h" #include "src/core/lib/surface/channel.h" @@ -337,6 +341,83 @@ Json ServerNode::RenderJson() { } // +// SocketNode::Security::Tls +// + +Json SocketNode::Security::Tls::RenderJson() { + Json::Object data; + if (type == NameType::kStandardName) { + data["standard_name"] = name; + } else if (type == NameType::kOtherName) { + data["other_name"] = name; + } + if (!local_certificate.empty()) { + data["local_certificate"] = absl::Base64Escape(local_certificate); + } + if (!remote_certificate.empty()) { + data["remote_certificate"] = absl::Base64Escape(remote_certificate); + } + return data; +} + +// +// SocketNode::Security +// + +Json SocketNode::Security::RenderJson() { + Json::Object data; + switch (type) { + case ModelType::kUnset: + break; + case ModelType::kTls: + if (tls) { + data["tls"] = tls->RenderJson(); + } + break; + case ModelType::kOther: + if (other) { + data["other"] = *other; + } + break; + } + return data; +} + +namespace { + +void* SecurityArgCopy(void* p) { + SocketNode::Security* xds_certificate_provider = + static_cast<SocketNode::Security*>(p); + return xds_certificate_provider->Ref().release(); +} + +void SecurityArgDestroy(void* p) { + SocketNode::Security* xds_certificate_provider = + static_cast<SocketNode::Security*>(p); + xds_certificate_provider->Unref(); +} + +int SecurityArgCmp(void* p, void* q) { return GPR_ICMP(p, q); } + +const grpc_arg_pointer_vtable kChannelArgVtable = { + SecurityArgCopy, SecurityArgDestroy, SecurityArgCmp}; + +} // namespace + +grpc_arg SocketNode::Security::MakeChannelArg() const { + return grpc_channel_arg_pointer_create( + const_cast<char*>(GRPC_ARG_CHANNELZ_SECURITY), + const_cast<SocketNode::Security*>(this), &kChannelArgVtable); +} + +RefCountedPtr<SocketNode::Security> SocketNode::Security::GetFromChannelArgs( + const grpc_channel_args* args) { + Security* security = grpc_channel_args_find_pointer<Security>( + args, GRPC_ARG_CHANNELZ_SECURITY); + return security != nullptr ? security->Ref() : nullptr; +} + +// // SocketNode // @@ -356,13 +437,22 @@ void PopulateSocketAddressJson(Json::Object* json, const char* name, if (!port.empty()) { port_num = atoi(port.data()); } - char* b64_host = grpc_base64_encode(host.data(), host.size(), false, false); - data["tcpip_address"] = Json::Object{ - {"port", port_num}, - {"ip_address", b64_host}, - }; - gpr_free(b64_host); - } else if (uri.ok() && uri->scheme() == "unix") { + grpc_resolved_address resolved_host; + grpc_error_handle error = + grpc_string_to_sockaddr(&resolved_host, host.c_str(), port_num); + if (error == GRPC_ERROR_NONE) { + std::string packed_host = grpc_sockaddr_get_packed_host(&resolved_host); + std::string b64_host = absl::Base64Escape(packed_host); + data["tcpip_address"] = Json::Object{ + {"port", port_num}, + {"ip_address", b64_host}, + }; + (*json)[name] = std::move(data); + return; + } + GRPC_ERROR_UNREF(error); + } + if (uri.ok() && uri->scheme() == "unix") { data["uds_address"] = Json::Object{ {"filename", uri->path()}, }; @@ -376,10 +466,12 @@ void PopulateSocketAddressJson(Json::Object* json, const char* name, } // namespace -SocketNode::SocketNode(std::string local, std::string remote, std::string name) +SocketNode::SocketNode(std::string local, std::string remote, std::string name, + RefCountedPtr<Security> security) : BaseNode(EntityType::kSocket, std::move(name)), local_(std::move(local)), - remote_(std::move(remote)) {} + remote_(std::move(remote)), + security_(std::move(security)) {} void SocketNode::RecordStreamStartedFromLocal() { streams_started_.FetchAdd(1, MemoryOrder::RELAXED); @@ -467,6 +559,10 @@ Json SocketNode::RenderJson() { }}, {"data", std::move(data)}, }; + if (security_ != nullptr && + security_->type != SocketNode::Security::ModelType::kUnset) { + object["security"] = security_->RenderJson(); + } PopulateSocketAddressJson(&object, "remote", remote_.c_str()); PopulateSocketAddressJson(&object, "local", local_.c_str()); return object; |