From fa4894e110c7a70837d366d9f50066a06b01f848 Mon Sep 17 00:00:00 2001 From: ncteisen Date: Wed, 31 Oct 2018 18:48:22 -0700 Subject: Handle uri parsing in channelz module --- src/core/lib/channel/channelz.cc | 100 +++++++++++++++++---------------------- src/core/lib/channel/channelz.h | 47 ++---------------- 2 files changed, 48 insertions(+), 99 deletions(-) (limited to 'src/core/lib/channel') diff --git a/src/core/lib/channel/channelz.cc b/src/core/lib/channel/channelz.cc index d8c826c54d..cd98b29b1e 100644 --- a/src/core/lib/channel/channelz.cc +++ b/src/core/lib/channel/channelz.cc @@ -30,6 +30,7 @@ #include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/status_util.h" +#include "src/core/lib/gpr/host_port.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/useful.h" #include "src/core/lib/gprpp/memory.h" @@ -39,6 +40,7 @@ #include "src/core/lib/surface/channel.h" #include "src/core/lib/surface/server.h" #include "src/core/lib/transport/error_utils.h" +#include "src/core/lib/uri/uri_parser.h" namespace grpc_core { namespace channelz { @@ -277,62 +279,59 @@ grpc_json* ServerNode::RenderJson() { return top_level_json; } -SocketAddress::SocketAddress() : type_(AddressType::kUnset), port_(-1) {} - -SocketAddress::SocketAddress(SocketAddress&& other) { - type_ = other.type_; - port_ = other.port_; - blob_.reset((other.blob_.release())); -} - -void SocketAddress::PopulateJson(const char* name, grpc_json* json) { - if (type_ == AddressType::kUnset) { - return; - } +static void PopulateSocketAddressJson(grpc_json* json, const char* name, + const char* addr_str) { + if (addr_str == nullptr) return; grpc_json* json_iterator = nullptr; json_iterator = grpc_json_create_child(json_iterator, json, name, nullptr, GRPC_JSON_OBJECT, false); json = json_iterator; json_iterator = nullptr; - switch (type_) { - case AddressType::kTcpAddress: - json_iterator = - grpc_json_create_child(json_iterator, json, "tcpip_address", nullptr, - GRPC_JSON_OBJECT, false); - json = json_iterator; - json_iterator = nullptr; - json_iterator = - grpc_json_add_number_string_child(json, json_iterator, "port", port_); - json_iterator = - grpc_json_create_child(json_iterator, json, "ip_address", blob_.get(), - GRPC_JSON_STRING, false); - break; - case AddressType::kUdsAddress: + int port_num = -1; + grpc_uri* uri = grpc_uri_parse(addr_str, true); + if (uri != nullptr && (strcmp(uri->scheme, "fd") != 0)) { + const char* host_port = uri->path; + if (*host_port == '/') ++host_port; + if (strcmp(uri->scheme, "unix") == 0) { json_iterator = grpc_json_create_child(json_iterator, json, "uds_address", nullptr, GRPC_JSON_OBJECT, false); json = json_iterator; json_iterator = nullptr; json_iterator = - grpc_json_create_child(json_iterator, json, "filename", blob_.get(), - GRPC_JSON_STRING, false); - break; - case AddressType::kDirectChannelAddress: + grpc_json_create_child(json_iterator, json, "filename", + gpr_strdup(host_port), GRPC_JSON_STRING, true); + } else { + char* host = nullptr; + char* port = nullptr; + GPR_ASSERT(gpr_split_host_port(host_port, &host, &port)); + if (port != nullptr) { + port_num = atoi(port); + } + char* b64_host = gpr_string_base64_encode(host); json_iterator = - grpc_json_create_child(json_iterator, json, "other_address", nullptr, + grpc_json_create_child(json_iterator, json, "tcpip_address", nullptr, GRPC_JSON_OBJECT, false); json = json_iterator; json_iterator = nullptr; - json_iterator = grpc_json_create_child( - json_iterator, json, "name", blob_.get(), GRPC_JSON_STRING, false); - break; - break; - default: - GPR_UNREACHABLE_CODE(GPR_ASSERT(0)); - break; + json_iterator = grpc_json_add_number_string_child(json, json_iterator, + "port", port_num); + json_iterator = grpc_json_create_child(json_iterator, json, "ip_address", + b64_host, GRPC_JSON_STRING, true); + gpr_free(host); + gpr_free(port); + } + } else { + json_iterator = grpc_json_create_child(json_iterator, json, "other_address", + nullptr, GRPC_JSON_OBJECT, false); + json = json_iterator; + json_iterator = nullptr; + json_iterator = grpc_json_create_child(json_iterator, json, "name", + addr_str, GRPC_JSON_STRING, false); } + grpc_uri_destroy(uri); } -SocketNode::SocketNode(SocketAddress local, SocketAddress remote) +SocketNode::SocketNode(UniquePtr local, UniquePtr remote) : BaseNode(EntityType::kSocket), local_(std::move(local)), remote_(std::move(remote)) {} @@ -374,8 +373,8 @@ grpc_json* SocketNode::RenderJson() { json_iterator = grpc_json_add_number_string_child(json, json_iterator, "socketId", uuid()); json = top_level_json; - remote_.PopulateJson("remote", json); - local_.PopulateJson("local", json); + PopulateSocketAddressJson(json, "remote", remote_.get()); + PopulateSocketAddressJson(json, "local", local_.get()); // reset json iterators to top level object json = top_level_json; json_iterator = nullptr; @@ -435,8 +434,8 @@ grpc_json* SocketNode::RenderJson() { return top_level_json; } -ListenSocketNode::ListenSocketNode(UniquePtr host, int port) - : BaseNode(EntityType::kSocket), host_(std::move(host)), port_(port) {} +ListenSocketNode::ListenSocketNode(UniquePtr local_addr) + : BaseNode(EntityType::kSocket), local_addr_(std::move(local_addr)) {} grpc_json* ListenSocketNode::RenderJson() { // We need to track these three json objects to build our object @@ -450,20 +449,7 @@ grpc_json* ListenSocketNode::RenderJson() { json_iterator = nullptr; json_iterator = grpc_json_add_number_string_child(json, json_iterator, "socketId", uuid()); - json = top_level_json; - json_iterator = nullptr; - json_iterator = grpc_json_create_child(json_iterator, json, "local", nullptr, - GRPC_JSON_OBJECT, false); - json = json_iterator; - json_iterator = nullptr; - json_iterator = grpc_json_create_child(json_iterator, json, "tcpip_address", - nullptr, GRPC_JSON_OBJECT, false); - json = json_iterator; - json_iterator = nullptr; - json_iterator = - grpc_json_add_number_string_child(json, json_iterator, "port", port_); - json_iterator = grpc_json_create_child(json_iterator, json, "ip_address", - host_.get(), GRPC_JSON_STRING, false); + PopulateSocketAddressJson(json, "local", local_addr_.get()); return top_level_json; } diff --git a/src/core/lib/channel/channelz.h b/src/core/lib/channel/channelz.h index 70efbd861d..cdbae581fd 100644 --- a/src/core/lib/channel/channelz.h +++ b/src/core/lib/channel/channelz.h @@ -229,45 +229,10 @@ class ServerNode : public BaseNode { ChannelTrace trace_; }; -// helper class for holding and rendering the information about a particular -// socket's address -class SocketAddress { - public: - enum class AddressType { - kUnset, - kTcpAddress, - kUdsAddress, - kDirectChannelAddress, - }; - - SocketAddress(); - SocketAddress(SocketAddress&& other); - - void PopulateJson(const char* name, grpc_json* json); - - void set_blob(char* blob) { blob_.reset(blob); } - void set_port(int port) { port_ = port; } - void set_type(AddressType type) { type_ = type; } - - const char* blob_view() { return blob_.get(); } - int port() { return port_; } - AddressType type() { return type_; } - - private: - AddressType type_; - // this holds different information for different address types: - // kTcpAddress = host - // kUdsAddress = filename - // kDirectChannelAddress = name (which is fd num) - UniquePtr blob_; - // used for kTcpAddress, otherwise -1 - int port_; -}; - // Handles channelz bookkeeping for sockets class SocketNode : public BaseNode { public: - SocketNode(SocketAddress local, SocketAddress remote); + SocketNode(UniquePtr local, UniquePtr remote); ~SocketNode() override {} grpc_json* RenderJson() override; @@ -297,23 +262,21 @@ class SocketNode : public BaseNode { gpr_atm last_remote_stream_created_millis_ = 0; gpr_atm last_message_sent_millis_ = 0; gpr_atm last_message_received_millis_ = 0; - SocketAddress local_; - SocketAddress remote_; + UniquePtr local_; + UniquePtr remote_; }; // Handles channelz bookkeeping for listen sockets class ListenSocketNode : public BaseNode { public: // ListenSocketNode takes ownership of host. - // TODO(ncteisen): use SocketAddress to support more address types. - ListenSocketNode(UniquePtr host, int port); + ListenSocketNode(UniquePtr local_addr); ~ListenSocketNode() override {} grpc_json* RenderJson() override; private: - UniquePtr host_; - int port_; + UniquePtr local_addr_; }; // Creation functions -- cgit v1.2.3