Support unix socket in grpc_sockaddr_to_string (#29169)

* Support unix socket in grpc_sockaddr_to_string

* make it return statusor

* clang fix

* made grpc_sockaddr_to_string() to return statusor

* Let Chttp2ServerListener::Start crash

* test failure fixed

* api_fuzzer fixed

* comments addressed.

* more comments addressed

* comments addressed

* fix other broken builds
pull/29424/head
Yousuk Seung 3 years ago committed by GitHub
parent e9ad1d5f47
commit 371d231b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      BUILD
  2. 7
      src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc
  3. 2
      src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc
  4. 7
      src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc
  5. 3
      src/core/ext/filters/client_channel/subchannel.cc
  6. 7
      src/core/ext/filters/client_channel/subchannel_pool_interface.cc
  7. 13
      src/core/ext/transport/chttp2/server/chttp2_server.cc
  8. 18
      src/core/ext/xds/xds_listener.cc
  9. 57
      src/core/lib/address_utils/sockaddr_utils.cc
  10. 11
      src/core/lib/address_utils/sockaddr_utils.h
  11. 6
      src/core/lib/http/httpcli.cc
  12. 6
      src/core/lib/iomgr/endpoint_cfstream.cc
  13. 4
      src/core/lib/iomgr/event_engine/endpoint.cc
  14. 3
      src/core/lib/iomgr/event_engine/resolved_address_internal.cc
  15. 3
      src/core/lib/iomgr/event_engine/resolved_address_internal.h
  16. 8
      src/core/lib/iomgr/event_engine/tcp.cc
  17. 5
      src/core/lib/iomgr/socket_utils_common_posix.cc
  18. 12
      src/core/lib/iomgr/tcp_client_cfstream.cc
  19. 17
      src/core/lib/iomgr/tcp_client_posix.cc
  20. 19
      src/core/lib/iomgr/tcp_client_windows.cc
  21. 6
      src/core/lib/iomgr/tcp_posix.cc
  22. 33
      src/core/lib/iomgr/tcp_server_posix.cc
  23. 57
      src/core/lib/iomgr/tcp_server_utils_posix_common.cc
  24. 11
      src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc
  25. 18
      src/core/lib/iomgr/tcp_server_windows.cc
  26. 6
      src/core/lib/iomgr/tcp_windows.cc
  27. 3
      src/core/lib/resolver/server_address.cc
  28. 92
      test/core/address_utils/sockaddr_utils_test.cc
  29. 4
      test/core/end2end/dualstack_socket_test.cc
  30. 5
      test/core/end2end/fuzzers/api_fuzzer.cc
  31. 2
      test/core/iomgr/tcp_server_posix_test.cc
  32. 5
      test/core/security/evaluate_args_test.cc
  33. 2
      test/core/util/fake_udp_and_tcp_server.cc
  34. 9
      test/cpp/naming/address_sorting_test.cc
  35. 3
      test/cpp/naming/resolver_component_test.cc

@ -1729,6 +1729,8 @@ grpc_cc_library(
"src/core/lib/address_utils/sockaddr_utils.h",
],
external_deps = [
"absl/status",
"absl/status:statusor",
"absl/strings",
"absl/strings:str_format",
],

@ -534,7 +534,8 @@ std::string GrpcLb::Serverlist::AsText() const {
} else {
grpc_resolved_address addr;
ParseServer(server, &addr);
ipport = grpc_sockaddr_to_string(&addr, false);
auto addr_str = grpc_sockaddr_to_string(&addr, false);
ipport = addr_str.ok() ? *addr_str : addr_str.status().ToString();
}
entries.push_back(absl::StrFormat(" %" PRIuPTR ": %s token=%s\n", i,
ipport, server.load_balance_token));
@ -582,10 +583,12 @@ ServerAddressList GrpcLb::Serverlist::GetServerAddressList(
server.load_balance_token, GPR_ARRAY_SIZE(server.load_balance_token));
std::string lb_token(server.load_balance_token, lb_token_length);
if (lb_token.empty()) {
auto addr_uri = grpc_sockaddr_to_uri(&addr);
gpr_log(GPR_INFO,
"Missing LB token for backend address '%s'. The empty token will "
"be used instead",
grpc_sockaddr_to_uri(&addr).c_str());
addr_uri.ok() ? addr_uri->c_str()
: addr_uri.status().ToString().c_str());
}
// Attach attribute to address containing LB token and stats object.
std::map<const char*, std::unique_ptr<ServerAddress::AttributeInterface>>

@ -323,7 +323,7 @@ RingHash::Ring::Ring(RingHash* parent,
ServerAddressWeightAttribute::kServerAddressWeightAttributeKey));
AddressWeight address_weight;
address_weight.address =
grpc_sockaddr_to_string(&sd->address().address(), false);
grpc_sockaddr_to_string(&sd->address().address(), false).value();
if (weight_attribute != nullptr) {
GPR_ASSERT(weight_attribute->weight() != 0);
address_weight.weight = weight_attribute->weight();

@ -526,12 +526,13 @@ static void log_address_sorting_list(const grpc_ares_request* r,
const ServerAddressList& addresses,
const char* input_output_str) {
for (size_t i = 0; i < addresses.size(); i++) {
std::string addr_str =
grpc_sockaddr_to_string(&addresses[i].address(), true);
auto addr_str = grpc_sockaddr_to_string(&addresses[i].address(), true);
gpr_log(GPR_INFO,
"(c-ares resolver) request:%p c-ares address sorting: %s[%" PRIuPTR
"]=%s",
r, input_output_str, i, addr_str.c_str());
r, input_output_str, i,
addr_str.ok() ? addr_str->c_str()
: addr_str.status().ToString().c_str());
}
}

@ -676,7 +676,8 @@ Subchannel::Subchannel(SubchannelKey key,
{GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT, 0,
INT_MAX}));
channelz_node_ = MakeRefCounted<channelz::SubchannelNode>(
grpc_sockaddr_to_uri(&key_.address()), channel_tracer_max_memory);
grpc_sockaddr_to_uri(&key_.address()).value(),
channel_tracer_max_memory);
channelz_node_->AddTraceEvent(
channelz::ChannelTrace::Severity::Info,
grpc_slice_from_static_string("subchannel created"));

