arrow::Result SockaddrToString()

in dissociated-ipc/ucx_utils.cc [75:108]


arrow::Result<std::string> SockaddrToString(const struct sockaddr_storage& address) {
  std::string result = "";
  if (address.ss_family != AF_INET && address.ss_family != AF_INET6) {
    return arrow::Status::NotImplemented("unknown address family");
  }

  uint16_t port = 0;
  if (address.ss_family == AF_INET) {
    result.resize(INET_ADDRSTRLEN + 1);
    const auto* in_addr = reinterpret_cast<const struct sockaddr_in*>(&address);
    if (!inet_ntop(address.ss_family, &in_addr->sin_addr, &result[0], INET_ADDRSTRLEN)) {
      return arrow::internal::IOErrorFromErrno(errno,
                                               "could not convert address to a string");
    }
    port = ntohs(in_addr->sin_port);
  } else {
    result.resize(INET6_ADDRSTRLEN + 1);
    const auto* in6_addr = reinterpret_cast<const struct sockaddr_in6*>(&address);
    if (!inet_ntop(address.ss_family, &in6_addr->sin6_addr, &result[0],
                   INET6_ADDRSTRLEN)) {
      return arrow::internal::IOErrorFromErrno(errno,
                                               "could not convert address to string");
    }
    port = ntohs(in6_addr->sin6_port);
  }

  const size_t pos = result.find('\0');
  DCHECK_NE(pos, std::string::npos);
  result[pos] = ':';
  result.resize(pos + 1);
  result += std::to_string(port);

  return result;
}