@ -84,8 +84,11 @@ void SubchannelKey::Init(
}
std::string SubchannelKey::ToString() const {
return absl::StrCat("{address=", grpc_sockaddr_to_uri(&address_),
", args=", grpc_channel_args_string(args_), "}");
auto addr_uri = grpc_sockaddr_to_uri(&address_);
return absl::StrCat(
"{address=",
addr_uri.ok() ? addr_uri.value() : addr_uri.status().ToString(),
", args=", grpc_channel_args_string(args_), "}");
}
namespace {

@ -671,11 +671,15 @@ grpc_error_handle Chttp2ServerListener::Create(
// Create channelz node.
if (grpc_channel_args_find_bool(args, GRPC_ARG_ENABLE_CHANNELZ,
GRPC_ENABLE_CHANNELZ_DEFAULT)) {
std::string string_address = grpc_sockaddr_to_uri(addr);
auto string_address = grpc_sockaddr_to_uri(addr);
if (!string_address.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(
string_address.status().ToString());
}
listener->channelz_listen_socket_ =
MakeRefCounted<channelz::ListenSocketNode>(
string_address.c_str(),
absl::StrFormat("chttp2 listener %s", string_address.c_str()));
*string_address,
absl::StrCat("chttp2 listener ", *string_address));
}
// Register with the server only upon success
server->AddListener(OrphanablePtr<Server::ListenerInterface>(listener));
@ -744,7 +748,8 @@ void Chttp2ServerListener::Start(
auto watcher = absl::make_unique<ConfigFetcherWatcher>(Ref());
config_fetcher_watcher_ = watcher.get();
server_->config_fetcher()->StartWatch(
grpc_sockaddr_to_string(&resolved_address_, false), std::move(watcher));
grpc_sockaddr_to_string(&resolved_address_, false).value(),
std::move(watcher));
} else {
{
MutexLock lock(&mu_);

@ -109,8 +109,10 @@ std::string XdsListenerResource::FilterChainData::ToString() const {
//
std::string XdsListenerResource::FilterChainMap::CidrRange::ToString() const {
auto addr_str = grpc_sockaddr_to_string(&address, false);
return absl::StrCat(
"{address_prefix=", grpc_sockaddr_to_string(&address, false),
"{address_prefix=",
addr_str.ok() ? addr_str.value() : addr_str.status().ToString(),
", prefix_len=", prefix_len, "}");
}
@ -776,9 +778,12 @@ grpc_error_handle AddFilterChainDataForSourceIpRange(
} else {
for (const auto& prefix_range :
filter_chain.filter_chain_match.source_prefix_ranges) {
auto addr_str = grpc_sockaddr_to_string(&prefix_range.address, false);
if (!addr_str.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_str.status().ToString());
}
auto insert_result = source_ip_map->emplace(
absl::StrCat(grpc_sockaddr_to_string(&prefix_range.address, false),
"/", prefix_range.prefix_len),
absl::StrCat(*addr_str, "/", prefix_range.prefix_len),
XdsListenerResource::FilterChainMap::SourceIp());
if (insert_result.second) {
insert_result.first->second.prefix_range.emplace(prefix_range);
@ -860,9 +865,12 @@ grpc_error_handle AddFilterChainDataForDestinationIpRange(
} else {
for (const auto& prefix_range :
filter_chain.filter_chain_match.prefix_ranges) {
auto addr_str = grpc_sockaddr_to_string(&prefix_range.address, false);
if (!addr_str.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_str.status().ToString());
}
auto insert_result = destination_ip_map->emplace(
absl::StrCat(grpc_sockaddr_to_string(&prefix_range.address, false),
"/", prefix_range.prefix_len),
absl::StrCat(*addr_str, "/", prefix_range.prefix_len),
InternalFilterChainMap::DestinationIp());
if (insert_result.second) {
insert_result.first->second.prefix_range.emplace(prefix_range);

@ -26,6 +26,7 @@
#include <string>
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
@ -42,12 +43,13 @@
#endif
#ifdef GRPC_HAVE_UNIX_SOCKET
static std::string grpc_sockaddr_to_uri_unix_if_possible(
static absl::StatusOr<std::string> grpc_sockaddr_to_uri_unix_if_possible(
const grpc_resolved_address* resolved_addr) {
const grpc_sockaddr* addr =
reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
if (addr->sa_family != AF_UNIX) {
return "";
return absl::InvalidArgumentError(
absl::StrCat("Socket family is not AF_UNIX: ", addr->sa_family));
}
const auto* unix_addr = reinterpret_cast<const struct sockaddr_un*>(addr);
if (unix_addr->sun_path[0] == '\0' && unix_addr->sun_path[1] != '\0') {
@ -60,9 +62,9 @@ static std::string grpc_sockaddr_to_uri_unix_if_possible(
return absl::StrCat("unix:", unix_addr->sun_path);
}
#else
static std::string grpc_sockaddr_to_uri_unix_if_possible(
static absl::StatusOr<std::string> grpc_sockaddr_to_uri_unix_if_possible(
const grpc_resolved_address* /* addr */) {
return "";
return absl::InvalidArgumentError("Unix socket is not supported.");
}
#endif
@ -182,8 +184,8 @@ void grpc_sockaddr_make_wildcard6(int port,
resolved_wild_out->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
}
std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr,
bool normalize) {
absl::StatusOr<std::string> grpc_sockaddr_to_string(
const grpc_resolved_address* resolved_addr, bool normalize) {
const int save_errno = errno;
grpc_resolved_address addr_normalized;
if (normalize && grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) {
@ -191,6 +193,28 @@ std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr,
}
const grpc_sockaddr* addr =
reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
std::string out;
#ifdef GRPC_HAVE_UNIX_SOCKET
if (addr->sa_family == GRPC_AF_UNIX) {
const sockaddr_un* addr_un = reinterpret_cast<const sockaddr_un*>(addr);
bool abstract = addr_un->sun_path[0] == '\0';
if (abstract) {
int len = resolved_addr->len - sizeof(addr->sa_family);
if (len <= 0) {
return absl::InvalidArgumentError("empty UDS abstract path");
}
out = std::string(addr_un->sun_path, len);
} else {
size_t maxlen = sizeof(addr_un->sun_path);
if (strnlen(addr_un->sun_path, maxlen) == maxlen) {
return absl::InvalidArgumentError("UDS path is not null-terminated");
}
out = std::string(addr_un->sun_path);
}
return out;
}
#endif
const void* ip = nullptr;
int port = 0;
uint32_t sin6_scope_id = 0;
@ -207,7 +231,6 @@ std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr,
sin6_scope_id = addr6->sin6_scope_id;
}
char ntop_buf[GRPC_INET6_ADDRSTRLEN];
std::string out;
if (ip != nullptr && grpc_inet_ntop(addr->sa_family, ip, ntop_buf,
sizeof(ntop_buf)) != nullptr) {
if (sin6_scope_id != 0) {
@ -219,15 +242,19 @@ std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr,
out = grpc_core::JoinHostPort(ntop_buf, port);
}
} else {
out = absl::StrFormat("(sockaddr family=%d)", addr->sa_family);
return absl::InvalidArgumentError(
absl::StrCat("Unknown sockaddr family: ", addr->sa_family));
}
/* This is probably redundant, but we wouldn't want to log the wrong error. */
errno = save_errno;
return out;
}
std::string grpc_sockaddr_to_uri(const grpc_resolved_address* resolved_addr) {
if (resolved_addr->len == 0) return "";
absl::StatusOr<std::string> grpc_sockaddr_to_uri(
const grpc_resolved_address* resolved_addr) {
if (resolved_addr->len == 0) {
return absl::InvalidArgumentError("Empty address");
}
grpc_resolved_address addr_normalized;
if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) {
resolved_addr = &addr_normalized;
@ -236,13 +263,9 @@ std::string grpc_sockaddr_to_uri(const grpc_resolved_address* resolved_addr) {
if (scheme == nullptr || strcmp("unix", scheme) == 0) {
return grpc_sockaddr_to_uri_unix_if_possible(resolved_addr);
}
std::string path =
grpc_sockaddr_to_string(resolved_addr, false /* normalize */);
std::string uri_str;
if (scheme != nullptr) {
uri_str = absl::StrCat(scheme, ":", path);
}
return uri_str;
auto path = grpc_sockaddr_to_string(resolved_addr, false /* normalize */);
if (!path.ok()) return path;
return absl::StrCat(scheme, ":", path.value());
}
const char* grpc_sockaddr_get_uri_scheme(

@ -23,6 +23,8 @@
#include <string>
#include "absl/status/statusor.h"
#include "src/core/lib/iomgr/resolved_address.h"
/* Returns true if addr is an IPv4-mapped IPv6 address within the
@ -60,14 +62,15 @@ int grpc_sockaddr_set_port(grpc_resolved_address* addr, int port);
// Converts a sockaddr into a newly-allocated human-readable string.
//
// Currently, only the AF_INET and AF_INET6 families are recognized.
// Currently, only the AF_INET, AF_INET6, and AF_UNIX families are recognized.
// If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are
// displayed as plain IPv4.
std::string grpc_sockaddr_to_string(const grpc_resolved_address* addr,
bool normalize) GRPC_MUST_USE_RESULT;
absl::StatusOr<std::string> grpc_sockaddr_to_string(
const grpc_resolved_address* addr, bool normalize) GRPC_MUST_USE_RESULT;
/* Returns the URI string corresponding to \a addr */
std::string grpc_sockaddr_to_uri(const grpc_resolved_address* addr);
absl::StatusOr<std::string> grpc_sockaddr_to_uri(
const grpc_resolved_address* addr);
/* Returns the URI scheme corresponding to \a addr */
const char* grpc_sockaddr_get_uri_scheme(const grpc_resolved_address* addr);

@ -242,10 +242,12 @@ void HttpRequest::AppendError(grpc_error_handle error) {
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed HTTP/1 client request");
}
const grpc_resolved_address* addr = &addresses_[next_address_ - 1];
std::string addr_text = grpc_sockaddr_to_uri(addr);
auto addr_text = grpc_sockaddr_to_uri(addr);
overall_error_ = grpc_error_add_child(
overall_error_,
grpc_error_set_str(error, GRPC_ERROR_STR_TARGET_ADDRESS, addr_text));
grpc_error_set_str(
error, GRPC_ERROR_STR_TARGET_ADDRESS,
addr_text.ok() ? addr_text.value() : addr_text.status().ToString()));
}
void HttpRequest::OnReadInternal(grpc_error_handle error) {

@ -351,11 +351,13 @@ grpc_endpoint* grpc_cfstream_endpoint_create(CFReadStreamRef read_stream,
if (native_handle) {
CFRelease(native_handle);
}
absl::StatusOr<std::string> addr_uri;
if (getsockname(sockfd, reinterpret_cast<sockaddr*>(resolved_local_addr.addr),
&resolved_local_addr.len) < 0) {
&resolved_local_addr.len) < 0 ||
!(addr_uri = grpc_sockaddr_to_uri(&resolved_local_addr)).ok()) {
ep_impl->local_address = "";
} else {
ep_impl->local_address = grpc_sockaddr_to_uri(&resolved_local_addr);
ep_impl->local_address = addr_uri.value();
}
ep_impl->read_cb = nil;
ep_impl->write_cb = nil;

@ -117,7 +117,7 @@ absl::string_view endpoint_get_peer(grpc_endpoint* ep) {
}
if (eeep->peer_address.empty()) {
const EventEngine::ResolvedAddress& addr = eeep->endpoint->GetPeerAddress();
eeep->peer_address = ResolvedAddressToURI(addr);
eeep->peer_address = ResolvedAddressToURI(addr).value();
}
return eeep->peer_address;
}
@ -130,7 +130,7 @@ absl::string_view endpoint_get_local_address(grpc_endpoint* ep) {
if (eeep->local_address.empty()) {
const EventEngine::ResolvedAddress& addr =
eeep->endpoint->GetLocalAddress();
eeep->local_address = ResolvedAddressToURI(addr);
eeep->local_address = ResolvedAddressToURI(addr).value();
}
return eeep->local_address;
}

@ -38,7 +38,8 @@ grpc_resolved_address CreateGRPCResolvedAddress(
}
// TODO(ctiller): Move this to somewhere more permanent as we're deleting iomgr.
std::string ResolvedAddressToURI(const EventEngine::ResolvedAddress& addr) {
absl::StatusOr<std::string> ResolvedAddressToURI(
const EventEngine::ResolvedAddress& addr) {
auto gra = CreateGRPCResolvedAddress(addr);
return grpc_sockaddr_to_uri(&gra);
}

@ -29,7 +29,8 @@ EventEngine::ResolvedAddress CreateResolvedAddress(
grpc_resolved_address CreateGRPCResolvedAddress(
const EventEngine::ResolvedAddress& ra);
std::string ResolvedAddressToURI(const EventEngine::ResolvedAddress& addr);
absl::StatusOr<std::string> ResolvedAddressToURI(
const EventEngine::ResolvedAddress& addr);
} // namespace experimental
} // namespace grpc_event_engine

@ -141,9 +141,15 @@ void tcp_connect(grpc_closure* on_connect, grpc_endpoint** endpoint,
const grpc_channel_args* channel_args,
const grpc_resolved_address* addr,
grpc_core::Timestamp deadline) {
auto addr_uri = grpc_sockaddr_to_uri(addr);
if (!addr_uri.ok()) {
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_connect,
absl_status_to_grpc_error(addr_uri.status()));
return;
}
grpc_event_engine_endpoint* ee_endpoint =
reinterpret_cast<grpc_event_engine_endpoint*>(
grpc_tcp_create(channel_args, grpc_sockaddr_to_uri(addr)));
grpc_tcp_create(channel_args, addr_uri.value()));
*endpoint = &ee_endpoint->base;
EventEngine::OnConnectCallback ee_on_connect =
GrpcClosureToOnConnectCallback(on_connect, endpoint);

@ -441,9 +441,10 @@ int grpc_ipv6_loopback_available(void) {
static grpc_error_handle error_for_fd(int fd,
const grpc_resolved_address* addr) {
if (fd >= 0) return GRPC_ERROR_NONE;
std::string addr_str = grpc_sockaddr_to_string(addr, false);
auto addr_str = grpc_sockaddr_to_string(addr, false);
grpc_error_handle err = grpc_error_set_str(
GRPC_OS_ERROR(errno, "socket"), GRPC_ERROR_STR_TARGET_ADDRESS, addr_str);
GRPC_OS_ERROR(errno, "socket"), GRPC_ERROR_STR_TARGET_ADDRESS,
addr_str.ok() ? addr_str.value() : addr_str.status().ToString());
return err;
}

@ -140,7 +140,7 @@ static void OnOpen(void* arg, grpc_error_handle error) {
static void ParseResolvedAddress(const grpc_resolved_address* addr,
CFStringRef* host, int* port) {
std::string host_port = grpc_sockaddr_to_string(addr, true);
std::string host_port = grpc_sockaddr_to_string(addr, true).value();
std::string host_string;
std::string port_string;
grpc_core::SplitHostPort(host_port, &host_string, &port_string);
@ -154,10 +154,18 @@ static void CFStreamClientConnect(grpc_closure* closure, grpc_endpoint** ep,
const grpc_channel_args* channel_args,
const grpc_resolved_address* resolved_addr,
grpc_core::Timestamp deadline) {
auto addr_uri = grpc_sockaddr_to_uri(resolved_addr);
if (!addr_uri.ok()) {
grpc_error_handle error =
GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_uri.status().ToString());
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
return;
}
CFStreamConnect* connect = new CFStreamConnect();
connect->closure = closure;
connect->endpoint = ep;
connect->addr_name = grpc_sockaddr_to_uri(resolved_addr);
connect->addr_name = addr_uri.value();
connect->refs = 2; // One for the connect operation, one for the timer.
gpr_ref_init(&connect->refcount, 1);
gpr_mu_init(&connect->mu);

@ -277,19 +277,26 @@ void grpc_tcp_client_create_from_prepared_fd(
addr->len);
} while (err < 0 && errno == EINTR);
std::string name = absl::StrCat("tcp-client:", grpc_sockaddr_to_uri(addr));
auto addr_uri = grpc_sockaddr_to_uri(addr);
if (!addr_uri.ok()) {
grpc_error_handle error =
GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_uri.status().ToString());
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
return;
}
std::string name = absl::StrCat("tcp-client:", addr_uri.value());
grpc_fd* fdobj = grpc_fd_create(fd, name.c_str(), true);
if (err >= 0) {
*ep = grpc_tcp_client_create_from_fd(fdobj, channel_args,
grpc_sockaddr_to_uri(addr));
*ep = grpc_tcp_client_create_from_fd(fdobj, channel_args, addr_uri.value());
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, GRPC_ERROR_NONE);
return;
}
if (errno != EWOULDBLOCK && errno != EINPROGRESS) {
grpc_error_handle error = GRPC_OS_ERROR(errno, "connect");
error = grpc_error_set_str(error, GRPC_ERROR_STR_TARGET_ADDRESS,
grpc_sockaddr_to_uri(addr));
addr_uri.value());
grpc_fd_orphan(fdobj, nullptr, nullptr, "tcp_client_connect_error");
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
return;
@ -302,7 +309,7 @@ void grpc_tcp_client_create_from_prepared_fd(
ac->ep = ep;
ac->fd = fdobj;
ac->interested_parties = interested_parties;
ac->addr_str = grpc_sockaddr_to_uri(addr);
ac->addr_str = addr_uri.value();
gpr_mu_init(&ac->mu);
ac->refs = 2;
GRPC_CLOSURE_INIT(&ac->write_closure, on_writable, ac,

@ -138,6 +138,13 @@ static void tcp_connect(grpc_closure* on_done, grpc_endpoint** endpoint,
grpc_winsocket_callback_info* info;
grpc_error_handle error = GRPC_ERROR_NONE;
async_connect* ac = NULL;
absl::StatusOr<std::string> addr_uri;
addr_uri = grpc_sockaddr_to_uri(addr);
if (!addr_uri.ok()) {
error = GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_uri.status().ToString());
goto failure;
}
*endpoint = NULL;
@ -199,7 +206,7 @@ static void tcp_connect(grpc_closure* on_done, grpc_endpoint** endpoint,
ac->socket = socket;
gpr_mu_init(&ac->mu);
ac->refs = 2;
ac->addr_name = grpc_sockaddr_to_uri(addr);
ac->addr_name = addr_uri.value();
ac->endpoint = endpoint;
ac->channel_args = grpc_channel_args_copy(channel_args);
GRPC_CLOSURE_INIT(&ac->on_connect, on_connect, ac, grpc_schedule_on_exec_ctx);
@ -213,11 +220,11 @@ static void tcp_connect(grpc_closure* on_done, grpc_endpoint** endpoint,
failure:
GPR_ASSERT(error != GRPC_ERROR_NONE);
std::string target_uri = grpc_sockaddr_to_uri(addr);
grpc_error_handle final_error =
grpc_error_set_str(GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
"Failed to connect", &error, 1),
GRPC_ERROR_STR_TARGET_ADDRESS, target_uri);
grpc_error_handle final_error = grpc_error_set_str(
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING("Failed to connect",
&error, 1),
GRPC_ERROR_STR_TARGET_ADDRESS,
addr_uri.ok() ? *addr_uri : addr_uri.status().ToString());
GRPC_ERROR_UNREF(error);
if (socket != NULL) {
grpc_winsocket_destroy(socket);

@ -1785,12 +1785,14 @@ grpc_endpoint* grpc_tcp_create(grpc_fd* em_fd,
grpc_resolved_address resolved_local_addr;
memset(&resolved_local_addr, 0, sizeof(resolved_local_addr));
resolved_local_addr.len = sizeof(resolved_local_addr.addr);
absl::StatusOr<std::string> addr_uri;
if (getsockname(tcp->fd,
reinterpret_cast<sockaddr*>(resolved_local_addr.addr),
&resolved_local_addr.len) < 0) {
&resolved_local_addr.len) < 0 ||
!(addr_uri = grpc_sockaddr_to_uri(&resolved_local_addr)).ok()) {
tcp->local_address = "";
} else {
tcp->local_address = grpc_sockaddr_to_uri(&resolved_local_addr);
tcp->local_address = addr_uri.value();
}
tcp->read_cb = nullptr;
tcp->write_cb = nullptr;

@ -256,13 +256,18 @@ static void on_read(void* arg, grpc_error_handle err) {
goto error;
}
std::string addr_str = grpc_sockaddr_to_uri(&addr);
auto addr_uri = grpc_sockaddr_to_uri(&addr);
if (!addr_uri.ok()) {
gpr_log(GPR_ERROR, "Invalid address: %s",
addr_uri.status().ToString().c_str());
goto error;
}
if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) {
gpr_log(GPR_INFO, "SERVER_CONNECT: incoming connection: %s",
addr_str.c_str());
addr_uri->c_str());
}
std::string name = absl::StrCat("tcp-server-connection:", addr_str);
std::string name = absl::StrCat("tcp-server-connection:", addr_uri.value());
grpc_fd* fdobj = grpc_fd_create(fd, name.c_str(), true);
read_notifier_pollset = (*(sp->server->pollsets))
@ -281,7 +286,7 @@ static void on_read(void* arg, grpc_error_handle err) {
acceptor->external_connection = false;
sp->server->on_accept_cb(
sp->server->on_accept_cb_arg,
grpc_tcp_create(fdobj, sp->server->channel_args, addr_str),
grpc_tcp_create(fdobj, sp->server->channel_args, addr_uri.value()),
read_notifier_pollset, acceptor);
}
@ -366,7 +371,7 @@ static grpc_error_handle add_wildcard_addrs_to_server(grpc_tcp_server* s,
static grpc_error_handle clone_port(grpc_tcp_listener* listener,
unsigned count) {
grpc_tcp_listener* sp = nullptr;
std::string addr_str;
absl::StatusOr<std::string> addr_str;
grpc_error_handle err;
for (grpc_tcp_listener* l = listener->next; l && l->is_sibling; l = l->next) {
@ -385,6 +390,9 @@ static grpc_error_handle clone_port(grpc_tcp_listener* listener,
if (err != GRPC_ERROR_NONE) return err;
listener->server->nports++;
addr_str = grpc_sockaddr_to_string(&listener->addr, true);
if (!addr_str.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_str.status().ToString());
}
sp = static_cast<grpc_tcp_listener*>(gpr_malloc(sizeof(grpc_tcp_listener)));
sp->next = listener->next;
listener->next = sp;
@ -397,7 +405,7 @@ static grpc_error_handle clone_port(grpc_tcp_listener* listener,
sp->fd = fd;
sp->emfd = grpc_fd_create(
fd,
absl::StrFormat("tcp-server-listener:%s/clone-%d", addr_str.c_str(), i)
absl::StrFormat("tcp-server-listener:%s/clone-%d", *addr_str, i)
.c_str(),
true);
memcpy(&sp->addr, &listener->addr, sizeof(grpc_resolved_address));
@ -604,12 +612,17 @@ class ExternalConnectionHandler : public grpc_core::TcpServerFdHandler {
return;
}
(void)grpc_set_socket_no_sigpipe_if_possible(fd);
std::string addr_str = grpc_sockaddr_to_uri(&addr);
auto addr_uri = grpc_sockaddr_to_uri(&addr);
if (!addr_uri.ok()) {
gpr_log(GPR_ERROR, "Invalid address: %s",
addr_uri.status().ToString().c_str());
return;
}
if (grpc_tcp_trace.enabled()) {
gpr_log(GPR_INFO, "SERVER_CONNECT: incoming external connection: %s",
addr_str.c_str());
addr_uri->c_str());
}
std::string name = absl::StrCat("tcp-server-connection:", addr_str);
std::string name = absl::StrCat("tcp-server-connection:", addr_uri.value());
grpc_fd* fdobj = grpc_fd_create(fd, name.c_str(), true);
read_notifier_pollset =
(*(s_->pollsets))[static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
@ -625,7 +638,7 @@ class ExternalConnectionHandler : public grpc_core::TcpServerFdHandler {
acceptor->listener_fd = listener_fd;
acceptor->pending_data = buf;
s_->on_accept_cb(s_->on_accept_cb_arg,
grpc_tcp_create(fdobj, s_->channel_args, addr_str),
grpc_tcp_create(fdobj, s_->channel_args, addr_uri.value()),
read_notifier_pollset, acceptor);
}

@ -85,38 +85,41 @@ static grpc_error_handle add_socket_to_server(grpc_tcp_server* s, int fd,
unsigned port_index,
unsigned fd_index,
grpc_tcp_listener** listener) {
grpc_tcp_listener* sp = nullptr;
*listener = nullptr;
int port = -1;
grpc_error_handle err =
grpc_tcp_server_prepare_socket(s, fd, addr, s->so_reuseport, &port);
if (err == GRPC_ERROR_NONE) {
GPR_ASSERT(port > 0);
std::string addr_str = grpc_sockaddr_to_string(addr, true);
std::string name = absl::StrCat("tcp-server-listener:", addr_str);
gpr_mu_lock(&s->mu);
s->nports++;
GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
sp = static_cast<grpc_tcp_listener*>(gpr_malloc(sizeof(grpc_tcp_listener)));
sp->next = nullptr;
if (s->head == nullptr) {
s->head = sp;
} else {
s->tail->next = sp;
}
s->tail = sp;
sp->server = s;
sp->fd = fd;
sp->emfd = grpc_fd_create(fd, name.c_str(), true);
memcpy(&sp->addr, addr, sizeof(grpc_resolved_address));
sp->port = port;
sp->port_index = port_index;
sp->fd_index = fd_index;
sp->is_sibling = 0;
sp->sibling = nullptr;
GPR_ASSERT(sp->emfd);
gpr_mu_unlock(&s->mu);
if (err != GRPC_ERROR_NONE) return err;
GPR_ASSERT(port > 0);
absl::StatusOr<std::string> addr_str = grpc_sockaddr_to_string(addr, true);
if (!addr_str.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_str.status().ToString());
}
std::string name = absl::StrCat("tcp-server-listener:", addr_str.value());
gpr_mu_lock(&s->mu);
s->nports++;
GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
grpc_tcp_listener* sp =
static_cast<grpc_tcp_listener*>(gpr_malloc(sizeof(grpc_tcp_listener)));
sp->next = nullptr;
if (s->head == nullptr) {
s->head = sp;
} else {
s->tail->next = sp;
}
s->tail = sp;
sp->server = s;
sp->fd = fd;
sp->emfd = grpc_fd_create(fd, name.c_str(), true);
memcpy(&sp->addr, addr, sizeof(grpc_resolved_address));
sp->port = port;
sp->port_index = port_index;
sp->fd_index = fd_index;
sp->is_sibling = 0;
sp->sibling = nullptr;
GPR_ASSERT(sp->emfd);
gpr_mu_unlock(&s->mu);
*listener = sp;
return err;

@ -133,21 +133,24 @@ grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
err = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed to set port");
break;
}
std::string addr_str = grpc_sockaddr_to_string(&addr, false);
auto addr_str = grpc_sockaddr_to_string(&addr, false);
if (!addr_str.ok()) {
return GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_str.status().ToString());
}
gpr_log(GPR_DEBUG,
"Adding local addr from interface %s flags 0x%x to server: %s",
ifa_name, ifa_it->ifa_flags, addr_str.c_str());
ifa_name, ifa_it->ifa_flags, addr_str->c_str());
/* We could have multiple interfaces with the same address (e.g., bonding),
so look for duplicates. */
if (find_listener_with_addr(s, &addr) != nullptr) {
gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s",
addr_str.c_str(), ifa_name);
addr_str->c_str(), ifa_name);
continue;
}
if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode,
&new_sp)) != GRPC_ERROR_NONE) {
grpc_error_handle root_err = GRPC_ERROR_CREATE_FROM_CPP_STRING(
absl::StrCat("Failed to add listener: ", addr_str));
absl::StrCat("Failed to add listener: ", addr_str.value()));
err = grpc_error_add_child(root_err, err);
break;
} else {

@ -225,11 +225,13 @@ static grpc_error_handle prepare_socket(SOCKET sock,
failure:
GPR_ASSERT(error != GRPC_ERROR_NONE);
auto addr_uri = grpc_sockaddr_to_uri(addr);
grpc_error_set_int(
grpc_error_set_str(GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
"Failed to prepare server socket", &error, 1),
GRPC_ERROR_STR_TARGET_ADDRESS,
grpc_sockaddr_to_uri(addr)),
grpc_error_set_str(
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
"Failed to prepare server socket", &error, 1),
GRPC_ERROR_STR_TARGET_ADDRESS,
addr_uri.ok() ? *addr_uri : addr_uri.status().ToString()),
GRPC_ERROR_INT_FD, (intptr_t)sock);
GRPC_ERROR_UNREF(error);
if (sock != INVALID_SOCKET) closesocket(sock);
@ -347,7 +349,13 @@ static void on_accept(void* arg, grpc_error_handle error) {
peer_name.len = (size_t)peer_name_len;
std::string peer_name_string;
if (!err) {
peer_name_string = grpc_sockaddr_to_uri(&peer_name);
auto addr_uri = grpc_sockaddr_to_uri(&peer_name);
if (addr_uri.ok()) {
peer_name_string = addr_uri.value();
} else {
gpr_log(GPR_ERROR, "invalid peer name: %s",
addr_uri.status().ToString().c_str());
}
} else {
char* utf8_message = gpr_format_message(WSAGetLastError());
gpr_log(GPR_ERROR, "getpeername error: %s", utf8_message);

@ -517,12 +517,14 @@ grpc_endpoint* grpc_tcp_create(grpc_winsocket* socket,
GRPC_CLOSURE_INIT(&tcp->on_write, on_write, tcp, grpc_schedule_on_exec_ctx);
grpc_resolved_address resolved_local_addr;
resolved_local_addr.len = sizeof(resolved_local_addr.addr);
absl::StatusOr<std::string> addr_uri;
if (getsockname(tcp->socket->socket,
reinterpret_cast<sockaddr*>(resolved_local_addr.addr),
&resolved_local_addr.len) < 0) {
&resolved_local_addr.len) < 0 ||
!(addr_uri = grpc_sockaddr_to_uri(&resolved_local_addr)).ok()) {
tcp->local_address = "";
} else {
tcp->local_address = grpc_sockaddr_to_uri(&resolved_local_addr);
tcp->local_address = grpc_sockaddr_to_uri(&resolved_local_addr).value();
}
tcp->peer_string = std::string(peer_string);
grpc_slice_buffer_init(&tcp->last_read_buffer);

@ -149,8 +149,9 @@ ServerAddress ServerAddress::WithAttribute(
}
std::string ServerAddress::ToString() const {
auto addr_str = grpc_sockaddr_to_string(&address_, false);
std::vector<std::string> parts = {
grpc_sockaddr_to_string(&address_, false),
addr_str.ok() ? addr_str.value() : addr_str.status().ToString(),
};
if (args_ != nullptr) {
parts.emplace_back(

@ -20,6 +20,9 @@
#include <errno.h>
#include <string.h>
#ifdef GRPC_HAVE_UNIX_SOCKET
#include <sys/un.h>
#endif
#include <gtest/gtest.h>
@ -163,47 +166,94 @@ TEST(SockAddrUtilsTest, SockAddrToString) {
errno = 0x7EADBEEF;
grpc_resolved_address input4 = MakeAddr4(kIPv4, sizeof(kIPv4));
EXPECT_EQ(grpc_sockaddr_to_string(&input4, false), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input4, true), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input4), "ipv4:192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input4, false).value(), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input4, true).value(), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input4).value(), "ipv4:192.0.2.1:12345");
grpc_resolved_address input6 = MakeAddr6(kIPv6, sizeof(kIPv6));
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false), "[2001:db8::1]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true), "[2001:db8::1]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6), "ipv6:[2001:db8::1]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false).value(),
"[2001:db8::1]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true).value(),
"[2001:db8::1]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6).value(), "ipv6:[2001:db8::1]:12345");
SetIPv6ScopeId(&input6, 2);
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false), "[2001:db8::1%252]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true), "[2001:db8::1%252]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6), "ipv6:[2001:db8::1%252]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false).value(),
"[2001:db8::1%252]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true).value(),
"[2001:db8::1%252]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6).value(),
"ipv6:[2001:db8::1%252]:12345");
SetIPv6ScopeId(&input6, 101);
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false),
EXPECT_EQ(grpc_sockaddr_to_string(&input6, false).value(),
"[2001:db8::1%25101]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true),
EXPECT_EQ(grpc_sockaddr_to_string(&input6, true).value(),
"[2001:db8::1%25101]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6), "ipv6:[2001:db8::1%25101]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6).value(),
"ipv6:[2001:db8::1%25101]:12345");
grpc_resolved_address input6x = MakeAddr6(kMapped, sizeof(kMapped));
EXPECT_EQ(grpc_sockaddr_to_string(&input6x, false),
EXPECT_EQ(grpc_sockaddr_to_string(&input6x, false).value(),
"[::ffff:192.0.2.1]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6x, true), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6x), "ipv4:192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6x, true).value(), "192.0.2.1:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6x).value(), "ipv4:192.0.2.1:12345");
grpc_resolved_address input6y =
MakeAddr6(kNotQuiteMapped, sizeof(kNotQuiteMapped));
EXPECT_EQ(grpc_sockaddr_to_string(&input6y, false),
EXPECT_EQ(grpc_sockaddr_to_string(&input6y, false).value(),
"[::fffe:c000:263]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6y, true), "[::fffe:c000:263]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6y), "ipv6:[::fffe:c000:263]:12345");
EXPECT_EQ(grpc_sockaddr_to_string(&input6y, true).value(),
"[::fffe:c000:263]:12345");
EXPECT_EQ(grpc_sockaddr_to_uri(&input6y).value(),
"ipv6:[::fffe:c000:263]:12345");
grpc_resolved_address phony;
memset(&phony, 0, sizeof(phony));
grpc_sockaddr* phony_addr = reinterpret_cast<grpc_sockaddr*>(phony.addr);
phony_addr->sa_family = 123;
EXPECT_EQ(grpc_sockaddr_to_string(&phony, false), "(sockaddr family=123)");
EXPECT_EQ(grpc_sockaddr_to_string(&phony, true), "(sockaddr family=123)");
EXPECT_TRUE(grpc_sockaddr_to_uri(&phony).empty());
EXPECT_EQ(grpc_sockaddr_to_string(&phony, false).status(),
absl::InvalidArgumentError("Unknown sockaddr family: 123"));
EXPECT_EQ(grpc_sockaddr_to_string(&phony, true).status(),
absl::InvalidArgumentError("Unknown sockaddr family: 123"));
EXPECT_EQ(grpc_sockaddr_to_uri(&phony).status(),
absl::InvalidArgumentError("Empty address"));
#ifdef GRPC_HAVE_UNIX_SOCKET
grpc_resolved_address inputun;
struct sockaddr_un* sock_un = reinterpret_cast<struct sockaddr_un*>(&inputun);
ASSERT_EQ(grpc_core::UnixSockaddrPopulate("/some/unix/path", &inputun),
GRPC_ERROR_NONE);
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).value(), "/some/unix/path");
std::string max_filepath(sizeof(sock_un->sun_path) - 1, 'x');
ASSERT_EQ(grpc_core::UnixSockaddrPopulate(max_filepath, &inputun),
GRPC_ERROR_NONE);
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).value(), max_filepath);
ASSERT_EQ(grpc_core::UnixSockaddrPopulate(max_filepath, &inputun),
GRPC_ERROR_NONE);
sock_un->sun_path[sizeof(sockaddr_un::sun_path) - 1] = 'x';
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).status(),
absl::InvalidArgumentError("UDS path is not null-terminated"));
ASSERT_EQ(grpc_core::UnixAbstractSockaddrPopulate("some_unix_path", &inputun),
GRPC_ERROR_NONE);
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).value(),
absl::StrCat(std::string(1, '\0'), "some_unix_path"));
std::string max_abspath(sizeof(sock_un->sun_path) - 1, '\0');
ASSERT_EQ(grpc_core::UnixAbstractSockaddrPopulate(max_abspath, &inputun),
GRPC_ERROR_NONE);
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).value(),
absl::StrCat(std::string(1, '\0'), max_abspath));
ASSERT_EQ(grpc_core::UnixAbstractSockaddrPopulate("", &inputun),
GRPC_ERROR_NONE);
inputun.len = sizeof(sock_un->sun_family);
EXPECT_EQ(grpc_sockaddr_to_string(&inputun, true).status(),
absl::InvalidArgumentError("empty UDS abstract path"));
#endif
}
TEST(SockAddrUtilsTest, SockAddrSetGetPort) {

@ -69,7 +69,7 @@ static void log_resolved_addrs(const char* label, const char* hostname) {
return;
}
for (const auto& addr : *addresses_or) {
gpr_log(GPR_INFO, "%s: %s", label, grpc_sockaddr_to_uri(&addr).c_str());
gpr_log(GPR_INFO, "%s: %s", label, grpc_sockaddr_to_uri(&addr)->c_str());
}
}
@ -292,7 +292,7 @@ int external_dns_works(const char* host) {
// "dualstack_socket_test" due to loopback4.unittest.grpc.io resolving to
// [64:ff9b::7f00:1]. (Working as expected for DNS64, but it prevents the
// dualstack_socket_test from functioning correctly). See b/201064791.
if (grpc_sockaddr_to_uri(&addr) == "ipv6:[64:ff9b::7f00:1]:80") {
if (grpc_sockaddr_to_uri(&addr).value() == "ipv6:[64:ff9b::7f00:1]:80") {
gpr_log(
GPR_INFO,
"Detected DNS64 server response. Tests that depend on "

@ -26,6 +26,7 @@
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
#include "src/core/lib/address_utils/parse_address.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/gpr/env.h"
#include "src/core/lib/iomgr/executor.h"
@ -95,8 +96,8 @@ static void finish_resolve(void* arg, grpc_error_handle error) {
if (error == GRPC_ERROR_NONE && 0 == strcmp(r->addr, "server")) {
*r->addresses = absl::make_unique<grpc_core::ServerAddressList>();
grpc_resolved_address fake_resolved_address;
memset(&fake_resolved_address, 0, sizeof(fake_resolved_address));
fake_resolved_address.len = 0;
GPR_ASSERT(
grpc_parse_ipv4_hostport("1.2.3.4:5", &fake_resolved_address, false));
(*r->addresses)->emplace_back(fake_resolved_address, nullptr);
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, GRPC_ERROR_NONE);
} else {

@ -137,7 +137,7 @@ static void server_weak_ref_set(server_weak_ref* weak_ref,
}
static void test_addr_init_str(test_addr* addr) {
std::string str = grpc_sockaddr_to_string(&addr->addr, false);
std::string str = grpc_sockaddr_to_string(&addr->addr, false).value();
size_t str_len = std::min(str.size(), sizeof(addr->str) - 1);
memcpy(addr->str, str.c_str(), str_len);
addr->str[str_len] = '\0';

@ -80,7 +80,7 @@ TEST_F(EvaluateArgsTest, TestLocalAddressAndPort) {
util_.SetLocalEndpoint("ipv6:[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:456");
EvaluateArgs args = util_.MakeEvaluateArgs();
grpc_resolved_address local_address = args.GetLocalAddress();
EXPECT_EQ(grpc_sockaddr_to_uri(&local_address),
EXPECT_EQ(grpc_sockaddr_to_uri(&local_address).value(),
"ipv6:[2001:db8:85a3::8a2e:370:7334]:456");
EXPECT_EQ(args.GetLocalAddressString(),
"2001:0db8:85a3:0000:0000:8a2e:0370:7334");
@ -91,7 +91,8 @@ TEST_F(EvaluateArgsTest, TestPeerAddressAndPort) {
util_.SetPeerEndpoint("ipv4:255.255.255.255:123");
EvaluateArgs args = util_.MakeEvaluateArgs();
grpc_resolved_address peer_address = args.GetPeerAddress();
EXPECT_EQ(grpc_sockaddr_to_uri(&peer_address), "ipv4:255.255.255.255:123");
EXPECT_EQ(grpc_sockaddr_to_uri(&peer_address).value(),
"ipv4:255.255.255.255:123");
EXPECT_EQ(args.GetPeerAddressString(), "255.255.255.255");
EXPECT_EQ(args.GetPeerPort(), 123);
}

@ -120,7 +120,7 @@ FakeUdpAndTcpServer::FakeUdpAndTcpServer(
grpc_resolved_address resolved_addr;
memcpy(resolved_addr.addr, &addr, sizeof(addr));
resolved_addr.len = sizeof(addr);
std::string addr_str = grpc_sockaddr_to_string(&resolved_addr, false);
std::string addr_str = grpc_sockaddr_to_string(&resolved_addr, false).value();
gpr_log(GPR_INFO, "Fake UDP and TCP server listening on %s",
addr_str.c_str());
if (bind(udp_socket_, reinterpret_cast<const sockaddr*>(&addr),

@ -111,8 +111,10 @@ class MockSourceAddrFactory : public address_sorting_source_addr_factory {
grpc_resolved_address dest_addr_as_resolved_addr;
memcpy(&dest_addr_as_resolved_addr.addr, dest_addr, dest_addr->len);
dest_addr_as_resolved_addr.len = dest_addr->len;
std::string ip_addr_str = grpc_sockaddr_to_string(
&dest_addr_as_resolved_addr, false /* normalize */);
std::string ip_addr_str =
grpc_sockaddr_to_string(&dest_addr_as_resolved_addr,
false /* normalize */)
.value();
auto it = dest_addr_to_src_addr_.find(ip_addr_str);
if (it == dest_addr_to_src_addr_.end()) {
gpr_log(GPR_DEBUG, "can't find |%s| in dest to src map",
@ -179,7 +181,8 @@ void VerifyLbAddrOutputs(const grpc_core::ServerAddressList& addresses,
EXPECT_EQ(addresses.size(), expected_addrs.size());
for (size_t i = 0; i < addresses.size(); ++i) {
std::string ip_addr_str =
grpc_sockaddr_to_string(&addresses[i].address(), false /* normalize */);
grpc_sockaddr_to_string(&addresses[i].address(), false /* normalize */)
.value();
EXPECT_EQ(expected_addrs[i], ip_addr_str);
}
}

@ -517,7 +517,8 @@ class CheckingResultHandler : public ResultHandler {
for (size_t i = 0; i < addresses.size(); i++) {
const grpc_core::ServerAddress& addr = addresses[i];
std::string str =
grpc_sockaddr_to_string(&addr.address(), true /* normalize */);
grpc_sockaddr_to_string(&addr.address(), true /* normalize */)
.value();
gpr_log(GPR_INFO, "%s", str.c_str());
out->emplace_back(GrpcLBAddress(std::move(str), is_balancer));
}

Loading…
Cancel
Save